forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenable-automerge.js
50 lines (45 loc) · 1.32 KB
/
enable-automerge.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
import { getOctokit } from '@actions/github'
main()
async function main() {
const [org, repo] = process.env.GITHUB_REPOSITORY.split('/')
if (!org || !repo) {
throw new Error('GITHUB_REPOSITORY environment variable not set')
}
const prNumber = process.env.AUTOMERGE_PR_NUMBER
if (!prNumber) {
throw new Error(`AUTOMERGE_PR_NUMBER environment variable not set`)
}
const token = process.env.GITHUB_TOKEN
if (!token) {
throw new Error(`GITHUB_TOKEN environment variable not set`)
}
const github = getOctokit(token)
const pull = await github.rest.pulls.get({
owner: org,
repo,
pull_number: parseInt(prNumber),
})
const pullNodeId = pull.data.node_id
console.log(`Pull request GraphQL Node ID: ${pullNodeId}`)
const mutation = `mutation ($id: ID!) {
enablePullRequestAutoMerge(input: {
pullRequestId: $id,
mergeMethod: MERGE
}) {
clientMutationId
}
}`
const variables = {
id: pullNodeId,
}
const graph = await github.graphql(mutation, variables)
console.log('GraphQL mutation result:\n' + JSON.stringify(graph))
if (graph.errors && graph.errors.length > 0) {
console.error(
'ERROR! Failed to enable auto-merge:\n - ' +
graph.errors.map((error) => error.message).join('\n - '),
)
} else {
console.log('Auto-merge enabled!')
}
}