Skip to content

Commit 1d863e3

Browse files
committed
fix(ui-scripts): fix publish and post-publish
Change-Id: Iec7798c4646b4fe96398dcbb7d9e2594ca559e5e Reviewed-on: https://gerrit.instructure.com/183228 Tested-by: Jenkins Product-Review: Jennifer Stern <jstern@instructure.com> Reviewed-by: Steve Jensen <sejensen@instructure.com> QA-Review: Jennifer Stern <jstern@instructure.com>
1 parent 89cff42 commit 1d863e3

File tree

6 files changed

+181
-146
lines changed

6 files changed

+181
-146
lines changed

packages/ui-scripts/lib/bump.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ async function bump (packageName, currentVersion, requestedVersion, config = {})
6767
}
6868

6969
info(`💾 Committing version bump commit for ${packageName} ${releaseVersion}...`)
70+
7071
try {
7172
commitVersionBump(releaseVersion)
7273
} catch (err) {

packages/ui-scripts/lib/post-publish.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,16 @@ const {
3232
} = require('./utils/slack')
3333
const {
3434
hasJiraConfig,
35-
getIssuesInRelease,
3635
getJiraVersion,
37-
updateJiraIssues,
38-
getIssuesInCommit
36+
updateJiraIssues
3937
} = require('./utils/jira')
4038
const {
4139
isReleaseCommit,
4240
createGitTagForRelease,
4341
setupGit,
44-
checkWorkingDirectory
42+
checkWorkingDirectory,
43+
getIssuesInCommit,
44+
getIssuesInRelease
4545
} = require('./utils/git')
4646

4747
try {
@@ -56,6 +56,8 @@ async function postPublish (packageName, releaseVersion, config = {}) {
5656
setupGit()
5757
checkWorkingDirectory()
5858

59+
const jiraProjectKey = `${config.jira_project_key}`
60+
5961
info(`📦 Running post-publish steps for ${releaseVersion} of ${packageName}...`)
6062

6163
let jiraVersion = { name: `${packageName} v${releaseVersion}` }
@@ -65,7 +67,7 @@ async function postPublish (packageName, releaseVersion, config = {}) {
6567
createGitTagForRelease(releaseVersion)
6668

6769
if (hasJiraConfig(config)) {
68-
issueKeys = await getIssuesInRelease(config)
70+
issueKeys = await getIssuesInRelease(jiraProjectKey)
6971
jiraVersion = await getJiraVersion(jiraVersion.name, config)
7072
if (issueKeys.length > 0 && jiraVersion.id) {
7173
await updateJiraIssues(issueKeys, jiraVersion.name, config)
@@ -77,7 +79,7 @@ async function postPublish (packageName, releaseVersion, config = {}) {
7779
}
7880
} else {
7981
if (hasJiraConfig(config)) {
80-
issueKeys = await getIssuesInCommit(config)
82+
issueKeys = await getIssuesInCommit(jiraProjectKey)
8183
}
8284
if (hasSlackConfig(config) && issueKeys.length > 0) {
8385
postReleaseCandidateSlackMessage(jiraVersion.name, issueKeys, config)

packages/ui-scripts/lib/publish.js

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@
2222
* SOFTWARE.
2323
*/
2424
const { getPackageJSON } = require('@instructure/pkg-utils')
25-
const { error, info, confirm } = require('@instructure/command-utils')
25+
const { error, info } = require('@instructure/command-utils')
2626

2727
const {
2828
publishPackages,
29-
bumpPackages,
3029
createNPMRCFile
3130
} = require('./utils/npm')
3231
const {
@@ -36,64 +35,36 @@ const {
3635
} = require('./utils/git')
3736
const {
3837
setupGit,
39-
checkWorkingDirectory,
40-
resetToCommit
38+
checkWorkingDirectory
4139
} = require('./utils/git')
4240
const { getConfig } = require('./utils/config')
4341

4442
try {
4543
const pkgJSON = getPackageJSON()
4644
// optional version argument:
47-
// e.g. ui-scripts --publish 5.11.0-dev
45+
// e.g. ui-scripts --publish alpha
4846
publish(pkgJSON.name, pkgJSON.version, process.argv[3], getConfig(pkgJSON))
4947
} catch (err) {
5048
error(err)
5149
process.exit(1)
5250
}
5351

54-
async function publish (packageName, currentVersion, requestedVersion = 'prerelease', config = {}) {
52+
async function publish (packageName, currentVersion, preidAndTag, config = {}) {
5553
setupGit()
5654
createNPMRCFile(config)
5755
checkWorkingDirectory()
5856

59-
let versionToRelease, npmTag
57+
let versionToRelease
6058

6159
if (isReleaseCommit(currentVersion)) {
6260
checkIfGitTagExists(currentVersion)
6361
checkIfCommitIsReviewed()
6462
info(`📦 Currently on release commit for ${currentVersion} of ${packageName}.`)
6563
versionToRelease = currentVersion
66-
npmTag = 'latest'
6764
} else {
68-
try {
69-
versionToRelease = await bumpPackages(packageName, requestedVersion)
70-
} catch (err) {
71-
error(err)
72-
process.exit(1)
73-
}
74-
info(`📦 Not on a release commit--${versionToRelease} will be a pre-release.`)
75-
npmTag = 'rc'
65+
info(`📦 Not on a release commit--publishing a pre-release...`)
66+
versionToRelease = 'prerelease'
7667
}
7768

78-
info(`📦 Version: ${versionToRelease}, Tag: ${npmTag}`)
79-
80-
if (!process.env.CI) {
81-
const reply = await confirm('Continue? [y/n]\n')
82-
if (!['Y', 'y'].includes(reply.trim())) {
83-
process.exit(0)
84-
}
85-
}
86-
87-
try {
88-
await publishPackages(packageName, versionToRelease, npmTag)
89-
} catch (err) {
90-
error(err)
91-
process.exit(1)
92-
}
93-
94-
if (npmTag === 'rc') {
95-
resetToCommit('HEAD')
96-
}
97-
98-
info(`📦 Version ${versionToRelease} of ${packageName} was successfully released!`)
69+
await publishPackages(packageName, versionToRelease, preidAndTag)
9970
}

packages/ui-scripts/lib/utils/git.js

Lines changed: 117 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
* SOFTWARE.
2323
*/
24-
const { runCommandSync, resolveBin, error } = require('@instructure/command-utils')
24+
const { runCommandSync, error, info } = require('@instructure/command-utils')
2525
const validateMessage = require('validate-commit-msg')
2626

2727
const runGitCommand = exports.runGitCommand = function runGitCommand (args = []) {
@@ -49,7 +49,7 @@ exports.setupGit = function setupGit () {
4949
remotes = runGitCommand(['remote']).split(/(\s+)/)
5050
} catch (err) {
5151
error(err)
52-
process.exit()
52+
process.exit(1)
5353
}
5454

5555
if (GIT_REMOTE_URL) {
@@ -79,21 +79,27 @@ exports.isReleaseCommit = function isReleaseCommit (version) {
7979
let result
8080

8181
try {
82-
const { stdout } = runCommandSync('git', [
83-
'log', '--oneline', '--format=%B', '-n', '1', 'HEAD', '|',
84-
resolveBin('head'), '-n', '1', '|',
85-
resolveBin('grep'), '"chore(release)"'
86-
], [], { stdio: 'pipe' })
87-
result = stdout
82+
result = runGitCommand(['log', '-1', '--pretty=format:%B'])
83+
result = result.split('\n')[0].trim()
8884
} catch (e) {
89-
return false
85+
error(e)
86+
process.exit(1)
9087
}
9188

92-
return result && (result.trim() === `chore(release): ${version}`)
89+
info(result)
90+
91+
return result && (result.startsWith(`chore(release): ${version}`))
9392
}
9493

9594
exports.checkWorkingDirectory = function checkWorkingDirectory () {
96-
const result = runGitCommand(['status', '--porcelain'])
95+
let result
96+
97+
try {
98+
result = runGitCommand(['status', '--porcelain'])
99+
} catch (e) {
100+
error(e)
101+
process.exit(1)
102+
}
97103

98104
if (result) {
99105
error(`Refusing to operate on unclean working directory!`)
@@ -104,7 +110,14 @@ exports.checkWorkingDirectory = function checkWorkingDirectory () {
104110

105111
exports.checkIfGitTagExists = function checkIfGitTagExists (version) {
106112
const tag = `v${version}`
107-
const result = runGitCommand(['tag', '--list', tag])
113+
let result
114+
115+
try {
116+
result = runGitCommand(['tag', '--list', tag])
117+
} catch (e) {
118+
error(e)
119+
process.exit(1)
120+
}
108121

109122
if (result) {
110123
error(`Git tag ${tag} already exists!`)
@@ -114,9 +127,16 @@ exports.checkIfGitTagExists = function checkIfGitTagExists (version) {
114127
}
115128

116129
exports.checkIfCommitIsReviewed = function checkIfCommitIsReviewed () {
117-
const result = runGitCommand(['log', '-n', '1', '|', resolveBin('grep'), 'Reviewed-on'])
130+
let result
118131

119-
if (!result) {
132+
try {
133+
result = runGitCommand(['log', '-1', '--pretty=format:%B'])
134+
} catch (e) {
135+
error(e)
136+
process.exit(1)
137+
}
138+
139+
if (!result || result.indexOf('Reviewed-on') < 0) {
120140
error('The release commit must be reviewed and merged prior to running the release!')
121141
error('Use "git pull --rebase" to pull down the latest from the remote.')
122142
process.exit(1)
@@ -128,9 +148,13 @@ exports.createGitTagForRelease = function createGitTagForRelease (version) {
128148
const { GIT_REMOTE_NAME } = process.env
129149
const origin = GIT_REMOTE_NAME || 'origin'
130150

131-
runGitCommand(['tag', '-am', `Version ${version}`, tag])
132-
133-
runGitCommand(['push', origin, tag])
151+
try {
152+
runGitCommand(['tag', '-am', `Version ${version}`, tag])
153+
runGitCommand(['push', origin, tag])
154+
} catch (e) {
155+
error(e)
156+
process.exit(1)
157+
}
134158
}
135159

136160
exports.commitVersionBump = function commitVersionBump (releaseVersion) {
@@ -140,3 +164,79 @@ exports.commitVersionBump = function commitVersionBump (releaseVersion) {
140164
exports.resetToCommit = function resetToCommit (commitish = 'HEAD') {
141165
runGitCommand(['reset', '--hard', commitish])
142166
}
167+
168+
function getPreviousReleaseCommit () {
169+
return runGitCommand(['rev-list', '--tags', '--skip=1', '--max-count=1'])
170+
}
171+
exports.getPreviousReleaseCommit = getPreviousReleaseCommit
172+
173+
function getCurrentReleaseTag () {
174+
return runGitCommand(['describe', '--exact-match'])
175+
}
176+
exports.getCurrentReleaseTag = getCurrentReleaseTag
177+
178+
function getPreviousReleaseTag () {
179+
return runGitCommand(['describe', '--abbrev=0', '--tags', getPreviousReleaseCommit()])
180+
}
181+
exports.getPreviousReleaseTag = getPreviousReleaseTag
182+
183+
const jiraMatcher = /((?!([A-Z0-9a-z]{1,10})-?$)[A-Z]{1}[A-Z0-9]+-\d+)/g
184+
185+
exports.getIssuesInRelease = function getIssuesInRelease (jiraProjectKey) {
186+
info(`Looking up issues for the ${jiraProjectKey} project...`)
187+
let currentReleaseTag, previousReleaseTag
188+
189+
try {
190+
currentReleaseTag = getCurrentReleaseTag()
191+
previousReleaseTag = getPreviousReleaseTag()
192+
} catch (e) {
193+
error(e)
194+
process.exit(1)
195+
}
196+
197+
let result
198+
199+
info(`${previousReleaseTag}..${currentReleaseTag}`)
200+
201+
try {
202+
result = runGitCommand(['log', `${previousReleaseTag}..${currentReleaseTag}`])
203+
} catch (e) {
204+
error(e)
205+
process.exit(1)
206+
}
207+
208+
let issueKeys = result.match(jiraMatcher) || []
209+
210+
issueKeys = issueKeys
211+
.filter(key => key.indexOf(jiraProjectKey) != -1)
212+
213+
if (issueKeys.length > 0) {
214+
issueKeys = Array.from(new Set(issueKeys))
215+
info(`Issues in this release: ${issueKeys.join(', ')}`)
216+
}
217+
218+
return issueKeys
219+
}
220+
221+
exports.getIssuesInCommit = function getIssuesInCommit (jiraProjectKey) {
222+
let result
223+
224+
try {
225+
result = runGitCommand(['log', '-1', '--pretty=format:%B'])
226+
} catch (e) {
227+
error(e)
228+
process.exit(1)
229+
}
230+
231+
let issueKeys = result.match(jiraMatcher) || []
232+
233+
issueKeys = issueKeys
234+
.filter(key => key.indexOf(jiraProjectKey) != -1)
235+
236+
if (issueKeys.length > 0) {
237+
issueKeys = Array.from(new Set(issueKeys))
238+
info(`Issues in this release: ${issueKeys.join(', ')}`)
239+
}
240+
241+
return issueKeys
242+
}

0 commit comments

Comments
 (0)