forked from okteto/deploy-preview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotify-pr.sh
36 lines (29 loc) · 963 Bytes
/
notify-pr.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
#!/usr/bin/env ruby
require "octokit"
require "json"
if ENV["GITHUB_EVENT_NAME"] != "pull_request" && ENV["GITHUB_EVENT_NAME"] != "repository_dispatch"
puts "This action only supports either pull_request or repository_dispatch events."
exit(1)
end
if !message = ARGV[1]
puts "Missing GITHUB_TOKEN"
exit(1)
end
message = ARGV[0]
repo = ENV["GITHUB_REPOSITORY"]
json = File.read(ENV.fetch("GITHUB_EVENT_PATH"))
event = JSON.parse(json)
if ENV["GITHUB_EVENT_NAME"] == "pull_request"
pr = event["number"]
else
pr = event["client_payload"]["pull_request"]["number"]
end
github = Octokit::Client.new(:access_token => ENV["GITHUB_TOKEN"])
comments = github.issue_comments(repo, pr)
comment = comments.find { |c| c["body"].start_with?("Your preview environment") }
if comment
puts "Message already exists in the PR. Updating"
github.update_comment(repo, comment["id"], message)
exit(0)
end
github.add_comment(repo, pr, message)