Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
80 changes: 80 additions & 0 deletions .github/workflows/pr-self-report-label.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
name: PR self-report label

Check failure on line 1 in .github/workflows/pr-self-report-label.yml

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, Node 22.x)

1:7 [quoted-strings] string value is not quoted with single quotes

# 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

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, Node 22.x)

12:29 [quoted-strings] string value is not quoted with single quotes

Check failure on line 12 in .github/workflows/pr-self-report-label.yml

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, Node 22.x)

12:21 [quoted-strings] string value is not quoted with single quotes

Check failure on line 12 in .github/workflows/pr-self-report-label.yml

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, Node 22.x)

12:13 [quoted-strings] string value is not quoted with single quotes

permissions:
contents: read

Check failure on line 15 in .github/workflows/pr-self-report-label.yml

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, Node 22.x)

15:13 [quoted-strings] string value is not quoted with single quotes
issues: write

Check failure on line 16 in .github/workflows/pr-self-report-label.yml

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, Node 22.x)

16:11 [quoted-strings] string value is not quoted with single quotes
pull-requests: write

Check failure on line 17 in .github/workflows/pr-self-report-label.yml

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, Node 22.x)

17:18 [quoted-strings] string value is not quoted with single quotes

concurrency:
group: pr-self-report-${{ github.event.pull_request.number }}

Check failure on line 20 in .github/workflows/pr-self-report-label.yml

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, Node 22.x)

20:10 [quoted-strings] string value is not quoted with single quotes
cancel-in-progress: true

jobs:
label:
if: ${{ github.repository == 'QwenLM/qwen-code' }}

Check failure on line 25 in .github/workflows/pr-self-report-label.yml

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, Node 22.x)

25:9 [quoted-strings] string value is not quoted with single quotes
runs-on: ubuntu-latest

Check failure on line 26 in .github/workflows/pr-self-report-label.yml

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, Node 22.x)

26:14 [quoted-strings] string value is not quoted with single quotes
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)"
Comment thread
qwen-code-dev-bot marked this conversation as resolved.
Outdated

# 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
112 changes: 112 additions & 0 deletions scripts/tests/pr-self-report-label.test.js
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';
Comment thread
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');
Comment thread
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(/\$\{\{/);
});
});
Loading