Skip to content

Commit

Permalink
feat: add tag regex input (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
thedaviddias committed Aug 3, 2023
1 parent 4e294ad commit 24cb334
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 20 deletions.
25 changes: 13 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,19 @@ jobs:
#### Inputs
| Name | Required | Default | Description |
| --------------------------- | -------- | ------- | --------------------------------------------------------------------------------------------------------- |
| `github_token` | yes | | Token to use to authorize label changes. Typically the GITHUB_TOKEN secret |
| `repo` | no | | Name of the repo (e.g. owner/repo) if not the current one |
| `contributor_replace_regex` | no | | Regular expression (regex) pattern to identify characters in the `contributor` name that will be replaced |
| `contributor_replace_char` | no | | The character that will replace specific characters in the `contributor` name |
| `slack_webhook_url` | no | | Slack webhook URL to receive release notifications |
| `jira_ticket_prefix` | no | | Prefix for JIRA ticket references in PR titles (e.g. ABC) |
| `jira_instance_url` | no | | URL for your JIRA instance to generate JIRA ticket links (e.g. https://your-jira-instance.com) |
| `sentry_project_name` | no | | ID of the Sentry project for error tracking |
| `sentry_project_name` | no | | Name of the Sentry project for error tracking |
| `grafana_dashboard_link` | no | | Link to the Grafana dashboard for monitoring |
| Name | Required | Default | Description |
| --------------------------- | -------- | --------------------------- | --------------------------------------------------------------------------------------------------------- |
| `github_token` | yes | | Token to use to authorize label changes. Typically the GITHUB_TOKEN secret |
| `repo` | no | | Name of the repo (e.g. owner/repo) if not the current one |
| `tag_regex` | no | ^v[0-9]+\\.[0-9]+\\.[0-9]+$ | Regex to accommodate varying tag formatting |
| `contributor_replace_regex` | no | | Regular expression (regex) pattern to identify characters in the `contributor` name that will be replaced |
| `contributor_replace_char` | no | | The character that will replace specific characters in the `contributor` name |
| `slack_webhook_url` | no | | Slack webhook URL to receive release notifications |
| `jira_ticket_prefix` | no | | Prefix for JIRA ticket references in PR titles (e.g. ABC) |
| `jira_instance_url` | no | | URL for your JIRA instance to generate JIRA ticket links (e.g. https://your-jira-instance.com) |
| `sentry_project_name` | no | | ID of the Sentry project for error tracking |
| `sentry_project_name` | no | | Name of the Sentry project for error tracking |
| `grafana_dashboard_link` | no | | Link to the Grafana dashboard for monitoring |


### Outputs
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ inputs:
description: Your GitHub token for authentication. It is used to get the repository information and generate the changelog.
required: true

tag_regex:
description: 'A regular expression to match against git tags. The first capture group should be the version number.'
required: false
default: '^v[0-9]+\\.[0-9]+\\.[0-9]+$' # Matches the "v" prefix by default

contributor_replace_regex:
description: An optional regular expression (regex) pattern to identify characters in the `contributor` name that will be replaced.
required: false
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as getContributorCommitsModule from '../utils/getContributorCommits'
import * as getBaseUrlFromContextModule from '../utils/getBaseUrlFromContext'
import * as generateRepoLinkModule from '../utils/generateRepoLink'
import * as generateReleaseLinkModule from '../utils/generateReleaseLink'
import * as compareSemVerModule from '../utils/compareSemVer'
// import * as getCommitOfTagModule from '../utils/getCommitOfTag'
// import * as generatePRListStringModule from '../utils/generatePRListString'

Expand All @@ -24,6 +25,7 @@ jest.mock('../utils/generateRepoLink')
jest.mock('../utils/generateReleaseLink')
jest.mock('../utils/getCommitOfTag')
jest.mock('../utils/generatePRListString')
jest.mock('../utils/compareSemVer')

const mockedGetInputs =
getInputsModule.getInputs as jest.Mock<getInputsModule.GetInputsType>
Expand All @@ -41,6 +43,7 @@ describe('run function', () => {
jiraTicketPrefix: 'ABC',
contributorReplaceChar: '.',
contributorReplaceRegex: '-',
tagRegex: '^v[0-9]+\\.[0-9]+\\.[0-9]+$'
})

const getOwnerAndRepoMock = jest.spyOn(getOwnerAndRepoModule, 'getOwnerAndRepo')
Expand Down Expand Up @@ -104,6 +107,7 @@ describe('run function', () => {
)
generateReleaseLinkMock.mockReturnValue('http://mock-release-link')


// const getCommitOfTagMock = jest.spyOn(getCommitOfTagModule, 'getCommitOfTag')
// getCommitOfTagMock.mockResolvedValue({ data: { html_url: 'http://mock-commit-url' } })

Expand Down
22 changes: 15 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +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
// Extract the version number from the tag using the provided regex
const tagRegex = new RegExp(options.tagRegex)
const currentVersionMatch = currentTag.name.match(tagRegex)
const previousVersionMatch = previousTag.name.match(tagRegex)

if (!currentVersionMatch || !previousVersionMatch) {
core.warning(
'Current or previous tag does not match the provided regular expression. Exiting.'
)
return
}

// Use the first capture group as the version number
const currentVersion = currentVersionMatch[1];
const previousVersion = previousVersionMatch[1];

const comparisonResult = compareSemVer(currentVersion, previousVersion)

Expand Down
8 changes: 8 additions & 0 deletions src/utils/__tests__/getInputs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ describe('getInputs', () => {
return '.'
case 'contributor_replace_regex':
return '-'
case 'tag_regex':
return 'my-tag-regex'
default:
return ''
}
Expand All @@ -42,6 +44,7 @@ describe('getInputs', () => {
jiraTicketPrefix: 'ABC',
contributorReplaceChar: '.',
contributorReplaceRegex: '-',
tagRegex: 'my-tag-regex'
}

expect(getInputs()).toEqual(expectedInputs)
Expand All @@ -52,6 +55,7 @@ describe('getInputs', () => {
expect(core.getInput).toHaveBeenCalledWith('slack_webhook_url')
expect(core.getInput).toHaveBeenCalledWith('jira_instance_url')
expect(core.getInput).toHaveBeenCalledWith('jira_ticket_prefix')
expect(core.getInput).toHaveBeenCalledWith('tag_regex')
expect(core.warning).toHaveBeenCalledWith(
"Invalid repository format: my-repo. It should be in 'owner/repo' format."
)
Expand Down Expand Up @@ -91,6 +95,8 @@ describe('getInputs', () => {
return '.'
case 'contributor_replace_regex':
return '-'
case 'tag_regex':
return '^v[0-9]+\\.[0-9]+\\.[0-9]+$'
default:
return ''
}
Expand All @@ -109,10 +115,12 @@ describe('getInputs', () => {
jiraTicketPrefix: 'ABC',
contributorReplaceChar: '.',
contributorReplaceRegex: '-',
tagRegex: '^v[0-9]+\\.[0-9]+\\.[0-9]+$',
}

expect(getInputs()).toEqual(expectedInputs)
expect(core.getInput).toHaveBeenCalledWith('repo')
expect(core.getInput).toHaveBeenCalledWith('tag_regex')
expect(core.getInput).toHaveBeenCalledWith('grafana_dashboard_link')
expect(core.getInput).toHaveBeenCalledWith('sentry_project_name')
expect(core.getInput).toHaveBeenCalledWith('sentry_project_id')
Expand Down
3 changes: 3 additions & 0 deletions src/utils/getInputs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ function isValidTicketPrefix(string: string): boolean {

export interface GetInputsType {
repo: string
tagRegex: string
sentryProjectName: string
sentryProjectId: string
grafanaDashboardLink: string
Expand All @@ -41,6 +42,7 @@ export function getInputs(): GetInputsType {
core.info('Input options obtained.')

const repo = core.getInput('repo') || ''
const tagRegex = core.getInput('tag_regex')
const jiraTicketPrefix = core.getInput('jira_ticket_prefix') || ''
const jiraInstanceUrl = core.getInput('jira_instance_url') || ''
const grafanaDashboardLink = core.getInput('grafana_dashboard_link') || ''
Expand Down Expand Up @@ -94,6 +96,7 @@ export function getInputs(): GetInputsType {

return {
repo,
tagRegex,
grafanaDashboardLink,
sentryProjectName,
sentryProjectId,
Expand Down

0 comments on commit 24cb334

Please sign in to comment.