-
Notifications
You must be signed in to change notification settings - Fork 2.9k
147 lines (124 loc) · 5.56 KB
/
shadow-pr.yml
File metadata and controls
147 lines (124 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
name: shadow pr
on:
pull_request_target:
types: [opened, synchronize, reopened]
jobs:
open-pr:
if: ${{ startsWith(github.event.pull_request.body, '<!-- from mono -->') != true }}
runs-on: ubuntu-latest
steps:
- name: Get pull request
uses: actions/github-script@v3
id: pr
with:
github-token: ${{secrets.HASURA_BOT_GH_TOKEN}}
script: |
const pullRequestNumber = context.payload.number;
const pr = await github.pulls.get({
owner: 'hasura',
repo: 'graphql-engine',
pull_number: pullRequestNumber
});
if (pr.status != 200) {
core.setFailed('API request to get pull request returned non-success status code ' + pr.status);
return;
}
core.setOutput('ossPrBaseSha', pr.data.base.sha);
core.setOutput('ossPrTitle', pr.data.title);
core.setOutput('ossPrUrl', pr.data.html_url);
const fs = require('fs');
fs.writeFileSync('pr-body.txt', pr.data.body);
let kodiakCommitMessage = `<!-- kodiak-commit-message-body-start: do not remove/edit this line -->\n`;
kodiakCommitMessage += `GITHUB_PR_NUMBER: ${pullRequestNumber}\nGITHUB_PR_URL: ${pr.data.html_url}`;
fs.writeFileSync('kodiak-commit-message-body.txt', kodiakCommitMessage);
- name: Get all authors of the pull request
uses: actions/github-script@v3
id: commit-authors
with:
github-token: ${{secrets.HASURA_BOT_GH_TOKEN}}
script: |
const pullRequestNumber = context.payload.number;
const commits = await github.pulls.listCommits({
owner: 'hasura',
repo: 'graphql-engine',
pull_number: pullRequestNumber
});
if (commits.status != 200) {
core.setFailed('API request to get commits of pull request returned non-success status code ' + commits.status);
return;
}
let authors = commits.data.map(c => `${c.commit.author.name} <${c.commit.author.email}>`);
authors = Array.from(new Set(authors));
core.setOutput('allCommitAuthors', authors.join(','));
core.setOutput('coAuthoredBy', authors.map(author => `Co-authored-by: ${author}`).join('\n'));
core.setOutput('firstCommitAuthor', authors[0]);
- uses: actions/checkout@v2
with:
fetch-depth: 0
token: ${{secrets.HASURA_BOT_GH_TOKEN}}
repository: hasura/graphql-engine-mono
path: graphql-engine-mono
- uses: actions/checkout@v2
with:
fetch-depth: 0
repository: hasura/graphql-engine
path: graphql-engine
- name: "Merge and push pr branch"
env:
PR_NUMBER: ${{ github.event.number }}
PR_TITLE: ${{ steps.pr.outputs.ossPrTitle }}
PR_URL: ${{ steps.pr.outputs.ossPrUrl }}
PR_CO_AUTHORS: ${{ steps.commit-authors.outputs.coAuthoredBy }}
COMMIT_AUTHOR: ${{ steps.commit-authors.outputs.firstCommitAuthor }}
run: |
cp graphql-engine-mono/bot.gitconfig $HOME/.gitconfig
COMMIT_MESSAGE=$(printf "$PR_TITLE\n\n$PR_CO_AUTHORS\nGITHUB_PR_NUMBER: $PR_NUMBER\nGITHUB_PR_URL: $PR_URL")
mkdir -p graphql-engine-transforms
pushd graphql-engine
git fetch origin pull/$PR_NUMBER/head:migration-source
git checkout migration-source
git merge master
mv .circleci ../graphql-engine-transforms/oss-.circleci
mv .github ../graphql-engine-transforms/oss-.github
mv .gitignore ../graphql-engine-transforms/oss-.gitignore
rsync -av --delete ./* ../graphql-engine-mono --exclude .git
popd
rsync -av --delete graphql-engine-transforms/* graphql-engine-mono/
pushd graphql-engine-mono
git status
git add .
git commit --author="$COMMIT_AUTHOR" -m "$COMMIT_MESSAGE"
git checkout -b oss_pr_refs/pull/$PR_NUMBER/head
git push origin oss_pr_refs/pull/$PR_NUMBER/head -f
popd
- name: "Open pull request"
uses: actions/github-script@v3
with:
github-token: ${{secrets.HASURA_BOT_GH_TOKEN}}
script: |
const fs = require('fs');
const prBody = fs.readFileSync('pr-body.txt', 'utf-8');
const kodiakCommitMessage = fs.readFileSync('kodiak-commit-message-body.txt', 'utf-8');
let body = `This PR was migrated from ${{ steps.pr.outputs.ossPrUrl }} \n\n---\n`;
body += `${prBody} \n\n---\n\n`;
body += `### Kodiak commit message\nInformation used by [Kodiak bot](https://kodiakhq.com/) while merging this PR.\n\n`;
body += `#### Commit title\nSame as the title of this pull request\n\n`;
body += `#### Commit body\n(Append below if you want to add something to the commit body)\n\n${kodiakCommitMessage}`
try {
const pr = await github.pulls.create({
owner: 'hasura',
repo: 'graphql-engine-mono',
head: 'oss_pr_refs/pull/${{ github.event.number }}/head',
base: 'main',
title: '${{ steps.pr.outputs.ossPrTitle }}',
body,
});
console.log('Migrated PR in graphql-engine-mono = ', pr.data.html_url);
} catch (err) {
if (err.message.includes('pull request already exists')) {
console.log(`Skipping pull request creation: ${err.message}`);
} else {
console.error(err);
core.setFailed(`Failed to create pull request: ${err.message}`);
}
}