Skip to content

Commit c8389a8

Browse files
author
dcs3spp
committed
fix(token): add token header if GITLAB_API_TOKEN exists
1 parent e81ace1 commit c8389a8

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

bin/validate-gitlab-ci

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ require 'optparse'
77
require 'yaml'
88

99

10+
=begin
11+
Define constants used in the application
12+
=end
13+
class AppConstants
14+
ApiPath= '/api/v4/ci/lint'
15+
EnvTokenKey = 'GITLAB_API_TOKEN'
16+
end
17+
18+
1019
=begin
1120
POST to GitLab api for linting ci yaml
1221
Params:
@@ -18,10 +27,15 @@ Aborts with HTTP Message for all other status codes
1827
=end
1928
def call_api(url, yaml)
2029
uri = URI.parse(url)
21-
30+
2231
req = Net::HTTP::Post.new(uri)
2332
req.content_type='application/json'
2433
req['Accept']='application/json'
34+
35+
if ENV.has_key?(AppConstants::EnvTokenKey)
36+
req['Private-Token']="#{ENV[AppConstants::EnvTokenKey]}"
37+
end
38+
2539
req.body = JSON.dump({"content" => yaml.to_json})
2640

2741
https = Net::HTTP.new(uri.host, uri.port)
@@ -39,12 +53,12 @@ def call_api(url, yaml)
3953
when Net::HTTPServerError
4054
abort('error' => "#{response.message}: server error, try again later?")
4155
when Net::HTTPBadRequest
42-
puts "Bad request..." + request.body
56+
puts "Bad request..." + req.body
4357
abort("#{response.message}: bad api request?")
4458
when Net::HTTPNotFound
4559
abort("#{response.message}: api request not found?")
4660
else
47-
puts "Failed validation\nJSON payload :: #{request.body}\nHTTP Response: #{response.message}"
61+
puts "Failed validation\nJSON payload :: #{req.body}\nHTTP Response: #{response.message}"
4862
abort("#{response.message}: failed api request?")
4963
end
5064
end
@@ -85,6 +99,7 @@ def load_yaml(path)
8599
end
86100
end
87101

102+
88103
=begin
89104
Parse command line options
90105
Returns:
@@ -93,8 +108,10 @@ Hash containing keys: {:yaml_file,:url}
93108
def read_args()
94109
options = {}
95110
OptionParser.new do |opt|
111+
opt.banner = 'Usage: validate-gitlab-ci [options]'
112+
96113
opt.on('-f', '--yaml YAML-PATH', 'Path to .gitlab-ci.yml') { |o| options[:yaml_file] = o }
97-
opt.on('-l', '--url GitLab url', 'GitLab API url') { |o| options[:url] = o }
114+
opt.on('-l', '--base-url GitLab url', 'GitLab API url') { |o| options[:url] = o + AppConstants::ApiPath }
98115
end.parse!
99116

100117
options
@@ -104,11 +121,12 @@ end
104121
Load yaml to send to GitLab API for linting
105122
Display report of linting retrieved from api
106123
Returns:
107-
Exits with 0 upon success and 1 when errors encountered
124+
0 upon success and 1 when errors encountered
108125
=end
109126
def main()
110127
# try and parse the arguments
111128
options = read_args()
129+
112130
unless !options.has_key?(:yaml_file) || !options.has_key?(:url)
113131
# try and load the yaml from path
114132
puts "Loading file #{options[:yaml_file]}"
@@ -125,7 +143,7 @@ def main()
125143
puts "Something went wrong parsing the json response " + response_data
126144
end
127145
else
128-
abort("Missing required arguments yaml_file and url, use -h for usage")
146+
abort("Missing required options --yaml and --base-url")
129147
end
130148
end
131149

0 commit comments

Comments
 (0)