Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul the complete labelling system #4438

Merged
merged 8 commits into from
Jun 6, 2024
Prev Previous commit
Next Next commit
Replace getLabel with getAllLabels to reduce API calls
  • Loading branch information
dhruvkb committed Jun 5, 2024
commit 7aa6d7166562b326dfdf3b0a40b3f2b73f1e74ac
27 changes: 16 additions & 11 deletions automations/js/src/label_pr.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,23 @@ function getIsFullyLabeled(labels) {
}

/**
* Get the `Label` instance from a label's name.
* Get all `Label` instances for a repository.
*
* @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
* @returns {import('./utils/pr.mjs').Label[]} the label with the `id` and `name` fields
*/
async function getLabel(octokit, repository, name) {
async function getAllLabels(octokit, repository) {
const [owner, repo] = repository.split('/')
const res = await octokit.rest.issues.getLabel({ owner, repo, name })
return {
id: res.data.node_id,
name,
}
const res = await octokit.rest.issues.listLabelsForRepo({
owner,
repo,
per_page: 100,
})
return res.data.map((item) => ({
id: item.node_id,
name: item.name,
}))
}

/**
Expand All @@ -58,6 +61,8 @@ export const main = async (octokit, core) => {
readFileSync('/tmp/event.json', 'utf-8')
)

const allLabels = await getAllLabels(octokit, GITHUB_REPOSITORY)

if (
eventName !== 'pull_request' ||
!['opened', 'edited'].includes(eventAction)
Expand Down Expand Up @@ -131,8 +136,8 @@ export const main = async (octokit, core) => {
} else {
attnLabel = '🚦 status: awaiting triage'
}
core.info(`Pull not fully labelled so adding "${attnLabel}".`)
attnLabel = await getLabel(octokit, GITHUB_REPOSITORY, attnLabel)
core.info(`PR not fully labelled so adding "${attnLabel}".`)
attnLabel = allLabels.filter((item) => item.name === attnLabel)[0]
finalLabels.add(attnLabel)
}

Expand Down