Repository navigation
Bring the API contract test and cookie-only web view to main #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
49422dc
Keep the token out of the dashboard page once its cookie is installed
float3 b9e04ba
Merge remote-tracking branch 'fork/ankiquest-networking' into webview…
float3 deb52b9
Hand the dashboard only the member once the server reports cookie ses…
float3 2af77b6
Check the app's parsing against the server's recorded API answers
float3 1d120e0
Merge remote-tracking branch 'fork/ankiquest-networking' into webview…
float3 d045cba
Merge remote-tracking branch 'fork/ankiquest-networking' into api-con…
float3 475138c
Check the share-as-link answer against the server's example
float3 8637c05
Merge pull request #92 from float3/api-contract
float3 b17b2e8
Merge pull request #91 from float3/webview-cookie-only
float3 71962d0
Merge remote-tracking branch 'fork/ankiquest-networking' into land-co…
float3 6f83be7
Contract workflow: run on main and never write the Gradle cache
float3 17d1668
Contract workflow: run the server checkout in its own job, without a …
float3 2d04ea5
Merge branch 'main' into land-contract-test
float3 4c908ac
Contract test: read the finished-deck share reply, and list mute as u…
float3 dd73017
Contract workflow: skip the server's art script on the nightly run
float3 c11a477
Move the art check into its own pull request and push workflow
float3 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 | ||
| 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" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
139 changes: 139 additions & 0 deletions
139
AnkiDroid/src/main/java/com/ichi2/anki/ankiquest/AnkiquestCapabilities.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.