comment-on-pr/entrypoint.sh

58 lines
1.2 KiB
Bash
Raw Normal View History

2019-05-23 02:40:32 +08:00
#!/usr/bin/env ruby
2019-05-23 07:03:07 +08:00
require "json"
2019-05-23 02:40:32 +08:00
require "octokit"
2019-05-23 07:03:07 +08:00
json = File.read(ENV.fetch("GITHUB_EVENT_PATH"))
2019-11-14 12:24:44 +08:00
event = JSON.parse(json)
2019-05-23 07:03:07 +08:00
2019-05-23 02:40:32 +08:00
github = Octokit::Client.new(access_token: ENV["GITHUB_TOKEN"])
if !ENV["GITHUB_TOKEN"]
puts "Missing GITHUB_TOKEN"
2019-05-23 02:40:32 +08:00
exit(1)
end
if ARGV[0].empty?
2019-05-24 23:40:10 +08:00
puts "Missing message argument."
exit(1)
end
message = ARGV[0]
check_duplicate_msg = ARGV[1]
2020-06-29 22:19:08 +08:00
duplicate_msg_pattern = ARGV[2]
2020-06-29 22:42:40 +08:00
2019-11-14 12:24:44 +08:00
repo = event["repository"]["full_name"]
2019-05-23 02:40:32 +08:00
2019-11-14 12:24:44 +08:00
if ENV.fetch("GITHUB_EVENT_NAME") == "pull_request"
pr_number = event["number"]
else
pulls = github.pull_requests(repo, state: "open")
2019-05-23 02:40:32 +08:00
2019-11-14 12:24:44 +08:00
push_head = event["after"]
pr = pulls.find { |pr| pr["head"]["sha"] == push_head }
2019-05-23 02:40:32 +08:00
2019-11-14 12:24:44 +08:00
if !pr
puts "Couldn't find an open pull request for branch with head at #{push_head}."
exit(1)
end
pr_number = pr["number"]
end
2019-08-02 20:57:40 +08:00
2019-11-14 12:24:44 +08:00
coms = github.issue_comments(repo, pr_number)
if check_duplicate_msg == "true"
2020-06-29 22:19:08 +08:00
if duplicate_msg_pattern != nil
2020-06-29 22:49:48 +08:00
duplicate = coms.find { |c| (c["body"] =~ (Regexp.new duplicate_msg_pattern)) != nil }
2020-06-29 22:19:08 +08:00
else
duplicate = coms.find { |c| c["body"] == message }
end
if duplicate
puts "The PR already contains this message"
exit(0)
end
end
2019-11-14 12:24:44 +08:00
github.add_comment(repo, pr_number, message)