-
Notifications
You must be signed in to change notification settings - Fork 61
173 lines (160 loc) · 6.33 KB
/
update-draft-release.yml
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
name: "Update Draft Release"
on:
push:
branches:
- 'release/**'
concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.event.pull_request.head.ref || github.ref }}
cancel-in-progress: true
env:
RELEASE_BRANCH_PREFIX: "release/"
jobs:
draft-release:
name: "Draft Release"
runs-on: ubuntu-latest
permissions:
contents: write
outputs:
id: ${{ steps.create-draft-release.outputs.id }}
rc-version: ${{ steps.create-draft-release.outputs.rc-version }}
steps:
- name: Create release draft
uses: actions/github-script@v7
id: create-draft-release
env:
TARGET_COMMITISH: "${{ github.ref_name }}"
RELEASE_BRANCH_PREFIX: "${{ env.RELEASE_BRANCH_PREFIX }}"
DEFAULT_BRANCH: "${{ github.event.repository.default_branch }}"
with:
script: |
const { repo, owner } = context.repo;
const target_commitish = process.env.TARGET_COMMITISH;
const default_branch = process.env.DEFAULT_BRANCH;
let version = target_commitish.replace(process.env.RELEASE_BRANCH_PREFIX, '')
const regexp = '^[v]?([0-9]+)\.([0-9]+)\.([0-9]+)(\.post([0-9]+))?$';
const {data: compare} = await github.rest.repos.compareCommits({
owner,
repo,
base: default_branch,
head: target_commitish,
});
const rc_version = `${version}rc${ compare.ahead_by }`
console.log(`rc-version: ${rc_version}`)
core.setOutput("rc-version", rc_version);
function compareVersions(a, b) {
if (a[1] === b[1])
if (a[2] === b[2])
if (a[3] === b[3])
return (+a[5] || -1) - (+b[5] || -1)
else
return +a[3] - b[3]
else
return +a[2] - b[2]
else
return +a[1] - b[1]
}
const versionMatch = version.match(regexp)
if (!versionMatch) {
core.setFailed(`Version "${version}" from branch "${target_commitish}" does not match the regexp ${regexp}`)
process.exit()
}
const tags = await github.paginate(
github.rest.repos.listTags,
{
owner,
repo,
per_page: 100
},
(response) => response.data
);
console.log(`Tags:`)
console.log(tags.map(e => e.name))
const matchedTags = tags.filter(e => e.name.indexOf(version) !== -1)
console.log(`Tags for ${version}:`)
console.log(matchedTags.map(e => e.name))
if (matchedTags.length !== 0) {
let newHotfixNumber = 0
for (let matchedTag of matchedTags) {
const matchVersion = matchedTag.name.match('^[v]?([0-9]+)\.([0-9]+)\.([0-9]+)(.post([0-9]+))?$')
if (matchVersion && matchVersion[5]) {
const hotfixNumber = parseInt(matchVersion[5])
if (newHotfixNumber <= hotfixNumber) {
newHotfixNumber = hotfixNumber + 1
}
}
}
version = `${version}.post${newHotfixNumber}`
}
console.log(`New version: ${version}`)
const rawTags = tags.map(e => e.name)
const tagsWithNew = [...rawTags, version]
const sortedTags = tagsWithNew
.filter(e => e.match(regexp))
.map((e => e.match(regexp)))
.sort(compareVersions)
.reverse()
.map(e => e[0])
const previousTag = sortedTags[sortedTags.indexOf(version)+1]
console.log(`Previous version: ${previousTag}`)
console.log('Find or Create a Draft release')
const releases = await github.paginate(
github.rest.repos.listReleases,
{
owner,
repo,
per_page: 100
},
(response) => response.data
);
let release = releases.find(e => target_commitish.endsWith(e.target_commitish) && e.draft)
if (release) {
console.log(`Draft release already exist ${release.html_url}`)
} else {
console.log(`Draft release is not found creating a new one`)
const {data: newDraftRelease} = await github.rest.repos.createRelease({
owner,
repo,
draft: true,
prerelease: false,
name: version,
tag_name: version,
target_commitish: target_commitish,
});
console.log(`Draft release is created ${newDraftRelease.html_url}`)
release = newDraftRelease;
core.setOutput("created", true);
}
console.log('Updating release with new release notes')
const {data: release_notes} = await github.rest.repos.generateReleaseNotes({
owner,
repo,
tag_name: version,
previous_tag_name: previousTag,
target_commitish: target_commitish,
});
const {data: updated_release} = await github.rest.repos.updateRelease({
owner,
repo,
release_id: release.id,
draft: true,
prerelease: false,
name: version,
tag_name: version,
target_commitish: target_commitish,
body: release_notes.body
});
console.log(`Draft release is updated ${updated_release.html_url}`)
core.setOutput("id", updated_release.id);
core.setOutput("tag_name", release.tag_name);
build-pypi:
name: "Build"
needs:
- draft-release
permissions:
contents: write
uses: ./.github/workflows/build_pypi.yml
with:
version: ${{ needs.draft-release.outputs.rc-version }}
ref: ${{ github.ref_name }}
release-id: ${{ needs.draft-release.outputs.id }}
secrets: inherit