Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ukupat committed Jul 25, 2024
1 parent 56c3f54 commit 3fcb8b3
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 96 deletions.
96 changes: 50 additions & 46 deletions dist/index.js

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions src/actions/addCardLinksToPullRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@ import logger from './utils/logger'
import matchCardIds from './utils/matchCardIds'

export default async function addCardLinksToPullRequest(conf: Conf, cardIds: string[]) {
logger.log('--- ADD CARD LINKS TO PR ---')

const bodyCardIds = await getCardIdsFromBody(conf)
const commentsCardIds = await getCardIdsFromComments(conf)
const linkedCardIds = [...bodyCardIds, ...commentsCardIds]

const unlinkedCardIds = cardIds.filter((id) => !linkedCardIds.includes(id))

if (!unlinkedCardIds.length) {
logger.log('LINK: Skipping card linking as all cards are already mentioned under the PR')
logger.log('Skipping card linking as all cards are already mentioned under the PR')

return
}
logger.log('LINK: Commenting Trello card URLs to PR', unlinkedCardIds)
logger.log('Commenting Trello card URLs to PR', unlinkedCardIds)

const cards = await Promise.all(unlinkedCardIds.map((id) => getCardInfo(id)))
const urls = cards.map((card) => card.shortUrl)
Expand Down
16 changes: 8 additions & 8 deletions src/actions/addLabelToCards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import { addLabelToCard, getBoardLabels, getCardInfo } from './api/trello'
import logger from './utils/logger'

export default async function addLabelToCards(conf: Conf, cardIds: string[], head?: PRHead) {
logger.log('--- ADD LABEL TO CARDS ---')

if (!conf.trelloAddLabelsToCards) {
logger.log('LABELS: Skipping label adding')
logger.log('Skipping label adding')

return
}
logger.log('LABELS: Starting to add labels to cards')

const branchLabel = await getBranchLabel(head)

if (!branchLabel) {
logger.log('LABELS: Could not find branch label')
logger.log('Could not find branch label')

return
}
Expand All @@ -27,7 +27,7 @@ export default async function addLabelToCards(conf: Conf, cardIds: string[], hea
)

if (hasConflictingLabel) {
logger.log('LABELS: Skipping label adding to a card as it has a conflicting label', cardInfo.labels)
logger.log('Skipping label adding to a card as it has a conflicting label', cardInfo.labels)

return
}
Expand All @@ -37,7 +37,7 @@ export default async function addLabelToCards(conf: Conf, cardIds: string[], hea
if (matchingLabel) {
await addLabelToCard(cardId, matchingLabel.id)
} else {
logger.log('LABELS: Could not find a matching label from the board', { branchLabel, boardLabels })
logger.log('Could not find a matching label from the board', { branchLabel, boardLabels })
}
}),
)
Expand All @@ -50,7 +50,7 @@ async function getBranchLabel(prHead?: PRHead) {
if (matches) {
return matches[1]
} else {
logger.log('LABELS: Did not find branch label', branchName)
logger.log('Did not find branch label', branchName)
}
}

Expand All @@ -60,7 +60,7 @@ function findMatchingLabel(branchLabel: string, boardLabels: BoardLabel[]) {
if (match) {
return match
}
logger.log('LABELS: Could not match the exact label name, trying to find partially matching label')
logger.log('Could not match the exact label name, trying to find partially matching label')

return boardLabels.find((label) => branchLabel.startsWith(label.name))
}
4 changes: 3 additions & 1 deletion src/actions/addPullRequestLinkToCards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ import { addAttachmentToCard, getCardAttachments } from './api/trello'
import logger from './utils/logger'

export default async function addPullRequestLinkToCards(cardIds: string[], pr: PR) {
logger.log('--- ADD PR LINK TO CARDS ---')

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))) {
logger.log('LINK: Found existing attachment, skipping adding attachment', { cardId, link })
logger.log('Found existing attachment, skipping adding attachment', { cardId, link })

return
}
Expand Down
4 changes: 2 additions & 2 deletions src/actions/api/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export async function getPullRequestRequestedReviewers() {
}

export async function createComment(shortUrl: string) {
logger.log('LINK: Creating PR comment', shortUrl)
logger.log('Creating PR comment', shortUrl)

await octokit.rest.issues.createComment({
owner,
Expand All @@ -96,7 +96,7 @@ export async function createComment(shortUrl: string) {
}

export async function updatePullRequestBody(newBody: string) {
logger.log('LINK: Updating PR body', newBody)
logger.log('Updating PR body', newBody)

await octokit.rest.issues.update({
owner,
Expand Down
12 changes: 6 additions & 6 deletions src/actions/api/trello.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export async function getCardAttachments(cardId: string): Promise<{ url: string
}

export async function addAttachmentToCard(cardId: string, link: string) {
logger.log('LINK: Adding attachment to the card', { cardId, link })
logger.log('Adding attachment to the card', { cardId, link })

return makeRequest('post', `https://api.trello.com/1/cards/${cardId}/attachments`, { url: link })
}
Expand All @@ -57,23 +57,23 @@ export async function getBoardLabels(boardId: string): Promise<BoardLabel[]> {
}

export async function addLabelToCard(cardId: string, labelId: string) {
logger.log('LABELS: Adding label to a card', { cardId, labelId })
logger.log('Adding label to a card', { cardId, labelId })

return makeRequest('post', `https://api.trello.com/1/cards/${cardId}/idLabels`, {
value: labelId,
})
}

export async function addMemberToCard(cardId: string, memberId: string) {
logger.log('MEMBERS: Adding member to a card', { cardId, memberId })
logger.log('Adding member to a card', { cardId, memberId })

return makeRequest('post', `https://api.trello.com/1/cards/${cardId}/idMembers`, {
value: memberId,
})
}

export async function removeMemberFromCard(cardId: string, memberId: string) {
logger.log('MEMBERS: Removing member from a card', { cardId, memberId })
logger.log('Removing member from a card', { cardId, memberId })

return makeRequest('delete', `https://api.trello.com/1/cards/${cardId}/idMembers/${memberId}`)
}
Expand All @@ -85,7 +85,7 @@ export async function getBoardLists(boardId: string): Promise<{ id: string }[]>
}

export async function moveCardToList(cardId: string, listId: string, boardId?: string) {
logger.log('MOVE: Moving card to list', { cardId, listId, boardId })
logger.log('Moving card to list', { cardId, listId, boardId })

return makeRequest('put', `https://api.trello.com/1/cards/${cardId}`, {
pos: trelloCardPosition,
Expand All @@ -107,7 +107,7 @@ export async function createCard(
title: string,
body?: string,
): Promise<{ id: string; url: string; shortLink: string }> {
logger.log('LINK: Creating card based on PR info', { title, body })
logger.log('Creating card based on PR info', { title, body })

const response = await makeRequest('post', `https://api.trello.com/1/cards`, {
idList: listId,
Expand Down
14 changes: 7 additions & 7 deletions src/actions/getCardIds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import isPullRequestInDraft from './utils/isPullRequestInDraft'
import logger from './utils/logger'

export default async function getCardIds(conf: Conf, pr: PR) {
logger.log('LINK: Searching for card IDs')
logger.log('--- FIND CARDS ---')

const latestPRInfo = (await getPullRequest()) || pr
let cardIds = matchCardIds(conf, latestPRInfo.body || '')
Expand Down Expand Up @@ -43,11 +43,11 @@ export default async function getCardIds(conf: Conf, pr: PR) {
}

if (cardIds.length) {
logger.log('LINK: Found card IDs', cardIds)
logger.log('Found card IDs', cardIds)

return [...new Set(cardIds)]
} else {
logger.log('LINK: Could not find card IDs')
logger.log('Could not find card IDs')

if (conf.githubRequireTrelloCard) {
setFailed('The PR does not contain a link to a Trello card')
Expand All @@ -60,13 +60,13 @@ export default async function getCardIds(conf: Conf, pr: PR) {
async function getCardIdsFromBranchName(conf: Conf, prHead?: PRHead) {
const branchName = prHead?.ref || (await getBranchName())

logger.log('LINK: Searching cards from branch name', branchName)
logger.log('Searching cards from branch name', branchName)

if (conf.githubAllowMultipleCardsInPrBranchName) {
const shortIdMatches = branchName.match(/(?<=^|\/)\d+(?:-\d+)+/gi)?.[0].split('-')

if (shortIdMatches && shortIdMatches.length > 1) {
logger.log('LINK: Matched multiple potential Trello short IDs from branch name', shortIdMatches)
logger.log('Matched multiple potential Trello short IDs from branch name', shortIdMatches)

const potentialCardIds = await Promise.all(
shortIdMatches.map((shortId: string) => getTrelloCardByShortId(shortId, conf.trelloBoardId)),
Expand All @@ -81,15 +81,15 @@ async function getCardIdsFromBranchName(conf: Conf, prHead?: PRHead) {
const matches = branchName.match(/(?<=^|\/)(\d+)-\S+/i)

if (matches) {
logger.log('LINK: Matched one potential card from branch name', matches)
logger.log('Matched one potential card from branch name', matches)

const cardsWithExactMatch = await searchTrelloCards(matches[0])

if (cardsWithExactMatch?.length) {
return [cardsWithExactMatch[0].shortLink]
}

logger.log('LINK: Could not find Trello card with branch name, trying only with short ID', matches[1])
logger.log('Could not find Trello card with branch name, trying only with short ID', matches[1])

const cardId = await getTrelloCardByShortId(matches[1])

Expand Down
14 changes: 8 additions & 6 deletions src/actions/moveOrArchiveCards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,37 @@ import isPullRequestApproved from './utils/isPullRequestApproved'
import logger from './utils/logger'

export default async function moveOrArchiveCards(conf: Conf, cardIds: string[], pr: PR) {
logger.log('--- MOVE OR ARCHIVE CARDS ---')

const isDraft = isPullRequestInDraft(pr)
const isChangesRequested = await isChangesRequestedInReview()
const isApproved = await isPullRequestApproved()
const isMerged = await isPullRequestMerged()

if (pr.state === 'open' && isDraft && conf.trelloListIdPrDraft) {
await moveCardsToList(cardIds, conf.trelloListIdPrDraft, conf.trelloBoardId)
logger.log('MOVE: Moved cards to draft PR list')
logger.log('Moved cards to draft PR list')

return
}

if (pr.state === 'open' && !isDraft && isChangesRequested && conf.trelloListIdPrChangesRequested) {
await moveCardsToList(cardIds, conf.trelloListIdPrChangesRequested, conf.trelloBoardId)
logger.log('MOVE: Moved cards to changes requested PR list')
logger.log('Moved cards to changes requested PR list')

return
}

if (pr.state === 'open' && !isDraft && !isChangesRequested && isApproved && conf.trelloListIdPrApproved) {
await moveCardsToList(cardIds, conf.trelloListIdPrApproved, conf.trelloBoardId)
logger.log('MOVE: Moved cards to approved PR list')
logger.log('Moved cards to approved PR list')

return
}

if (pr.state === 'open' && !isDraft && conf.trelloListIdPrOpen) {
await moveCardsToList(cardIds, conf.trelloListIdPrOpen, conf.trelloBoardId)
logger.log('MOVE: Moved cards to opened PR list')
logger.log('Moved cards to opened PR list')

return
}
Expand All @@ -48,12 +50,12 @@ export default async function moveOrArchiveCards(conf: Conf, cardIds: string[],

if (pr.state === 'closed' && conf.trelloListIdPrClosed) {
await moveCardsToList(cardIds, conf.trelloListIdPrClosed, conf.trelloBoardId)
logger.log('MOVE: Moved cards to closed PR list')
logger.log('Moved cards to closed PR list')

return
}

logger.log('MOVE: Skipping moving and archiving the cards', { state: pr.state, isDraft, isMerged })
logger.log('Skipping moving and archiving the cards', { state: pr.state, isDraft, isMerged })
}

async function moveCardsToList(cardIds: string[], listId: string, boardId?: string) {
Expand Down
Loading

0 comments on commit 3fcb8b3

Please sign in to comment.