Skip to content

Commit fcb4666

Browse files
authored
Merge pull request #147 from p-costa/add_twitter_ci
add CI for publishing on the fortran-lang twitter account upon merging a certain PR
2 parents 96eb56b + 26070b1 commit fcb4666

File tree

2 files changed

+152
-0
lines changed

2 files changed

+152
-0
lines changed

.github/workflows/gen_tweet.yaml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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}}

.github/workflows/send_tweet.yaml

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
name: send a tweet if PR is merged (and there is a tweet to be sent)
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
env:
7+
TWEET_KEYWORD: '__[tweet]__'
8+
TWEET_SENT_KEYWORD: '__[tweet sent]__'
9+
jobs:
10+
publish_tweet:
11+
runs-on: ubuntu-latest
12+
if: github.event.pull_request.merged
13+
steps:
14+
- name: find the tweet comment matching the keyword env.TWEET_KEYWORD
15+
uses: peter-evans/find-comment@v1
16+
id: fc
17+
with:
18+
issue-number: ${{github.event.pull_request.number}}
19+
comment-author: github-actions[bot]
20+
body-includes: ${{env.TWEET_KEYWORD}}
21+
- name: if tweet CI comment is found, extract tweet from comment body
22+
id: extract-tweet
23+
if: ${{steps.fc.outputs.comment-id != null}}
24+
run: |
25+
body=$(echo "${{steps.fc.outputs.comment-body}}" | sed '1d' | sed 's/> //')
26+
# escape file content to preserve new lines
27+
# (see example in https://github.com/peter-evans/create-or-update-comment)
28+
body="${body//'%'/'%25'}"
29+
body="${body//$'\n'/'%0A'}"
30+
body="${body//$'\r'/'%0D'}"
31+
echo ::set-output name=body::$body
32+
- name: tweet the message
33+
if: ${{steps.fc.outputs.comment-id != null}}
34+
uses: ethomson/send-tweet-action@v1
35+
with:
36+
status: ${{steps.extract-tweet.outputs.body}}
37+
consumer-key: ${{secrets.TWITTER_API_KEY}}
38+
consumer-secret: ${{secrets.TWITTER_API_SECRET_KEY}}
39+
access-token: ${{secrets.TWITTER_ACCESS_TOKEN}}
40+
access-token-secret: ${{secrets.TWITTER_ACCESS_TOKEN_SECRET}}
41+
- name: clear tweet keyword
42+
if: ${{steps.fc.outputs.comment-id != null}}
43+
uses: peter-evans/create-or-update-comment@v1
44+
with:
45+
comment-id: ${{steps.fc.outputs.comment-id}}
46+
edit-mode: replace
47+
body: ${{env.TWEET_SENT_KEYWORD}}

0 commit comments

Comments
 (0)