Skip to content

Commit

Permalink
chore: fix issue labeler (vercel#47206)
Browse files Browse the repository at this point in the history
### What?

A fork of https://github.com/github/issue-labeler that we pull in and
maintain.

### Why?

The official issue labeler had some issues which I tried to fix in PRs
and eventually got merged upstream, but the release cycle was a bit
slow, and that GitHub action had many parts we did not really need
anyway. So it makes sense to maintain it ourselves.

### How?

This PR removes the
https://github.com/vercel/next.js/blob/canary/.github/issue-labeler.yml
config file, which in our case is just a sublist of
https://github.com/vercel/next.js/labels anyway. Instead, we can pull in
the labels from GitHub directly and filter out those that we want to
apply to issues. This will keep things more in sync.

fix NEXT-750 ([link](https://linear.app/vercel/issue/NEXT-750))

---------
  • Loading branch information
balazsorban44 authored Mar 21, 2023
1 parent cf66c2d commit 1a47872
Show file tree
Hide file tree
Showing 11 changed files with 784 additions and 50 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ packages/react-dev-overlay/lib/**
**/__tmp__/**
.github/actions/next-stats-action/.work
.github/actions/issue-validator/index.mjs
.github/actions/issue-labeler/lib/index.js
packages/next-codemod/transforms/__testfixtures__/**/*
packages/next-codemod/transforms/__tests__/**/*
packages/next-codemod/**/*.js
Expand Down
1 change: 1 addition & 0 deletions .github/actions/issue-labeler/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
7 changes: 7 additions & 0 deletions .github/actions/issue-labeler/lib/index.js

Large diffs are not rendered by default.

635 changes: 635 additions & 0 deletions .github/actions/issue-labeler/lib/licenses.txt

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions .github/actions/issue-labeler/lib/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"type": "module"
}
18 changes: 18 additions & 0 deletions .github/actions/issue-labeler/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"private": true,
"name": "issue-labeler",
"type": "module",
"exports": "./lib/index.js",
"scripts": {
"build": "pnpm types && pnpm ncc -m -o ./lib build src/index.ts --license licenses.txt",
"types": "tsc"
},
"dependencies": {
"@actions/core": "^1.10.0",
"@actions/github": "^5.1.1"
},
"devDependencies": {
"@types/node": "^18.15.3",
"@vercel/ncc": "0.36.1"
}
}
102 changes: 102 additions & 0 deletions .github/actions/issue-labeler/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { setFailed, debug } from '@actions/core'
import { context, getOctokit } from '@actions/github'

type GitHubClient = ReturnType<typeof getOctokit>['rest']

async function run() {
const token = process.env.GITHUB_TOKEN
if (!token) throw new Error('No GITHUB_TOKEN provided')

const { issue } = context.payload
if (!issue) return console.log('Not an issue, exiting')

const { body: issue_body, number: issue_number, title: issue_title } = issue
if (!issue_number) return console.log('Could not get issue number, exiting')
if (!issue_body) return console.log('Could not get issue body, exiting')
if (!issue_title) return console.log('Could not get issue title, exiting')

// A client to load data from GitHub
const { rest: client } = getOctokit(token)

// Load our regex rules from the repo labels
const labels = await loadAreaLabels(client)

debug(`Loaded labels: ${Array.from(labels.keys()).join(', ')}`)

/** List of labels to add */
const toAdd: string[] = []

// https://github.com/vercel/next.js/blame/canary/.github/ISSUE_TEMPLATE/1.bug_report.yml

const matchSection = issue_body
.split('Which area(s) of Next.js are affected? (leave empty if unsure)')[1]
?.split('Link to the code that reproduces this issue')[0]

if (!matchSection) {
console.log(
`Issue #${issue_number} does not contain a match section, likely not a bug template, exiting`
)
return
}

debug(`Match section: ${matchSection}`)

for (const [label, description] of labels.entries()) {
if (matchSection.includes(description)) {
toAdd.push(label)
}
}

debug(`Labels to add: ${toAdd.join(', ')}`)

if (!toAdd.length) return console.log('No labels to add, exiting')

await addLabels(client, issue_number, toAdd)

debug(`Added labels to issue #${issue_number}: ${toAdd.join(', ')}`)
}

/** Load label descriptions from the repo. */
async function loadAreaLabels(client: GitHubClient) {
try {
const { data } = await client.issues.listLabelsForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
per_page: 100,
})

const labels = new Map<string, string>()
// Only load labels that start with `area:` and have a description
for (const label of data) {
if (label.name.startsWith('area:') && label.description) {
labels.set(label.name, label.description)
}
}
return labels
} catch (error) {
console.error('Error loading labels: ' + error)
throw error
}
}

async function addLabels(
client: GitHubClient,
issue_number: number,
labels: string[]
) {
try {
const formatted = labels.map((l) => `"${l}"`).join(', ')
debug(`Adding label(s) (${formatted}) to issue #${issue_number}`)
return await client.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number,
labels,
})
} catch (error) {
console.error(`Could not add label(s) ${labels} to issue #${issue_number}`)
throw error
}
}

run().catch(setFailed)
10 changes: 10 additions & 0 deletions .github/actions/issue-labeler/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"compilerOptions": {
"noEmit": true,
"target": "esnext",
"moduleResolution": "node",
"rootDir": "./src",
"strict": true,
"noImplicitAny": true
}
}
45 changes: 0 additions & 45 deletions .github/issue-labeler.yml

This file was deleted.

11 changes: 6 additions & 5 deletions .github/workflows/issue_labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ jobs:
name: Triage
runs-on: ubuntu-latest
steps:
- uses: github/issue-labeler@v3.0
- uses: actions/setup-node@v3
with:
repo-token: '${{ secrets.GITHUB_TOKEN }}'
configuration-path: '.github/issue-labeler.yml'
enable-versioned-regex: 0
sync-labels: 0
node-version: 18
- name: 'Run issue labeler'
run: node ./.github/actions/issue-labeler/lib
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1 change: 1 addition & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ packages/react-dev-overlay/lib/**
lerna.json
.github/actions/next-stats-action/.work
.github/actions/issue-validator/index.mjs
.github/actions/issue-labeler/lib/index.js
packages/next-swc/crates/**/*
packages/next-swc/target/**/*
packages/next-swc/native/**/*
Expand Down

0 comments on commit 1a47872

Please sign in to comment.