Skip to content

Commit Messages to Slack Alert #166

Commit Messages to Slack Alert

Commit Messages to Slack Alert #166

name: Commit Messages to Slack Alert
on:
schedule:
- cron: '59 15 * * *' # 분 시 일 월 요일. UTC +9 매일 23:59 에 실행
jobs:
fetch-commit-messages:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Fetch commit messages
id: read-commit-messages
run: |
today=$(date +'%Y-%m-%d')' 00:00:00'
nextday=$(date -d '+1 day' +'%Y-%m-%d')' 00:00:00'
commit_messages=$(git log --since="$today" --until="$nextday" --pretty=format:"[%ad] | [msg] %s | [author] %an" --date=format:'%H:%M:%S' | sed 's/$/ \\n /' | tr -d '\n')
echo "COMMIT_MESSAGES=$commit_messages" >> $GITHUB_OUTPUT
- name: Fetch open PRs
id: fetch-open-prs
run: |
PRS=$(gh pr list --state open --format json | jq -r '.[] | select((now - .created_at | strptime("%Y-%m-%dT%H:%M:%SZ") | .days) > 7) | "[PR #\(.number)](\(.html_url)): \(.title)"' | sed 's/$/ \\n /' | tr -d '\n')
echo "OLD_PRS=$PRS" >> $GITHUB_OUTPUT
- name: Get today date
id: today-date
run: |
TODAY_ONLY_DATE=$(date +'%Y-%m-%d')
echo "TODAY_ONLY_DATE set to: $TODAY_ONLY_DATE"
echo "TODAY_DATE=$TODAY_ONLY_DATE" >> $GITHUB_OUTPUT
echo $commit_messages
- name: Post to a Slack channel
id: slack
uses: slackapi/slack-github-action@v1.25.0
with:
# Slack channel id, channel name, or user id to post message.
# See also: https://api.slack.com/methods/chat.postMessage#channels
channel-id: ${{ secrets.SLACK_CHANNEL_ID_V2 }}
# For posting a rich message using Block Kit
payload: |
{
"text": "Today's commits",
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "${{ steps.today-date.outputs.TODAY_DATE }} Commits: \n ${{ steps.read-commit-messages.outputs.COMMIT_MESSAGES }}"
}
},
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "Old PRs: \n ${{ steps.fetch-open-prs.outputs.OLD_PRS }}"
}
}
]
}
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN_V2 }}