|
| 1 | +name: twitter bot |
| 2 | + |
| 3 | +# runs on issues that have been created |
| 4 | +on: |
| 5 | + issue_comment: |
| 6 | + types: [created] |
| 7 | + |
| 8 | +# setup environment variables |
| 9 | +env: |
| 10 | + TWEET_KEYWORD: '__[tweet]__' |
| 11 | + UNTWEET_EDIT_COMMENT_KEYWORD: '__[tweet withdrawn]__' |
| 12 | + TWEET_MAX_CHAR: 280 |
| 13 | + TWITTER_HANDLE: fortranlang |
| 14 | + TWITTER_URL: https://twitter.com/fortranlang |
| 15 | + |
| 16 | +jobs: |
| 17 | + # withdraws the tweet CI comment |
| 18 | + untweet: |
| 19 | + runs-on: ubuntu-latest |
| 20 | + if: github.event.issue.pull_request && startsWith(github.event.comment.body,'#untweet') |
| 21 | + steps: |
| 22 | + - name: find the tweet comment matching the keyword env.TWEET_KEYWORD |
| 23 | + uses: peter-evans/find-comment@v1 |
| 24 | + id: fc |
| 25 | + with: |
| 26 | + issue-number: ${{github.event.issue.number}} |
| 27 | + comment-author: github-actions[bot] |
| 28 | + body-includes: ${{env.TWEET_KEYWORD}} |
| 29 | +# - run: echo ${{steps.fc.outputs.comment-id}} |
| 30 | + - name: if tweet CI comment is found, replace CI comment to undo it |
| 31 | + if: ${{steps.fc.outputs.comment-id != null}} |
| 32 | + uses: peter-evans/create-or-update-comment@v1 |
| 33 | + with: |
| 34 | + comment-id: ${{steps.fc.outputs.comment-id}} |
| 35 | + edit-mode: replace |
| 36 | + body: | |
| 37 | + ${{env.UNTWEET_EDIT_COMMENT_KEYWORD}} |
| 38 | + - name: fetch comment url |
| 39 | + if: ${{steps.fc.outputs.comment-id != null}} |
| 40 | + id: get-comment-url |
| 41 | + run: | |
| 42 | + echo ::set-output name=comment-url::$(echo "${{github.event.issue.pull_request.url}}\#issuecomment-${{steps.fc.outputs.comment-id}}" | sed 's/api\.//; s/repos\///; s/pulls/pull/') |
| 43 | + - name: if tweet CI comment found, report it in a new comment |
| 44 | + if: ${{steps.fc.outputs.comment-id != null}} |
| 45 | + uses: peter-evans/create-or-update-comment@v1 |
| 46 | + with: |
| 47 | + issue-number: ${{github.event.issue.number}} |
| 48 | + body: | |
| 49 | + ${{env.UNTWEET_KEYWORD}} removed previous tweet [above](${{steps.get-comment-url.outputs.comment-url}}). |
| 50 | + # writes a tweet CI comment |
| 51 | + tweet: |
| 52 | + runs-on: ubuntu-latest |
| 53 | + if: github.event.issue.pull_request && startsWith(github.event.comment.body,'#tweet') |
| 54 | + steps: |
| 55 | + - name: search for a previous tweet CI comment matching the keyword env.TWEET_KEYWORD |
| 56 | + uses: peter-evans/find-comment@v1 |
| 57 | + id: fc |
| 58 | + with: |
| 59 | + issue-number: ${{github.event.issue.number}} |
| 60 | + comment-author: github-actions[bot] |
| 61 | + body-includes: ${{env.TWEET_KEYWORD}} |
| 62 | +# - run: echo ${{steps.fc.outputs.comment-id}} |
| 63 | + - name: if tweet CI comment is found, replace CI comment to undo it |
| 64 | + if: ${{steps.fc.outputs.comment-id != null}} |
| 65 | + uses: peter-evans/create-or-update-comment@v1 |
| 66 | + with: |
| 67 | + comment-id: ${{steps.fc.outputs.comment-id}} |
| 68 | + edit-mode: replace |
| 69 | + body: | |
| 70 | + ${{env.UNTWEET_EDIT_COMMENT_KEYWORD}} |
| 71 | +# - name: fetch comment url |
| 72 | +# if: ${{steps.fc.outputs.comment-id != null}} |
| 73 | +# id: get-comment-url |
| 74 | +# run: | |
| 75 | +# echo ::set-output name=comment-url::$(echo "${{github.event.issue.pull_request.url}}\#issuecomment-${{steps.fc.outputs.comment-id}}" | sed 's/api\.//; s/repos\///') |
| 76 | +# - name: if tweet CI comment is found, report it in a new comment |
| 77 | +# if: ${{steps.fc.outputs.comment-id != null}} |
| 78 | +# uses: peter-evans/create-or-update-comment@v1 |
| 79 | +# with: |
| 80 | +# issue-number: ${{github.event.issue.number}} |
| 81 | +# body: | |
| 82 | +# ${{env.UNTWEET_KEYWORD}} removed previous tweet [above](${{steps.get-comment-url.outputs.comment-url}}). |
| 83 | + - name: extract the new tweet message |
| 84 | + id: get-comment-body |
| 85 | + run: | |
| 86 | + body=$(echo "${{github.event.comment.body}}" | sed '1 s/#tweet//' | sed '1 s/ //') |
| 87 | + echo ::set-output name=tweet_length::$(echo $body | tr -d '\n\r%' | wc -c) |
| 88 | + body_quote=$(echo $body | sed 's/^/> /') |
| 89 | + # escape string content to preserve new lines |
| 90 | + # (see example in https://github.com/peter-evans/create-or-update-comment) |
| 91 | + body="${body//'%'/'%25'}" |
| 92 | + body="${body//$'\n'/'%0A'}" |
| 93 | + body="${body//$'\r'/'%0D'}" |
| 94 | + body_quote="${body_quote//'%'/'%25'}" |
| 95 | + body_quote="${body_quote//$'\n'/'%0A'}" |
| 96 | + body_quote="${body_quote//$'\r'/'%0D'}" |
| 97 | + echo ::set-output name=body::$body |
| 98 | + echo ::set-output name=body_quote::$body_quote |
| 99 | + - name: print new tweet CI message in a new comment |
| 100 | + uses: peter-evans/create-or-update-comment@v1 |
| 101 | + with: |
| 102 | + issue-number: ${{github.event.issue.number}} |
| 103 | + body: | |
| 104 | + ${{env.TWEET_KEYWORD}} the following message will be tweeted in [@${{env.TWITTER_HANDLE}}](${{env.TWITTER_URL}}) after this PR is merged (character count: ${{steps.get-comment-body.outputs.tweet_length}}/${{env.TWEET_MAX_CHAR}}): |
| 105 | + ${{steps.get-comment-body.outputs.body_quote}} |
0 commit comments