Skip to content

Commit 69a3121

Browse files
authored
Adding action to generate branch for forks (#2977)
* adding action to generate branch for forks + build comment for forks * only create/update a new branch if the trigger comment was made in a fork pr * changing comment used to trigger build * restrict to open PRs only * changing trigger for testing
1 parent 76e5fc2 commit 69a3121

File tree

4 files changed

+136
-11
lines changed

4 files changed

+136
-11
lines changed

.circleci/comment.js

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,57 @@ const octokit = new Octokit({
77
run();
88

99
async function run() {
10+
let pr;
11+
// If we aren't running on a PR commit, double check if this is a branch created for a fork. If so, we'll need to
12+
// comment the build link on the fork.
1013
if (!process.env.CIRCLE_PULL_REQUEST) {
11-
return;
14+
try {
15+
const commit = await octokit.git.getCommit({
16+
owner: 'adobe',
17+
repo: 'react-spectrum',
18+
commit_sha: process.env.CIRCLE_SHA1
19+
});
20+
21+
// Check if it is a merge commit from the github "Branch from fork action"
22+
if (commit && commit.data?.parents?.length === 2 && commit.data.message.indexOf('Merge') > -1) {
23+
// Unfortunately listPullRequestsAssociatedWithCommit doesn't return fork prs so have to use search api
24+
// to find the fork PR the original commit lives in
25+
const forkHeadCommit = commit.data.parents[1].sha;
26+
const searchRes = await octokit.search.issuesAndPullRequests({
27+
q: `${forkHeadCommit}+repo:adobe/react-spectrum+is:pr+is:open`
28+
});
29+
30+
// Look for a PR that is from a fork and has a matching head commit as the current branch
31+
const pullNumbers = searchRes.data.items.filter(i => i.pull_request !== undefined).map(j => j.number);
32+
for (let pull_number of pullNumbers) {
33+
const {data} = await octokit.pulls.get({
34+
owner: 'adobe',
35+
repo: 'react-spectrum',
36+
pull_number
37+
});
38+
if (data && data.head.repo.full_name !== 'adobe/react-spectrum' && data.head.sha === forkHeadCommit) {
39+
pr = pull_number;
40+
break;
41+
}
42+
}
43+
}
44+
} catch (error) {
45+
console.error(error);
46+
}
47+
} else {
48+
pr = process.env.CIRCLE_PULL_REQUEST.split('/').pop();
1249
}
1350

14-
let pr = process.env.CIRCLE_PULL_REQUEST.split('/').pop();
15-
await octokit.issues.createComment({
16-
owner: 'adobe',
17-
repo: 'react-spectrum',
18-
issue_number: pr,
19-
body: `Build successful! 🎉
51+
if (pr != null) {
52+
await octokit.issues.createComment({
53+
owner: 'adobe',
54+
repo: 'react-spectrum',
55+
issue_number: pr,
56+
body: `Build successful! 🎉
2057
21-
* [View the storybook](https://reactspectrum.blob.core.windows.net/reactspectrum/${process.env.CIRCLE_SHA1}/storybook/index.html)
22-
* [View the storybook-16](https://reactspectrum.blob.core.windows.net/reactspectrum/${process.env.CIRCLE_SHA1}/storybook-16/index.html)
23-
* [View the documentation](https://reactspectrum.blob.core.windows.net/reactspectrum/${process.env.CIRCLE_SHA1}/docs/index.html)`
24-
});
58+
* [View the storybook](https://reactspectrum.blob.core.windows.net/reactspectrum/${process.env.CIRCLE_SHA1}/storybook/index.html)
59+
* [View the storybook-16](https://reactspectrum.blob.core.windows.net/reactspectrum/${process.env.CIRCLE_SHA1}/storybook-16/index.html)
60+
* [View the documentation](https://reactspectrum.blob.core.windows.net/reactspectrum/${process.env.CIRCLE_SHA1}/docs/index.html)`
61+
});
62+
}
2563
}

.github/actions/branch/action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: 'Branch from fork'
2+
description: 'creates a branch based off PR from fork'
3+
runs:
4+
using: 'node16'
5+
main: 'index.js'

.github/actions/branch/index.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
const core = require('@actions/core');
2+
const github = require('@actions/github');
3+
4+
const octokit = new github.GitHub(process.env.GITHUB_TOKEN);
5+
run();
6+
// Creates a branch off a fork PR
7+
async function run() {
8+
const context = github.context;
9+
try {
10+
// Get info of the fork PR
11+
const prNumber = context.issue.number;
12+
const {data} = await octokit.pulls.get({
13+
...context.repo,
14+
pull_number: prNumber
15+
});
16+
17+
// Only create/update a branch if the PR from a fork
18+
if (data.head.repo.full_name !== context.payload.repository.full_name) {
19+
const branch = `${data.user.login}-${data.head.ref}`;
20+
const ref = `refs/heads/${branch}`;
21+
const sha = data.merge_commit_sha;
22+
23+
let res;
24+
// Look up if branch for fork PR exists in base repo
25+
try {
26+
res = await octokit.repos.getBranch({
27+
...context.repo,
28+
branch
29+
});
30+
} catch (error) {
31+
if (!(error.name === 'HttpError' && error.status === 404)) {
32+
throw error;
33+
} else {
34+
// If branch doesn't exist for the forked PR, create one so we can get a
35+
// build for it and return
36+
await octokit.git.createRef({
37+
...context.repo,
38+
ref,
39+
sha
40+
});
41+
return;
42+
}
43+
}
44+
45+
// If branch already exists update it to match fork PR state.
46+
if (res.status === 200) {
47+
await octokit.git.updateRef({
48+
...context.repo,
49+
sha,
50+
ref: `heads/${branch}`,
51+
force: true
52+
})
53+
}
54+
}
55+
} catch (error) {
56+
core.setFailed(error.message);
57+
}
58+
}

.github/workflows/pr-comment.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: PR comment
2+
on: issue_comment
3+
4+
jobs:
5+
pr_commented:
6+
name: PR comment workflow
7+
if: |
8+
github.event.issue.pull_request &&
9+
github.repository == 'adobe/react-spectrum' &&
10+
github.actor == 'LFDanLu'
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@main
14+
- name: Use Node 16
15+
uses: actions/setup-node@v2
16+
with:
17+
node-version: '16'
18+
- name: install
19+
run: yarn install
20+
- name: Comment contains trigger
21+
if: contains(github.event.comment.body, 'GET_BUILD')
22+
uses: ./.github/actions/branch
23+
env:
24+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

0 commit comments

Comments
 (0)