forked from rhs/pr-comment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.sh
executable file
·61 lines (46 loc) · 1.27 KB
/
entrypoint.sh
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
#!/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.length < 2
puts "Missing arguments."
exit(1)
end
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
file_path = ARGV[0]
prefix = ARGV[1]
action = ARGV[2]
puts Dir.entries(".")
message = File.read(file_path)
coms = github.issue_comments(repo, pr_number)
com = coms.find { |com| com["body"].start_with?(prefix) }
if com == nil
github.add_comment(repo, pr_number, prefix + "\n\n" + message)
else
cur_time = Time.new
timestamp = "\n\nUpdate on " + cur_time.inspect + "\n\n"
if action == "replace"
body = prefix + timestamp + message
else
body = com["body"] + timestamp + message
end
com = github.update_comment(repo, com["id"], body)
end