forked from Spatial-Quotient/pr-semver-bump
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathversion.js
130 lines (110 loc) · 3.62 KB
/
version.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const core = require('@actions/core')
const github = require('@actions/github')
const semver = require('semver')
const childProcess = require('child_process')
const fs = require('fs')
const DEFAULT_VERSION = '0.0.0'
async function getCommitsOnBranch(branch, config) {
const commits = new Set()
// eslint-disable-next-line no-restricted-syntax
for await (const response of config.octokit.paginate.iterator(
config.octokit.rest.repos.listCommits,
{
...github.context.repo,
sha: branch,
},
)) {
response.data.forEach((commit) => {
commits.add(commit.sha)
})
}
return commits
}
async function getLatestVersionInCommits(
commits,
sortedVersions,
objectsByVersion,
config,
) {
for (let i = 0; i < sortedVersions.length; i++) {
const refObj = objectsByVersion[sortedVersions[i]]
if (refObj.type === 'commit' && commits.has(refObj.sha)) {
return `${sortedVersions[i]}`
}
if (refObj.type === 'tag') {
// eslint-disable-next-line no-await-in-loop
const tag = await config.octokit.rest.git.getTag({
...github.context.repo,
tag_sha: refObj.sha,
})
if (commits.has(tag.data.object.sha)) {
return `${sortedVersions[i]}`
}
}
}
return DEFAULT_VERSION
}
// Tags the specified version and annotates it with the provided release notes.
async function createRelease(version, releaseNotes, config) {
const tag = `${config.v}${version}`
if (!config.useSSH) {
const tagCreateResponse = await config.octokit.rest.git.createTag({
...github.context.repo,
tag: tag,
message: releaseNotes,
object: process.env.GITHUB_SHA,
type: 'commit',
})
await config.octokit.rest.git.createRef({
...github.context.repo,
ref: `refs/tags/${tag}`,
sha: tagCreateResponse.data.sha,
})
} else {
const releaseNotesFile = './.release-notes.txt'
fs.writeFileSync(releaseNotesFile, releaseNotes)
// The git checkout is already on the correct commit, simply
// add a tag to add the /refs/tags/...
childProcess.execSync(`git tag -F ${releaseNotesFile} ${tag}`)
childProcess.execSync('git push --tags')
}
return tag
}
// Returns the most recent tagged version in git.
async function getCurrentVersion(config) {
const data = await config.octokit.rest.git.listMatchingRefs({
...github.context.repo,
ref: 'tags/',
})
const objectsByVersion = new Map()
const versions = []
data.data.forEach((ref) => {
const version = semver.parse(ref.ref.replace(/^refs\/tags\//g, ''), {
loose: true,
})
if (version !== null) {
objectsByVersion[version] = ref.object
versions.push(version)
}
})
versions.sort(semver.rcompare)
if (config.baseBranch) {
const branch = process.env.GITHUB_BASE_REF
|| (process.env.GITHUB_REF
&& process.env.GITHUB_REF.replace('refs/heads/', ''))
core.info(`Only considering tags on branch ${branch}`)
const commits = await getCommitsOnBranch(branch, config)
return getLatestVersionInCommits(
commits,
versions,
objectsByVersion,
config,
)
}
if (versions[0] !== undefined) {
return `${versions[0]}`
}
return DEFAULT_VERSION
}
exports.createRelease = createRelease
exports.getCurrentVersion = getCurrentVersion