Skip to content

Commit

Permalink
Use the GitHub API to get the label's node ID (#3483)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhruvkb authored Dec 8, 2023
1 parent 46971e0 commit 6dbd3d1
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions automations/js/src/label_pr.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,23 @@ function getIsFullyLabeled(labels) {
return true
}

/**
* Get the `Label` instance from a label's name.
*
* @param octokit {import('@octokit/rest').Octokit} the Octokit instance to use
* @param repository {string} the full name of the repository, including owner
* @param name {string} the name of the label for which to get node ID
* @returns {Label} the label with the `id` and `name` fields
*/
async function getLabel(octokit, repository, name) {
const [owner, repo] = repository.split('/')
const res = await octokit.issues.getLabel({ owner, repo, name })
return {
id: res.data.node_id,
name,
}
}

/**
* Apply labels to a PR based on the PR's linked issues.
*
Expand All @@ -36,6 +53,7 @@ function getIsFullyLabeled(labels) {
* @param core {import('@actions/core')} GitHub Actions toolkit, for logging
*/
export const main = async (octokit, core) => {
const { GITHUB_REPOSITORY } = process.env
const { eventName, eventAction, prNodeId } = JSON.parse(
readFileSync('/tmp/event.json', 'utf-8')
)
Expand Down Expand Up @@ -109,17 +127,12 @@ export const main = async (octokit, core) => {
if (!getIsFullyLabeled(finalLabels.items)) {
let attnLabel
if (isTriaged) {
attnLabel = {
id: 'MDU6TGFiZWwzMDI5NTEyMjMw',
name: '🏷 status: label work required',
}
attnLabel = '🏷 status: label work required'
} else {
attnLabel = {
id: 'MDU6TGFiZWwzMDI5NTEyMjc1',
name: '🚦 status: awaiting triage',
}
attnLabel = '🚦 status: awaiting triage'
}
core.info(`Pull not fully labelled so adding "${attnLabel}".`)
attnLabel = await getLabel(octokit, GITHUB_REPOSITORY, attnLabel)
finalLabels.add(attnLabel)
}

Expand Down

0 comments on commit 6dbd3d1

Please sign in to comment.