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