Skip to content

Commit

Permalink
Fix release tag parsing (segmentio#1007)
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky authored Nov 29, 2023
1 parent 47498c4 commit 72dc35a
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"devDependencies": {
"@changesets/changelog-github": "^0.4.5",
"@changesets/cli": "^2.23.2",
"@npmcli/promise-spawn": "^3.0.0",
"@npmcli/promise-spawn": "^7.0.0",
"@types/express": "4",
"@types/jest": "^28.1.1",
"@types/lodash": "^4",
Expand Down
128 changes: 127 additions & 1 deletion scripts/create-release-from-tags/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseReleaseNotes } from '..'
import { parseReleaseNotes, parseRawTags } from '..'
import fs from 'fs'
import path from 'path'

Expand Down Expand Up @@ -38,3 +38,129 @@ describe('parseReleaseNotes', () => {
`)
})
})

describe('parseRawTags', () => {
test('should work if all are on a single line', () => {
const rawTags =
'@segment/[email protected] @segment/[email protected] @segment/[email protected]'
const tags = parseRawTags(rawTags)
expect(tags).toMatchInlineSnapshot(`
Array [
Object {
"name": "@segment/analytics-next",
"raw": "@segment/[email protected]",
"versionNumber": "2.1.1",
},
Object {
"name": "@segment/analytics-foo",
"raw": "@segment/[email protected]",
"versionNumber": "1.0.1",
},
Object {
"name": "@segment/analytics-core",
"raw": "@segment/[email protected]",
"versionNumber": "1.0.0",
},
]
`)
})
test('should work if there are multiple columns', () => {
const rawTags = `
@segment/[email protected] @segment/[email protected]
@segment/[email protected] @segment/[email protected]
`
const tags = parseRawTags(rawTags)
expect(tags).toMatchInlineSnapshot(`
Array [
Object {
"name": "@segment/analytics-next",
"raw": "@segment/[email protected]",
"versionNumber": "2.1.1",
},
Object {
"name": "@segment/analytics-foo",
"raw": "@segment/[email protected]",
"versionNumber": "1.0.1",
},
Object {
"name": "@segment/analytics-core",
"raw": "@segment/[email protected]",
"versionNumber": "1.0.0",
},
Object {
"name": "@segment/analytics-bar",
"raw": "@segment/[email protected]",
"versionNumber": "1.0.1",
},
]
`)
})
test('should work if there are many many columns', () => {
const rawTags = `
@segment/[email protected] @segment/[email protected] @segment/[email protected]
@segment/[email protected] @segment/[email protected] @segment/[email protected]
@segment/[email protected]
`
const tags = parseRawTags(rawTags)
expect(tags).toMatchInlineSnapshot(`
Array [
Object {
"name": "@segment/analytics-next",
"raw": "@segment/[email protected]",
"versionNumber": "2.1.1",
},
Object {
"name": "@segment/analytics-foo",
"raw": "@segment/[email protected]",
"versionNumber": "1.0.1",
},
Object {
"name": "@segment/analytics-bar",
"raw": "@segment/[email protected]",
"versionNumber": "1.0.1",
},
Object {
"name": "@segment/analytics-next",
"raw": "@segment/[email protected]",
"versionNumber": "2.1.1",
},
Object {
"name": "@segment/analytics-baz",
"raw": "@segment/[email protected]",
"versionNumber": "1.0.1",
},
Object {
"name": "@segment/analytics-foobar",
"raw": "@segment/[email protected]",
"versionNumber": "1.0.1",
},
Object {
"name": "@segment/analytics-core",
"raw": "@segment/[email protected]",
"versionNumber": "1.0.0",
},
]
`)
})
test('should work if there is newline characters', () => {
const rawTags = `
@segment/[email protected]
@segment/[email protected]
`
const tags = parseRawTags(rawTags)
expect(tags).toMatchInlineSnapshot(`
Array [
Object {
"name": "@segment/analytics-next",
"raw": "@segment/[email protected]",
"versionNumber": "2.1.1",
},
Object {
"name": "@segment/analytics-core",
"raw": "@segment/[email protected]",
"versionNumber": "1.0.0",
},
]
`)
})
})
15 changes: 9 additions & 6 deletions scripts/create-release-from-tags/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export const getCurrentGitTags = async (): Promise<Tag[]> => {
'tag',
'--points-at',
'HEAD',
'--column',
])
if (code !== 0) {
throw new Error(stderr.toString())
Expand All @@ -34,10 +33,8 @@ export const getCurrentGitTags = async (): Promise<Tag[]> => {
return parseRawTags(stdout.toString())
}

export const getConfig = async ({
DRY_RUN,
TAGS,
}: NodeJS.ProcessEnv): Promise<Config> => {
export const getConfig = async (): Promise<Config> => {
const { DRY_RUN, TAGS } = process.env
const isDryRun = Boolean(DRY_RUN)
const tags = TAGS ? parseRawTags(TAGS) : await getCurrentGitTags()

Expand Down Expand Up @@ -116,7 +113,12 @@ const extractPartsFromTag = (rawTag: string): Tag | undefined => {
* @param rawTags - string delimited list of tags (e.g. `@segment/[email protected] @segment/[email protected]`)
*/
export const parseRawTags = (rawTags: string): Tag[] => {
return rawTags.trim().split(' ').map(extractPartsFromTag).filter(exists)
return rawTags
.trim()
.replace(new RegExp('\\n', 'g'), ' ') // remove any newLine characters
.split(' ')
.map(extractPartsFromTag)
.filter(exists)
}

/**
Expand Down Expand Up @@ -187,6 +189,7 @@ export const createReleaseFromTags = async (config: Config) => {
console.log('Processing tags:', config.tags, '\n')

for (const tag of config.tags) {
console.log(`\n ---> Creating release for tag: ${tag.raw}`)
await createGithubReleaseFromTag(tag, { dryRun: config.isDryRun })
}
}
2 changes: 1 addition & 1 deletion scripts/create-release-from-tags/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createReleaseFromTags, getConfig } from '.'

async function run() {
const config = await getConfig(process.env)
const config = await getConfig()
return createReleaseFromTags(config)
}

Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3587,12 +3587,12 @@ __metadata:
languageName: node
linkType: hard

"@npmcli/promise-spawn@npm:^3.0.0":
version: 3.0.0
resolution: "@npmcli/promise-spawn@npm:3.0.0"
"@npmcli/promise-spawn@npm:^7.0.0":
version: 7.0.0
resolution: "@npmcli/promise-spawn@npm:7.0.0"
dependencies:
infer-owner: ^1.0.4
checksum: 3454465a2731cea5875ba51f80873e2205e5bd878c31517286b0ede4ea931c7bf3de895382287e906d03710fff6f9e44186bd0eee068ce578901c5d3b58e7692
which: ^4.0.0
checksum: 22a8c4fd4ef2729cf75d13b0b294e8c695e08bdb2143e951288056656091fc5281e8baf330c97a6bc803e6fc09489028bf80dcd787972597ef9fda9a9349fc0f
languageName: node
linkType: hard

Expand Down Expand Up @@ -7151,7 +7151,7 @@ __metadata:
dependencies:
"@changesets/changelog-github": ^0.4.5
"@changesets/cli": ^2.23.2
"@npmcli/promise-spawn": ^3.0.0
"@npmcli/promise-spawn": ^7.0.0
"@types/express": 4
"@types/jest": ^28.1.1
"@types/lodash": ^4
Expand Down

0 comments on commit 72dc35a

Please sign in to comment.