diff --git a/.github/ai-code-review/README.md b/.github/ai-code-review/README.md new file mode 100644 index 0000000..ea174c6 --- /dev/null +++ b/.github/ai-code-review/README.md @@ -0,0 +1,38 @@ +# AI code review + +Advisory AI code review for pull requests. It uses the shared workflow in +[`aodn/common-workflow`](https://github.com/aodn/common-workflow/tree/main/ai-code-review). +The scripts, tests, security notes and the Kiro engine are all documented there. +This repository only holds its configuration. + +## Files + +| File | Role | +| ---------------------------------------- | ----------------------------------------------------------------------------------- | +| `.github/workflows/ai-code-review.yaml` | Caller: triggers, skip rules (forks, `skip-ai-review` label), engine and credential | +| `.github/ai-code-review/prompt.md` | Repository-specific review direction added to the shared baseline | +| `.github/ai-code-review/instructions.md` | Repository context, focus areas, reuse locations and conventions | + +Both files are read from the PR head, so a PR that edits them is reviewed +with its own version. The shared workflow adds the sandbox description, the +baseline review rules (review priorities, how to judge, the +duplicate-implementation check) and the output format around them. + +## Behaviour + +- Runs on PRs to `main`, including drafts, when opened, pushed or reopened. + PRs labelled `skip-ai-review` and **PRs from forks** are skipped. A new push + cancels the in-flight review. +- **No credential** (Dependabot, or secret not set): a "skipped" comment. + Maintainers can run it manually from _Actions → AI code review (Kiro) → Run + workflow_ with the PR number. The shared workflow still skips fork PRs on + manual runs. +- Errors or timeouts: a comment links to the run. The check stays green and + never blocks merging. + +## Configuration + +- `KIRO_API_KEY` **secret**: a Kiro API key (needs Kiro Pro or higher). +- `KIRO_MODEL` **variable** (optional): model id. Unset uses the account default. +- **Version**: the caller uses the floating `ai-code-review-v1` tag, so each + compatible release of the shared workflow is picked up automatically. diff --git a/.github/ai-code-review/instructions.md b/.github/ai-code-review/instructions.md new file mode 100644 index 0000000..c44c816 --- /dev/null +++ b/.github/ai-code-review/instructions.md @@ -0,0 +1,86 @@ +This repository is `ocean-current-api`, the REST API of the IMOS Ocean Current +website: Java 17, Spring Boot 3, Gradle, Lombok, the Elasticsearch Java client, +AWS SDK v2 (S3), SQLite via plain JDBC, Caffeine caches, JUnit 5, Mockito and +Testcontainers (Elasticsearch). It never serves images. It indexes metadata +about pre-rendered images on the legacy file server (from JSON index files +under `REMOTE_BASE_URL` + `/resource/`, plus surface-wave files in S3) into +Elasticsearch, and tells `ocean-current-frontend` which products, regions and +dates exist. All endpoints live under the `/api/v1` context path. + +Pay particular attention to: + +- Reindexing (`IndexingService`): a new timestamped index is built, validated + (not empty, at least 80% of the old doc count, no productId from the old + index missing) and only then swapped onto the alias, after which old indices + are deleted. Check that failures leave the alias on the old index, that + partial failures are logged rather than silently dropped, and that new + indices are cleaned up on failure. +- Elasticsearch: query construction (term vs match, keyword fields, sorting, + `size` limits and pagination), bulk request error handling, and document IDs + from `DocumentIdGenerator` (`productId_region_fileName`), which collide when + file names are not unique within a region. +- Product configuration: `config/products.yaml` (served to the frontend and + used to validate `productId`, `region` and `depth`) and + `config/json-paths-config.yaml` (what gets indexed) must stay consistent + with each other, with `ProductConstants.PRODUCT_ID_MAPPINGS`, and with the + product IDs the frontend uses. Renaming or removing a productId breaks the + frontend and blocks the reindex validation. +- API contracts: changed response fields, status codes, request parameter + names or defaults are regressions for `ocean-current-frontend`. Invalid + input should return 4xx through `GlobalExceptionHandler`, not 500. +- Dates and file names: `latest-dates` only matches `\d{8,14}\.gif`; products + encode dates in file names in different formats and resolutions. Watch for + off-by-one days/months and timezone mistakes. `IndexingScheduler` and + `CacheInvalidationScheduler` use Australia/Hobart; `SqliteDownloadScheduler` + uses the server timezone. +- Caching: cache names come from `CacheNames`. Only `image-list` is evicted on + reindex; the others stay until the 03:00 eviction. Check cache keys include + every parameter the result depends on, and that cached values are not + mutated. +- Remote calls (`RestTemplate`, S3, SQLite download): timeouts, error handling, + resource and stream closing, and URLs built from user or remote data. Use + `UrlUtils.maskSensitiveUrl` before logging URLs. +- Security: `SecurityConfig` permits all requests except `/monitoring/**`, + which `Ec2InstanceAuthenticationFilter` protects with an EC2 instance + identity signature in the `production` and `edge` profiles. + `IndexingController` and `DebugController` must stay limited to the `dev` + and `edge` profiles. Flag new endpoints that mutate state or expose internal + data without that protection, SQL built by string concatenation in the + SQLite services, and anything committed that looks like a credential + (`.env*`, `application*.yaml`, keys). +- Configuration: profiles are `dev`, `edge`, `production` and `test`; a value + added to `application.yaml` may need overriding in the profile files, the + `Dockerfile` or `.env.sample`. `REMOTE_BASE_URL` must be `https://`. +- Tests: behaviour changes and bug fixes should have JUnit coverage under + `src/test/java`, mirroring the main package. Product or JSON path changes + should extend `ProductConfigIntegrationTest`, `JsonPathsConfigTest` and + `ProductServiceTest`. Elasticsearch behaviour belongs in Testcontainers + tests based on `ElasticsearchTestBase`, not in mocks only. + +Reusable code, for the duplicate-implementation check (package root +`src/main/java/au/org/aodn/oceancurrent/`): + +- Utilities: `util/` (`ProductIdUtils` for product ID versions, `UrlUtils`, + `WaveFileValidator`), `util/elasticsearch/` (`BulkRequestProcessor`, + `DocumentIdGenerator`) and `util/converter/` (`ImageMetadataConverter`). +- Constants: `constant/` (`CacheNames`, `ConfigConstants`, `ProductConstants`, + `DateDirection`). +- Services: `service/` (`SearchService` for Elasticsearch queries, + `RemoteJsonService` for remote JSON, `S3Service`, `SqliteBaseService`, + `ProductService` for the product tree) and `service/tags/` (implement + `ProductTagService` rather than adding product-specific code to + `TagService`). +- Configuration and properties: `configuration/` (`@ConfigurationProperties` + classes, `RestTemplateConfig`, `CacheConfig`). +- Exceptions: `exception/`, mapped to responses in `GlobalExceptionHandler`. + +Only report duplication when the behaviour really matches, not when the two +only look alike. Name the existing code the PR should reuse, with its file +and line, and say whether it can be used as is or needs a small change. + +Conventions: Lombok (`@RequiredArgsConstructor`, `@Slf4j`, `@Data`) with +constructor injection; config in `@ConfigurationProperties` classes under +`configuration/`; product knowledge in `products.yaml` and +`json-paths-config.yaml` rather than in code; new endpoints only when the +generic `image-list` endpoint cannot express the query; Conventional Commits. +Do not report issues the Java compiler would catch. diff --git a/.github/ai-code-review/prompt.md b/.github/ai-code-review/prompt.md new file mode 100644 index 0000000..13c78df --- /dev/null +++ b/.github/ai-code-review/prompt.md @@ -0,0 +1,17 @@ +## Repository-specific review direction + +Describe only risks, architecture boundaries or kinds of changed code that +need special attention in this repository. The shared workflow already adds +the general review priorities, evidence requirements, output format and the +duplicate-implementation check; the reuse locations for that check are below +in the repository-specific guidance. + +Pay particular attention to: + +- Changes to indexing, reindex validation or the alias swap: a mistake here + can leave the live index empty or stale for the frontend. +- Changes to `products.yaml` or `json-paths-config.yaml`: product IDs are a + data contract with `ocean-current-frontend` and the upstream scanner. +- New services, controllers, utilities and constants: these are the places + most likely to re-implement something that already exists in this + codebase. diff --git a/.github/workflows/ai-code-review.yaml b/.github/workflows/ai-code-review.yaml new file mode 100644 index 0000000..56494e1 --- /dev/null +++ b/.github/workflows/ai-code-review.yaml @@ -0,0 +1,48 @@ +name: AI code review (Kiro) + +# Advisory AI review from the shared aodn/common-workflow workflow. This repo +# owns the triggers, skip rules, engine choice and credential, and the review +# direction in .github/ai-code-review/prompt.md and instructions.md. +on: + pull_request: + branches: ["main"] + types: [opened, synchronize, reopened] + # Manual run, e.g. to re-run after the credential is added. + workflow_dispatch: + inputs: + pr-number: + description: Pull request number to review + type: number + required: true + +permissions: {} + +# One review per PR; a new push cancels the review of the previous commit. +concurrency: + group: ai-code-review-${{ github.event.pull_request.number || inputs.pr-number }} + cancel-in-progress: true + +jobs: + review: + name: Code Review + # Skip fork PRs (the review config comes from the PR head, so it must be + # trusted) and PRs labelled skip-ai-review, to save credits. Drafts are + # reviewed too. + if: >- + github.event_name == 'workflow_dispatch' || + (github.event.pull_request.head.repo.full_name == github.repository && + !contains(github.event.pull_request.labels.*.name, 'skip-ai-review')) + permissions: + contents: read + pull-requests: write + # Floating major tag: gets every compatible ai-code-review-v1.x.x release. + uses: aodn/common-workflow/.github/workflows/ai-code-review.yaml@ai-code-review-v1 + with: + pr-number: ${{ github.event.pull_request.number || inputs.pr-number }} + engine: kiro + model: ${{ vars.KIRO_MODEL }} + prompt-file: .github/ai-code-review/prompt.md + instruction-files: | + .github/ai-code-review/instructions.md + secrets: + ai-code-review-api-key: ${{ secrets.KIRO_API_KEY }} diff --git a/AGENTS.md b/AGENTS.md index ce0f584..b8c8c9b 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -146,6 +146,7 @@ Reference: the FishSOOP change, PR #78 (`aodn/backlog#8577`). | `ci.yaml` | push / PR to `main` | `./gradlew clean build` | | `cd_edge.yaml` | CI workflow completion on `main` | jar → Docker → ECR (`:sha`, `:latest`) → SSM `/apps/oceancurrent/edge/image_digest` → dispatch `aodn/appdeploy` (edge). The workflow currently does not check the CI conclusion. | | `cd_production.yaml` | tag `v*.*.*` | same, image tagged with the version, metadata attached to the GitHub release → `aodn/appdeploy` (`oceancurrent/ecs`, production) | +| `ai-code-review.yaml` | PR to `main`, manual dispatch | Advisory Kiro review via the shared `aodn/common-workflow` workflow; repo-specific direction in `.github/ai-code-review/` (see its `README.md`). Never blocks merging. | Release to production by pushing a semver tag (e.g. `v0.3.5`) after verifying on edge.