Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
PR count notifications (#2303)
Zack Krida7fe79e5d654e
diff --git a/.github/workflows/pr_limit_reminders.yml b/.github/workflows/pr_limit_reminders.ymlnew file mode 100644index 000000000..142b93768--- /dev/null+++ b/.github/workflows/pr_limit_reminders.yml@@ -0,0 +1,50 @@+name: PR Limit Reminders++on:+ pull_request:+ types: [opened, reopened]+ push:+ branches:+ - "pr-count-prompt"++jobs:+ analyze-user-prs:+ name: Count user's open pull requests+ runs-on: ubuntu-latest+ outputs:+ pr_count: ${{ steps.count-prs.outputs.pr_count }}+ slack_id: ${{ steps.count-prs.outputs.slack_id }}+ steps:+ - uses: actions/checkout@v3+ - uses: actions/github-script@v6+ id: count-prs+ env:+ GH_SLACK_USERNAME_MAP: ${{ secrets.GH_SLACK_USERNAME_MAP }}+ with:+ github-token: ${{ secrets.ACCESS_TOKEN }}+ script: |+ const script = require('./automations/js/src/count_user_reviewable_prs.js')+ await script({github,context,core})++ send_message:+ needs: analyze-user-prs+ name: Send Slack message+ if: ${{ needs.analyze-user-prs.outputs.pr_count >= 3 }}+ runs-on: ubuntu-latest+ env:+ slack_url: ${{ secrets.SLACK_DM_WEBHOOK_URL }}+ pr_count: ${{ needs.analyze-user-prs.outputs.pr_count }}+ slack_id: ${{ needs.analyze-user-prs.outputs.slack_id }}+ steps:+ - name: Send notification+ run: |+ msg="Hi, Opener! \nYou currently have $pr_count Pull Request(s) open with requested reviews (totalling $((pr_count * 2)) required reviews). \nTo help ease the review burden, increase review velocity for older PRs, and improve the equitable distribution of project maintenance tasks across the team, please consider reviewing this list of ways to contribute instead of working on new code contributions: \nhttps://docs.openverse.org/meta/maintainer_tasks.html"++ curl \+ "$slack_url" \+ -X POST \+ -H 'Content-Type: application/json' \+ -d '{+ "user": "'"$slack_id"'",+ "message": "'"$msg"'"+ }'diff --git a/automations/js/src/count_user_reviewable_prs.js b/automations/js/src/count_user_reviewable_prs.jsnew file mode 100644index 000000000..90ea326cd--- /dev/null+++ b/automations/js/src/count_user_reviewable_prs.js@@ -0,0 +1,91 @@+// @ts-check+/**+ * Checks the reviewable PR count for the current Github actor.+ * Return their Slack username and PR count, if found.+ *+ * @param {Object} options+ * @param {import('@octokit/rest').Octokit} options.github+ * @param {import('@actions/github')['context']} options.context+ * @param {import('@actions/core')} options.core+ */+module.exports = async ({ github, context, core }) => {+ const { GITHUB_REPOSITORY, GH_SLACK_USERNAME_MAP } = process.env+ const slackID = JSON.parse(GH_SLACK_USERNAME_MAP)[context.actor]++ if (!GITHUB_REPOSITORY || !GH_SLACK_USERNAME_MAP) {+ core.setFailed('Required dependencies were not supplied')+ }++ if (!slackID) {+ core.warning(`Slack username not found for ${context.actor}.`)+ return {}+ }++ const GET_PULL_REQUESTS = `+query ($repoOwner: String!, $repo: String!, $cursor: String) {+ repository(name:$repo, owner:$repoOwner) {+ pullRequests(states:OPEN, first:100, after: $cursor) {+ pageInfo {+ endCursor+ }+ nodes {+ author {+ login+ }+ labels(first: 100) {+ nodes {+ name+ }+ }+ isDraft+ }+ }+ }+}+`+ const ignoredLabels = [+ '🤖 aspect: text',+ '🧱 stack: documentation',+ '🟥 priority: critical',+ ]+ const [owner, repo] = GITHUB_REPOSITORY.split('/')++ try {+ let hasNextPage = true+ let cursor = null+ let reviewablePRs = []++ while (hasNextPage) {+ const result = await github.graphql(GET_PULL_REQUESTS, {+ repoOwner: owner,+ repo: repo,+ cursor: cursor,+ })++ const { nodes, pageInfo } = result.repository.pullRequests+ const validPRs = nodes.filter(+ (pr) =>+ pr.author.login === context.actor &&+ !pr.isDraft &&+ !pr.labels.nodes.some((label) => ignoredLabels.includes(label.name))+ )+ reviewablePRs.push(...validPRs)++ if (pageInfo.hasNextPage) {+ cursor = pageInfo.endCursor+ } else {+ hasNextPage = false+ }+ }++ const result = {+ pr_count: reviewablePRs.length,+ slack_id: slackID,+ }+ core.info(`Current user has ${result.pr_count} PR(s).`)+ core.setOutput('pr_count', result.pr_count)+ core.setOutput('slack_id', result.slack_id)+ } catch (error) {+ core.setFailed(`Error fetching pull requests: ${error.message}`)+ }+}