|
| 1 | +name: Blog Slack Notifications |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - "apps/web/content/articles/**" |
| 9 | + |
| 10 | +jobs: |
| 11 | + notify: |
| 12 | + if: startsWith(github.head_ref, 'blog/') |
| 13 | + runs-on: ubuntu-latest |
| 14 | + permissions: |
| 15 | + contents: read |
| 16 | + |
| 17 | + steps: |
| 18 | + - name: Checkout code |
| 19 | + uses: actions/checkout@v4 |
| 20 | + with: |
| 21 | + fetch-depth: 0 |
| 22 | + |
| 23 | + - name: Get changed files |
| 24 | + id: changed-files |
| 25 | + run: | |
| 26 | + FILES=$(git diff --name-only origin/${{ github.base_ref }}...HEAD -- 'apps/web/content/articles/*.mdx' | tr '\n' ' ') |
| 27 | + echo "files=$FILES" >> $GITHUB_OUTPUT |
| 28 | + if [ -z "$FILES" ]; then |
| 29 | + echo "has_files=false" >> $GITHUB_OUTPUT |
| 30 | + else |
| 31 | + echo "has_files=true" >> $GITHUB_OUTPUT |
| 32 | + fi |
| 33 | +
|
| 34 | + - name: Extract article info |
| 35 | + if: steps.changed-files.outputs.has_files == 'true' |
| 36 | + id: article-info |
| 37 | + run: | |
| 38 | + FILE=$(echo "${{ steps.changed-files.outputs.files }}" | awk '{print $1}') |
| 39 | + if [ -f "$FILE" ]; then |
| 40 | + RAW_TITLE=$(grep -m1 "^display_title:" "$FILE" | sed 's/display_title:[[:space:]]*"\(.*\)"/\1/' || echo "") |
| 41 | + if [ -z "$RAW_TITLE" ]; then |
| 42 | + RAW_TITLE=$(grep -m1 "^meta_title:" "$FILE" | sed 's/meta_title:[[:space:]]*"\(.*\)"/\1/' || echo "Untitled") |
| 43 | + fi |
| 44 | + RAW_AUTHOR=$(grep -m1 "^author:" "$FILE" | sed 's/author:[[:space:]]*"\(.*\)"/\1/' || echo "Unknown") |
| 45 | + RAW_DATE=$(grep -m1 "^date:" "$FILE" | sed 's/date:[[:space:]]*"\(.*\)"/\1/' || echo "") |
| 46 | +
|
| 47 | + TITLE=$(echo "$RAW_TITLE" | jq -Rs . | sed 's/^"//;s/"$//') |
| 48 | + AUTHOR=$(echo "$RAW_AUTHOR" | jq -Rs . | sed 's/^"//;s/"$//') |
| 49 | + DATE=$(echo "$RAW_DATE" | jq -Rs . | sed 's/^"//;s/"$//') |
| 50 | + else |
| 51 | + TITLE="Untitled" |
| 52 | + AUTHOR="Unknown" |
| 53 | + DATE="" |
| 54 | + fi |
| 55 | + echo "title=$TITLE" >> $GITHUB_OUTPUT |
| 56 | + echo "author=$AUTHOR" >> $GITHUB_OUTPUT |
| 57 | + echo "date=$DATE" >> $GITHUB_OUTPUT |
| 58 | +
|
| 59 | + # Determine if this is a new article or edit (edit branches have timestamp suffix) |
| 60 | + BRANCH="${{ github.head_ref }}" |
| 61 | + if [[ "$BRANCH" =~ -[0-9]+$ ]]; then |
| 62 | + echo "is_edit=true" >> $GITHUB_OUTPUT |
| 63 | + else |
| 64 | + echo "is_edit=false" >> $GITHUB_OUTPUT |
| 65 | + fi |
| 66 | +
|
| 67 | + # Map GitHub username to Slack user ID |
| 68 | + GH_USER="${{ github.event.pull_request.user.login }}" |
| 69 | + case "$GH_USER" in |
| 70 | + "yujonglee") SLACK_USER="<@U0628P6TAPL>" ;; |
| 71 | + "ComputelessComputer") SLACK_USER="<@U08PVBSGL31>" ;; |
| 72 | + "harshikaalagh-netizen") SLACK_USER="<@U0976J9CAKF>" ;; |
| 73 | + *) SLACK_USER="$GH_USER" ;; |
| 74 | + esac |
| 75 | + echo "slack_user=$SLACK_USER" >> $GITHUB_OUTPUT |
| 76 | +
|
| 77 | + - name: Check ready_for_review status |
| 78 | + if: steps.changed-files.outputs.has_files == 'true' |
| 79 | + id: review-status |
| 80 | + run: | |
| 81 | + FILE=$(echo "${{ steps.changed-files.outputs.files }}" | awk '{print $1}') |
| 82 | + if [ -f "$FILE" ]; then |
| 83 | + READY_FOR_REVIEW=$(grep -m1 "^ready_for_review:" "$FILE" | sed 's/ready_for_review:[[:space:]]*//' || echo "false") |
| 84 | + echo "ready_for_review=$READY_FOR_REVIEW" >> $GITHUB_OUTPUT |
| 85 | + else |
| 86 | + echo "ready_for_review=false" >> $GITHUB_OUTPUT |
| 87 | + fi |
| 88 | +
|
| 89 | + - name: Notify Slack |
| 90 | + if: steps.changed-files.outputs.has_files == 'true' |
| 91 | + uses: slackapi/slack-github-action@v2.0.0 |
| 92 | + with: |
| 93 | + method: chat.postMessage |
| 94 | + token: ${{ secrets.SLACK_BOT_TOKEN }} |
| 95 | + payload: | |
| 96 | + { |
| 97 | + "channel": "${{ secrets.SLACK_BLOG_CHANNEL_ID }}", |
| 98 | + "text": "${{ steps.review-status.outputs.ready_for_review == 'true' && format('Article submitted for review: {0}', steps.article-info.outputs.title) || (steps.article-info.outputs.is_edit == 'true' && format('{0} made changes to {1}', steps.article-info.outputs.slack_user, steps.article-info.outputs.title) || format('{0} is ready to publish: {1}', steps.article-info.outputs.slack_user, steps.article-info.outputs.title)) }}", |
| 99 | + "attachments": [ |
| 100 | + { |
| 101 | + "color": "${{ steps.review-status.outputs.ready_for_review == 'true' && '#3b82f6' || '#2eb886' }}", |
| 102 | + "blocks": [ |
| 103 | + { |
| 104 | + "type": "section", |
| 105 | + "text": { |
| 106 | + "type": "mrkdwn", |
| 107 | + "text": "${{ steps.review-status.outputs.ready_for_review == 'true' && format('👀 *Article submitted for review*\n<@U08PVBSGL31> please review\n\n>*{0}*', steps.article-info.outputs.title) || (steps.article-info.outputs.is_edit == 'true' && format('✏️ {0} made changes to *{1}*', steps.article-info.outputs.slack_user, steps.article-info.outputs.title) || format('🚀 {0} is ready to publish a new article: *{1}*', steps.article-info.outputs.slack_user, steps.article-info.outputs.title)) }}" |
| 108 | + } |
| 109 | + }, |
| 110 | + { |
| 111 | + "type": "context", |
| 112 | + "elements": [ |
| 113 | + { |
| 114 | + "type": "mrkdwn", |
| 115 | + "text": "👤 ${{ steps.article-info.outputs.author }}${{ steps.article-info.outputs.date != '' && format(' • 📅 {0}', steps.article-info.outputs.date) || '' }}" |
| 116 | + } |
| 117 | + ] |
| 118 | + }, |
| 119 | + { |
| 120 | + "type": "actions", |
| 121 | + "elements": [ |
| 122 | + { |
| 123 | + "type": "button", |
| 124 | + "text": { |
| 125 | + "type": "plain_text", |
| 126 | + "text": "View PR", |
| 127 | + "emoji": true |
| 128 | + }, |
| 129 | + "url": "${{ github.event.pull_request.html_url }}", |
| 130 | + "style": "primary" |
| 131 | + } |
| 132 | + ] |
| 133 | + } |
| 134 | + ] |
| 135 | + } |
| 136 | + ] |
| 137 | + } |
0 commit comments