diff --git a/.gitattributes b/.gitattributes index 8851706bc..b8beb0f1f 100644 --- a/.gitattributes +++ b/.gitattributes @@ -12,6 +12,7 @@ /.github/workflows/codecov.yml linguist-generated /.github/workflows/integ.yml linguist-generated /.github/workflows/issue-label-assign.yml linguist-generated +/.github/workflows/pr-labeler.yml linguist-generated /.github/workflows/pull-request-lint.yml linguist-generated /.github/workflows/release.yml linguist-generated /.github/workflows/upgrade.yml linguist-generated diff --git a/.github/workflows/pr-labeler.yml b/.github/workflows/pr-labeler.yml new file mode 100644 index 000000000..ea5cc10d9 --- /dev/null +++ b/.github/workflows/pr-labeler.yml @@ -0,0 +1,21 @@ +# ~~ Generated by projen. To modify, edit .projenrc.ts and run "npx projen". + +name: pr-labeler +on: + pull_request_target: + types: + - opened + - edited + - reopened +jobs: + copy-labels: + name: PR Labeler + runs-on: aws-cdk_ubuntu-latest_4-core + permissions: + issues: write + pull-requests: write + steps: + - name: PR Labeler + uses: cdklabs/pr-triage-manager@main + with: + github-token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index b9c3303ae..9892d6ebb 100644 --- a/.gitignore +++ b/.gitignore @@ -50,5 +50,6 @@ jspm_packages/ !/.github/workflows/integ.yml !/.github/workflows/codecov.yml !/.github/workflows/issue-label-assign.yml +!/.github/workflows/pr-labeler.yml !/.projenrc.ts !/.github/workflows/release.yml diff --git a/.projen/files.json b/.projen/files.json index ed7690bd3..12528934f 100644 --- a/.projen/files.json +++ b/.projen/files.json @@ -10,6 +10,7 @@ ".github/workflows/codecov.yml", ".github/workflows/integ.yml", ".github/workflows/issue-label-assign.yml", + ".github/workflows/pr-labeler.yml", ".github/workflows/pull-request-lint.yml", ".github/workflows/release.yml", ".github/workflows/upgrade.yml", diff --git a/.projenrc.ts b/.projenrc.ts index 7c4dd5415..a0d2461a3 100644 --- a/.projenrc.ts +++ b/.projenrc.ts @@ -8,6 +8,7 @@ import { CodeCovWorkflow } from './projenrc/codecov'; import { ESLINT_RULES } from './projenrc/eslint'; import { IssueLabeler } from './projenrc/issue-labeler'; import { JsiiBuild } from './projenrc/jsii'; +import { PrLabeler } from './projenrc/pr-labeler'; import { RecordPublishingTimestamp } from './projenrc/record-publishing-timestamp'; import { S3DocsPublishing } from './projenrc/s3-docs-publishing'; @@ -1407,5 +1408,6 @@ new CodeCovWorkflow(repo, { }); new IssueLabeler(repo); +new PrLabeler(repo); repo.synth(); diff --git a/projenrc/issue-labeler.ts b/projenrc/issue-labeler.ts index 0c8e6654c..1c1774c06 100644 --- a/projenrc/issue-labeler.ts +++ b/projenrc/issue-labeler.ts @@ -1,6 +1,7 @@ import { Component, github } from 'projen'; import { JobPermission } from 'projen/lib/github/workflows-model'; import { TypeScriptProject } from 'projen/lib/typescript'; +import { GitHubToken, stringifyList } from './util'; const OSDS_DEVS = ['ashishdhingra', 'khushail', 'hunhsieh']; const AREA_AFFIXES = ['@aws-cdk/']; @@ -13,11 +14,6 @@ const AREA_PARAMS = [ { area: 'cdk-assets', keywords: ['assets', 'cdk-assets'], labels: ['cdk-assets'] }, ]; -enum GitHubToken { - GITHUB_TOKEN = 'secrets.GITHUB_TOKEN', - PROJEN_GITHUB_TOKEN = 'secrets.PROJEN_GITHUB_TOKEN', -} - /** * See https://github.com/aws-github-ops/aws-issue-triage-manager */ @@ -41,10 +37,6 @@ interface TriageManagerOptions { githubToken?: GitHubToken; } -function stringifyList(list: string[]) { - return `[${list.join('|')}]`; -} - function triageManagerJob(triageManagerOptions: TriageManagerOptions) { return { name: 'Triage Manager', diff --git a/projenrc/pr-labeler.ts b/projenrc/pr-labeler.ts new file mode 100644 index 000000000..002e8bf81 --- /dev/null +++ b/projenrc/pr-labeler.ts @@ -0,0 +1,56 @@ +import { Component, github } from 'projen'; +import { JobPermission } from 'projen/lib/github/workflows-model'; +import { TypeScriptProject } from 'projen/lib/typescript'; +import { GitHubToken, stringifyList } from './util'; + +/** + * See https://github.com/cdklabs/pr-triage-manager + */ +interface PrLabelerOptions { + /** + * @default GitHubToken.GITHUB_TOKEN + */ + githubToken?: GitHubToken; + priorityLabels?: string[]; + classificationLabels?: string[]; + onPulls?: boolean; +} + +function prLabelerJob(prLabelerOptions: PrLabelerOptions = {}) { + return { + name: 'PR Labeler', + runsOn: ['aws-cdk_ubuntu-latest_4-core'], + permissions: { issues: JobPermission.WRITE, pullRequests: JobPermission.WRITE }, + steps: [ + { + name: 'PR Labeler', + uses: 'cdklabs/pr-triage-manager@main', + with: { + 'github-token': `\${{ ${prLabelerOptions.githubToken ?? 'secrets.GITHUB_TOKEN'} }}`, + 'priority-labels': prLabelerOptions.priorityLabels ? stringifyList(prLabelerOptions.priorityLabels) : undefined, + 'classification-labels': prLabelerOptions.classificationLabels ? stringifyList(prLabelerOptions.classificationLabels) : undefined, + 'on-pulls': prLabelerOptions.onPulls, + }, + }, + ], + }; +} + +export class PrLabeler extends Component { + public readonly workflow: github.GithubWorkflow; + + constructor(repo: TypeScriptProject) { + super(repo); + + if (!repo.github) { + throw new Error('Given repository does not have a GitHub component'); + } + + this.workflow = repo.github.addWorkflow('pr-labeler'); + this.workflow.on({ + pullRequestTarget: { types: ['opened', 'edited', 'reopened'] }, + }); + + this.workflow.addJob('copy-labels', prLabelerJob()); + } +} diff --git a/projenrc/util.ts b/projenrc/util.ts new file mode 100644 index 000000000..b639c1b33 --- /dev/null +++ b/projenrc/util.ts @@ -0,0 +1,8 @@ +export enum GitHubToken { + GITHUB_TOKEN = 'secrets.GITHUB_TOKEN', + PROJEN_GITHUB_TOKEN = 'secrets.PROJEN_GITHUB_TOKEN', +} + +export function stringifyList(list: string[]) { + return `[${list.join('|')}]`; +}