forked from gr2m/create-or-update-pull-request-action
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
260 lines (214 loc) · 7.2 KB
/
index.js
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
const assert = require("assert");
const { inspect } = require("util");
const { command } = require("execa");
const core = require("@actions/core");
const { request } = require("@octokit/request");
const TEMPORARY_BRANCH_NAME = `tmp-create-or-update-pull-request-action-${Math.random()
.toString(36)
.substr(2)}`;
main();
async function main() {
if (!process.env.GITHUB_TOKEN) {
core.setFailed(
`GITHUB_TOKEN is not configured. Make sure you made it available to your action
uses: gr2m/create-or-update-pull-request-action@master
env:
GITHUB_TOKEN: \${{ secrets.GITHUB_TOKEN }}`
);
return;
}
if (!process.env.GITHUB_REPOSITORY) {
core.setFailed(
'GITHUB_REPOSITORY missing, must be set to "<repo owner>/<repo name>"'
);
return;
}
if (!process.env.GITHUB_REF) {
core.setFailed(
"GITHUB_REF missing, must be set to the repository's default branch"
);
return;
}
try {
const inputs = {
title: core.getInput("title"),
body: core.getInput("body"),
branch: core.getInput("branch").replace(/^refs\/heads\//, ""),
path: core.getInput("path"),
commitMessage: core.getInput("commit-message"),
author: core.getInput("author")
};
core.debug(`Inputs: ${inspect(inputs)}`);
const {
data: { default_branch }
} = await request(`GET /repos/${process.env.GITHUB_REPOSITORY}`, {
headers: {
authorization: `token ${process.env.GITHUB_TOKEN}`
}
});
const DEFAULT_BRANCH = default_branch;
core.debug(`DEFAULT_BRANCH: ${DEFAULT_BRANCH}`);
const { hasChanges } = await getLocalChanges(inputs.path);
if (!hasChanges) {
if (inputs.path) {
core.info(`No local changes matching "${inputs.path}"`);
} else {
core.info("No local changes");
}
process.exit(0); // there is currently no neutral exit code
}
core.debug(`Local changes found`);
await runShellCommand(`git checkout -b "${TEMPORARY_BRANCH_NAME}"`);
const gitUser = await getGitUser();
if (gitUser) {
core.debug(`Git User already configured as: ${inspect(gitUser)}`);
} else {
const matches = inputs.author.match(/^([^<]+)\s*<([^>]+)>$/);
assert(
matches,
`The "author" input "${inputs.author}" does conform to the "Name <[email protected]>" format`
);
const [, name, email] = matches;
await setGitUser({
name,
email
});
}
if (inputs.path) {
core.debug(`Committing local changes matching "${inputs.path}"`);
await runShellCommand(`git add "${inputs.path}"`);
} else {
core.debug(`Committing all local changes`);
await runShellCommand("git add .");
}
await runShellCommand(
`git commit -m "${inputs.commitMessage}" --author "${inputs.author}"`
);
try {
await runShellCommand(`git branch "${inputs.branch}"`)
} catch (error) {
}
await runShellCommand(`git checkout "${inputs.branch}"`)
await runShellCommand(`git reset --hard "${TEMPORARY_BRANCH_NAME}"`)
// const currentBranch = await runShellCommand(
// `git rev-parse --abbrev-ref HEAD`
// );
// if (currentBranch === DEFAULT_BRANCH) {
// core.info(`Already in base branch "${currentBranch}".`);
// } else {
// core.debug(`rebase all local changes on base branch`);
// await runShellCommand(
// `git fetch https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git ${DEFAULT_BRANCH}:${DEFAULT_BRANCH}`
// );
// await runShellCommand(`git stash --include-untracked`);
// await runShellCommand(`git rebase -X theirs "${DEFAULT_BRANCH}"`);
// }
core.debug(`Try to fetch and checkout remote branch "${inputs.branch}"`);
// const remoteBranchExists = await checkOutRemoteBranch(inputs.branch);
core.debug(`Pushing local changes`);
await runShellCommand(
`GIT_CURL_VERBOSE=1 GIT_TRACE=1 git push -f https://x-access-token:${process.env.GITHUB_TOKEN}@github.com/${process.env.GITHUB_REPOSITORY}.git HEAD:refs/heads/${inputs.branch}`
);
// if (remoteBranchExists) {
// core.debug(`Requestion Branch if exist ololol`);
// const q = `head:${inputs.branch} type:pr is:open repo:${process.env.GITHUB_REPOSITORY}`;
// const { data } = await request("GET /search/issues", {
// headers: {
// authorization: `token ${process.env.GITHUB_TOKEN}`
// },
// q: q,
// });
// if (data.total_count > 0) {
// core.info(
// `Existing pull request for branch "${inputs.branch}" updated`
// );
// return;
// }
// }
core.debug(`Creating pull request`);
const {
data: { html_url }
} = await request(`POST /repos/${process.env.GITHUB_REPOSITORY}/pulls`, {
headers: {
authorization: `token ${process.env.GITHUB_TOKEN}`
},
title: inputs.title,
body: inputs.body,
head: inputs.branch,
base: DEFAULT_BRANCH
});
core.info(`Pull request created: ${html_url}`);
await runShellCommand(`git stash pop || true`);
core.info(`Getting pull request number info`);
const re = /\d{1,}$/i
const pr_numer = html_url.match(re)
core.info(`::set-output name=PULL_REQUEST_NUMBER::${pr_numer}`)
} catch (error) {
core.debug(inspect(error));
core.setFailed(error.message);
}
}
async function getLocalChanges(path) {
const output = await runShellCommand(`git status ${path || "*"}`);
if (/nothing to commit, working tree clean/i.test(output)) {
return {};
}
const hasUncommitedChanges = /(Changes to be committed|Changes not staged|Untracked files)/.test(
output
);
return {
hasUncommitedChanges,
hasChanges: hasUncommitedChanges
};
}
async function getGitUser() {
try {
const name = await runShellCommand("git config --get user.name");
const email = await runShellCommand("git config --get user.email");
return {
name,
email
};
} catch (error) {
return;
}
}
async function setGitUser({ name, email }) {
core.debug(`Configuring user.name as "${name}"`);
await runShellCommand(`git config --global user.name "${name}"`);
core.debug(`Configuring user.email as "${email}"`);
await runShellCommand(`git config --global user.email "${email}"`);
}
async function checkOutRemoteBranch(branch) {
try {
core.debug('Fetch origin')
await runShellCommand(
`git fetch origin`
);
const currentBranch = await runShellCommand(
`git rev-parse --abbrev-ref HEAD`
);
if (currentBranch === branch) {
core.info(`Already in "${branch}".`);
return true;
}
await runShellCommand(`git checkout ${branch}`);
return true;
} catch (error) {
core.info(`Branch "${branch}" does not yet exist on remote.`);
await runShellCommand(`git checkout -b ${branch}`);
return false;
}
}
async function runShellCommand(commandString) {
core.debug(`$ ${commandString}`);
try {
const { stdout, stderr } = await command(commandString, { shell: true });
const output = [stdout, stderr].filter(Boolean).join("\n");
core.debug(output);
return output;
} catch (error) {
core.debug(inspect(error));
throw error;
}
}