-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
79 lines (66 loc) · 2.3 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
const github = require('@actions/github')
const core = require('@actions/core')
const { Octokit } = require('@octokit/rest')
const token = core.getInput('github_token')
const target_branch = core.getInput('target_branch')
const source_ref = core.getInput('source_ref', { required: false }) || github.context.sha
let commit_message = core.getInput('commit_message')
const allow_fast_forward = core.getBooleanInput('allow_fast_forward', { required: false })
const force_fast_forward = core.getBooleanInput('force_fast_forward', { required: false })
const octokit = new Octokit({auth: token});
const repo = github.context.repo
const merge = async () => {
commit_message = commit_message
.replace('{source_ref}', source_ref)
.replace('{target_branch}', target_branch);
let mergeParams = {
owner: repo.owner,
repo: repo.repo,
base: target_branch,
head: source_ref,
commit_message: commit_message || null
}
Object.keys(mergeParams).forEach((k) => mergeParams[k] == null && delete mergeParams[k])
core.debug(`Trying to merge`)
if (core.isDebug()) {
console.log(mergeParams)
}
return await octokit.rest.repos.merge(mergeParams).then(() => {
core.info(`Merged ${source_ref} into ${target_branch}`)
return true
}).catch((e) => {
core.warning(e.toString())
return false
})
}
const fastForward = async () => {
let fastForwardParams = {
owner: repo.owner,
repo: repo.repo,
ref: `heads/${target_branch}`,
sha: source_ref,
force: force_fast_forward
}
Object.keys(fastForwardParams).forEach((k) => fastForwardParams[k] == null && delete fastForwardParams[k])
core.debug(`Trying to fast-forward`)
if (core.isDebug()) {
console.log(fastForwardParams)
}
return await octokit.rest.git.updateRef(fastForwardParams).then(() => {
core.info(`Fast-Forwarded ${source_ref} into ${target_branch}`)
return true
}).catch((e) => {
core.warning(e.toString())
return merge()
})
}
try {
(allow_fast_forward ? fastForward() : merge()).then((success) => {
core.debug('Done')
if (!success) {
core.setFailed('Something went wrong')
}
})
} catch (e) {
core.setFailed(e.message)
}