forked from jtarchie/github-pullrequest-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathout.rb
executable file
·101 lines (82 loc) · 3.26 KB
/
out.rb
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env ruby
require 'json'
require_relative 'base'
require_relative '../repository'
require_relative '../status'
module Commands
class Out < Commands::Base
attr_reader :destination
def initialize(destination:, input: Input.instance)
@destination = destination
super(input: input)
end
def output
check_defaults!
path = File.join(destination, params['path'])
raise %(`path` "#{params['path']}" does not exist) unless File.exist?(path)
if params.comment
comment_path = File.join(destination, params.comment)
raise %(`comment` "#{params.comment}" does not exist) unless File.exist?(comment_path)
end
if params.merge.commit_msg
commit_path = File.join(destination, params.merge.commit_msg)
raise %(`merge.commit_msg` "#{params.merge.commit_msg}" does not exist) unless File.exist?(commit_path)
end
id = Dir.chdir(path) { `git config --get pullrequest.id`.chomp }
sha = Dir.chdir(path) { `git rev-parse HEAD`.chomp }
repo = Repository.new(name: input.source.repo)
metadata = [{ 'name' => 'status', 'value' => params['status'] }]
if id.empty?
version = { 'ref' => sha }
else
pr = PullRequest.from_github(repo: repo, id: id)
metadata << { 'name' => 'url', 'value' => pr.url }
version = { 'pr' => id, 'ref' => sha }
end
atc_url = input.source.base_url || ENV['ATC_EXTERNAL_URL']
context = params.context || 'status'
Status.new(
state: params.status,
atc_url: atc_url,
sha: sha,
repo: repo,
context: context
).create!
if params.comment
comment_path = File.join(destination, params.comment)
comment = File.read(comment_path, encoding: Encoding::UTF_8)
Octokit.add_comment(input.source.repo, id, comment)
metadata << { 'name' => 'comment', 'value' => comment }
end
if params.merge.method
commit_msg = if params.merge.commit_msg
commit_path = File.join(destination, params.merge.commit_msg)
File.read(commit_path, encoding: Encoding::UTF_8)
else
''
end
Octokit.merge_pull_request(input.source.repo, id, commit_msg, merge_method: params.merge.method, accept: 'application/vnd.github.polaris-preview+json')
metadata << { 'name' => 'merge', 'value' => params.merge.method }
metadata << { 'name' => 'merge_commit_msg', 'value' => commit_msg }
end
{
'version' => version,
'metadata' => metadata
}
end
private
def params
input.params
end
def check_defaults!
raise %(`status` "#{params.status}" is not supported -- only success, failure, error, or pending) unless %w(success failure error pending).include?(params.status)
raise %(`merge.method` "#{params.merge.method}" is not supported -- only merge, squash, or rebase) if params.merge.method && !%w(merge squash rebase).include?(params.merge.method)
raise '`path` required in `params`' unless params.path
end
end
end
if __FILE__ == $PROGRAM_NAME
destination = ARGV.shift
command = Commands::Out.new(destination: destination)
puts JSON.generate(command.output)
end