Skip to content

Commit e81c463

Browse files
kfischer-okarinRedmine Patch Meetup
authored and
Redmine Patch Meetup
committed
Comment link to patch as comment
1 parent cc73c77 commit e81c463

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

.github/actions/comment_patch_url.rb

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env ruby
2+
3+
require 'json'
4+
5+
require 'faraday'
6+
7+
REPO = 'redmine-patch-meetup/redmine-dev-mirror'
8+
9+
WORKFLOW_RUN = JSON.parse ENV['WORKFLOW_RUN_JSON']
10+
11+
CONNECTION = Faraday.new('https://api.github.com/') do |conn|
12+
conn.response :raise_error
13+
conn.adapter Faraday.default_adapter
14+
end
15+
16+
def repo_resource(resource)
17+
"repos/#{REPO}/#{resource}"
18+
end
19+
20+
def get_repo_resource(resource)
21+
response = CONNECTION.get repo_resource(resource)
22+
JSON.parse response.body
23+
end
24+
25+
def post_to_repo_resource(resource, body)
26+
response = CONNECTION.post repo_resource(resource),
27+
body.to_json,
28+
"Content-Type" => "application/json",
29+
"Authorization" => "token #{ENV['GITHUB_TOKEN']}"
30+
JSON.parse response.body
31+
end
32+
33+
def patch_artifact_id
34+
response = JSON.parse CONNECTION.get(WORKFLOW_RUN['artifacts_url']).body
35+
patch_artifact = response['artifacts'].find { |artifact| artifact['name'] == 'patch' }
36+
patch_artifact['id']
37+
end
38+
39+
def get_suite_id
40+
suite_url = WORKFLOW_RUN['check_suite_url']
41+
id_start_index = suite_url.rindex('/') + 1
42+
suite_url[id_start_index..-1]
43+
end
44+
45+
def patch_artifact_download_url
46+
"https://github.com/#{REPO}/suites/#{get_suite_id}/artifacts/#{patch_artifact_id}"
47+
end
48+
49+
def pull_request_number
50+
WORKFLOW_RUN['pull_requests'][0]['number']
51+
end
52+
53+
def post_pr_comment(pr_number, comment)
54+
post_to_repo_resource "issues/#{pr_number}/comments", { body: comment }
55+
end
56+
57+
def find_previous_comment_id(pr_number)
58+
comments = get_repo_resource "issues/#{pr_number}/comments"
59+
previous_comment = comments.find { |comment|
60+
comment['body'].include?('Patch can be downloaded [here]') && comment['user']['login'].include?('github-actions')
61+
}
62+
previous_comment['id'] if previous_comment
63+
end
64+
65+
def delete_comment(comment_id)
66+
CONNECTION.delete repo_resource("issues/comments/#{comment_id}"), nil, "Authorization" => "token #{ENV['GITHUB_TOKEN']}"
67+
end
68+
69+
def main
70+
existing_comment_id = find_previous_comment_id(pull_request_number)
71+
delete_comment(existing_comment_id) if existing_comment_id
72+
73+
post_pr_comment pull_request_number, "Patch can be downloaded [here](#{patch_artifact_download_url})"
74+
end
75+
76+
main if __FILE__ == $0

.github/workflows/comment-patch.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Comment Patch
2+
3+
on:
4+
workflow_run:
5+
workflows:
6+
- Create Patch
7+
types:
8+
- completed
9+
branches-ignore:
10+
- master
11+
- development
12+
13+
jobs:
14+
create-patch:
15+
runs-on: ubuntu-latest
16+
steps:
17+
- name: Install gems
18+
run: sudo gem install faraday
19+
- uses: actions/checkout@v2
20+
- name: Comment Patch URL
21+
run: ./.github/actions/comment_patch_url.rb
22+
env:
23+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
24+
WORKFLOW_RUN_JSON: ${{ toJSON(github.event.workflow_run) }}

0 commit comments

Comments
 (0)