Post SNS #392
This file contains hidden or 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
| name: Post SNS | |
| on: | |
| workflow_run: | |
| workflows: ["Deploy to Vercel"] | |
| types: | |
| - completed | |
| branches: | |
| - main | |
| permissions: | |
| pull-requests: read | |
| contents: read | |
| jobs: | |
| check-should-post: | |
| if: github.event.workflow_run.conclusion == 'success' | |
| runs-on: ubuntu-22.04 | |
| outputs: | |
| should-post: ${{ steps.check.outputs.should-post }} | |
| is-blog: ${{ steps.check.outputs.is-blog }} | |
| post-url: ${{ steps.check.outputs.post-url }} | |
| pr-number: ${{ steps.check.outputs.pr-number }} | |
| steps: | |
| - uses: actions/checkout@v4 | |
| - name: Check if should post to SNS | |
| id: check | |
| uses: actions/github-script@v8 | |
| with: | |
| script: | | |
| const { workflow_run, repository } = context.payload; | |
| const owner = repository.owner.login; | |
| const repo = repository.name; | |
| const headSha = workflow_run.head_sha; | |
| console.log('Owner:', owner); | |
| console.log('Repo:', repo); | |
| console.log('Head SHA:', headSha); | |
| console.log('Workflow run pull_requests:', workflow_run.pull_requests); | |
| // Get PRs associated with this commit | |
| const { data: prs } = await github.rest.repos.listPullRequestsAssociatedWithCommit({ | |
| owner, | |
| repo, | |
| commit_sha: headSha | |
| }); | |
| console.log(`Found ${prs.length} PR(s) for commit`); | |
| if (prs.length === 0) { | |
| console.log('No PR found for commit, skipping SNS post'); | |
| core.setOutput('should-post', false); | |
| core.setOutput('is-blog', false); | |
| core.setOutput('post-url', 'https://gitauto.ai'); | |
| return; | |
| } | |
| const pullRequest = prs[0]; | |
| console.log(`Using PR #${pullRequest.number}: ${pullRequest.title}`); | |
| const { getBlogInfo } = require('.github/scripts/utils.js'); | |
| const triggeringContext = { | |
| repo: { owner, repo }, | |
| payload: { pull_request: pullRequest } | |
| }; | |
| // Check if it's a blog post | |
| const { isBlog, postUrl } = await getBlogInfo({ github, context: triggeringContext }); | |
| // Check if has sns label | |
| const hasSnsLabel = pullRequest.labels?.some(label => label.name === 'sns') || false; | |
| // Should post if it's a blog post OR has sns label | |
| const shouldPost = isBlog || hasSnsLabel; | |
| core.setOutput('should-post', shouldPost); | |
| core.setOutput('is-blog', isBlog); | |
| core.setOutput('post-url', postUrl); | |
| core.setOutput('pr-number', pullRequest.number); | |
| console.log(`Blog post detected: ${isBlog}`); | |
| console.log(`SNS label detected: ${hasSnsLabel}`); | |
| console.log(`Should post to SNS: ${shouldPost}`); | |
| post-sns: | |
| needs: check-should-post | |
| if: needs.check-should-post.outputs.should-post == 'true' | |
| runs-on: ubuntu-22.04 | |
| steps: | |
| # https://github.com/actions/checkout | |
| - uses: actions/checkout@v4 | |
| # https://github.com/actions/setup-node | |
| - uses: actions/setup-node@v4 | |
| with: | |
| node-version: "22" | |
| # https://github.com/plhery/node-twitter-api-v2 | |
| # https://github.com/linkedin-developers/linkedin-api-js-client#linkedin-api-javascript-client | |
| - name: Install dependencies | |
| run: | | |
| npm install twitter-api-v2 | |
| npm install linkedin-api-client | |
| npm install playwright | |
| npx playwright install chromium | |
| # https://github.com/actions/github-script | |
| - name: Post to X | |
| uses: actions/github-script@v8 | |
| continue-on-error: true | |
| with: | |
| script: | | |
| const postTwitter = require('.github/scripts/post-twitter.js'); | |
| const isBlog = ${{ needs.check-should-post.outputs.is-blog }}; | |
| const postUrl = '${{ needs.check-should-post.outputs.post-url }}'; | |
| const prNumber = ${{ needs.check-should-post.outputs.pr-number }}; | |
| const { data: pullRequest } = await github.rest.pulls.get({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: prNumber | |
| }); | |
| const triggeringContext = { | |
| repo: context.repo, | |
| payload: { pull_request: pullRequest } | |
| }; | |
| await postTwitter({ context: triggeringContext, isBlog, postUrl }); | |
| env: | |
| TWITTER_API_KEY: ${{ secrets.TWITTER_API_KEY }} | |
| TWITTER_API_SECRET: ${{ secrets.TWITTER_API_SECRET }} | |
| TWITTER_ACCESS_TOKEN: ${{ secrets.TWITTER_ACCESS_TOKEN }} | |
| TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }} | |
| TWITTER_API_KEY_WES: ${{ secrets.TWITTER_API_KEY_WES }} | |
| TWITTER_API_SECRET_WES: ${{ secrets.TWITTER_API_SECRET_WES }} | |
| TWITTER_ACCESS_TOKEN_WES: ${{ secrets.TWITTER_ACCESS_TOKEN_WES }} | |
| TWITTER_ACCESS_TOKEN_SECRET_WES: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET_WES }} | |
| - name: Post to LinkedIn | |
| uses: actions/github-script@v8 | |
| continue-on-error: true | |
| with: | |
| script: | | |
| const postLinkedIn = require('.github/scripts/post-linkedin.js'); | |
| const isBlog = ${{ needs.check-should-post.outputs.is-blog }}; | |
| const postUrl = '${{ needs.check-should-post.outputs.post-url }}'; | |
| const prNumber = ${{ needs.check-should-post.outputs.pr-number }}; | |
| const { data: pullRequest } = await github.rest.pulls.get({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: prNumber | |
| }); | |
| const triggeringContext = { | |
| repo: context.repo, | |
| payload: { pull_request: pullRequest } | |
| }; | |
| await postLinkedIn({ context: triggeringContext, isBlog, postUrl }); | |
| env: | |
| LINKEDIN_ACCESS_TOKEN: ${{ secrets.LINKEDIN_ACCESS_TOKEN }} | |
| - name: Post to Hacker News | |
| uses: actions/github-script@v8 | |
| continue-on-error: true | |
| with: | |
| script: | | |
| const postHackerNews = require('.github/scripts/post-hackernews.js'); | |
| const isBlog = ${{ needs.check-should-post.outputs.is-blog }}; | |
| const postUrl = '${{ needs.check-should-post.outputs.post-url }}'; | |
| const prNumber = ${{ needs.check-should-post.outputs.pr-number }}; | |
| const { data: pullRequest } = await github.rest.pulls.get({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| pull_number: prNumber | |
| }); | |
| const triggeringContext = { | |
| repo: context.repo, | |
| payload: { pull_request: pullRequest } | |
| }; | |
| await postHackerNews({ context: triggeringContext, isBlog, postUrl }); | |
| env: | |
| HN_USERNAME: ${{ secrets.HN_USERNAME }} | |
| HN_PASSWORD: ${{ secrets.HN_PASSWORD }} | |
| SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} |