|
| 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 "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 |
0 commit comments