Skip to content
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

Add auto building action #2

Merged
merged 2 commits into from
May 23, 2024
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
89 changes: 89 additions & 0 deletions .github/workflows/pr-comment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
name: Add Template to Pull Request
on:
workflow_run:
workflows: ['Build Template']
types: [completed]
jobs:
pr_comment:
# if Build Template was successful and ran on a branch that is currently the head in a pull request
if: github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest

permissions:
pull-requests: write

steps:
- uses: actions/github-script@v6
with:
# This snippet is public-domain, taken from
# https://github.com/oprypin/nightly.link/blob/master/.github/workflows/pr-comment.yml
script: |
async function upsertComment(owner, repo, issue_number, purpose,old_body, body) {
const {data: comments} = await github.rest.issues.listComments(
{owner, repo, issue_number});

const marker = `\r\n<!-- DO NOT REMOVE!! -->\r\n<!-- bot: ${purpose} -->\r\n`;

const old_marker = new RegExp(marker.replace("\r\n", "\r?\n")).exec(old_body)?.[0] ?? marker;

body = old_body.split(old_marker)[0] + marker + body;
await github.request('PATCH /repos/{owner}/{repo}/pulls/{pull_number}', {
owner: owner,
repo: repo,
pull_number: issue_number,
body: body,
headers: {
'X-GitHub-Api-Version': '2022-11-28'
}
})
}

const {owner, repo} = context.repo;
const run_id = ${{github.event.workflow_run.id}};

const pull_head_sha = '${{github.event.workflow_run.head_sha}}';

/** @type {{old_body: string}} */
const {issue_number, old_body} = await (async () => {
const pulls = await github.rest.pulls.list({owner, repo});
for await (const {data} of github.paginate.iterator(pulls)) {
for (const pull of data) {
if (pull.head.sha === pull_head_sha) {
return {issue_number: pull.number, old_body: pull.body};
}
}
}
return {};
})();
if (issue_number) {
core.info(`Using pull request ${issue_number}`);
} else {
return core.error(`No matching pull request found`);
}

let old_sha = /\<\!-- commit-sha: (?<sha>[a-z0-9]+) --\>/i.exec(old_body)?.groups?.sha
if (old_sha != undefined && pull_head_sha == old_sha) return core.error("Comment is already up-to-date!")

const artifacts = await github.paginate(
github.rest.actions.listWorkflowRunArtifacts, {owner, repo, run_id});
if (!artifacts.length) {
return core.error(`No artifacts found, perhaps Build Template was skipped`);
}
const template = artifacts[0];

let body = `<!-- commit-sha: ${pull_head_sha} -->\n`;
body +=`## Download the template for this pull request: \n\n`;
body += `> [!NOTE]
> This is auto generated from [\`${{ github.workflow }}\`](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})\n`;
body += `- via manual download: [${template.name}.zip](https://nightly.link/${owner}/${repo}/actions/artifacts/${template.id}.zip)\n`;
body += `- via PROS Integrated Terminal: \n \`\`\`
curl -o ${template.name}.zip https://nightly.link/${owner}/${repo}/actions/artifacts/${template.id}.zip;
pros c fetch ${template.name}.zip;
pros c apply ${template.name};
rm ${template.name}.zip;
\`\`\``;

core.info(`Review thread message body: \n${body}`);

await upsertComment(owner, repo, issue_number,
"nightly-link", old_body, body);
18 changes: 18 additions & 0 deletions .github/workflows/pros-build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Build Template

on:
push:
branches: "**"
pull_request:
branches: "**"
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: LemLib/[email protected]
with:
library-path: gamepad
Loading