Repository navigation
ci: label a PR that closes an issue its own author opened #7630
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 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e1d5c1b
ci: label a PR that closes an issue its own author opened
0124bb9
Merge branch 'main' into ci/pr-self-report-label
wenshao c4806c7
ci: single-quote workflow string values for yamllint
3820273
fix(ci): never strip self-report label on a failed GraphQL query
96951dc
fix(ci): add timeout-minutes and labelCreated test assertion (#7630)
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,80 @@ | ||
| name: PR self-report label | ||
|
|
||
| # When a PR closes an issue that its OWN author opened (self-reported and | ||
| # self-fixed), apply a label so a reviewer can see at a glance that the problem | ||
| # itself was not independently reported — worth a look that the issue is real, | ||
| # not only that the fix is correct. Metadata only: it reads the PR's linked | ||
| # issues and applies a label, and NEVER checks out the PR's code, so running | ||
| # under pull_request_target (needed for write access on fork PRs) is safe. | ||
|
|
||
| on: | ||
| pull_request_target: | ||
| types: [opened, edited, reopened] | ||
|
Check failure on line 12 in .github/workflows/pr-self-report-label.yml
|
||
|
|
||
| permissions: | ||
| contents: read | ||
| issues: write | ||
| pull-requests: write | ||
|
|
||
| concurrency: | ||
| group: pr-self-report-${{ github.event.pull_request.number }} | ||
| cancel-in-progress: true | ||
|
|
||
| jobs: | ||
| label: | ||
| if: ${{ github.repository == 'QwenLM/qwen-code' }} | ||
| runs-on: ubuntu-latest | ||
| steps: | ||
| - name: 'Label a self-reported PR' | ||
| # PR-controlled values reach the script ONLY through env, never | ||
| # interpolated into the run body — the injection-safe pattern for | ||
| # pull_request_target. | ||
| env: | ||
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
| REPO: ${{ github.repository }} | ||
| PR: ${{ github.event.pull_request.number }} | ||
| PR_AUTHOR: ${{ github.event.pull_request.user.login }} | ||
| LABEL: review/self-reported | ||
| run: | | ||
| set -euo pipefail | ||
| # Issues this PR closes — "Fixes/Closes #N" keywords AND Development | ||
| # sidebar links — and the login that OPENED each. Fail-open to empty | ||
| # (an API blip must never strip a correct label). | ||
| ISSUE_AUTHORS="$(gh api graphql \ | ||
| -f owner="${REPO%/*}" -f name="${REPO#*/}" -F pr="${PR}" -f query=' | ||
| query($owner: String!, $name: String!, $pr: Int!) { | ||
| repository(owner: $owner, name: $name) { | ||
| pullRequest(number: $pr) { | ||
| closingIssuesReferences(first: 50) { nodes { author { login } } } | ||
| } | ||
| } | ||
| }' --jq '.data.repository.pullRequest.closingIssuesReferences.nodes[] | ||
| | .author.login // empty' 2> /dev/null || true)" | ||
|
|
||
| # Self-reported iff SOME closed issue was opened by the PR author. | ||
| SELF='false' | ||
| while IFS= read -r a; do | ||
| if [[ -n "${a}" && "${a}" == "${PR_AUTHOR}" ]]; then | ||
| SELF='true' | ||
| break | ||
| fi | ||
| done <<< "${ISSUE_AUTHORS}" | ||
|
|
||
| HAS_LABEL="$(gh pr view "${PR}" --repo "${REPO}" --json labels \ | ||
| --jq "[.labels[].name] | index(\"${LABEL}\") != null" 2> /dev/null || echo 'false')" | ||
|
|
||
| if [[ "${SELF}" == 'true' && "${HAS_LABEL}" != 'true' ]]; then | ||
| # Create the label on first use (idempotent), then apply it. | ||
| gh label create "${LABEL}" --repo "${REPO}" --color 'BFD4F2' \ | ||
| --description 'The linked issue was opened by the PR author (self-reported)' \ | ||
| 2> /dev/null || true | ||
| gh pr edit "${PR}" --repo "${REPO}" --add-label "${LABEL}" | ||
| echo "🏷️ #${PR}: added ${LABEL} — a closed issue was opened by ${PR_AUTHOR}" | ||
| elif [[ "${SELF}" != 'true' && "${HAS_LABEL}" == 'true' ]]; then | ||
| # The link was removed or re-pointed to someone else's issue — clear | ||
| # the now-stale label so it never lies. | ||
| gh pr edit "${PR}" --repo "${REPO}" --remove-label "${LABEL}" | ||
| echo "🏷️ #${PR}: removed ${LABEL} — no self-reported linked issue" | ||
| else | ||
| echo "#${PR}: no change (self=${SELF}, labeled=${HAS_LABEL})" | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| /** | ||
| * @license | ||
| * Copyright 2025 Google LLC | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| import { describe, expect, it } from 'vitest'; | ||
| import { | ||
| chmodSync, | ||
| existsSync, | ||
| mkdirSync, | ||
| mkdtempSync, | ||
| readFileSync, | ||
| rmSync, | ||
| writeFileSync, | ||
| } from 'node:fs'; | ||
| import { join } from 'node:path'; | ||
| import { tmpdir } from 'node:os'; | ||
| import { execFileSync } from 'node:child_process'; | ||
| import yaml from 'js-yaml'; | ||
|
qwen-code-dev-bot marked this conversation as resolved.
Outdated
|
||
|
|
||
| const workflow = yaml.load( | ||
| readFileSync('.github/workflows/pr-self-report-label.yml', 'utf8'), | ||
| ); | ||
| const runBlock = workflow.jobs.label.steps[0].run; | ||
|
|
||
| // The step decides label state from two inputs: the logins that opened the PR's | ||
| // closed issues (gh api graphql) and whether the label is already present (gh pr | ||
| // view). Stub both and record which mutation, if any, it makes. | ||
| const GH_STUB = [ | ||
| '#!/usr/bin/env bash', | ||
| 'echo "gh $*" >> "${CALLS_LOG}"', | ||
| 'case "$*" in', | ||
| " *'api graphql'*) printf '%s\\n' ${GH_ISSUE_AUTHORS:-} ;;", | ||
| " *'pr view'*labels*) { [ \"${GH_HAS_LABEL:-false}\" = true ] && printf 'true' || printf 'false'; } ;;", | ||
| ' *) : ;;', | ||
| 'esac', | ||
| 'exit 0', | ||
| ].join('\n'); | ||
|
qwen-code-dev-bot marked this conversation as resolved.
|
||
|
|
||
| describe('pr-self-report-label', () => { | ||
| const run = ({ prAuthor = 'alice', issueAuthors = '', hasLabel = false }) => { | ||
| const dir = mkdtempSync(join(tmpdir(), 'lbl-')); | ||
| const bin = join(dir, 'bin'); | ||
| mkdirSync(bin); | ||
| const callsLog = join(dir, 'calls.log'); | ||
| writeFileSync(join(bin, 'gh'), GH_STUB); | ||
| chmodSync(join(bin, 'gh'), 0o755); | ||
| const out = execFileSync('bash', ['-c', runBlock], { | ||
| env: { | ||
| ...process.env, | ||
| PATH: `${bin}:${process.env.PATH}`, | ||
| CALLS_LOG: callsLog, | ||
| REPO: 'o/r', | ||
| PR: '1', | ||
| PR_AUTHOR: prAuthor, | ||
| LABEL: 'review/self-reported', | ||
| GH_ISSUE_AUTHORS: issueAuthors, | ||
| GH_HAS_LABEL: String(hasLabel), | ||
| }, | ||
| encoding: 'utf8', | ||
| }); | ||
| const calls = existsSync(callsLog) ? readFileSync(callsLog, 'utf8') : ''; | ||
| rmSync(dir, { recursive: true, force: true }); | ||
| return { | ||
| added: /add-label/.test(calls), | ||
| removed: /remove-label/.test(calls), | ||
| out: out.trim(), | ||
| }; | ||
| }; | ||
|
|
||
| it('labels a PR that closes an issue its own author opened', () => { | ||
| expect(run({ prAuthor: 'alice', issueAuthors: 'alice' })).toMatchObject({ | ||
| added: true, | ||
| removed: false, | ||
| }); | ||
| // One self-reported issue among several linked is enough. | ||
| expect( | ||
| run({ prAuthor: 'alice', issueAuthors: 'bob\nalice\ncarol' }).added, | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('removes the label once no closed issue is self-reported', () => { | ||
| // The link was re-pointed to someone else's issue… | ||
| expect( | ||
| run({ prAuthor: 'alice', issueAuthors: 'bob', hasLabel: true }), | ||
| ).toMatchObject({ added: false, removed: true }); | ||
| // …or removed entirely. | ||
| expect( | ||
| run({ prAuthor: 'alice', issueAuthors: '', hasLabel: true }).removed, | ||
| ).toBe(true); | ||
| }); | ||
|
|
||
| it('makes no change when the label already matches the state', () => { | ||
| expect( | ||
| run({ prAuthor: 'alice', issueAuthors: 'alice', hasLabel: true }), | ||
| ).toMatchObject({ added: false, removed: false }); | ||
| expect( | ||
| run({ prAuthor: 'alice', issueAuthors: 'bob', hasLabel: false }), | ||
| ).toMatchObject({ added: false, removed: false }); | ||
| // A PR that closes no issue is never labelled. | ||
| expect( | ||
| run({ prAuthor: 'alice', issueAuthors: '', hasLabel: false }), | ||
| ).toMatchObject({ added: false, removed: false }); | ||
| }); | ||
|
|
||
| it('never interpolates PR-controlled data into the run body (env only)', () => { | ||
| // pull_request_target hardening: the author/number/label reach the script | ||
| // only through env, so a crafted title or body cannot inject shell. | ||
| expect(runBlock).not.toMatch(/\$\{\{/); | ||
| }); | ||
| }); | ||
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.