Skip to content
Merged
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
128 changes: 128 additions & 0 deletions .github/workflows/pr-guidelines.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
name: PR guidelines

# Labels and comments on fork PRs that don't follow CONTRIBUTING.md, and closes
# them after 5 days without activity.
on:
pull_request_target:
types: [opened, edited, reopened, synchronize]
schedule:
- cron: "0 6 * * *"
workflow_dispatch:
inputs:
pr:
description: Fork PR number to check. Leave empty to check every open fork PR.
required: false

permissions: {}

env:
LABEL: not-following-guidelines
STALE_LABEL: stale

jobs:
check:
if: github.event_name != 'schedule'
runs-on: ubuntu-latest
concurrency:
group: pr-guidelines-${{ github.event.pull_request.number || github.run_id }}
cancel-in-progress: true
permissions:
issues: write
pull-requests: write
steps:
- uses: actions/github-script@v9
env:
PR_NUMBER: ${{ inputs.pr }}
with:
script: |
const { LABEL, STALE_LABEL, PR_NUMBER } = process.env;
const MARKER = "<!-- pr-guidelines -->";
const { owner, repo } = context.repo;

let prs;
if (context.payload.pull_request) {
prs = [context.payload.pull_request];
} else if (PR_NUMBER) {
prs = [(await github.rest.pulls.get({ owner, repo, pull_number: Number(PR_NUMBER) })).data];
} else {
prs = await github.paginate(github.rest.pulls.list, { owner, repo, state: "open" });
}
// Checks only open fork PRs, including ones whose fork was deleted (head.repo is null).
prs = prs.filter((pr) => pr.state === "open" && pr.head.repo?.full_name !== `${owner}/${repo}`);

const { html_url, default_branch } = context.payload.repository;
const GUIDE = `${html_url}/blob/${default_branch}/CONTRIBUTING.md`;

for (const pr of prs) {
const issue_number = pr.number;
const files = await github.paginate(github.rest.pulls.listFiles, {
owner, repo, pull_number: issue_number,
});

const problems = [];
if (pr.base.ref !== "staging") {
problems.push(`The PR targets \`${pr.base.ref}\`. Every PR [targets \`staging\`](${GUIDE}#branches): use **Edit** next to the title to change the base branch.`);
}
const paths = files.map((f) => f.filename);
const changesCode = paths.some((p) => /^(publisher|scripts|sdk\/src)\/|^[^/]+\.sh$/.test(p));
const changesTests = paths.some((p) => /^(tests|sdk\/tests)\//.test(p));
if (changesCode && !changesTests) {
problems.push(`The PR changes code but no tests. [New behavior and bug fixes need tests](${GUIDE}#pull-request-checklist).`);
}

const labels = pr.labels.map((l) => l.name);
let body;
if (problems.length) {
body = [
MARKER,
`Thanks for the PR. It doesn't follow the [contribution guide](${GUIDE}) yet:`,
"",
...problems.map((p) => `- ${p}`),
"",
`Also [run the checks CI runs](${GUIDE}#run-the-checks-ci-runs) before you push. This comment updates when you push or edit the PR. Without activity, the PR is closed after 5 days.`,
].join("\n");
if (!labels.includes(LABEL)) {
await github.rest.issues.createLabel({ owner, repo, name: LABEL, color: "d93f0b",
description: "The PR doesn't follow CONTRIBUTING.md yet" }).catch(() => {});
await github.rest.issues.addLabels({ owner, repo, issue_number, labels: [LABEL] });
}
} else {
body = `${MARKER}\nThanks, the base branch and tests now follow the [contribution guide](${GUIDE}).`;
for (const name of [LABEL, STALE_LABEL].filter((n) => labels.includes(n))) {
await github.rest.issues.removeLabel({ owner, repo, issue_number, name });
}
}

const comments = await github.paginate(github.rest.issues.listComments, { owner, repo, issue_number });
const existing = comments.find((c) => c.user.type === "Bot" && c.body.includes(MARKER));
if (existing) {
if (existing.body !== body) {
await github.rest.issues.updateComment({ owner, repo, comment_id: existing.id, body });
}
} else if (problems.length) {
await github.rest.issues.createComment({ owner, repo, issue_number, body });
}
}

close-stale:
if: github.event_name == 'schedule'
runs-on: ubuntu-latest
permissions:
actions: write
issues: write
pull-requests: write
steps:
- uses: actions/stale@v11
with:
only-pr-labels: ${{ env.LABEL }}
stale-pr-label: ${{ env.STALE_LABEL }}
days-before-issue-stale: -1
days-before-issue-close: -1
days-before-pr-stale: 3
days-before-pr-close: 2
stale-pr-message: >-
This PR has had no activity for 3 days and still doesn't follow the
contribution guide. It will be closed in 2 days without new activity.
close-pr-message: >-
Closed because it didn't follow the contribution guide and had no
activity for 5 days. Push the fixes and open a new PR when it's ready.
Loading
Loading