comment-on-pr/entrypoint.sh
Tobias Løfgren a37a2c6369
Test puts
2020-06-29 16:32:53 +02:00

59 lines
1.3 KiB
Bash
Executable file

#!/usr/bin/env ruby
require "json"
require "octokit"
json = File.read(ENV.fetch("GITHUB_EVENT_PATH"))
event = JSON.parse(json)
github = Octokit::Client.new(access_token: ENV["GITHUB_TOKEN"])
if !ENV["GITHUB_TOKEN"]
puts "Missing GITHUB_TOKEN"
exit(1)
end
if ARGV[0].empty?
puts "Missing message argument."
exit(1)
end
message = ARGV[0]
check_duplicate_msg = ARGV[1]
duplicate_msg_pattern = ARGV[2]
repo = event["repository"]["full_name"]
if ENV.fetch("GITHUB_EVENT_NAME") == "pull_request"
pr_number = event["number"]
else
pulls = github.pull_requests(repo, state: "open")
push_head = event["after"]
pr = pulls.find { |pr| pr["head"]["sha"] == push_head }
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
coms = github.issue_comments(repo, pr_number)
if check_duplicate_msg == "true"
puts "duplicate_msg_pattern: #{duplicate_msg_pattern}"
if duplicate_msg_pattern != nil
duplicate = coms.find { |c| (c["body"] =~ (Regexp.new duplicate_msg_pattern)) != nil }
else
duplicate = coms.find { |c| c["body"] == message }
end
puts "duplicate: #{duplicate}"
if duplicate
puts "The PR already contains this message"
exit(0)
end
end
github.add_comment(repo, pr_number, message)