Skip to content

Commit

Permalink
Merge pull request #2 from rematocorp/labels
Browse files Browse the repository at this point in the history
Introduce labels feature
  • Loading branch information
ukupat authored Nov 28, 2021
2 parents a7aa869 + 0aa394e commit 578c6c4
Showing 1 changed file with 101 additions and 0 deletions.
101 changes: 101 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const trelloListIdPrOpen = core.getInput('trello-list-id-pr-open')
const trelloListIdPrClosed = core.getInput('trello-list-id-pr-closed')

async function run(pr) {
console.log('Running labels feature')
const url = pr.html_url || pr.url

try {
Expand All @@ -22,6 +23,7 @@ async function run(pr) {
console.log('Found card ids', cardIds)

await addAttachmentToCards(cardIds, url)
await addLabelToCards(cardIds, pr.head?.ref)

if (pr.state === 'open' && pr.mergeable_state !== 'draft' && trelloListIdPrOpen) {
await moveCardsToList(cardIds, trelloListIdPrOpen)
Expand Down Expand Up @@ -142,4 +144,103 @@ function moveCardsToList(cardIds, listId) {
})
}

async function addLabelToCards(cardIds, branchName) {
console.log('Starting to add label to cards')

if (!branchName) {
console.warn('Skipping label adding to cards because PR branchName is missing')
}
cardIds.forEach((cardId) => {
const cardInfo = await getCardInfo(cardId)

if (cardInfo.idLabels.length) {
console.log('Skipping label adding to a card because card already has labels')
return
}
const boardLabels = await getBoardLabels(cardInfo.idBoard)
const branchLabel = getBranchLabel(branchName)
const matchingLabel = findMatchingLabel(branchLabel, boardLabels)

if (matchingLabel) {
await addLabelToCard(cardId, matchingLabel.id)
} else {
console.log(
'Skipping label adding to a card because could not find a matching label from the board',
branchLabel,
boardLabels,
)
}
})
}

async function getCardInfo(cardId) {
console.log('Getting card info')

const url = `https://api.trello.com/1/cards/${cardId}`

try {
const response = await axios.get(url, {
params: {
key: trelloApiKey,
token: trelloAuthToken,
},
})
console.log('Card info', JSON.stringify(cardInfo))
return response.data
} catch (error) {
console.error(`Error ${error.response.status} ${error.response.statusText}`, url)
}
}

async function getBoardLabels(boardId) {
const url = `https://api.trello.com/1/boards/${boardId}/labels`

try {
const response = await axios.get(url, {
params: {
key: trelloApiKey,
token: trelloAuthToken,
fields: 'idBoard',
},
})
console.log('Board labels', reponse.data)
return response.data
} catch (error) {
console.error(`Error ${error.response.status} ${error.response.statusText}`, url)
}
}

function getBranchLabel(branchName) {
const matches = branchName.match(/^([^\/]*)\//)

if (matches) {
return matches[1]
} else {
console.log('Did not found branch label', branchName)
}
}

function findMatchingLabel(branchLabel, boardLabels) {
if (!branchLabel) {
return
}
return boardLabels.find((label) => label.name === branchLabel)
}

async function addLabelToCard(cardId, labelId) {
console.log('Adding label to a card', cardId, labelId)

const url = `https://api.trello.com/1/cards/${cardId}/idLabels`

axios
.post(url, {
key: trelloApiKey,
token: trelloAuthToken,
value: labelId,
})
.catch((error) => {
console.error(`Error ${error.response.status} ${error.response.statusText}`, url)
})
}

run(payload.pull_request || payload.issue)

0 comments on commit 578c6c4

Please sign in to comment.