-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
102 lines (86 loc) · 2.73 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
const core = require('@actions/core');
const github = require('@actions/github');
const token = core.getInput('token');
if(!token) {
console.log("token is nil")
core.setFailed("The github token is nil");
return
}
const octokit = github.getOctokit(token)
async function getPullRequestId(url) {
const query = `query($pullRequestURL: URI!) {
resource(url: $pullRequestURL) {
... on PullRequest {
id
}
}
}`;
const variables = {
pullRequestURL: url
}
const result = await octokit.graphql(query, variables)
if (result.errors && result.errors.length > 0) {
throw result.errors[0].message
}
if (!result.resource.id) {
throw 'unexpected error. Could not get id for pull request'
}
return result.resource.id
}
async function enablePullRequestAutoMerge(pullId, mergeMethod, author, commitHeadline, commitBody) {
const query = `mutation($pullId: ID!, $mergeMethod: PullRequestMergeMethod!, $authorEmail: String!, $commitHeadline: String!, $commitBody: String) {
enablePullRequestAutoMerge(input: {pullRequestId: $pullId, authorEmail: $authorEmail, commitBody: $commitBody, commitHeadline: $commitHeadline, mergeMethod: $mergeMethod}) {
__typename
}
}`;
const variables = {
pullId: pullId,
mergeMethod: mergeMethod,
authorEmail: author,
commitHeadline: commitHeadline,
commitBody: commitBody
}
const result = await octokit.graphql(query, variables)
if (result.errors && result.errors.length > 0) {
throw result.errors[0].message
}
if (!result.enablePullRequestAutoMerge) {
throw 'unexpected error'
}
}
function getMergeMethod(type) {
if (type == "squash") {
return "SQUASH"
} else if (type == "rebase") {
return "REBASE"
} else {
return "MERGE"
}
}
async function run() {
var pullRequestUrl = core.getInput('pull-request-url');
const mergeType = core.getInput('type');
const author = core.getInput('author');
const headline = core.getInput('commit-headline');
const message = core.getInput('commit-message');
if (!pullRequestUrl && github.context.eventName == 'pull_request') {
pullRequestUrl = github.context.payload.pull_request.html_url
}
if (!pullRequestUrl) {
core.setFailed("Pull request url is required");
return
}
try {
const pullId = await getPullRequestId(pullRequestUrl)
await enablePullRequestAutoMerge(
pullId,
getMergeMethod(mergeType),
author,
headline,
message
)
} catch (error) {
core.setFailed(error.message)
}
}
run()