From 42d11af12d3eb037cce9795ac9f43bb6a5f33236 Mon Sep 17 00:00:00 2001 From: Uku Pattak Date: Wed, 18 Sep 2024 15:11:05 +0300 Subject: [PATCH] Fix situation where label already exists on the card (#126) --- dist/index.js | 14 ++++++++++++-- src/actions/addLabelToCards.test.ts | 18 ++++++++++++++++++ src/actions/addLabelToCards.ts | 10 +++++++++- src/actions/api/trello.ts | 2 +- 4 files changed, 40 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index 7a25bb6..f467061 100644 --- a/dist/index.js +++ b/dist/index.js @@ -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 }); @@ -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, diff --git a/src/actions/addLabelToCards.test.ts b/src/actions/addLabelToCards.test.ts index 87dff37..607da1c 100644 --- a/src/actions/addLabelToCards.test.ts +++ b/src/actions/addLabelToCards.test.ts @@ -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 } @@ -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' }]) diff --git a/src/actions/addLabelToCards.ts b/src/actions/addLabelToCards.ts index e6d1729..1d84357 100644 --- a/src/actions/addLabelToCards.ts +++ b/src/actions/addLabelToCards.ts @@ -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 }) } diff --git a/src/actions/api/trello.ts b/src/actions/api/trello.ts index 38fb904..cc860e4 100644 --- a/src/actions/api/trello.ts +++ b/src/actions/api/trello.ts @@ -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,