-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcheckBranch.js
65 lines (58 loc) · 2.14 KB
/
checkBranch.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
/**
* API: https://docs.github.com/en/rest/reference/repos#get-a-branch
*/
const core = require('@actions/core')
const github = require('@actions/github')
async function checkBranch({ token, owner, repo, branchName }) {
const octokit = github.getOctokit(token)
core.debug(
`Checking if branch exists with name ${branchName} in repo ${repo}`
)
// load all branches, since we need the info about the default branch as well
const { data: branches } = await octokit.rest.repos
.listBranches({ owner, repo })
.catch(({ status, data, message }) => {
core.error('Error loading existing branches from API')
core.debug(`status: ${status}`)
core.error(JSON.stringify(data))
throw new Error(message)
})
core.debug(`Branches: ${JSON.stringify(branches)}`)
if (!branches || !branches.length) {
throw new Error('No branches found')
}
// check if the branch name already exists
let branch = branches.find(function (branch) {
return branch.name === branchName
})
if (branch) {
core.debug(
`Branch with name ${branchName} already exists, continuing as normal`
)
core.debug('✔️ Check branch Done')
return branch
} else {
core.debug(`Need to create a new branch first with name ${branchName}`)
let defaultBranch = branches[0]
core.debug(
`Found default branch to branch of: ${defaultBranch.name} with sha: ${defaultBranch.commit.sha}`
)
// Docs: https://octokit.github.io/rest.js/v18#git-create-ref
const { data } = await octokit.rest.git
.createRef({
owner,
repo,
ref: `refs/heads/${branchName}`,
sha: defaultBranch.commit.sha,
})
.catch(err => {
core.error(`Error creatng new branch: ${err}`)
throw err
})
core.debug(
`Created new branch with ref: ${data.ref} based on ${defaultBranch.name}`
)
}
core.debug('✔️ Check branch Done')
}
module.exports = checkBranch