blob: c27eb3c5b727427fb11620881862e93de4a5ae94 [file]
# Copyright 2013 The Flutter Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
# Privileged half of the "no PRs on main" check.
#
# Runs after `no-main-prs.yaml` fails and leaves an explanatory comment on the
# pull request. The artifact produced by `no-main-prs.yaml` is UNTRUSTED (a pull
# request can modify that workflow), so the PR number is only used after
# binding it to the triggering run's head commit, and the comment text is fixed.
#
# Runs for first-time contributors are held for approval and created already
# concluded as `action_required`, without an artifact. Those fire `requested`
# (but not `completed`), so in that case the PR is looked up from the run's head
# branch and SHA instead, and the comment is posted without waiting for approval.
name: No PRs on main Comment
on:
# Safe because this only consumes an artifact from the triggering run as
# untrusted data (validated and bound to the PR's head SHA); no PR code is
# checked out or executed.
workflow_run: # zizmor: ignore[dangerous-triggers]
workflows: [No PRs on main]
types: [requested, completed]
permissions:
contents: read
jobs:
comment:
name: Comment on PRs targeting main
runs-on: ubuntu-latest
# Only a failed or held check can need a comment, so other runs never allocate a runner.
if: >-
github.repository == 'flutter/flutter' &&
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.path == '.github/workflows/no-main-prs.yaml' &&
((github.event.action == 'completed' && github.event.workflow_run.conclusion == 'failure') ||
(github.event.action == 'requested' && github.event.workflow_run.conclusion == 'action_required'))
permissions:
actions: read
pull-requests: write
steps:
- name: Download metadata
id: download
if: github.event.action == 'completed'
# The check may have failed before uploading the artifact.
continue-on-error: true
uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2
with:
name: no-main-metadata
path: ${{ runner.temp }}/no-main-metadata
run-id: ${{ github.event.workflow_run.id }}
github-token: ${{ github.token }}
- name: Comment on PRs targeting main
if: github.event.action == 'requested' || steps.download.outcome == 'success'
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
METADATA_PATH: ${{ runner.temp }}/no-main-metadata/metadata.json
with:
github-token: ${{ github.token }}
script: |
const fs = require('fs');
const run = context.payload.workflow_run;
let issueNumber;
if (context.payload.action === 'requested') {
// Awaiting approval: no artifact, so find the open PR for the
// run's head branch. This only uses data from GitHub's payload.
const { data: prs } = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
head: `${run.head_repository.owner.login}:${run.head_branch}`,
});
const match = prs.find(pr => pr.head.sha === run.head_sha);
if (!match) {
core.notice('No open PR found for the triggering run; skipping.');
return;
}
issueNumber = match.number;
} else {
const metadata = JSON.parse(fs.readFileSync(process.env.METADATA_PATH, 'utf8'));
const prNumberString = String(metadata.pr_number ?? '');
if (!/^[1-9][0-9]*$/.test(prNumberString)) {
core.setFailed('Artifact contains an invalid PR number.');
return;
}
issueNumber = Number(prNumberString);
}
// Bind the PR number to the pull request that actually triggered the run.
const { data: pr } = await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: issueNumber,
});
if (pr.head.sha !== run.head_sha || pr.head.repo?.full_name !== run.head_repository?.full_name) {
core.notice(`PR #${issueNumber} has changed since the check ran; skipping.`);
return;
}
if (pr.base.ref !== 'main') {
core.notice(`PR #${issueNumber} does not target main; skipping.`);
return;
}
const comments = await github.paginate(github.rest.issues.listComments, {
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
});
const docUrl = 'https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/changing-the-base-branch-of-a-pull-request';
const alreadyCommented = comments.some(comment =>
comment.body && comment.body.includes(docUrl)
);
if (!alreadyCommented) {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
body: 'This pull request targets the `main` branch, but the primary branch for this repository is `master`.\n\n' +
'Please change the base branch of this pull request to `master`.\n\n' +
`See: ${docUrl}`,
});
}