Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
83 changes: 80 additions & 3 deletions .github/workflows/operator-discovery.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
name: Operator Example Discovery

# Report-only: inventories the documented operator examples at the head of mongodb/docs, generates
# a candidate with its gaps, and integrates it under both mappers. It uploads the reports and never
# commits, promotes or files issues; a changed inventory is a proposal to review, not a failure.
# Inventories the documented operator examples at the head of mongodb/docs, generates a candidate
# with its gaps, and integrates it under both mappers, uploading every report. It never commits or
# promotes. The Publish job then files its findings as issues in this repository: each binding the
# generated cases lack, each example that appeared upstream with no test, and a failing integration.
# An issue's marker keeps reruns from filing it twice; a closed issue is not reopened.
on:
workflow_dispatch:
inputs:
publish:
description: File the findings as issues (otherwise only report what would be filed)
type: boolean
default: true
schedule:
- cron: '17 6 * * 1'

permissions:
contents: read

# Overlapping runs would publish from two journals at once.
concurrency:
group: operator-discovery
cancel-in-progress: false

jobs:
Discover:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -51,3 +63,68 @@ jobs:
${{ runner.temp }}/discovery/*.diff
${{ runner.temp }}/discovery/candidate/
retention-days: 90

Publish:
needs: Discover
# Discovery's reports are uploaded even when its integration fails, and that failure is a finding.
if: ${{ !cancelled() }}
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
issues: write
steps:
- name: Checkout
uses: actions/checkout@v7

- name: Setup Java
uses: actions/setup-java@v6
with:
java-version: '17'
distribution: temurin
cache: maven

- name: Download reports
uses: actions/download-artifact@v7
with:
name: operator-discovery
path: ${{ runner.temp }}/discovery

# The journal names each finding's issue, which is read by number. The issue listing can lag
# a recent write by minutes, so without it a rerun soon after a run could file a finding twice.
- name: Restore the previous journal
env:
GH_TOKEN: ${{ github.token }}
run: |
for run in $(gh run list -R "$GITHUB_REPOSITORY" --workflow operator-discovery.yml -L 20 \
--json databaseId --jq ".[].databaseId | select(. != $GITHUB_RUN_ID)"); do
if gh run download "$run" -R "$GITHUB_REPOSITORY" -n operator-publications -D "$RUNNER_TEMP/previous" 2>/dev/null \
&& [ -f "$RUNNER_TEMP/previous/journal.json" ]; then
cp "$RUNNER_TEMP/previous/journal.json" "$RUNNER_TEMP/journal.json"
echo "Journal from run $run"
break
fi
rm -rf "$RUNNER_TEMP/previous"
done

- name: File findings
env:
AUDITS_GITHUB_TOKEN: ${{ github.token }}
PUBLISH: ${{ github.event_name == 'schedule' || inputs.publish }}
run: |
flag=$([ "$PUBLISH" = "true" ] && echo --publish || true)
./mvnw -B -ntp -q -Daudits -pl audits compile exec:java \
-Daudits.mainClass=dev.morphia.audits.publish.PublishMainKt \
-Dexec.args="discovery $RUNNER_TEMP/discovery $PWD/audits/catalog/documented-examples.json $PWD/audits/catalog/publication-policy.json $RUNNER_TEMP/journal.json ${{ github.repository }} $flag" \
| tee "$RUNNER_TEMP/publications.json"

- name: Upload publications
uses: actions/upload-artifact@v7
if: always()
with:
name: operator-publications
path: |
${{ runner.temp }}/publications.json
${{ runner.temp }}/journal.json
if-no-files-found: ignore
retention-days: 90
19 changes: 16 additions & 3 deletions audits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,19 @@ Every command below runs through the exec plugin from the repository root, after
## Weekly discovery

`.github/workflows/operator-discovery.yml` runs `audits/discover.sh` every Monday against the head
of mongodb/docs, read-only. Open the run's summary and its `operator-discovery` artifact:
of mongodb/docs, then its Publish job files the findings as issues in this repository:

- **Bind `<node>` for generated operator tests** (`enhancement`, `aggregation`, `proposed`): an
MQL node that documented examples need and `dsl-bindings.json` lacks, with the examples it blocks.
- **Cover the documented `<operator>` example "…"** (`Task`, `proposed`): an example that appeared
upstream since the committed inventory, with no test.
- **Generated operator cases fail integration** (`bug`), naming the failing tests.

Each issue carries a hidden marker, so a rerun updates the publisher's section of an open issue
instead of filing it again, and keeps any edits outside that section. The journal of filed issues
carries over from run to run. A closed issue is a decision and is not reopened. A manual run can
set `publish` off to see what it would file. Open the run's summary and its `operator-discovery`
artifact:

- **The inventory matches**: nothing changed upstream that the inventory sees.
- **`inventory.diff` is not empty**: examples were added, renamed or removed. Accept it locally:
Expand Down Expand Up @@ -122,8 +134,9 @@ never-locked fixtures come from `fetch.py references`, `fetch.py pages` and

## Publishing findings

`PublishMainKt <coverage-report.json> <policy.json> <journal.json> <owner/repo>` only prints what
it would file. Add `--publish` to file issues, which needs `gh` authenticated with write access to
`PublishMainKt <coverage-report.json> <policy.json> <journal.json> <owner/repo>`, or
`PublishMainKt discovery <discovery-out> <committed-inventory.json> <policy.json> <journal.json> <owner/repo>`
for a discovery run's findings, only prints what it would file. Add `--publish` to file issues, which needs `gh` authenticated with write access to
issues (or `AUDITS_GITHUB_TOKEN`). The journal makes reruns idempotent: keep it, and never point a
trial run at MorphiaOrg/morphia. Use the disposable sandbox repository instead.

Expand Down
21 changes: 19 additions & 2 deletions audits/catalog/publication-policy.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
{
"labels" : {
"MISSING_API" : [ "enhancement", "proposed" ],
"VERSION_ANNOTATION" : [ "bug" ]
"INTEGRATION_FAILURE" : [
"bug"
],
"MISSING_API" : [
"enhancement",
"proposed"
],
"MISSING_BINDING" : [
"enhancement",
"aggregation",
"proposed"
],
"NEW_EXAMPLE" : [
"Task",
"proposed"
],
"VERSION_ANNOTATION" : [
"bug"
]
},
"milestone" : null,
"schemaVersion" : 1
Expand Down
2 changes: 1 addition & 1 deletion audits/discover.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def load(name):
path = os.path.join(out, name)
return json.load(open(path)) if os.path.exists(path) else None
inventory = load("documented-examples.json")
gaps = load("candidate/gaps.json") or []
gaps = (load("candidate/gaps.json") or {}).get("gaps", [])
attestation = load("candidate/attestation.json")
candidate = load("candidate/candidate.json")
lines = [f"# Operator example discovery at mongodb/docs `{inventory['commit'][:10]}`", ""]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import dev.morphia.audits.generate.Candidate
import dev.morphia.audits.generate.CurrentPages
import dev.morphia.audits.generate.DslBindingCatalog
import dev.morphia.audits.generate.ExampleInventory
import dev.morphia.audits.generate.GenerationGaps
import dev.morphia.audits.generate.GenerationReview
import dev.morphia.audits.naming.ExampleNameRegistry
import dev.morphia.audits.naming.NameProposal
Expand Down Expand Up @@ -195,6 +196,8 @@ object AuditJson {

fun generationReview(json: String): GenerationReview = read(json, GenerationReview::class.java)

fun generationGaps(json: String): GenerationGaps = read(json, GenerationGaps::class.java)

fun exampleInventory(json: String): ExampleInventory = read(json, ExampleInventory::class.java)

fun publicationJournal(json: String): PublicationJournal =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ object Candidates {
Files.write(target, bytes)
}
Files.writeString(out.resolve(DESCRIPTOR), AuditJson.write(candidate))
Files.writeString(out.resolve(GAPS), AuditJson.write(generation.gaps))
Files.writeString(
out.resolve(GAPS),
AuditJson.write(GenerationGaps(gaps = generation.gaps)),
)
return candidate
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ data class GenerationGap(
val detail: String,
)

/** The gaps of one generation, as `candidate` writes them to `gaps.json`. */
data class GenerationGaps(val schemaVersion: Int = 1, val gaps: List<GenerationGap>)

/** A reviewed exclusion of a generated case, by its qualified name. */
data class Exclusion(val name: String, val reason: String)

Expand Down
115 changes: 115 additions & 0 deletions audits/src/main/kotlin/dev/morphia/audits/publish/DiscoveryFindings.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package dev.morphia.audits.publish

import dev.morphia.audits.generate.Attestation
import dev.morphia.audits.generate.ExampleInventory
import dev.morphia.audits.generate.ExampleStatus
import dev.morphia.audits.generate.GapKind
import dev.morphia.audits.generate.GenerationGap

/**
* The publishable findings of one discovery run (`audits/discover.sh`): each binding the generated
* cases lack, each example that appeared upstream with no test since [committed] was accepted, and
* a failing integration. Keys are stable across runs, so a recurring finding updates its issue.
*/
object DiscoveryFindings {
private val unbound = Regex("""^\S*: (.+) has no binding$""")
private val failed = Regex("""^\[ERROR] (\S+) -- Time elapsed: .* <<< (FAILURE|ERROR)!$""")

fun findings(
committed: ExampleInventory,
current: ExampleInventory,
gaps: List<GenerationGap>,
attestation: Attestation?,
integrationLog: String?,
): List<Finding> =
(bindings(current, gaps) +
examples(committed, current, gaps) +
listOfNotNull(integration(current, attestation, integrationLog)))
.sortedBy { it.key }

/** One finding per unbound node, listing the examples it blocks. */
private fun bindings(current: ExampleInventory, gaps: List<GenerationGap>): List<Finding> =
gaps
.filter { it.kind == GapKind.UNBOUND }
.groupBy { unbound.find(it.detail)?.groupValues?.get(1) ?: it.detail }
.map { (node, blocked) ->
Finding(
"${FindingType.MISSING_BINDING}:$node",
FindingType.MISSING_BINDING,
"Bind $node for generated operator tests",
"Generated operator tests cannot be written for these documented examples " +
"(mongodb/docs `${current.commit.take(10)}`) because $node has no entry in " +
"`audits/catalog/dsl-bindings.json`. If Morphia has an API for it, add the " +
"binding; if not, the API comes first.\n\n" +
blocked.joinToString("\n") {
"- ${it.context} `${it.operator}`: ${it.title.trim()} (`${it.detail}`)"
},
)
}

/** Examples without a handwritten owner that the committed inventory did not have. */
private fun examples(
committed: ExampleInventory,
current: ExampleInventory,
gaps: List<GenerationGap>,
): List<Finding> {
fun key(context: String, operator: String, title: String) =
"$context/$operator/${title.trim()}"
val known =
committed.pages
.flatMap { page ->
page.examples.map { key(page.context, page.operator, it.title) }
}
.toSet()
val gapOf = gaps.associateBy { key(it.context, it.operator, it.title) }
return current.pages.flatMap { page ->
page.examples
.filter { it.status != ExampleStatus.OWNED }
.filter { key(page.context, page.operator, it.title) !in known }
.map { example ->
val key = key(page.context, page.operator, example.title)
val state =
gapOf[key]?.let { "Generation reports ${it.kind}: ${it.detail}" }
?: if (example.status == ExampleStatus.UNOWNED)
"It is generated when its family is in `audits/catalog/generation-review.json`."
else "It is ${example.status}: ${example.detail}"
Finding(
"${FindingType.NEW_EXAMPLE}:$key",
FindingType.NEW_EXAMPLE,
"Cover the documented ${page.operator} example \"${example.title.trim()}\"",
"mongodb/docs `${current.commit.take(10)}` documents this ${page.context} example " +
"at `${page.source.path}`, and no test covers it.\n\n$state\n\n" +
"Accept the new inventory (see audits/README.md, Weekly discovery) and " +
"regenerate, or cover it by hand.",
)
}
}
}

private fun integration(
current: ExampleInventory,
attestation: Attestation?,
log: String?,
): Finding? {
if (attestation == null || attestation.passed()) return null
val tests =
log.orEmpty()
.lines()
.mapNotNull { failed.find(it.trim())?.groupValues?.get(1) }
.distinct()
.sorted()
return Finding(
FindingType.INTEGRATION_FAILURE.name,
FindingType.INTEGRATION_FAILURE,
"Generated operator cases fail integration",
"Scheduled discovery at mongodb/docs `${current.commit.take(10)}` integrated the regenerated " +
"candidate and it failed: " +
attestation.results.entries.joinToString { "${it.key} exited ${it.value}" } +
".\n\n" +
(if (tests.isEmpty()) "No failing test was named; see the run's `integration.log`."
else "Failing tests:\n" + tests.joinToString("\n") { "- `$it`" }) +
"\n\nDiagnose from the run's `operator-discovery` artifact; a case the page or server makes " +
"unverifiable is excluded in `audits/catalog/generation-review.json` with its evidence.",
)
}
}
50 changes: 40 additions & 10 deletions audits/src/main/kotlin/dev/morphia/audits/publish/PublishMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,44 @@ import java.nio.file.Files
import java.nio.file.Path

private const val USAGE =
"Usage: PublishMainKt <coverage-report.json> <policy.json> <journal.json> <OWNER/REPO> [--publish]"
"""Usage: PublishMainKt <coverage-report.json> <policy.json> <journal.json> <OWNER/REPO> [--publish]
PublishMainKt discovery <discovery-out> <committed-inventory.json> <policy.json> <journal.json> <OWNER/REPO> [--publish]"""

/**
* Files a validated coverage report's findings as issues in OWNER/REPO. Without `--publish` it only
* reports what it would do and never spawns a mutating command. `AUDITS_GITHUB_TOKEN`, when set, is
* the token `gh` uses; otherwise `gh` uses its stored credentials. Prints each publication.
* Files findings as issues in OWNER/REPO: a validated coverage report's, or, with `discovery`,
* those of an `audits/discover.sh` output directory against the committed example inventory.
* Without `--publish` it only reports what it would do and never spawns a mutating command.
* `AUDITS_GITHUB_TOKEN`, when set, is the token `gh` uses (CI maps its job token to it); otherwise
* `gh` uses its stored credentials. Prints each publication.
*/
fun main(args: Array<String>) {
require(args.size == 4 || args.size == 5 && args[4] == "--publish") { USAGE }
fun read(path: String) = Files.readString(Path.of(path))
fun read(path: Path) = Files.readString(path)
val discovery = args.firstOrNull() == "discovery"
val positional = if (discovery) 6 else 4
require(args.size == positional || args.size == positional + 1 && args.last() == "--publish") {
USAGE
}
val publish = args.size == positional + 1
val (owner, repository) =
args[3].split('/').also { require(it.size == 2) { USAGE } }.let { it[0] to it[1] }
val publish = args.size == 5
args[positional - 1]
.split('/')
.also { require(it.size == 2) { USAGE } }
.let { it[0] to it[1] }
val findings =
if (discovery) {
val out = Path.of(args[1])
val optional = { name: String ->
out.resolve(name).takeIf { Files.exists(it) }?.let(::read)
}
DiscoveryFindings.findings(
AuditJson.exampleInventory(read(Path.of(args[2]))),
AuditJson.exampleInventory(read(out.resolve("documented-examples.json"))),
AuditJson.generationGaps(read(out.resolve("candidate/gaps.json"))).gaps,
optional("candidate/attestation.json")?.let(AuditJson::attestation),
optional("candidate/integration.log"),
)
} else Publisher.findings(AuditJson.report(read(Path.of(args[0]))))
val (policy, journal) = if (discovery) args[3] to args[4] else args[1] to args[2]
val gateway =
GhGateway(
GhTarget(owner, repository),
Expand All @@ -26,7 +51,12 @@ fun main(args: Array<String>) {
)
gateway.checkVersion()
val publications =
Publisher(gateway, AuditJson.publicationPolicy(read(args[1])), Path.of(args[2]), publish)
.publish(Publisher.findings(AuditJson.report(read(args[0]))))
Publisher(
gateway,
AuditJson.publicationPolicy(read(Path.of(policy))),
Path.of(journal),
publish,
)
.publish(findings)
println(AuditJson.write(publications))
}
Loading