-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·182 lines (158 loc) · 5.66 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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const core = require('@actions/core');
const Octokit = require("@octokit/core").Octokit
const github = require('@actions/github');
const releaseType = "patch"
const patternRelease = /(?<prefix>[a-zA-Z0-9\-]+)?(?<major>\d+)(\.)(?<minor>\d+)(\.)(?<patch>\d+)?(?<postfix>[a-zA-Z0-9\-]+)?/g
const owner = process.env.OWNER
const repo = process.env.REPO
const githubToken = process.env.GITHUB_TOKEN
const octokit = new Octokit({ auth: githubToken });
async function createRelease() {
try {
const prnum = github.context.payload.number
const service = core.getInput('service');
if (service == ""){
console.log("service name is required")
return
}
const prefix = core.getInput('prefix');
const postfix = core.getInput('postfix');
response = await octokit.request(`GET /repos/${owner}/${repo}/pulls/${prnum}`, {
owner: owner,
repo: repo,
})
const { closed_at, merged_at, user, title, labels } = response.data
var isCorrectService = false
for (const label of labels) {
isCorrectService = label.name == service
if (isCorrectService) {
console.log("service tag found: ", label.name)
break
}
}
if(!isCorrectService) {
console.log("label service doesn't match with ", service)
return
}
var isOpenPR = closed_at == null && merged_at == null
if (isOpenPR) {
console.log("Status: OPEN")
return
}
var isClosePR = closed_at != null && merged_at == null
if (isClosePR) {
console.log("Status: CLOSE")
return
}
var isMergePR = closed_at != null && merged_at != null
if (isMergePR) {
console.log("Status: MERGE")
}
response = await octokit.request(`GET /repos/${owner}/${repo}/releases`, {
owner: owner,
repo: repo,
})
var isDraftExist = false
var existingDraftID = 0
var latestRelease = ""
var latestMajor = 0
var latestMinor = 0
var latestPatch = 0
for (const release of response.data) {
var regexPattern = new RegExp(patternRelease, 'i')
var result = regexPattern.exec(release.tag_name)
var isMatchTagName = result == null
if (isMatchTagName) {
continue
}
var isMatchPrefixOrPostfix = prefix == result.groups['prefix'] || postfix == result.groups['postfix']
if (isMatchPrefixOrPostfix) {
var isLatestReleaseEmpty = latestRelease == ""
if (isLatestReleaseEmpty) {
latestRelease = release.tag_name
latestMajor = parseInt(result.groups['major'])
latestMinor = parseInt(result.groups['minor'])
latestPatch = parseInt(result.groups['patch'])
}
isDraftExist = release.draft
if (isDraftExist) {
existingDraftID = release.id
break
}
}
}
changesLog = `* ${title} #${prnum} @${user.login}`
console.log('latest release:', latestRelease, ', draft:', isDraftExist)
if(isDraftExist) {
console.log('using existing draft release:', latestRelease)
await appendChangesLog(latestRelease, existingDraftID, changesLog)
return
}
var { latestMajor, latestMinor, latestPatch } = increaseVersion(releaseType, latestMajor, latestMinor, latestPatch)
latestRelease = `${prefix}${latestMajor}.${latestMinor}.${latestPatch}${postfix}`
console.log('create new draft release:', latestRelease)
await generateNewRelease(latestRelease, changesLog)
} catch(err) {
console.log("error ", err)
}
}
createRelease()
function increaseVersion(releaseType, latestMajor, latestMinor, latestPatch) {
switch (releaseType) {
case 'major':
latestMajor++
break
case 'minor':
if (latestMinor < 100) {
latestMinor++
break
}
latestMinor = 0
latestMajor++
break
default:
if (latestPatch < 100) {
latestPatch++
break
}
latestPatch = 0
if (latestMinor < 100) {
latestMinor++
break
}
latestMinor = 0
latestMajor++
break
}
return {latestMajor, latestMinor, latestPatch}
}
async function generateNewRelease(latestRelease, changesLog) {
await octokit.request(`POST /repos/{owner}/{repo}/releases`, {
owner: owner,
repo: repo,
tag_name: latestRelease,
draft: true,
prerelease: true,
name: latestRelease,
body: `Changes: \n${changesLog}`,
})
console.log('create new draft release')
}
async function appendChangesLog(latestRelease, existingDraftID, changesLog) {
var response = await octokit.request(`GET /repos/{owner}/{repo}/releases/{asset_id}`, {
owner: owner,
repo: repo,
asset_id: `${existingDraftID}`,
})
var newBody = `${response.data.body} \n${changesLog}`
var response = await octokit.request(`PATCH /repos/{owner}/{repo}/releases/{asset_id}`, {
owner: owner,
repo: repo,
asset_id: existingDraftID,
draft: true,
prerelease: true,
tag_name: latestRelease,
body: newBody,
})
console.log('update draft release')
}