Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions .github/workflows/ankiquest_api_contract.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# SPDX-License-Identifier: AGPL-3.0-only
name: ankiquest API contract

on:
pull_request:
push:
branches: [main]
schedule:
- cron: "17 5 * * *"
workflow_dispatch:
inputs:
server_ref:
description: "float3/ankiquest branch, tag or commit whose API examples to check against"
required: true
default: master
type: string

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event_name }}
cancel-in-progress: true

env:
JAVA_TOOL_OPTIONS: -Dkotlin.daemon.options=autoshutdownIdleSeconds=10

jobs:
# Everything from the server checkout runs here, in a job with no cache, so it
# can never reach the shared Gradle cache. The contract job gets the examples as data.
server:
name: Server examples
if: github.event_name != 'schedule' || github.repository == 'float3/AnkiQuest-Android'
runs-on: ubuntu-latest
timeout-minutes: 15
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: float3/ankiquest
ref: ${{ inputs.server_ref || 'master' }}
path: server
persist-credentials: false

- name: Keep the server's current examples
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
with:
name: server-api-examples
path: server/docs/api/examples/*.json
if-no-files-found: warn
retention-days: 1

contract:
name: API contract
needs: server
runs-on: ubuntu-latest
timeout-minutes: 45
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

- name: Download the server's current examples
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
continue-on-error: true
with:
name: server-api-examples
path: ${{ runner.temp }}/server-api-examples

- name: Use the server's current examples
shell: bash
run: |
examples="$RUNNER_TEMP/server-api-examples"
resources=AnkiDroid/src/test/resources/ankiquest-api
if compgen -G "$examples/*.json" > /dev/null; then
find "$resources" -name '*.json' -delete
cp "$examples"/*.json "$resources"/
git status --short -- "$resources"
else
echo "The server at this ref has no docs/api/examples; checking the committed snapshot"
fi

- name: Configure JDK
uses: actions/setup-java@de7274f081f381c8f8158605e0321c36c376e2e6 # v6.0.1
with:
distribution: "jetbrains"
java-version: "21"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Setup Gradle
uses: gradle/actions/setup-gradle@3f5f9adaf7d9fecd50b5935e54106014257a94e6 # v6.4.0
timeout-minutes: 10
with:
cache-provider: basic
cache-read-only: ${{ github.ref != 'refs/heads/main' }}

- name: Gradle Dependency Download
uses: nick-fields/retry@ad984534de44a9489a53aefd81eb77f87c70dc60 # v4.0.0
with:
timeout_minutes: 10
retry_wait_seconds: 60
max_attempts: 3
command: ./gradlew robolectricSdkDownload --daemon

- name: Run the contract test
run: ./gradlew :AnkiDroid:testFullDebugUnitTest --tests 'com.ichi2.anki.ankiquest.AnkiquestApiContractTest' --daemon

- name: Report Unit Test Results
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
if: always() && hashFiles('**/build/test-results/**/TEST-*.xml') != ''
run: python3 .github/report_test_failures.py . --pattern "build/test-results/**/TEST-*.xml" --title "API contract failures"
44 changes: 44 additions & 0 deletions .github/workflows/ankiquest_art_check.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# SPDX-License-Identifier: AGPL-3.0-only
name: ankiquest art check

# Runs the server's art exporter against the app's drawables. It runs the server's code, so it
# only runs for pull requests and pushes, never on a schedule or by hand.
on:
pull_request:
push:
branches: [main]

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
art:
name: App art matches the server's exporter
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false

- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: float3/ankiquest
ref: master
path: server
persist-credentials: false

- name: Check the app's art against the server's exporter
shell: bash
run: |
if [ -f server/tools/export_android_art.py ]; then
python3 -m venv "$RUNNER_TEMP/art"
"$RUNNER_TEMP/art/bin/pip" install --quiet pillow
"$RUNNER_TEMP/art/bin/python" server/tools/export_android_art.py --check .
else
echo "The server has no tools/export_android_art.py"
fi
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import com.ichi2.anki.preferences.PreferencesActivity
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Job
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.async
import kotlinx.coroutines.currentCoroutineContext
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -76,6 +77,7 @@ internal class AnkiquestBrowserSessionBridge {
class AnkiquestActivity : AnkiActivity(R.layout.activity_ankiquest) {
private lateinit var webView: WebView
private var session: AnkiquestWebSession? = null
private var sessionCookie = false
private var account = ""
private var refreshAfterSettings = false
private var clearHistoryAfterLoad = false
Expand Down Expand Up @@ -202,7 +204,7 @@ class AnkiquestActivity : AnkiActivity(R.layout.activity_ankiquest) {
clearHistoryAfterLoad = false
back.isEnabled = view.canGoBack()
}
current.script(url)?.let { view.evaluateJavascript(it, null) }
current.script(url, sessionCookie)?.let { view.evaluateJavascript(it, null) }
}

override fun doUpdateVisitedHistory(
Expand All @@ -228,8 +230,11 @@ class AnkiquestActivity : AnkiActivity(R.layout.activity_ankiquest) {
val expected = session ?: return
val expectedAccount = account
prepareSession?.cancel()
sessionCookie = false
prepareSession =
lifecycleScope.launch {
var installed = false
val features = async { AnkiquestHomeData.account()?.let { AnkiquestCapabilities.current(it) }.orEmpty() }
val ready =
sessionBridge.prepare(
current = {
Expand All @@ -247,13 +252,14 @@ class AnkiquestActivity : AnkiActivity(R.layout.activity_ankiquest) {
null
}
},
install = { cookie -> setSessionCookie(expected.dashboard, cookie) },
install = { cookie -> setSessionCookie(expected.dashboard, cookie).also { installed = it } },
)
if (expected != session || expected != Ankiquest.webSession() ||
expectedAccount != account || expectedAccount != AnkiquestNavigation.accountFingerprint()
) {
return@launch
}
sessionCookie = ready && installed && AnkiquestCapabilities.COOKIE_SESSION in features.await()
if (!ready) {
findViewById<ProgressBar>(R.id.progress_bar).visibility = View.GONE
val message = getString(R.string.ankiquest_web_prepare_failed).htmlEncode()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// SPDX-License-Identifier: AGPL-3.0-only

package com.ichi2.anki.ankiquest

import android.content.SharedPreferences
import androidx.annotation.VisibleForTesting
import androidx.core.content.edit
import com.ichi2.anki.AnkiDroidApp
import com.ichi2.anki.common.time.TimeManager
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import timber.log.Timber
import java.util.concurrent.ConcurrentHashMap

/**
* The optional features a server reports at `GET /api/capabilities`, so the app can use a feature as
* soon as the server it talks to has it, whatever order the app and the server are updated in. A
* server too old to answer reports nothing; while a server cannot be reached, the features it last
* reported still count.
*/
internal object AnkiquestCapabilities {
/** The server keeps a dashboard signed in by its HttpOnly cookie alone, without the token. */
const val COOKIE_SESSION = "cookie_session"

@VisibleForTesting
internal const val PREF_CAPABILITIES = "ankiquestCapabilities"

@VisibleForTesting
internal const val MAX_AGE_MS = 6L * 60 * 60 * 1000

private data class Known(
val features: Set<String>,
val fetchedAt: Long,
val scope: String,
)

private val known = ConcurrentHashMap<String, Known>()

/** Whether [account]'s server last reported [feature], without asking it again. */
fun has(
account: HomeAccount,
feature: String,
): Boolean = feature in features(account)

/** The features [account]'s server last reported, without asking it again. */
fun features(account: HomeAccount): Set<String> = lookup(account.server)?.features.orEmpty()

/**
* The features of [account]'s server, asked again when they were last fetched over [MAX_AGE_MS]
* ago or for a different account.
*/
suspend fun current(account: HomeAccount): Set<String> {
val last = lookup(account.server)
if (last != null && last.scope == account.scope && age(last) in 0 until MAX_AGE_MS) return last.features
return refresh(account)
}

/** Asks [account]'s server for its features; when it cannot be reached, answers the ones it last reported. */
suspend fun refresh(account: HomeAccount): Set<String> =
withContext(Dispatchers.IO) {
val fetched =
try {
fetch(account)
} catch (e: CancellationException) {
throw e
} catch (e: Exception) {
Timber.d(e, "ankiquest capabilities unavailable")
return@withContext features(account)
}
store(account, fetched)
fetched
}

/** The features in a `/api/capabilities` answer; anything unreadable counts as none. */
fun parse(body: String): Set<String> =
try {
parse(JSONObject(body))
} catch (_: JSONException) {
emptySet()
}

private fun parse(answer: JSONObject): Set<String> {
val features = answer.optJSONArray("features") ?: return emptySet()
return (0 until features.length()).mapNotNull { features.opt(it) as? String }.filter { it.isNotEmpty() }.toSet()
}

private fun fetch(account: HomeAccount): Set<String> {
val request = AnkiquestApi.request(account.url("api/capabilities"), "", account.language).build()
return try {
AnkiquestApi.call(request, AnkiquestApi.Timeout.QUICK) { parse(it.body.string()) }
} catch (e: AnkiquestHttpException) {
if (e.code == 404) emptySet() else throw e
}
}

private fun age(entry: Known): Long = TimeManager.time.intTimeMS() - entry.fetchedAt

private fun preferences(): SharedPreferences = AnkiDroidApp.sharedPrefs()

private fun key(server: String) = scopedKey(PREF_CAPABILITIES, server)

private fun lookup(server: String): Known? = known[server] ?: stored(server)?.also { known[server] = it }

private fun stored(server: String): Known? =
try {
val value = JSONObject(preferences().getString(key(server), null) ?: return null)
Known(parse(value), value.optLong("at"), value.optString("scope"))
} catch (_: JSONException) {
null
} catch (_: ClassCastException) {
null
}

private fun store(
account: HomeAccount,
features: Set<String>,
) {
val entry = Known(features, TimeManager.time.intTimeMS(), account.scope)
known[account.server] = entry
preferences().edit {
putString(
key(account.server),
JSONObject()
.put("features", JSONArray(features.sorted()))
.put("at", entry.fetchedAt)
.put("scope", entry.scope)
.toString(),
)
}
}

/** Forgets the features held in memory, as a restarted app would. */
@VisibleForTesting
internal fun forgetMemory() = known.clear()
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import java.util.concurrent.TimeUnit

/**
* Uploads pending reviews and fetches the leaderboard and profile in the background, redraws the
* widget and raises rank and streak notifications. Runs every 15 minutes and on demand.
* widget and raises rank and streak notifications, and keeps the server's capabilities current.
* Runs every 15 minutes and on demand.
*/
object AnkiquestPoll {
private const val PERIODIC_NAME = "ankiquestPoll"
Expand Down Expand Up @@ -158,6 +159,7 @@ object AnkiquestPoll {
Timber.d(e, "ankiquest optional photos could not refresh")
}
}
AnkiquestHomeData.account(prefs)?.let { AnkiquestCapabilities.current(it) }
return result
}
}
Expand Down
Loading
Loading