From 1a407b1f6ce68dc3271fd515f96087d827df0958 Mon Sep 17 00:00:00 2001 From: Eric Buckley Date: Tue, 15 Oct 2024 13:04:05 -0700 Subject: [PATCH 01/17] moving codecov.yml to .github because its only used for GH workflows --- codecov.yml => .github/codecov.yml | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename codecov.yml => .github/codecov.yml (100%) diff --git a/codecov.yml b/.github/codecov.yml similarity index 100% rename from codecov.yml rename to .github/codecov.yml From d0d58318f70d0145e30d2f8a3791c3cfc98610b4 Mon Sep 17 00:00:00 2001 From: Eric Buckley Date: Tue, 15 Oct 2024 16:46:18 -0700 Subject: [PATCH 02/17] updating review notifier icon --- .github/workflows/slack_notifier_review.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/slack_notifier_review.yaml b/.github/workflows/slack_notifier_review.yaml index 066fdb21..0e7177ce 100644 --- a/.github/workflows/slack_notifier_review.yaml +++ b/.github/workflows/slack_notifier_review.yaml @@ -22,13 +22,13 @@ jobs: with: payload: | { - "text": ":code-review: Pull request ready for review by ${{ github.event.pull_request.user.login }}\n\n<${{ github.event.pull_request.html_url }}|${{ github.event.pull_request.title }}>", + "text": ":pr-open: Pull request ready for review by ${{ github.event.pull_request.user.login }}\n\n<${{ github.event.pull_request.html_url }}|${{ github.event.pull_request.title }}>", "blocks": [ { "type": "section", "text": { "type": "mrkdwn", - "text": ":code-review: Pull request ready for review by ${{ github.event.pull_request.user.login }}\n\n<${{ github.event.pull_request.html_url }}|${{ github.event.pull_request.title }}>" + "text": ":pr-open: Pull request ready for review by ${{ github.event.pull_request.user.login }}\n\n<${{ github.event.pull_request.html_url }}|${{ github.event.pull_request.title }}>" } } ] From d3eddda2e86b88fe02a0156ee2aea5ab19bea450 Mon Sep 17 00:00:00 2001 From: Eric Buckley Date: Tue, 15 Oct 2024 16:46:49 -0700 Subject: [PATCH 03/17] chaning extension --- .../{slack_notifier_merged.yaml => slack_notifier_merged.yml} | 0 .../{slack_notifier_review.yaml => slack_notifier_review.yml} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename .github/workflows/{slack_notifier_merged.yaml => slack_notifier_merged.yml} (100%) rename .github/workflows/{slack_notifier_review.yaml => slack_notifier_review.yml} (100%) diff --git a/.github/workflows/slack_notifier_merged.yaml b/.github/workflows/slack_notifier_merged.yml similarity index 100% rename from .github/workflows/slack_notifier_merged.yaml rename to .github/workflows/slack_notifier_merged.yml diff --git a/.github/workflows/slack_notifier_review.yaml b/.github/workflows/slack_notifier_review.yml similarity index 100% rename from .github/workflows/slack_notifier_review.yaml rename to .github/workflows/slack_notifier_review.yml From 4d6b3dd822ab78c949ffc5ccf806622d50d554e9 Mon Sep 17 00:00:00 2001 From: Eric Buckley Date: Tue, 15 Oct 2024 16:51:18 -0700 Subject: [PATCH 04/17] adding a script and workflow to list open pull requests --- .../workflows/slack_notifier_open_pulls.yml | 45 ++++++++++++++++++ scripts/open_pulls.sh | 47 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 .github/workflows/slack_notifier_open_pulls.yml create mode 100755 scripts/open_pulls.sh diff --git a/.github/workflows/slack_notifier_open_pulls.yml b/.github/workflows/slack_notifier_open_pulls.yml new file mode 100644 index 00000000..2a4e41cb --- /dev/null +++ b/.github/workflows/slack_notifier_open_pulls.yml @@ -0,0 +1,45 @@ +name: List Open PRs and Post to Slack + +on: + schedule: + # Run daily at 13:00 Eastern / 10:00 Pacific + - cron: "0 17 * * *" + +jobs: + list-open-prs: + runs-on: ubuntu-latest + + steps: + - name: Check out the repository + uses: actions/checkout@v3 + + - name: List open pull requests + id: list_prs + run: | + PR_LIST=$(GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} scripts/open_pulls.sh) + MSG=":code-review Open Pull Requests\n\n" + while IFS= read -r line; do + MSG+="$line\n" + done < <(echo "$PR_LIST" | jq -r '.[] | "- <\(.html_url)|\(.title)> opened by \(.user) \(.time_since) days ago"') + echo "MSG=$MSG" >> $GITHUB_ENV + + - name: Post to Slack + id: slack + uses: slackapi/slack-github-action@v1.27.0 + with: + payload: | + { + "text": "${{ env.MSG }}" + "blocks": [ + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "${{ env.MSG }}" + } + } + ] + } + env: + SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} + SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK diff --git a/scripts/open_pulls.sh b/scripts/open_pulls.sh new file mode 100755 index 00000000..5bef9f3b --- /dev/null +++ b/scripts/open_pulls.sh @@ -0,0 +1,47 @@ +#!/bin/bash + +# This script lists all open pull requests for this repository. +# +# Usage: open_pulls.sh +# Requires: curl, jq + +set -e + +cd "$(dirname "$0")/.." + +# check if the GITHUB_TOKEN environment variable is set +if [ -z "$GITHUB_TOKEN" ]; then + echo "Missing environment variable: GITHUB_TOKEN" + exit 1 +fi +# set the URL to the GitHub API +REPO=$(git config --get remote.origin.url | sed -E 's#git@github.com:(.*)\.git#\1#') +URL="https://api.github.com/repos/$(echo $REPO | cut -d'/' -f1)/$(echo $REPO | cut -d'/' -f2)" + +# create function to call GH API, the first argument is the endpoint +gh_api() { + curl --silent -H "Authorization: token ${GITHUB_TOKEN}" -H "Accept: application/vnd.github.v3+json" ${URL}/$1 +} + +# List all open non-draft pull requests, and reduce to key fields +pr_list=$(gh_api pulls?state=open | jq -r 'map(select(.draft == false))' | jq '[.[] | {title, number, user: .user.login, html_url}]') + +# For each PR, get the timestamp of the ready_for_review event using the timeline API +# create an array of JSON objects +results="" +while IFS= read -r pr; do + number=$(echo "$pr" | jq -r '.number') + # get the timestamp of the ready_for_review event + ready_for_review=$(gh_api issues/${number}/timeline | jq -r 'map(select(.event == "ready_for_review")) | .[0].created_at') + # calculate the number of days since the PR was ready for review + time_since=$((($(date +%s) - $(date -d "$ready_for_review" +%s)) / 86400)) + # add the time_since field to the JSON object + pr=$(echo "$pr" | jq ". + {time_since: $time_since}") + if [ -z "$results" ]; then + results="$pr" + else + results="$results, $pr" + fi +done < <(echo "$pr_list" | jq -c '.[]') +# output the results as a JSON array +echo "[$results]" | jq . From 325b387d817b04d92ac98394141fb38f48b034d0 Mon Sep 17 00:00:00 2001 From: Eric Buckley Date: Tue, 15 Oct 2024 16:53:06 -0700 Subject: [PATCH 05/17] adding temp workflow dispatch --- .github/workflows/slack_notifier_open_pulls.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/slack_notifier_open_pulls.yml b/.github/workflows/slack_notifier_open_pulls.yml index 2a4e41cb..7f395d9f 100644 --- a/.github/workflows/slack_notifier_open_pulls.yml +++ b/.github/workflows/slack_notifier_open_pulls.yml @@ -4,6 +4,7 @@ on: schedule: # Run daily at 13:00 Eastern / 10:00 Pacific - cron: "0 17 * * *" + workflow_dispatch: jobs: list-open-prs: From c953bb34239c4049024a3bd87980f5b0f376fd97 Mon Sep 17 00:00:00 2001 From: Eric Buckley Date: Tue, 15 Oct 2024 17:03:16 -0700 Subject: [PATCH 06/17] updating to test --- .github/workflows/slack_notifier_open_pulls.yml | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/slack_notifier_open_pulls.yml b/.github/workflows/slack_notifier_open_pulls.yml index 7f395d9f..94d0049f 100644 --- a/.github/workflows/slack_notifier_open_pulls.yml +++ b/.github/workflows/slack_notifier_open_pulls.yml @@ -1,10 +1,11 @@ -name: List Open PRs and Post to Slack +name: "post open pulls to slack" on: schedule: # Run daily at 13:00 Eastern / 10:00 Pacific - cron: "0 17 * * *" - workflow_dispatch: + pull_request: + branches: ["feature/list-open-prs"] jobs: list-open-prs: From 0f480604acc34cdaf8b2dd8b70177106493e2960 Mon Sep 17 00:00:00 2001 From: Eric Buckley Date: Tue, 15 Oct 2024 17:09:24 -0700 Subject: [PATCH 07/17] small formatting changes --- .github/workflows/slack_notifier_open_pulls.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/slack_notifier_open_pulls.yml b/.github/workflows/slack_notifier_open_pulls.yml index 94d0049f..11bc59e2 100644 --- a/.github/workflows/slack_notifier_open_pulls.yml +++ b/.github/workflows/slack_notifier_open_pulls.yml @@ -17,12 +17,15 @@ jobs: - name: List open pull requests id: list_prs - run: | + run: > PR_LIST=$(GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} scripts/open_pulls.sh) + MSG=":code-review Open Pull Requests\n\n" + while IFS= read -r line; do MSG+="$line\n" done < <(echo "$PR_LIST" | jq -r '.[] | "- <\(.html_url)|\(.title)> opened by \(.user) \(.time_since) days ago"') + echo "MSG=$MSG" >> $GITHUB_ENV - name: Post to Slack From 08dae6dc6dbaf0644eac9d31b3b01f88b42b372f Mon Sep 17 00:00:00 2001 From: Eric Buckley Date: Tue, 15 Oct 2024 17:14:53 -0700 Subject: [PATCH 08/17] changing branch name for testing --- .github/workflows/slack_notifier_open_pulls.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/slack_notifier_open_pulls.yml b/.github/workflows/slack_notifier_open_pulls.yml index 11bc59e2..6a0e0118 100644 --- a/.github/workflows/slack_notifier_open_pulls.yml +++ b/.github/workflows/slack_notifier_open_pulls.yml @@ -5,7 +5,7 @@ on: # Run daily at 13:00 Eastern / 10:00 Pacific - cron: "0 17 * * *" pull_request: - branches: ["feature/list-open-prs"] + branches: [feature/list-open-prs] jobs: list-open-prs: From 595115d43ec688bc1e45db8e9694509ef5508513 Mon Sep 17 00:00:00 2001 From: Eric Buckley Date: Tue, 15 Oct 2024 17:16:14 -0700 Subject: [PATCH 09/17] changing on syntax again for testing --- .github/workflows/slack_notifier_open_pulls.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/slack_notifier_open_pulls.yml b/.github/workflows/slack_notifier_open_pulls.yml index 6a0e0118..a990a197 100644 --- a/.github/workflows/slack_notifier_open_pulls.yml +++ b/.github/workflows/slack_notifier_open_pulls.yml @@ -5,7 +5,9 @@ on: # Run daily at 13:00 Eastern / 10:00 Pacific - cron: "0 17 * * *" pull_request: - branches: [feature/list-open-prs] + branches: [main] + push: + branches: [main] jobs: list-open-prs: From 1a554bee4be6e8580c49ee39e0f00e0e1c78c955 Mon Sep 17 00:00:00 2001 From: Eric Buckley Date: Tue, 15 Oct 2024 17:24:46 -0700 Subject: [PATCH 10/17] slight changes --- .github/workflows/slack_notifier_open_pulls.yml | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/.github/workflows/slack_notifier_open_pulls.yml b/.github/workflows/slack_notifier_open_pulls.yml index a990a197..6bce54f7 100644 --- a/.github/workflows/slack_notifier_open_pulls.yml +++ b/.github/workflows/slack_notifier_open_pulls.yml @@ -20,15 +20,18 @@ jobs: - name: List open pull requests id: list_prs run: > - PR_LIST=$(GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} scripts/open_pulls.sh) + scripts/open_pulls.sh + PR_LIST=$(scripts/open_pulls.sh) - MSG=":code-review Open Pull Requests\n\n" + MSG=":code-review: Open Pull Requests\n\n" while IFS= read -r line; do MSG+="$line\n" done < <(echo "$PR_LIST" | jq -r '.[] | "- <\(.html_url)|\(.title)> opened by \(.user) \(.time_since) days ago"') echo "MSG=$MSG" >> $GITHUB_ENV + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} - name: Post to Slack id: slack From 5a6d2c2909bd880e3ef780deb1f45eae6a3e37a9 Mon Sep 17 00:00:00 2001 From: Eric Buckley Date: Tue, 15 Oct 2024 17:28:00 -0700 Subject: [PATCH 11/17] granting read permissions on the issues and pulls api endpoints --- .github/workflows/slack_notifier_open_pulls.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/slack_notifier_open_pulls.yml b/.github/workflows/slack_notifier_open_pulls.yml index 6bce54f7..67cc3147 100644 --- a/.github/workflows/slack_notifier_open_pulls.yml +++ b/.github/workflows/slack_notifier_open_pulls.yml @@ -9,6 +9,10 @@ on: push: branches: [main] +permissions: + pull-requests: read + issues: read + jobs: list-open-prs: runs-on: ubuntu-latest From d121de9e0dad4fc57441bd49a3e3c58f39456311 Mon Sep 17 00:00:00 2001 From: Eric Buckley Date: Tue, 15 Oct 2024 17:30:45 -0700 Subject: [PATCH 12/17] moving permissions to job --- .github/workflows/slack_notifier_open_pulls.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/slack_notifier_open_pulls.yml b/.github/workflows/slack_notifier_open_pulls.yml index 67cc3147..02d40b67 100644 --- a/.github/workflows/slack_notifier_open_pulls.yml +++ b/.github/workflows/slack_notifier_open_pulls.yml @@ -9,13 +9,13 @@ on: push: branches: [main] -permissions: - pull-requests: read - issues: read jobs: list-open-prs: runs-on: ubuntu-latest + permissions: + pull-requests: read + issues: read steps: - name: Check out the repository From 0a032a4de6828ca5614202ba9b4434656fdb3461 Mon Sep 17 00:00:00 2001 From: Eric Buckley Date: Tue, 15 Oct 2024 17:45:57 -0700 Subject: [PATCH 13/17] overriding github repo in CI --- .github/workflows/slack_notifier_open_pulls.yml | 1 + scripts/open_pulls.sh | 14 +++++++++++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/.github/workflows/slack_notifier_open_pulls.yml b/.github/workflows/slack_notifier_open_pulls.yml index 02d40b67..128cba22 100644 --- a/.github/workflows/slack_notifier_open_pulls.yml +++ b/.github/workflows/slack_notifier_open_pulls.yml @@ -36,6 +36,7 @@ jobs: echo "MSG=$MSG" >> $GITHUB_ENV env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GITHUB_REPO: ${{ github.repository }} - name: Post to Slack id: slack diff --git a/scripts/open_pulls.sh b/scripts/open_pulls.sh index 5bef9f3b..ab3f53bf 100755 --- a/scripts/open_pulls.sh +++ b/scripts/open_pulls.sh @@ -14,9 +14,13 @@ if [ -z "$GITHUB_TOKEN" ]; then echo "Missing environment variable: GITHUB_TOKEN" exit 1 fi + +if [ -z "$GITHUB_REPO" ]; then + REMOTE=$(git config --get remote.origin.url | sed -E 's#git@github.com:(.*)\.git#\1#') + GITHUB_REPO="$(echo $REMOTE | cut -d'/' -f1)/$(echo $REMOTE | cut -d'/' -f2)" +fi # set the URL to the GitHub API -REPO=$(git config --get remote.origin.url | sed -E 's#git@github.com:(.*)\.git#\1#') -URL="https://api.github.com/repos/$(echo $REPO | cut -d'/' -f1)/$(echo $REPO | cut -d'/' -f2)" +URL="https://api.github.com/repos/${GITHUB_REPO}" # create function to call GH API, the first argument is the endpoint gh_api() { @@ -24,7 +28,7 @@ gh_api() { } # List all open non-draft pull requests, and reduce to key fields -pr_list=$(gh_api pulls?state=open | jq -r 'map(select(.draft == false))' | jq '[.[] | {title, number, user: .user.login, html_url}]') +pr_list=$(gh_api pulls?state=open | jq -r 'map(select(.draft == false))' | jq '[.[] | {title, number, user: .user.login, html_url, created_at}]') # For each PR, get the timestamp of the ready_for_review event using the timeline API # create an array of JSON objects @@ -34,6 +38,10 @@ while IFS= read -r pr; do # get the timestamp of the ready_for_review event ready_for_review=$(gh_api issues/${number}/timeline | jq -r 'map(select(.event == "ready_for_review")) | .[0].created_at') # calculate the number of days since the PR was ready for review + # only calculate if ready_for_review does not equal "null" + if [ "$ready_for_review" == "null" ]; then + ready_for_review=$(echo $pr | jq -r '.created_at') + fi time_since=$((($(date +%s) - $(date -d "$ready_for_review" +%s)) / 86400)) # add the time_since field to the JSON object pr=$(echo "$pr" | jq ". + {time_since: $time_since}") From 86e88074bed7ba339bdf7fee7f42c4d40a717ca3 Mon Sep 17 00:00:00 2001 From: Eric Buckley Date: Tue, 15 Oct 2024 17:47:24 -0700 Subject: [PATCH 14/17] adding more debugging code --- .github/workflows/slack_notifier_open_pulls.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/slack_notifier_open_pulls.yml b/.github/workflows/slack_notifier_open_pulls.yml index 128cba22..fb4752ef 100644 --- a/.github/workflows/slack_notifier_open_pulls.yml +++ b/.github/workflows/slack_notifier_open_pulls.yml @@ -24,7 +24,6 @@ jobs: - name: List open pull requests id: list_prs run: > - scripts/open_pulls.sh PR_LIST=$(scripts/open_pulls.sh) MSG=":code-review: Open Pull Requests\n\n" @@ -33,6 +32,8 @@ jobs: MSG+="$line\n" done < <(echo "$PR_LIST" | jq -r '.[] | "- <\(.html_url)|\(.title)> opened by \(.user) \(.time_since) days ago"') + echo $MSG + echo "MSG=$MSG" >> $GITHUB_ENV env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} From aecb09aa9d56126201cfdeef10c805fb08fdbc20 Mon Sep 17 00:00:00 2001 From: Eric Buckley Date: Tue, 15 Oct 2024 17:48:49 -0700 Subject: [PATCH 15/17] adding missing comma --- .github/workflows/slack_notifier_open_pulls.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/slack_notifier_open_pulls.yml b/.github/workflows/slack_notifier_open_pulls.yml index fb4752ef..71550ddd 100644 --- a/.github/workflows/slack_notifier_open_pulls.yml +++ b/.github/workflows/slack_notifier_open_pulls.yml @@ -45,7 +45,7 @@ jobs: with: payload: | { - "text": "${{ env.MSG }}" + "text": "${{ env.MSG }}", "blocks": [ { "type": "section", From ebf27c609ffb8612e190dfe87efbbd7a49a1a2c9 Mon Sep 17 00:00:00 2001 From: Eric Buckley Date: Tue, 15 Oct 2024 17:51:45 -0700 Subject: [PATCH 16/17] remove debugging code --- .github/workflows/slack_notifier_open_pulls.yml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.github/workflows/slack_notifier_open_pulls.yml b/.github/workflows/slack_notifier_open_pulls.yml index 71550ddd..ba2113bc 100644 --- a/.github/workflows/slack_notifier_open_pulls.yml +++ b/.github/workflows/slack_notifier_open_pulls.yml @@ -30,9 +30,7 @@ jobs: while IFS= read -r line; do MSG+="$line\n" - done < <(echo "$PR_LIST" | jq -r '.[] | "- <\(.html_url)|\(.title)> opened by \(.user) \(.time_since) days ago"') - - echo $MSG + done < <(echo "$PR_LIST" | jq -r '.[] | " - <\(.html_url)|\(.title)> opened by \(.user) \(.time_since) days ago"') echo "MSG=$MSG" >> $GITHUB_ENV env: From 1f6078dc4e437677c958c9347027ac6070fb4eba Mon Sep 17 00:00:00 2001 From: Eric Buckley Date: Tue, 15 Oct 2024 17:55:20 -0700 Subject: [PATCH 17/17] skip post to slack if prlist is empty --- .github/workflows/slack_notifier_open_pulls.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/slack_notifier_open_pulls.yml b/.github/workflows/slack_notifier_open_pulls.yml index ba2113bc..5615542e 100644 --- a/.github/workflows/slack_notifier_open_pulls.yml +++ b/.github/workflows/slack_notifier_open_pulls.yml @@ -4,10 +4,6 @@ on: schedule: # Run daily at 13:00 Eastern / 10:00 Pacific - cron: "0 17 * * *" - pull_request: - branches: [main] - push: - branches: [main] jobs: @@ -33,6 +29,7 @@ jobs: done < <(echo "$PR_LIST" | jq -r '.[] | " - <\(.html_url)|\(.title)> opened by \(.user) \(.time_since) days ago"') echo "MSG=$MSG" >> $GITHUB_ENV + echo "PR_LIST=$PR_LIST" >> $GITHUB_ENV env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_REPO: ${{ github.repository }} @@ -40,6 +37,7 @@ jobs: - name: Post to Slack id: slack uses: slackapi/slack-github-action@v1.27.0 + if: ${{ env.PR_LIST != '[]' }} with: payload: | {