forked from GENIVI/CANdevStudio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.trigger_appveyor.rb
executable file
·144 lines (123 loc) · 3.4 KB
/
.trigger_appveyor.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env ruby
require 'net/https'
require 'json'
require 'uri'
require 'securerandom'
require 'pathname'
require 'date'
def get_git_config(setting_name)
val =`git config #{setting_name}`
if $?.success?
val
else
nil
end
end
def get_git_blob_contents(ref, fileName)
pr_number = ENV["TRAVIS_PULL_REQUEST"]
if pr_number == "false"
contents = `git show #{ref}:#{fileName}`
else
# sed is so so evil... but it works ;)
contents = `git show HEAD:#{fileName} | sed 's/rem git fetch origin pull\\/XXX/git fetch origin pull\\/#{pr_number}/g' | sed 's/rem git checkout FETCH_HEAD/git checkout FETCH_HEAD/g'`
end
if $?.success?
contents
else
nil
end
end
def parse_commits(lines, separator)
commits = []
commit = {
:author => {}
}
num = 1
message = []
lines.each { |line|
case
when num == 1
commit[:id] = line
when num == 2
commit[:author][:name] = line
when num == 3
commit[:author][:email] = line
when num == 4
commit[:timestamp] = DateTime.parse(line)
when num > 4 && line != separator
message << line
when line == separator
commit[:message] = message.join('\n')
commits << commit
num = 0
message = []
commit = {
:author => {}
}
end
num += 1
}
commits
end
start_commit_id = nil
end_commit_id = nil
ref = nil
# read input data from STDIN in the format "base commit ref"
STDIN.each_line do |line|
start_commit_id, end_commit_id, ref = line.strip.split
end
# current directory is repository root
repo_path = Dir.pwd
repo_name = Pathname.new(repo_path).basename
#puts "Input data: #{start_commit_id} #{end_commit_id} #{ref}"
#puts "Repository path: #{repo_path}"
# get git config
webhook_url = ENV["APPVEYOR_WEBHOOK"]
#puts "Webhook URL: #{webhook_url}"
pr_number = ENV["TRAVIS_PULL_REQUEST"]
comments_end = SecureRandom.hex
log_format = "--date=rfc --format=%H%n%an%n%ae%n%ad%n%B%n#{comments_end}"
commits = []
if pr_number != "false"
result = `git log -1 #{log_format}`.split("\n")
commits += parse_commits(result, comments_end)
commits[0][:message] = "Pull Request ##{pr_number}: #{commits[0][:message]}"
elsif start_commit_id == "0000000000000000000000000000000000000000"
# tag
result = `git log #{end_commit_id} -1 #{log_format}`.split("\n")
commits += parse_commits(result, comments_end)
else
# get commits exclude start commit
result = `git log #{start_commit_id}..#{end_commit_id} #{log_format}`.split("\n")
commits += parse_commits(result, comments_end)
# get start commit only
result = `git log #{start_commit_id} -1 #{log_format}`.split("\n")
commits += parse_commits(result, comments_end)
end
#commits.each { |commit| puts "commit: #{commit[:id]}" }
# get blob contents
appveyor_yml = get_git_blob_contents(end_commit_id, ".appveyor.yml")
payload = {
:ref => ref,
:before => start_commit_id,
:after => end_commit_id,
:commits => commits,
:head_commit => commits.first,
:repository => {
:name => repo_name
},
:config => appveyor_yml
}
# send webhook
uri = URI(webhook_url)
req = Net::HTTP::Post.new(uri.request_uri, initheader = {'Content-Type' =>'application/json'})
# ruby 2.0: req = Net::HTTP::Post.new uri
#req.basic_auth 'username', 'password'
req.body = payload.to_json
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == "https") do |http|
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
http.request req
end
if response.code != "200" and response.code != "204"
raise "Error sending webhook: #{response.code}"
end