Skip to content

Commit

Permalink
Merge pull request #91 from rematocorp/split-main-into-smaller-files
Browse files Browse the repository at this point in the history
Split main into smaller files
  • Loading branch information
ukupat authored Mar 26, 2024
2 parents 5fb4169 + 59ce0cc commit 685d51a
Show file tree
Hide file tree
Showing 22 changed files with 2,299 additions and 1,496 deletions.
1,870 changes: 1,270 additions & 600 deletions dist/index.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@
],
"collectCoverage": true,
"collectCoverageFrom": [
"./src/**"
"src/**",
"!src/index.ts",
"!src/actions/api/**"
]
},
"eslintConfig": {
Expand Down
54 changes: 54 additions & 0 deletions src/actions/addCardLinksToPullRequest.test.ts
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()
})
31 changes: 31 additions & 0 deletions src/actions/addCardLinksToPullRequest.ts
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'))
}
67 changes: 67 additions & 0 deletions src/actions/addLabelToCards.test.ts
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()
})
68 changes: 68 additions & 0 deletions src/actions/addLabelToCards.ts
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))
}
27 changes: 27 additions & 0 deletions src/actions/addPullRequestLinkToCards.test.ts
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()
})
20 changes: 20 additions & 0 deletions src/actions/addPullRequestLinkToCards.ts
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.
2 changes: 1 addition & 1 deletion src/trelloRequests.ts → src/actions/api/trello.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios'
import * as core from '@actions/core'
import { BoardLabel } from './types'
import { BoardLabel } from '../../types'

const trelloApiKey = core.getInput('trello-api-key', { required: true })
const trelloAuthToken = core.getInput('trello-auth-token', { required: true })
Expand Down
Loading

0 comments on commit 685d51a

Please sign in to comment.