-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use repo variables instead of artifacts
- Loading branch information
Showing
2 changed files
with
74 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,9 @@ name: Post comments on slack, with rate limiting. | |
on: | ||
workflow_call: | ||
secrets: | ||
GITHUB_PAT: | ||
description: 'The github token used to read the timeout variable.' | ||
required: true | ||
SLACK_BOT_TOKEN: | ||
description: 'The slack API token, with `chat:write` permissions.' | ||
required: true | ||
|
@@ -14,8 +17,8 @@ on: | |
description: 'The message to send to slack.' | ||
required: true | ||
type: string | ||
message-label: | ||
description: 'A label identifying the message type. Used to rate limit messages.' | ||
timeout-variable: | ||
description: 'A repository variable used to store the last message timestamp.' | ||
required: true | ||
type: string | ||
timeout-minutes: | ||
|
@@ -37,11 +40,41 @@ jobs: | |
steps: | ||
- name: Check last message timestamp | ||
id: last-sent | ||
uses: dawidd6/action-download-artifact@v6 | ||
with: | ||
# Downloads the artifact from the most recent successful run | ||
name: last-sent-${{ inputs.message-label }}.txt | ||
if_no_artifact_found: ignore | ||
run: | | ||
set +e | ||
gh api \ | ||
-H "Accept: application/vnd.github+json" \ | ||
-H "X-GitHub-Api-Version: 2022-11-28" \ | ||
/repos/$OWNER/$REPO/actions/variables/$VAR \ | ||
> read-variable.json | ||
if [ $? -ne 0 ] | ||
then | ||
echo "Could not read the variable." | ||
echo "missing=true" >> $GITHUB_OUTPUT | ||
else | ||
jq -r '.value' read-variable.json > last-sent.txt | ||
echo "missing=false" >> $GITHUB_OUTPUT | ||
fi | ||
env: | ||
GH_TOKEN: ${{ secrets.GITHUB_PAT }} | ||
OWNER: ${{ github.repository_owner }} | ||
REPO: ${{ github.event.repository.name }} | ||
VAR: ${{ inputs.timeout-variable }} | ||
|
||
- name: Create the timestamp variable if it's missing | ||
if: ${{ steps.last-sent.outputs.missing == 'true' }} | ||
run: | | ||
gh api \ | ||
--method POST \ | ||
-H "Accept: application/vnd.github+json" \ | ||
-H "X-GitHub-Api-Version: 2022-11-28" \ | ||
/repos/$OWNER/$REPO/actions/variables \ | ||
-f "name=$VAR" -f "value=1990-01-01T00:00:00Z" | ||
env: | ||
GH_TOKEN: ${{ secrets.GITHUB_PAT }} | ||
OWNER: ${{ github.repository_owner }} | ||
REPO: ${{ github.event.repository.name }} | ||
VAR: ${{ inputs.timeout-variable }} | ||
|
||
- name: Rate limit | ||
id: rate-limit | ||
|
@@ -52,42 +85,52 @@ jobs: | |
echo "\"$MESSAGE\"" | ||
echo | ||
if [ -f last-sent-${{ inputs.message-label }}.txt ] | ||
if [ -f last-sent.txt ] | ||
then | ||
LAST_SENT=$( cat last-sent-${{ inputs.message-label }}.txt ) | ||
LAST_SENT=$( cat last-sent.txt ) | ||
NOW=$( date +'%FT%TZ' ) | ||
echo "Last sent: $LAST_SENT" | ||
NOW=$( date +%s ) | ||
echo "Now: $NOW" | ||
DIFF_MINUTES=$(( ( $( date -d "$NOW" +%s ) - $( date -d "$LAST_SENT" +%s ) ) / 60 )) | ||
echo "Timeout: $TIMEOUT mins" | ||
echo "Difference: $(( NOW - LAST_SENT ))" | ||
echo "Difference: $DIFF_MINUTES mins" | ||
if [ $(( NOW - LAST_SENT )) -lt $(( TIMEOUT * 60 )) ] | ||
if [ $DIFF_MINUTES -lt $TIMEOUT ] | ||
then | ||
echo "Rate limiting. Not sending the message." | ||
echo "On timeout period. Not sending the message." | ||
echo "send=false" >> "$GITHUB_OUTPUT" | ||
exit 0 | ||
fi | ||
else | ||
echo "No last-sent file found." | ||
echo "Last-sent variable was not set." | ||
fi | ||
echo "send=true" >> "$GITHUB_OUTPUT" | ||
date +%s > last-sent-${{ inputs.message-label }}.txt | ||
date +%s > last-sent.txt | ||
env: | ||
TIMEOUT: ${{ inputs.timeout-minutes }} | ||
MESSAGE: ${{ inputs.slack-message }} | ||
|
||
- name: Send notification | ||
if: ${{ steps.rate-limit.outputs.send == 'true' }} | ||
if: false && ${{ steps.rate-limit.outputs.send == 'true' }} | ||
uses: slackapi/[email protected] | ||
with: | ||
channel-id: ${{ inputs.channel-id }} | ||
slack-message: ${{ inputs.slack-message }} | ||
env: | ||
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }} | ||
|
||
- name: Upload the new last-sent timestamp | ||
- name: Modify the variable to save the new timestamp | ||
if: ${{ steps.rate-limit.outputs.send == 'true' }} | ||
uses: actions/upload-artifact@v4 | ||
with: | ||
name: last-sent-${{ inputs.message-label }}.txt | ||
path: last-sent-${{ inputs.message-label }}.txt | ||
run: | | ||
gh api \ | ||
--method PATCH \ | ||
-H "Accept: application/vnd.github+json" \ | ||
-H "X-GitHub-Api-Version: 2022-11-28" \ | ||
/repos/$OWNER/$REPO/actions/variables/$VAR \ | ||
-f "name=$VAR" -f "value=$( date +'%FT%TZ' )" | ||
env: | ||
GH_TOKEN: ${{ secrets.GITHUB_PAT }} | ||
OWNER: ${{ github.repository_owner }} | ||
REPO: ${{ github.event.repository.name }} | ||
VAR: ${{ inputs.timeout-variable }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters