Skip to content

Commit

Permalink
Fix situation where label already exists on the card (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
ukupat authored Sep 18, 2024
1 parent 2b77f10 commit 42d11af
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
14 changes: 12 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34025,7 +34025,17 @@ async function addLabelToCards(conf, cardIds, head) {
const boardLabels = await (0, trello_1.getBoardLabels)(cardInfo.idBoard);
const matchingLabel = findMatchingLabel(branchLabel, boardLabels);
if (matchingLabel) {
await (0, trello_1.addLabelToCard)(cardId, matchingLabel.id);
try {
await (0, trello_1.addLabelToCard)(cardId, matchingLabel.id);
}
catch (error) {
if (error.response?.data === 'that label is already on the card') {
logger_1.default.log('Label already exists on the card', cardId, matchingLabel);
}
else {
throw error;
}
}
}
else {
logger_1.default.log('Could not find a matching label from the board', { branchLabel, boardLabels });
Expand Down Expand Up @@ -34342,7 +34352,7 @@ async function makeRequest(method, url, params) {
}
catch (error) {
const errorMessage = {
message: 'Failed to make a request',
message: 'Trello request was rejected',
method,
url,
params,
Expand Down
18 changes: 18 additions & 0 deletions src/actions/addLabelToCards.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jest.mock('./api/trello')
const getCardInfoMock = getCardInfo as jest.Mock
const getBoardLabelsMock = getBoardLabels as jest.Mock
const getBranchNameMock = getBranchName as jest.Mock
const addLabelToCardMock = addLabelToCard as jest.Mock

const head = { ref: 'chore/clean-code' }
const conf = { trelloAddLabelsToCards: true }
Expand Down Expand Up @@ -78,6 +79,23 @@ it('skips when correct label is already assigned', async () => {
expect(addLabelToCard).not.toHaveBeenCalled()
})

it('skips when correct label was just assigned moments ago', async () => {
getCardInfoMock.mockResolvedValueOnce({ id: 'card', labels: [] })
getBoardLabelsMock.mockResolvedValueOnce([{ id: 'chore-id', name: 'chore' }])
addLabelToCardMock.mockRejectedValue({ response: { data: 'that label is already on the card' } })

await addLabelToCards(conf, ['card'], head)
})

it('throws error when unexpected rejection comes from Trello', async () => {
getCardInfoMock.mockResolvedValueOnce({ id: 'card', labels: [] })
getBoardLabelsMock.mockResolvedValueOnce([{ id: 'chore-id', name: 'chore' }])

addLabelToCardMock.mockRejectedValue({ response: { status: 500 } })

await expect(addLabelToCards(conf, ['card'], head)).rejects.toMatchObject({ response: { status: 500 } })
})

it('skips when turned off', async () => {
getCardInfoMock.mockResolvedValueOnce({ id: 'card', labels: [] })
getBoardLabelsMock.mockResolvedValueOnce([{ id: 'chore-id', name: 'chore' }])
Expand Down
10 changes: 9 additions & 1 deletion src/actions/addLabelToCards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ export default async function addLabelToCards(conf: Conf, cardIds: string[], hea
const matchingLabel = findMatchingLabel(branchLabel, boardLabels)

if (matchingLabel) {
await addLabelToCard(cardId, matchingLabel.id)
try {
await addLabelToCard(cardId, matchingLabel.id)
} catch (error: any) {
if (error.response?.data === 'that label is already on the card') {
logger.log('Label already exists on the card', cardId, matchingLabel)
} else {
throw error
}
}
} else {
logger.log('Could not find a matching label from the board', { branchLabel, boardLabels })
}
Expand Down
2 changes: 1 addition & 1 deletion src/actions/api/trello.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async function makeRequest(method: 'get' | 'put' | 'post' | 'delete', url: strin
return response
} catch (error: any) {
const errorMessage = {
message: 'Failed to make a request',
message: 'Trello request was rejected',
method,
url,
params,
Expand Down

0 comments on commit 42d11af

Please sign in to comment.