diff --git a/.github/workflows/pull-request.yml b/.github/workflows/pull-request.yml index ea7b6c5..f354d24 100644 --- a/.github/workflows/pull-request.yml +++ b/.github/workflows/pull-request.yml @@ -31,7 +31,7 @@ jobs: sdlc-label: runs-on: ubuntu-latest if: >- - ${{ !github.event.pull_request.draft && github.event_name == 'pull_request' && (contains(fromJson('["opened", "reopened", "synchronize", "ready_for_review"]'), github.event.action) || (github.event.action == 'labeled' && github.event.label.name == 'change: needs review')) }} + ${{ !github.event.pull_request.draft && github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.repository && (contains(fromJson('["opened", "reopened", "synchronize", "ready_for_review"]'), github.event.action) || (github.event.action == 'labeled' && github.event.label.name == 'change: needs review')) }} concurrency: group: sdlc-label-${{ github.repository }}-${{ github.event.pull_request.number }} cancel-in-progress: false @@ -41,7 +41,7 @@ jobs: pull-requests: write issues: write steps: - - name: Ensure SDLC labels + - name: Create missing SDLC labels uses: actions/github-script@v9 with: script: | @@ -60,30 +60,10 @@ jobs: for (const label of labels) { try { - await github.rest.issues.getLabel({ - owner: context.repo.owner, - repo: context.repo.repo, - name: label.name, - }); - - await github.rest.issues.updateLabel({ - owner: context.repo.owner, - repo: context.repo.repo, - name: label.name, - new_name: label.name, - color: label.color, - description: label.description, - }); + await github.rest.issues.getLabel({ ...context.repo, name: label.name }); } catch (error) { if (error.status !== 404) throw error; - - await github.rest.issues.createLabel({ - owner: context.repo.owner, - repo: context.repo.repo, - name: label.name, - color: label.color, - description: label.description, - }); + await github.rest.issues.createLabel({ ...context.repo, ...label }); } } diff --git a/README.md b/README.md index 0285a48..0380e99 100644 --- a/README.md +++ b/README.md @@ -24,6 +24,8 @@ On the first run, Claude reads the full PR diff and returns `change: routine` or Draft PRs are skipped until they are ready for review. When a PR gets `change: needs review`, from Claude or by hand, the workflow requests a human reviewer. +PRs from forks are skipped, because GitHub gives their workflows a read-only token and no secrets. Label them by hand and request a reviewer yourself when one needs review. + The prompt maps the four release risk levels of the Software Release and Deployment Procedure onto the two labels and includes notes per repository. Add a note to the prompt when a repository needs specific guidance. Set `internal_app: true` for an internal app without a repository note, so changes that do not touch personal data or security stay `change: routine`. The workflow defaults to `claude-haiku-5-5`; override it with `claude_model`. diff --git a/test/pull-request-policy.test.mjs b/test/pull-request-policy.test.mjs index 7d4d31d..b1c672d 100644 --- a/test/pull-request-policy.test.mjs +++ b/test/pull-request-policy.test.mjs @@ -107,6 +107,7 @@ function githubScriptFor(stepName) { } const prepareClassification = githubScriptFor("Prepare SDLC classification"); +const createMissingLabels = githubScriptFor("Create missing SDLC labels"); const finalizeLabel = githubScriptFor("Finalize SDLC label"); const requestReview = githubScriptFor("Request human review"); const routine = "change: routine"; @@ -689,7 +690,47 @@ test("a failed reviewer request remains visible as a workflow failure", async () assert.deepEqual(state.mutations, []); }); -test("manual needs-review labels and ready drafts trigger the shared job", () => { +test("missing SDLC labels are created and existing labels stay as they are", async () => { + const created = []; + const github = { + rest: { + issues: { + async getLabel({ name }) { + if (name === needsReview) return { data: { name } }; + throw Object.assign(new Error("Not Found"), { status: 404 }); + }, + async createLabel(label) { + created.push(label); + }, + }, + }, + }; + const context = { repo: { owner: "simpleanalytics", repo: "dashboard" } }; + await createMissingLabels(github, context); + assert.deepEqual(created, [ + { + owner: "simpleanalytics", + repo: "dashboard", + name: routine, + color: "fbca04", + description: + "Routine or ordinary change that needs at most one human or AI-assisted review", + }, + ]); + + github.rest.issues.getLabel = async () => { + throw Object.assign(new Error("Resource not accessible by integration"), { + status: 403, + }); + }; + await assert.rejects( + createMissingLabels(github, context), + /Resource not accessible/, + ); + assert.equal(created.length, 1); +}); + +test("manual needs-review labels and ready drafts trigger the shared job, but forks do not", () => { const expression = workflow.match( /^ if: >-\n \$\{\{ (.+) \}\}$/m, )?.[1]; @@ -700,7 +741,8 @@ test("manual needs-review labels and ready drafts trigger the shared job", () => "fromJson", `return (${expression});`, ); - for (const [action, label, draft, expected] of [ + const repository = "simpleanalytics/example"; + for (const [action, label, draft, expected, headRepository = repository] of [ ["opened", undefined, false, true], ["reopened", undefined, false, true], ["synchronize", undefined, false, true], @@ -710,15 +752,22 @@ test("manual needs-review labels and ready drafts trigger the shared job", () => ["labeled", "bug", false, false], ["labeled", needsReview, true, false], ["closed", undefined, false, false], + ["opened", undefined, false, false, "contributor/example"], + ["labeled", needsReview, false, false, "contributor/example"], ]) { const github = { event_name: "pull_request", - event: { action, label: { name: label }, pull_request: { draft } }, + repository, + event: { + action, + label: { name: label }, + pull_request: { draft, head: { repo: { full_name: headRepository } } }, + }, }; assert.equal( shouldRun(github, (values, value) => values.includes(value), JSON.parse), expected, - `${action}: ${label}, draft=${draft}`, + `${action}: ${label}, draft=${draft}, head=${headRepository}`, ); } });