|
| 1 | +import { Component, github } from 'projen'; |
| 2 | +import { JobPermission } from 'projen/lib/github/workflows-model'; |
| 3 | +import { TypeScriptProject } from 'projen/lib/typescript'; |
| 4 | + |
| 5 | +const OSDS_DEVS = ['ashishdhingra', 'khushail', 'hunhsieh']; |
| 6 | +const AREA_AFFIXES = ['@aws-cdk/']; |
| 7 | +const AREA_PARAMS = [ |
| 8 | + { area: '@aws-cdk/cli-lib-alpha', keywords: ['cli', 'cli-lib', 'cli-lib-alpha'], labels: ['@aws-cdk/cli-lib-alpha'] }, |
| 9 | + { area: '@aws-cdk/cloud-assembly-schema', keywords: ['cloud-assembly', 'schema'], labels: ['@aws-cdk/cloud-assembly-schema'] }, |
| 10 | + { area: '@aws-cdk/cloudformation-diff', keywords: ['diff', 'cloudformation'], labels: ['@aws-cdk/cloudformation-diff'] }, |
| 11 | + { area: '@aws-cdk/toolkit-lib', keywords: ['toolkit', 'programmtic toolkit', 'toolkit-lib'], labels: ['@aws-cdk/toolkit-lib'] }, |
| 12 | + { area: 'aws-cdk', keywords: ['aws-cdk', 'cli', 'cdk cli'], labels: ['aws-cdk'] }, |
| 13 | + { area: 'cdk-assets', keywords: ['assets', 'cdk-assets'], labels: ['cdk-assets'] }, |
| 14 | +]; |
| 15 | + |
| 16 | +enum GitHubToken { |
| 17 | + GITHUB_TOKEN = 'secrets.GITHUB_TOKEN', |
| 18 | + PROJEN_GITHUB_TOKEN = 'secrets.PROJEN_GITHUB_TOKEN', |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * See https://github.com/aws-github-ops/aws-issue-triage-manager |
| 23 | + */ |
| 24 | +interface TriageManagerOptions { |
| 25 | + target: 'pull-requests' | 'issues' | 'both'; |
| 26 | + excludedExpressions?: string[]; |
| 27 | + includedLabels?: string[]; |
| 28 | + excludedLabels?: string[]; |
| 29 | + defaultArea?: string; |
| 30 | + parameters?: string; |
| 31 | + affixes?: string; |
| 32 | + areaIsKeyword?: boolean; |
| 33 | + /** |
| 34 | + * Whether or not the env variables are needed for the job. |
| 35 | + * Workflow-level env variables are not configurable via Projen |
| 36 | + */ |
| 37 | + needEnvs?: boolean; |
| 38 | + /** |
| 39 | + * @default GitHubToken.GITHUB_TOKEN |
| 40 | + */ |
| 41 | + githubToken?: GitHubToken; |
| 42 | +} |
| 43 | + |
| 44 | +function stringifyList(list: string[]) { |
| 45 | + return `[${list.join('|')}]`; |
| 46 | +} |
| 47 | + |
| 48 | +function triageManagerJob(triageManagerOptions: TriageManagerOptions) { |
| 49 | + return { |
| 50 | + name: 'Triage Manager', |
| 51 | + runsOn: ['aws-cdk_ubuntu-latest_4-core'], |
| 52 | + permissions: { issues: JobPermission.WRITE, pullRequests: JobPermission.WRITE }, |
| 53 | + steps: [ |
| 54 | + { |
| 55 | + name: 'Triage Manager', |
| 56 | + uses: 'aws-github-ops/aws-issue-triage-manager@main', |
| 57 | + with: { |
| 58 | + 'github-token': `\${{ ${triageManagerOptions.githubToken ?? 'secrets.GITHUB_TOKEN'} }}`, |
| 59 | + 'target': triageManagerOptions.target, |
| 60 | + 'excluded-expressions': triageManagerOptions.excludedExpressions ? stringifyList(triageManagerOptions.excludedExpressions) : undefined, |
| 61 | + 'included-labels': triageManagerOptions.includedLabels ? stringifyList(triageManagerOptions.includedLabels) : undefined, |
| 62 | + 'excluded-labels': triageManagerOptions.excludedLabels ? stringifyList(triageManagerOptions.excludedLabels) : undefined, |
| 63 | + 'default-area': triageManagerOptions.defaultArea, |
| 64 | + 'parameters': triageManagerOptions.parameters, |
| 65 | + 'affixes': triageManagerOptions.affixes, |
| 66 | + 'area-is-keyword': triageManagerOptions.areaIsKeyword, |
| 67 | + }, |
| 68 | + }, |
| 69 | + ], |
| 70 | + ...(triageManagerOptions.needEnvs ? { |
| 71 | + env: { |
| 72 | + AREA_PARAMS: JSON.stringify(AREA_PARAMS), |
| 73 | + AREA_AFFIXES: `{"prefixes":${JSON.stringify(AREA_AFFIXES)}}`, |
| 74 | + OSDS_DEVS: `{"assignees":${JSON.stringify(OSDS_DEVS)}}`, |
| 75 | + }, |
| 76 | + } : {}), |
| 77 | + }; |
| 78 | +} |
| 79 | + |
| 80 | +export class IssueLabeler extends Component { |
| 81 | + public readonly workflow: github.GithubWorkflow; |
| 82 | + |
| 83 | + constructor(repo: TypeScriptProject) { |
| 84 | + super(repo); |
| 85 | + |
| 86 | + if (!repo.github) { |
| 87 | + throw new Error('Given repository does not have a GitHub component'); |
| 88 | + } |
| 89 | + |
| 90 | + this.workflow = repo.github.addWorkflow('issue-label-assign'); |
| 91 | + this.workflow.on({ |
| 92 | + pullRequestTarget: { types: ['opened'] }, |
| 93 | + issues: { types: ['opened', 'edited'] }, |
| 94 | + }); |
| 95 | + |
| 96 | + this.workflow.addJob('Triage Issues', triageManagerJob({ |
| 97 | + target: 'issues', |
| 98 | + excludedExpressions: ['CDK CLI Version', 'TypeScript', 'Java', 'Python', 'Go'], |
| 99 | + includedLabels: ['needs-triage'], |
| 100 | + excludedLabels: ['p1', 'p2', 'p0', 'effort-small', 'effort-medium', 'effort-large', 'guidance'], |
| 101 | + defaultArea: '${{ env.OSDS_DEVS }}', |
| 102 | + parameters: '${{ env.AREA_PARAMS }}', |
| 103 | + affixes: '${{ env.AREA_AFFIXES }}', |
| 104 | + needEnvs: true, |
| 105 | + })); |
| 106 | + this.workflow.addJob('Triage Pull Requests', triageManagerJob({ |
| 107 | + target: 'pull-requests', |
| 108 | + areaIsKeyword: true, |
| 109 | + defaultArea: '{"reviewers":{"teamReviewers":["aws-cdk-owners"]}}', |
| 110 | + parameters: '[{"area":"pullrequests","keywords":["pullrequestkeyword"]}]', |
| 111 | + githubToken: GitHubToken.PROJEN_GITHUB_TOKEN, |
| 112 | + })); |
| 113 | + } |
| 114 | +} |
0 commit comments