Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for Jira in title #10

Merged
merged 2 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .node-version
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

132 changes: 132 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,138 @@ describe('#pull-request', () => {
})
})

describe('when title includes Jira ticket', () => {
describe('and when PR update request is successful', () => {
describe('and when PR description already includes preview/Jira links', () => {
describe('and when links are changing', () => {
beforeAll(async () => {
jest.resetModules()
jest.resetAllMocks()
ticket = 'A1C-1234'
preview = 'https://preview-123.example.com'
const options = {
branch: `some-feature`,
preview,
updateStatus: HTTP_STATUS_SUCCESS,
prBody: '**[Preview](foo-bar.com)**\n**[Jira ticket](jira.com)**\n\nMore details',
prTitle: `${ticket} - some-feature`,
}
;({ setFailedSpy, errorSpy, prUpdateSpy } = await mockContext(options))
await import('.')
})

it('does not set failed status', () => {
expect(setFailedSpy).not.toHaveBeenCalled()
expect(errorSpy).not.toHaveBeenCalled()
})

it('updates the current links in description', () => {
expect(prUpdateSpy).toHaveBeenCalledWith({
...DEFAULT_REQUEST_OPTIONS,
body:
`**[Preview](${preview})**\n` +
`**[Jira ticket](https://account.atlassian.net/browse/${ticket})**\n\nMore details`,
})
})
})

describe('and when links are not changing', () => {
describe('and when PR title already includes Jira ticket', () => {
beforeAll(async () => {
jest.resetModules()
jest.resetAllMocks()
ticket = 'A1C-1234'
preview = 'preview-123'
const options = {
branch: `${ticket}-some-feature`,
preview,
updateStatus: HTTP_STATUS_SUCCESS,
prTitle: `${ticket} - Some feature`,
prBody:
`**[Preview](${preview})**\n` +
`**[Jira ticket](https://account.atlassian.net/browse/${ticket})**\n\nMore details`,
}
;({ setFailedSpy, errorSpy, prUpdateSpy } = await mockContext(options))
await import('.')
})

it('does not set failed status', () => {
expect(setFailedSpy).not.toHaveBeenCalled()
expect(errorSpy).not.toHaveBeenCalled()
})

it('does not update PR', () => {
expect(prUpdateSpy).not.toHaveBeenCalled()
})
})
})
})

describe('and when PR description does not include preview/Jira links yet', () => {
beforeAll(async () => {
jest.resetModules()
jest.resetAllMocks()
ticket = 'A1C-1234'
preview = 'preview-123'
const options = {
branch: `${ticket}-some-feature`,
preview,
updateStatus: HTTP_STATUS_SUCCESS,
}
;({ setFailedSpy, errorSpy, prUpdateSpy } = await mockContext(options))
await import('.')
})

it('does not set failed status', () => {
expect(setFailedSpy).not.toHaveBeenCalled()
expect(errorSpy).not.toHaveBeenCalled()
})

it('updates PR title and description', () => {
expect(prUpdateSpy).toHaveBeenCalledWith({
...DEFAULT_REQUEST_OPTIONS,
title: `${ticket} - title`,
body:
`**[Preview](${preview})**\n` +
`**[Jira ticket](https://account.atlassian.net/browse/${ticket})**\n\nbody`,
})
})
})
})

describe('and when PR update request fails', () => {
beforeAll(async () => {
jest.resetModules()
jest.resetAllMocks()
ticket = 'A1C-1234'
preview = 'preview-123'
const options = {
branch: `${ticket}-some-feature`,
preview,
updateStatus: HTTP_STATUS_ERROR,
}
;({ errorSpy, prUpdateSpy } = await mockContext(options))
await import('.')
})

it('tries to update PR title and description', () => {
expect(prUpdateSpy).toHaveBeenCalledWith({
...DEFAULT_REQUEST_OPTIONS,
title: `${ticket} - title`,
body:
`**[Preview](${preview})**\n` +
`**[Jira ticket](https://account.atlassian.net/browse/${ticket})**\n\nbody`,
})
})

it('sets error status', () => {
expect(errorSpy).toHaveBeenCalledWith(
`Updating the pull request has failed with ${HTTP_STATUS_ERROR}`
)
})
})
})

describe('when current branch does not include Jira ticket', () => {
describe('and when exception-regex input does not match current branch', () => {
beforeAll(async () => {
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ async function run(): Promise<void> {

let ticketLine = ''
const headBranch = context.payload.pull_request.head.ref
const [ticketInBranch] = headBranch.match(ticketRegex) || []
const [ticketInBranch] =
headBranch.match(ticketRegex) || context.payload.pull_request.title.match(ticketRegex) || []

if (ticketInBranch) {
const jiraLink = `https://${jiraAccount}.atlassian.net/browse/${ticketInBranch}`
Expand Down
Loading