-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
63 lines (53 loc) · 1.69 KB
/
index.js
File metadata and controls
63 lines (53 loc) · 1.69 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
import * as core from "@actions/core";
import * as path from "path";
const gqlDoc = `
mutation CreatePullRequest($identifier: String, $branch: String!, $name: String!, $context: Json!) {
createPullRequest(identifier: $identifier, branch: $branch, name: $name, context: $context) {
url
}
}
`
async function main() {
const url = core.getInput('url');
const token = core.getInput('token');
const prAutomation = core.getInput('prAutomation');
const branch = core.getInput('branch');
let identifier = core.getInput('identifier');
let context = core.getInput('context');
if (!context) {
context = '{}';
}
if (!identifier) {
identifier = null
}
try {
JSON.parse(context);
} catch (e) {
core.setFailed(`context must be a valid JSON object, got: ${context}`);
return;
}
const response = await fetch(path.join(url, 'gql'), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
},
body: JSON.stringify({
query: gqlDoc,
variables: { identifier, branch, context, name: prAutomation }
}),
});
if (!response.ok) {
const body = await response.text();
core.setFailed(`Failed to create pull request: ${response.status}\n${body}`);
return;
}
const resp = await response.json();
const pr = resp.data?.createPullRequest;
if (!pr) {
core.setFailed(`Failed to create pull request: ${JSON.stringify(resp.errors)}`);
return;
}
core.info(`Created pull request: ${pr.url}! Go to the given URL to review and approve your PR.`);
}
main();