From fd064511b5b04337613c148538f5429bdc352020 Mon Sep 17 00:00:00 2001 From: Alain Krok Date: Wed, 4 Oct 2023 13:51:04 -0500 Subject: [PATCH] feat: Feature/projenrc (#12) * add github badger action * add stale and monthly metrics workflows * add contributors workflow * add auto approve workflow * changes from linter * allow core team to be listed as contributors and earn badges, restrict autoapprove --- .gitattributes | 5 + .github/workflows/auto-approve.yml | 23 ++ .github/workflows/github-merit-badger.yml | 20 ++ .github/workflows/monthly-repo-metrics.yml | 49 +++++ .github/workflows/stale.yml | 30 +++ .github/workflows/update-contributors.yml | 30 +++ .gitignore | 5 + .projen/files.json | 5 + .projenrc.ts | 8 + projenrc/github-workflows.ts | 236 +++++++++++++++++++++ 10 files changed, 411 insertions(+) create mode 100644 .github/workflows/auto-approve.yml create mode 100644 .github/workflows/github-merit-badger.yml create mode 100644 .github/workflows/monthly-repo-metrics.yml create mode 100644 .github/workflows/stale.yml create mode 100644 .github/workflows/update-contributors.yml create mode 100644 projenrc/github-workflows.ts diff --git a/.gitattributes b/.gitattributes index 9036f6b5..50c6a9fe 100644 --- a/.gitattributes +++ b/.gitattributes @@ -4,9 +4,14 @@ /.eslintrc.json linguist-generated /.gitattributes linguist-generated /.github/pull_request_template.md linguist-generated +/.github/workflows/auto-approve.yml linguist-generated /.github/workflows/build.yml linguist-generated +/.github/workflows/github-merit-badger.yml linguist-generated +/.github/workflows/monthly-repo-metrics.yml linguist-generated /.github/workflows/pull-request-lint.yml linguist-generated /.github/workflows/release.yml linguist-generated +/.github/workflows/stale.yml linguist-generated +/.github/workflows/update-contributors.yml linguist-generated /.github/workflows/upgrade-main.yml linguist-generated /.gitignore linguist-generated /.mergify.yml linguist-generated diff --git a/.github/workflows/auto-approve.yml b/.github/workflows/auto-approve.yml new file mode 100644 index 00000000..4cc452d7 --- /dev/null +++ b/.github/workflows/auto-approve.yml @@ -0,0 +1,23 @@ +# ~~ Generated by projen. To modify, edit .projenrc.ts and run "npx projen". + +name: auto-approve +on: + pull_request_target: + types: + - labeled + - unlabeled + - opened + - synchronize + - reopened + - ready_for_review + - review_requested +jobs: + auto-approve: + runs-on: ubuntu-latest + permissions: + pull-requests: write + if: github.repository == aws-samples/emerging-tech-cdk-constructs && github.actor == emerging-tech-cdk-constructs-bot + steps: + - uses: hmarr/auto-approve-action@v3.2.1 + with: + github-token: ${{ secrets.PROJEN_GITHUB_TOKEN }} diff --git a/.github/workflows/github-merit-badger.yml b/.github/workflows/github-merit-badger.yml new file mode 100644 index 00000000..5fa45b35 --- /dev/null +++ b/.github/workflows/github-merit-badger.yml @@ -0,0 +1,20 @@ +# ~~ Generated by projen. To modify, edit .projenrc.ts and run "npx projen". + +name: github-merit-badger +on: + pull_request_target: + types: + - opened +jobs: + call-action: + runs-on: ubuntu-latest + permissions: + pull-requests: write + steps: + - id: github-merit-badger + uses: aws-github-ops/github-merit-badger@main + with: + github-token: ${{ secrets.PROJEN_GITHUB_TOKEN }} + badges: "[beginning-contributor,repeat-contributor,valued-contributor,admired-contributor,star-contributor,distinguished-contributor]" + thresholds: "[0,3,6,13,25,50]" + ignore-usernames: "[emerging-tech-cdk-constructs-bot, dependabot[bot], amazon-auto, github-actions]" diff --git a/.github/workflows/monthly-repo-metrics.yml b/.github/workflows/monthly-repo-metrics.yml new file mode 100644 index 00000000..c2c64924 --- /dev/null +++ b/.github/workflows/monthly-repo-metrics.yml @@ -0,0 +1,49 @@ +# ~~ Generated by projen. To modify, edit .projenrc.ts and run "npx projen". + +name: monthly-repo-metrics +on: + workflow_dispatch: + schedule: + cron: 0 2 1 * * +jobs: + build: + name: metrics + runs-on: ubuntu-latest + permissions: + issues: write + pull-requests: read + if: github.repository == aws-samples/emerging-tech-cdk-constructs + steps: + - name: Get dates for last month + run: |- + # Calculate the first day of the previous month + first_day=$(date -d "last month" +%Y-%m-01) + # Calculate the last day of the previous month + last_day=$(date -d $first_day +1 month -1 day +%Y-%m-%d) + # Set an environment variable with the date range + echo "$first_day..$last_day" + echo "last_month=$first_day..$last_day" >> "$GITHUB_ENV" + - name: Report on issues + uses: github/issue-metrics@v2 + env: + GH_TOKEN: ${{ secrets.PROJEN_GITHUB_TOKEN }} + SEARCH_QUERY: repo:aws-samples/emerging-tech-cdk-constructs is:issue created:${{ env.last_month }} -reason:"not planned" + - name: Create report for issues + uses: peter-evans/create-issue-from-file@v4 + with: + title: Monthly issue metrics report + token: ${{ secrets.PROJEN_GITHUB_TOKEN }} + content-filepath: ./issue_metrics.md + assignees: krokoko + - name: Report on PRs + uses: github/issue-metrics@v2 + env: + GH_TOKEN: ${{ secrets.PROJEN_GITHUB_TOKEN }} + SEARCH_QUERY: repo:aws-samples/emerging-tech-cdk-constructs is:pr created:${{ env.last_month }} -is:draft + - name: Create report for PRs + uses: peter-evans/create-issue-from-file@v4 + with: + title: Monthly issue metrics report + token: ${{ secrets.PROJEN_GITHUB_TOKEN }} + content-filepath: ./issue_metrics.md + assignees: krokoko diff --git a/.github/workflows/stale.yml b/.github/workflows/stale.yml new file mode 100644 index 00000000..ca07d5a2 --- /dev/null +++ b/.github/workflows/stale.yml @@ -0,0 +1,30 @@ +# ~~ Generated by projen. To modify, edit .projenrc.ts and run "npx projen". + +name: stale +on: + schedule: + - cron: 0 1 * * * + workflow_dispatch: {} +jobs: + stale: + runs-on: ubuntu-latest + permissions: + issues: write + pull-requests: write + steps: + - uses: actions/stale@v4 + with: + days-before-stale: -1 + days-before-close: -1 + days-before-pr-stale: 14 + days-before-pr-close: 2 + stale-pr-message: This pull request is now marked as stale because it hasn't seen activity for a while. Add a comment or it will be closed soon. If you wish to exclude this issue from being marked as stale, add the "backlog" label. + close-pr-message: Closing this pull request as it hasn't seen activity for a while. Please add a comment @mentioning a maintainer to reopen. If you wish to exclude this issue from being marked as stale, add the "backlog" label. + stale-pr-label: stale + exempt-pr-labels: backlog + days-before-issue-stale: 60 + days-before-issue-close: 7 + stale-issue-message: This issue is now marked as stale because it hasn't seen activity for a while. Add a comment or it will be closed soon. If you wish to exclude this issue from being marked as stale, add the "backlog" label. + close-issue-message: Closing this issue as it hasn't seen activity for a while. Please add a comment @mentioning a maintainer to reopen. If you wish to exclude this issue from being marked as stale, add the "backlog" label. + stale-issue-label: stale + exempt-issue-labels: backlog diff --git a/.github/workflows/update-contributors.yml b/.github/workflows/update-contributors.yml new file mode 100644 index 00000000..519c6d85 --- /dev/null +++ b/.github/workflows/update-contributors.yml @@ -0,0 +1,30 @@ +# ~~ Generated by projen. To modify, edit .projenrc.ts and run "npx projen". + +name: update-contributors +on: + workflow_dispatch: + schedule: + cron: 0 0 1 * * +jobs: + main: + runs-on: ubuntu-latest + permissions: + pull-requests: write + if: github.repository == aws-samples/emerging-tech-cdk-constructs + steps: + - uses: actions/checkout@v4 + - name: Update a projects CONTRIBUTORS file + uses: minicli/action-contributors@v3.3 + env: + CONTRIB_REPOSITORY: aws-samples/emerging-tech-cdk-constructs + CONTRIB_OUTPUT_FILE: CONTRIBUTORS.md + CONTRIB_IGNORE: emerging-tech-cdk-constructs-bot, dependabot[bot], amazon-auto, github-actions + - name: Create a PR + uses: peter-evans/create-pull-request@v5 + with: + branch: automation/update-contributors + author: emerging-tech-cdk-constructs-bot + commit-message: "chore: update Contributors File" + title: "chore: update Contributors File" + labels: auto-approve + token: ${{ secrets.PROJEN_GITHUB_TOKEN }} diff --git a/.gitignore b/.gitignore index c8e9a283..c718a018 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ !/.projen/deps.json !/.projen/files.json !/.github/workflows/pull-request-lint.yml +!/.github/workflows/stale.yml !/package.json !/LICENSE !/.npmignore @@ -51,3 +52,7 @@ junit.xml !/.eslintrc.json .jsii tsconfig.json +!/.github/workflows/github-merit-badger.yml +!/.github/workflows/monthly-repo-metrics.yml +!/.github/workflows/update-contributors.yml +!/.github/workflows/auto-approve.yml diff --git a/.projen/files.json b/.projen/files.json index c6df2fb4..a1d2ad1d 100644 --- a/.projen/files.json +++ b/.projen/files.json @@ -3,9 +3,14 @@ ".eslintrc.json", ".gitattributes", ".github/pull_request_template.md", + ".github/workflows/auto-approve.yml", ".github/workflows/build.yml", + ".github/workflows/github-merit-badger.yml", + ".github/workflows/monthly-repo-metrics.yml", ".github/workflows/pull-request-lint.yml", ".github/workflows/release.yml", + ".github/workflows/stale.yml", + ".github/workflows/update-contributors.yml", ".github/workflows/upgrade-main.yml", ".gitignore", ".mergify.yml", diff --git a/.projenrc.ts b/.projenrc.ts index 177fff65..7392b935 100644 --- a/.projenrc.ts +++ b/.projenrc.ts @@ -11,6 +11,7 @@ * and limitations under the License. */ import { awscdk } from 'projen'; +import { buildMeritBadgerWorkflow, buildMonthlyIssuesMetricsWorkflow, buildUpdateContributorsWorkflow, buildAutoApproveWorkflow } from './projenrc/github-workflows'; // Constants const GITHUB_USER = 'aws-samples'; @@ -64,8 +65,15 @@ const project = new awscdk.AwsCdkConstructLibrary({ gitignore: ['*.DS_STORE'], stability: 'experimental', sampleCode: false, + stale: true, }); +// Add some useful github workflows +buildMeritBadgerWorkflow(project); +buildMonthlyIssuesMetricsWorkflow(project); +buildUpdateContributorsWorkflow(project); +buildAutoApproveWorkflow(project); + // Add License header automatically project.eslint?.addPlugins('header'); project.eslint?.addRules({ diff --git a/projenrc/github-workflows.ts b/projenrc/github-workflows.ts new file mode 100644 index 00000000..e445a296 --- /dev/null +++ b/projenrc/github-workflows.ts @@ -0,0 +1,236 @@ +/** + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance + * with the License. A copy of the License is located at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * or in the 'license' file accompanying this file. This file is distributed on an 'AS IS' BASIS, WITHOUT WARRANTIES + * OR CONDITIONS OF ANY KIND, express or implied. See the License for the specific language governing permissions + * and limitations under the License. + */ +import { AwsCdkConstructLibrary } from 'projen/lib/awscdk'; +import { Job, JobPermission } from 'projen/lib/github/workflows-model'; + +/** + * https://github.com/aws-github-ops/github-merit-badger + * GitHub Action that can add well-known merit badges to pull requests + * that come in to your repository. It will gamify contributions and + * galvanize the open-source community to contribute more and more to your project! + * @param project AwsCdkConstructLibrary + */ +export function buildMeritBadgerWorkflow(project: AwsCdkConstructLibrary) { + const applymeritbadge: Job = { + runsOn: ['ubuntu-latest'], + permissions: { + pullRequests: JobPermission.WRITE, + }, + steps: [ + { + uses: 'aws-github-ops/github-merit-badger@main', + id: 'github-merit-badger', + with: { + 'github-token': '${{ secrets.PROJEN_GITHUB_TOKEN }}', + 'badges': '[beginning-contributor,repeat-contributor,valued-contributor,admired-contributor,star-contributor,distinguished-contributor]', + 'thresholds': '[0,3,6,13,25,50]', + 'ignore-usernames': '[emerging-tech-cdk-constructs-bot, dependabot[bot], amazon-auto, github-actions]', + }, + }, + ], + }; + + if (project.github) { + const workflow = project.github.addWorkflow('github-merit-badger'); + if (workflow) { + workflow.on({ + pullRequestTarget: { + types: [ + 'opened', + ], + }, + }); + workflow.addJobs({ 'call-action': applymeritbadge }); + } + } +} + +/** + * https://github.com/aws/aws-cdk/blob/main/.github/workflows/repo-metrics.yml + * GitHub action that runs monthly to report on metrics for issues and PRs created last month. + * @param project + */ +export function buildMonthlyIssuesMetricsWorkflow(project: AwsCdkConstructLibrary) { + const buildjob: Job = { + permissions: { + issues: JobPermission.WRITE, + pullRequests: JobPermission.READ, + }, + if: 'github.repository == aws-samples/emerging-tech-cdk-constructs', + runsOn: ['ubuntu-latest'], + name: 'metrics', + steps: [ + { + name: 'Get dates for last month', + run: [ + '# Calculate the first day of the previous month', + 'first_day=$(date -d "last month" +%Y-%m-01)', + '# Calculate the last day of the previous month', + 'last_day=$(date -d $first_day +1 month -1 day +%Y-%m-%d)', + '# Set an environment variable with the date range', + 'echo "$first_day..$last_day"', + 'echo "last_month=$first_day..$last_day" >> "$GITHUB_ENV"', + ].join('\n'), + }, + { + name: 'Report on issues', + uses: 'github/issue-metrics@v2', + env: + { + GH_TOKEN: '${{ secrets.PROJEN_GITHUB_TOKEN }}', + SEARCH_QUERY: 'repo:aws-samples/emerging-tech-cdk-constructs is:issue created:${{ env.last_month }} -reason:"not planned"', + }, + }, + { + name: 'Create report for issues', + uses: 'peter-evans/create-issue-from-file@v4', + with: + { + 'title': 'Monthly issue metrics report', + 'token': '${{ secrets.PROJEN_GITHUB_TOKEN }}', + 'content-filepath': './issue_metrics.md', + 'assignees': 'krokoko', + }, + }, + { + name: 'Report on PRs', + uses: 'github/issue-metrics@v2', + env: + { + GH_TOKEN: '${{ secrets.PROJEN_GITHUB_TOKEN }}', + SEARCH_QUERY: 'repo:aws-samples/emerging-tech-cdk-constructs is:pr created:${{ env.last_month }} -is:draft', + }, + }, + { + name: 'Create report for PRs', + uses: 'peter-evans/create-issue-from-file@v4', + with: + { + 'title': 'Monthly issue metrics report', + 'token': '${{ secrets.PROJEN_GITHUB_TOKEN }}', + 'content-filepath': './issue_metrics.md', + 'assignees': 'krokoko', + }, + }, + ], + }; + + if (project.github) { + const workflow = project.github.addWorkflow('monthly-repo-metrics'); + if (workflow) { + workflow.on({ + workflowDispatch: { + schedule: { + cron: '0 2 1 * *', + }, + }, + }); + workflow.addJobs({ build: buildjob }); + } + } +} + +/** + * https://github.com/aws/aws-cdk/blob/main/.github/workflows/update-contributors.yml + * GitHub action that runs monthly to create a pull request for updating a + * CONTRIBUTORS file with the top contributors. + * @param project AwsCdkConstructLibrary + */ +export function buildUpdateContributorsWorkflow(project: AwsCdkConstructLibrary) { + + const updateContributors: Job = { + permissions: { + pullRequests: JobPermission.WRITE, + }, + if: 'github.repository == aws-samples/emerging-tech-cdk-constructs', + runsOn: ['ubuntu-latest'], + steps: [ + { + uses: 'actions/checkout@v4', + }, + { + uses: 'minicli/action-contributors@v3.3', + name: 'Update a projects CONTRIBUTORS file', + env: + { + CONTRIB_REPOSITORY: 'aws-samples/emerging-tech-cdk-constructs', + CONTRIB_OUTPUT_FILE: 'CONTRIBUTORS.md', + CONTRIB_IGNORE: 'emerging-tech-cdk-constructs-bot, dependabot[bot], amazon-auto, github-actions', + }, + }, + { + name: 'Create a PR', + uses: 'peter-evans/create-pull-request@v5', + with: { + 'branch': 'automation/update-contributors', + 'author': 'emerging-tech-cdk-constructs-bot', + 'commit-message': 'chore: update Contributors File', + 'title': 'chore: update Contributors File', + 'labels': 'auto-approve', + 'token': '${{ secrets.PROJEN_GITHUB_TOKEN }}', + }, + }, + ], + }; + + if (project.github) { + const workflow = project.github.addWorkflow('update-contributors'); + if (workflow) { + workflow.on({ + workflowDispatch: { + schedule: { + cron: '0 0 1 * *', + }, + }, + }); + workflow.addJobs({ main: updateContributors }); + } + } +} + +/** + * https://github.com/aws/aws-cdk/blob/main/.github/workflows/auto-approve.yml + * Approves merging PRs with the auto-approve label. + * @param project AwsCdkConstructLibrary + */ +export function buildAutoApproveWorkflow(project: AwsCdkConstructLibrary) { + const autoapprove: Job = { + runsOn: ['ubuntu-latest'], + if: 'github.repository == aws-samples/emerging-tech-cdk-constructs && github.actor == emerging-tech-cdk-constructs-bot', + permissions: { + pullRequests: JobPermission.WRITE, + }, + steps: [ + { + uses: 'hmarr/auto-approve-action@v3.2.1', + with: { + 'github-token': '${{ secrets.PROJEN_GITHUB_TOKEN }}', + }, + }, + ], + }; + + if (project.github) { + const workflow = project.github.addWorkflow('auto-approve'); + if (workflow) { + workflow.on({ + pullRequestTarget: { + types: [ + 'labeled', 'unlabeled', 'opened', 'synchronize', 'reopened', 'ready_for_review', 'review_requested', + ], + }, + }); + workflow.addJobs({ 'auto-approve': autoapprove }); + } + } +} \ No newline at end of file