-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #91 from rematocorp/split-main-into-smaller-files
Split main into smaller files
- Loading branch information
Showing
22 changed files
with
2,299 additions
and
1,496 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import addCardLinksToPullRequest from './addCardLinksToPullRequest' | ||
import { createComment, getPullRequestComments } from './api/github' | ||
import { getCardInfo } from './api/trello' | ||
|
||
jest.mock('@actions/core') | ||
jest.mock('@actions/github') | ||
jest.mock('./api/github') | ||
jest.mock('./api/trello') | ||
|
||
const getCardInfoMock = getCardInfo as jest.Mock | ||
const getPullRequestCommentsMock = getPullRequestComments as jest.Mock | ||
|
||
const conf = { githubIncludePrBranchName: true } | ||
const pr = { number: 0, state: 'open', title: 'Title' } | ||
|
||
it('adds link', async () => { | ||
getCardInfoMock.mockResolvedValueOnce({ shortUrl: 'short-url' }) | ||
|
||
await addCardLinksToPullRequest(conf, ['card'], pr) | ||
|
||
expect(getCardInfo).toHaveBeenCalledWith('card') | ||
expect(createComment).toHaveBeenCalledWith('short-url') | ||
}) | ||
|
||
it('adds multiple cards link', async () => { | ||
getCardInfoMock | ||
.mockResolvedValueOnce({ shortUrl: '1-short-url' }) | ||
.mockResolvedValueOnce({ shortUrl: '2-short-url' }) | ||
|
||
await addCardLinksToPullRequest({ ...conf, githubAllowMultipleCardsInPrBranchName: true }, ['1-card', '2-card'], pr) | ||
|
||
expect(getCardInfo).toHaveBeenNthCalledWith(1, '1-card') | ||
expect(getCardInfo).toHaveBeenNthCalledWith(2, '2-card') | ||
expect(createComment).toHaveBeenCalledWith('1-short-url\n2-short-url') | ||
}) | ||
|
||
it('skips link adding when already in PR description', async () => { | ||
await addCardLinksToPullRequest(conf, ['card'], { ...pr, body: 'https://trello.com/c/card/title' }) | ||
expect(createComment).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('skips link adding when already in PR comment', async () => { | ||
getPullRequestCommentsMock.mockResolvedValueOnce([{ body: 'https://trello.com/c/card/title' }]) | ||
|
||
await addCardLinksToPullRequest(conf, ['card'], pr) | ||
|
||
expect(createComment).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('skips when turned off', async () => { | ||
await addCardLinksToPullRequest({ githubIncludePrBranchName: false }, ['card'], pr) | ||
|
||
expect(createComment).not.toHaveBeenCalled() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { Conf, PR } from '../types' | ||
import { createComment, getPullRequest, getPullRequestComments } from './api/github' | ||
import { getCardInfo } from './api/trello' | ||
import matchCardIds from './utils/matchCardIds' | ||
|
||
export default async function addCardLinksToPullRequest(conf: Conf, cardIds: string[], pr: PR) { | ||
if (!conf.githubIncludePrBranchName) { | ||
return | ||
} | ||
const pullRequest = conf.githubIncludeNewCardCommand ? await getPullRequest() : pr | ||
|
||
if (matchCardIds(conf, pullRequest.body || '')?.length) { | ||
console.log('Card is already linked in the PR description') | ||
|
||
return | ||
} | ||
const comments = (await getPullRequestComments()) || [] | ||
|
||
for (const comment of comments) { | ||
if (matchCardIds(conf, comment.body)?.length) { | ||
console.log('Card is already linked in the comment') | ||
|
||
return | ||
} | ||
} | ||
console.log('Commenting Trello card URLs to PR', cardIds) | ||
|
||
const cards = await Promise.all(cardIds.map((id) => getCardInfo(id))) | ||
|
||
await createComment(cards.map((card) => card.shortUrl).join('\n')) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import addLabelToCards from './addLabelToCards' | ||
import { addLabelToCard, getBoardLabels, getCardInfo } from './api/trello' | ||
|
||
jest.mock('@actions/core') | ||
jest.mock('@actions/github') | ||
jest.mock('./api/github') | ||
jest.mock('./api/trello') | ||
|
||
const getCardInfoMock = getCardInfo as jest.Mock | ||
const getBoardLabelsMock = getBoardLabels as jest.Mock | ||
|
||
const head = { ref: 'chore/clean-code' } | ||
const conf = { trelloAddLabelsToCards: true } | ||
|
||
it('adds branch category as a card label', async () => { | ||
getCardInfoMock.mockResolvedValueOnce({ id: 'card', labels: [] }) | ||
getBoardLabelsMock.mockResolvedValueOnce([{ id: 'chore-id', name: 'chore' }]) | ||
|
||
await addLabelToCards(conf, ['card'], head) | ||
|
||
expect(addLabelToCard).toHaveBeenCalledWith('card', 'chore-id') | ||
}) | ||
|
||
it('adds partially matching branch category as a card label', async () => { | ||
getCardInfoMock.mockResolvedValueOnce({ id: 'card', labels: [] }) | ||
getBoardLabelsMock.mockResolvedValueOnce([{ id: 'bug-id', name: 'bug' }]) | ||
|
||
await addLabelToCards(conf, ['card'], { ref: 'bugfix/stupid-bug' }) | ||
|
||
expect(addLabelToCard).toHaveBeenCalledWith('card', 'bug-id') | ||
}) | ||
|
||
it('skips when branch category is not found', async () => { | ||
getCardInfoMock.mockResolvedValueOnce({ id: 'card', labels: [] }) | ||
getBoardLabelsMock.mockResolvedValueOnce([{ id: 'chore-id', name: 'chore' }]) | ||
|
||
await addLabelToCards(conf, ['card'], { ref: 'clean-code' }) | ||
|
||
expect(addLabelToCard).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('skips when branch category does not match existing labels', async () => { | ||
getCardInfoMock.mockResolvedValueOnce({ id: 'card', labels: [] }) | ||
getBoardLabelsMock.mockResolvedValueOnce([{ id: 'feature-id', name: 'feature' }]) | ||
|
||
await addLabelToCards(conf, ['card'], head) | ||
|
||
expect(addLabelToCard).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('skips when has conflicting label', async () => { | ||
getCardInfoMock.mockResolvedValueOnce({ id: 'card', labels: [{ name: 'bugfix' }] }) | ||
getBoardLabelsMock.mockResolvedValueOnce([{ id: 'chore-id', name: 'chore' }]) | ||
|
||
await addLabelToCards({ ...conf, trelloConflictingLabels: ['bugfix'] }, ['card'], head) | ||
|
||
expect(addLabelToCard).not.toHaveBeenCalled() | ||
}) | ||
|
||
it('skips when turned off', async () => { | ||
getCardInfoMock.mockResolvedValueOnce({ id: 'card', labels: [] }) | ||
getBoardLabelsMock.mockResolvedValueOnce([{ id: 'chore-id', name: 'chore' }]) | ||
|
||
await addLabelToCards({ trelloAddLabelsToCards: false }, ['card'], head) | ||
|
||
expect(addLabelToCard).not.toHaveBeenCalled() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { BoardLabel, Conf, PRHead } from '../types' | ||
import { getBranchName } from './api/github' | ||
import { addLabelToCard, getBoardLabels, getCardInfo } from './api/trello' | ||
|
||
export default async function addLabelToCards(conf: Conf, cardIds: string[], head: PRHead) { | ||
if (!conf.trelloAddLabelsToCards) { | ||
console.log('Skipping label adding') | ||
|
||
return | ||
} | ||
console.log('Starting to add labels to cards') | ||
|
||
const branchLabel = await getBranchLabel(head) | ||
|
||
if (!branchLabel) { | ||
console.log('Could not find branch label') | ||
|
||
return | ||
} | ||
|
||
return Promise.all( | ||
cardIds.map(async (cardId) => { | ||
const cardInfo = await getCardInfo(cardId) | ||
const hasConflictingLabel = cardInfo.labels.find( | ||
(label) => conf.trelloConflictingLabels?.includes(label.name) || label.name === branchLabel, | ||
) | ||
|
||
if (hasConflictingLabel) { | ||
console.log('Skipping label adding to a card because it has a conflicting label', cardInfo.labels) | ||
|
||
return | ||
} | ||
const boardLabels = await getBoardLabels(cardInfo.idBoard) | ||
const matchingLabel = findMatchingLabel(branchLabel, boardLabels) | ||
|
||
if (matchingLabel) { | ||
await addLabelToCard(cardId, matchingLabel.id) | ||
} else { | ||
console.log('Could not find a matching label from the board', branchLabel, boardLabels) | ||
} | ||
}), | ||
) | ||
} | ||
|
||
async function getBranchLabel(prHead?: PRHead) { | ||
const branchName = prHead?.ref || (await getBranchName()) | ||
const matches = branchName.match(/^([^\/]*)\//) | ||
|
||
if (matches) { | ||
return matches[1] | ||
} else { | ||
console.log('Did not find branch label', branchName) | ||
} | ||
} | ||
|
||
function findMatchingLabel(branchLabel: string, boardLabels: BoardLabel[]) { | ||
if (!branchLabel) { | ||
return | ||
} | ||
const match = boardLabels.find((label) => label.name === branchLabel) | ||
|
||
if (match) { | ||
return match | ||
} | ||
console.log('Could not match the exact label name, trying to find partially matching label') | ||
|
||
return boardLabels.find((label) => branchLabel.startsWith(label.name)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import addPullRequestLinkToCards from './addPullRequestLinkToCards' | ||
import { addAttachmentToCard, getCardAttachments } from './api/trello' | ||
|
||
jest.mock('@actions/core') | ||
jest.mock('@actions/github') | ||
jest.mock('./api/github') | ||
jest.mock('./api/trello') | ||
|
||
const getCardAttachmentsMock = getCardAttachments as jest.Mock | ||
|
||
const pr = { number: 0, state: 'open', title: 'Title' } | ||
|
||
it('adds link', async () => { | ||
await addPullRequestLinkToCards(['card'], { ...pr, html_url: 'html_url' }) | ||
expect(addAttachmentToCard).toHaveBeenCalledWith('card', 'html_url') | ||
|
||
await addPullRequestLinkToCards(['card'], { ...pr, url: 'url' }) | ||
expect(addAttachmentToCard).toHaveBeenCalledWith('card', 'url') | ||
}) | ||
|
||
it('skips link adding when already exists', async () => { | ||
getCardAttachmentsMock.mockResolvedValueOnce([{ url: 'pr-url' }]) | ||
|
||
await addPullRequestLinkToCards(['card'], { ...pr, url: 'pr-url' }) | ||
|
||
expect(addAttachmentToCard).not.toHaveBeenCalled() | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { PR } from '../types' | ||
import { addAttachmentToCard, getCardAttachments } from './api/trello' | ||
|
||
export default async function addPullRequestLinkToCards(cardIds: string[], pr: PR) { | ||
const link = pr.html_url || pr.url | ||
|
||
return Promise.all( | ||
cardIds.map(async (cardId) => { | ||
const existingAttachments = await getCardAttachments(cardId) | ||
|
||
if (existingAttachments?.some((it) => it.url.includes(link))) { | ||
console.log('Found existing attachment, skipping adding attachment', cardId, link) | ||
|
||
return | ||
} | ||
|
||
return addAttachmentToCard(cardId, link) | ||
}), | ||
) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.