Skip to content

Commit

Permalink
Use repo variables instead of artifacts
Browse files Browse the repository at this point in the history
  • Loading branch information
aborgna-q committed Oct 25, 2024
1 parent 5346805 commit 35709ae
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 25 deletions.
86 changes: 65 additions & 21 deletions .github/workflows/slack-notifier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand All @@ -37,11 +40,42 @@ 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
continue-on-error: true
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 -e '.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
Expand All @@ -52,42 +86,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: 0 && ${{ steps.rate-limit.outputs.send == 'true' }}
uses: slackapi/slack-github-action@v1.27.0
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 }}
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,20 +206,20 @@ jobs:
with:
channel-id: "SOME CHANNEL ID"
slack-message: "Hello 🌎!"
# An unique identifier for the message type, to use in rate limiting.
message-label: "hello"
# A minimum time in minutes to wait before sending another message.
timeout-minutes: 60
# A repository variable used to store the last message timestamp.
timeout-variable: "HELLO_MESSAGE_TIMESTAMP"
secrets:
GITHUB_PAT: ${{ secrets.GITHUB_PAT }}
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
```

### Inputs

- `channel-id`: The ID of the channel to post the message to. (**required**)
- `slack-message`: The message to post. (**required**)
- `message-label`: An identifier for the message type, to use in rate limiting.
This label is tied to the workflow name. (**required**)
- `timeout-variable`: A repository variable used to store the last message timestamp. (**required**)
- `timeout-minutes`: A minimum time in minutes to wait before sending another message. Defaults to 24 hours.

### Outputs
Expand All @@ -234,3 +234,9 @@ channel. See the
documentation for more information.
If you are using a slack app, make sure to add it to the channel.
See formatting options in the [Slack API documentation](https://api.slack.com/reference/surfaces/formatting).

The fine-grained `GITHUB_PAT` secret must include the following permissions:

| Permission | Access |
| --- | --- |
| Variables (repository) | Read and write |

0 comments on commit 35709ae

Please sign in to comment.