Skip to content

Commit

Permalink
feat: add detection of versions for tags (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
thedaviddias authored Aug 3, 2023
1 parent fca8630 commit 4e294ad
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

16 changes: 16 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { generatePRListString } from './utils/generatePRListString'
import { generateSlackMessage } from './utils/generateSlackMessage'
import { parseTicketNumberFromTitle } from './utils/parseTicketNumberFromTitle'
import { generateJiraTicketLink } from './utils/generateJiraTicketLink'
import { compareSemVer } from './utils/compareSemVer'

// eslint-disable-next-line @typescript-eslint/require-await
export async function run(): Promise<void> {
Expand All @@ -36,6 +37,21 @@ export async function run(): Promise<void> {
return
}

// remove the "v" from the start of the tag, if it's there
const currentVersion = currentTag.name.startsWith('v')
? currentTag.name.slice(1)
: currentTag.name
const previousVersion = previousTag.name.startsWith('v')
? previousTag.name.slice(1)
: previousTag.name

const comparisonResult = compareSemVer(currentVersion, previousVersion)

if (comparisonResult <= 0) {
core.warning('Current tag is not greater than the previous tag. Exiting.')
return
}

// Get all the commits between the current and previous tags
const commits = await getCommitsBetweenTags(
octokit,
Expand Down
11 changes: 11 additions & 0 deletions src/utils/__tests__/compareSemVer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { compareSemVer } from '../compareSemVer'

describe('compareSemVer', () => {
test('it should correctly compare version numbers', () => {
expect(compareSemVer('1.0.0', '1.0.0')).toBe(0)
expect(compareSemVer('1.0.0', '0.9.9')).toBe(1)
expect(compareSemVer('1.0.0', '1.0.1')).toBe(-1)
expect(compareSemVer('1.2.3', '1.2')).toBe(1)
expect(compareSemVer('1.0', '1.0.1')).toBe(-1)
})
})
26 changes: 26 additions & 0 deletions src/utils/compareSemVer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Compare two semantic version strings.
*
* @param version1 - The first version string
* @param version2 - The second version string
*
* @returns -1 if version1 < version2, 0 if they're equal, and 1 if version1 > version2.
*/
export function compareSemVer(version1: string, version2: string): number {
const v1parts = version1.split('.').map(Number)
const v2parts = version2.split('.').map(Number)

for (let i = 0; i < v1parts.length; ++i) {
if (v2parts.length === i) {
return 1
}
if (v1parts[i] === v2parts[i]) {
continue
} else if (v1parts[i] > v2parts[i]) {
return 1
} else {
return -1
}
}
return v1parts.length !== v2parts.length ? -1 : 0
}
1 change: 0 additions & 1 deletion src/utils/generateJiraTicketLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export function generateJiraTicketLink(
ticketNumber: string,
instanceUrl: string
): string {

// Ensure instanceUrl ends with a slash
const formattedInstanceUrl = instanceUrl.endsWith('/')
? instanceUrl
Expand Down

0 comments on commit 4e294ad

Please sign in to comment.