blob: ae62cbac1672a3c634621862f5c47f5da12671b6 [file] [edit]
# Implements the /compile-time comment (usable by llvm-committers only).
# The changes get pushed to a perf/pr-NNNNNN branch on the
# llvmbot/llvm-project fork, which the compile-time tracker
# will pick up.
#
# For a PR that targets a branch other than main, such as a stacked PR,
# the base branch is pushed as one extra commit on top of its merge base
# with main. The PR itself goes on top of that commit. PRs to release
# branches are not supported.
name: Compile-time tracker
on:
issue_comment:
types:
- created
permissions:
contents: read
env:
BOT_REPOSITORY: llvmbot/llvm-project
jobs:
permission-check:
if: github.event.issue.pull_request && startswith(github.event.comment.body, '/compile-time')
runs-on: ubuntu-26.04
environment:
name: main-branch-only
deployment: false
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
sparse-checkout: |
.github/workflows/
- name: Check Permissions
uses: $/.github/workflows/require-team-membership
with:
team-slug: llvm-committers
LLVM_TOKEN_GENERATOR_CLIENT_ID: ${{ secrets.LLVM_TOKEN_GENERATOR_CLIENT_ID }}
LLVM_TOKEN_GENERATOR_PRIVATE_KEY: ${{ secrets.LLVM_TOKEN_GENERATOR_PRIVATE_KEY }}
push-branch:
name: Push branch for compile-time tracker
runs-on: ubuntu-26.04
timeout-minutes: 10
needs:
- permission-check
environment:
name: main-branch-only
deployment: false
concurrency:
group: compile-time-${{ github.event.issue.number }}
cancel-in-progress: false
steps:
- name: Get pull request
id: get-pr
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
const fs = require('fs');
const tmp = process.env.RUNNER_TEMP;
const fail = (reason) => {
fs.writeFileSync(`${tmp}/failure-reason`, reason);
core.setFailed(reason);
};
const getPR = async () => (await github.rest.pulls.get({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: context.payload.issue.number
})).data;
let pr = await getPR();
const defaultBranch = context.payload.repository.default_branch;
if (pr.state !== 'open') {
fail('The PR is not open.');
return;
}
if (pr.base.ref.startsWith('release/')) {
fail(`The PR targets \`${pr.base.ref}\`, release branches are not supported.`);
return;
}
const stacked = pr.base.ref !== defaultBranch;
// mergeable is null while GitHub is (re)computing the test merge
// commit in the background.
for (let i = 0; i < 12 && pr.mergeable === null; i++) {
await new Promise((resolve) => setTimeout(resolve, 5000));
pr = await getPR();
}
if (pr.mergeable === null) {
fail('GitHub has not computed a merge commit for the PR yet, please try again later.');
return;
}
if (!pr.mergeable) {
fail(`The PR has merge conflicts with \`${pr.base.ref}\`, please resolve them.`);
return;
}
let stackBase = '';
if (stacked) {
const mergeCommit = (await github.rest.git.getCommit({
owner: context.repo.owner,
repo: context.repo.repo,
commit_sha: pr.merge_commit_sha
})).data;
const stackHead = mergeCommit.parents[0].sha;
const comparison = (await github.rest.repos.compareCommitsWithBasehead({
owner: context.repo.owner,
repo: context.repo.repo,
basehead: `${defaultBranch}...${stackHead}`,
per_page: 1
})).data;
if (comparison.merge_base_commit.sha !== stackHead)
stackBase = comparison.merge_base_commit.sha;
}
// Insert zero-width spaces to prevent the commit in the fork
// from creating backlinks in referenced issues and PRs.
const ZWSP = '\u200b';
const protect = (s) => s
.replace(/#(?=\d)/g, '#' + ZWSP)
.replace(/\bGH-(?=\d)/gi, (m) => m + ZWSP)
.replace(/github\.com/gi, 'github' + ZWSP + '.com');
const body = (pr.body || '').replace(/\r\n/g, '\n').trim();
let message = `${pr.title} (#${pr.number})\n\n`;
if (body)
message += `${body}\n\n`;
message += `Compile-time testing for ${pr.html_url}, ` +
`requested by ${context.payload.comment.user.login}.\n`;
fs.writeFileSync(`${tmp}/commit-msg`, protect(message));
core.setOutput('merge-sha', pr.merge_commit_sha);
core.setOutput('stack-base', stackBase);
core.setOutput('author-name', pr.user.login);
core.setOutput('author-email',
`${pr.user.id}+${pr.user.login}@users.noreply.github.com`);
- name: Create and push branch
env:
PR_NUMBER: ${{ github.event.issue.number }}
MERGE_SHA: ${{ steps.get-pr.outputs.merge-sha }}
STACK_BASE: ${{ steps.get-pr.outputs.stack-base }}
GIT_AUTHOR_NAME: ${{ steps.get-pr.outputs.author-name }}
GIT_AUTHOR_EMAIL: ${{ steps.get-pr.outputs.author-email }}
GIT_COMMITTER_NAME: llvmbot
GIT_COMMITTER_EMAIL: llvmbot@llvm.org
BOT_TOKEN: ${{ secrets.RELEASE_WORKFLOW_PUSH_SECRET }}
run: |
git init -q llvm-project
cd llvm-project
# The first parent of the merge commit is the base branch, the
# second the PR. Use a blobless clone, blobs needed for the push
# will be fetched on demand.
git fetch --no-tags --progress --depth 2 --filter=blob:none \
"https://github.com/$GITHUB_REPOSITORY.git" "$MERGE_SHA"
PARENT="$MERGE_SHA^1"
if [ -n "$STACK_BASE" ]; then
# Squash the stack below the PR onto the main commit it is based
# on. Fixed identity, date and message keep the hash stable, so
# the tracker reuses its results for reruns and sibling PRs.
git fetch --no-tags --progress --depth 1 --filter=blob:none \
"https://github.com/$GITHUB_REPOSITORY.git" "$STACK_BASE"
STACK_HEAD=$(git rev-parse "$MERGE_SHA^1")
STACK_DATE=$(git log -1 --format=%cI "$STACK_HEAD")
PARENT=$(GIT_AUTHOR_NAME=llvmbot GIT_AUTHOR_EMAIL=llvmbot@llvm.org \
GIT_AUTHOR_DATE="$STACK_DATE" GIT_COMMITTER_DATE="$STACK_DATE" \
git commit-tree "$STACK_HEAD^{tree}" -p "$STACK_BASE" \
-m "Stacked PR base at $STACK_HEAD")
git show --stat "$PARENT"
fi
# Convert merge commit into a non-merge commit.
COMMIT=$(git commit-tree "$MERGE_SHA^{tree}" -p "$PARENT" -F "$RUNNER_TEMP/commit-msg")
git show --stat "$COMMIT"
# As we only have a shallow clone, git doesn't know which objects
# already exist in the fork, and would push the whole tree. Use
# push.negotiate to determine common commits with the remote first.
git -c push.negotiate=true \
push --force --progress \
"https://x-access-token:$BOT_TOKEN@github.com/$BOT_REPOSITORY.git" \
"$COMMIT:refs/heads/perf/pr-$PR_NUMBER"
- name: Comment on failure
if: failure()
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
env:
RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
with:
script: |
const fs = require('fs');
const reasonFile = `${process.env.RUNNER_TEMP}/failure-reason`;
let body = `Failed to create compile-time tracker branch: ${process.env.RUN_URL}`;
if (fs.existsSync(reasonFile))
body += '\n\n' + fs.readFileSync(reasonFile, 'utf8');
// Write files in the format expected by issue-write.yml.
fs.writeFileSync('comments', JSON.stringify([{ body }]));
fs.writeFileSync('pr_number', String(context.payload.issue.number));
- name: Upload comment and PR number
if: failure()
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: workflow-args
path: |
comments
pr_number
react-to-comment:
needs:
- push-branch
runs-on: ubuntu-26.04
permissions:
pull-requests: write
steps:
- name: Thumbs up comment
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
script: |
await github.rest.reactions.createForIssueComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: context.payload.comment.id,
content: '+1'
});