@@ -7,6 +7,15 @@ require 'optparse'
7
7
require 'yaml'
8
8
9
9
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
+
10
19
=begin
11
20
POST to GitLab api for linting ci yaml
12
21
Params:
@@ -18,10 +27,15 @@ Aborts with HTTP Message for all other status codes
18
27
=end
19
28
def call_api ( url , yaml )
20
29
uri = URI . parse ( url )
21
-
30
+
22
31
req = Net ::HTTP ::Post . new ( uri )
23
32
req . content_type = 'application/json'
24
33
req [ 'Accept' ] = 'application/json'
34
+
35
+ if ENV . has_key? ( AppConstants ::EnvTokenKey )
36
+ req [ 'Private-Token' ] = "#{ ENV [ AppConstants ::EnvTokenKey ] } "
37
+ end
38
+
25
39
req . body = JSON . dump ( { "content" => yaml . to_json } )
26
40
27
41
https = Net ::HTTP . new ( uri . host , uri . port )
@@ -39,12 +53,12 @@ def call_api(url, yaml)
39
53
when Net ::HTTPServerError
40
54
abort ( 'error' => "#{ response . message } : server error, try again later?" )
41
55
when Net ::HTTPBadRequest
42
- puts "Bad request..." + request . body
56
+ puts "Bad request..." + req . body
43
57
abort ( "#{ response . message } : bad api request?" )
44
58
when Net ::HTTPNotFound
45
59
abort ( "#{ response . message } : api request not found?" )
46
60
else
47
- puts "Failed validation\n JSON payload :: #{ request . body } \n HTTP Response: #{ response . message } "
61
+ puts "Failed validation\n JSON payload :: #{ req . body } \n HTTP Response: #{ response . message } "
48
62
abort ( "#{ response . message } : failed api request?" )
49
63
end
50
64
end
@@ -85,6 +99,7 @@ def load_yaml(path)
85
99
end
86
100
end
87
101
102
+
88
103
=begin
89
104
Parse command line options
90
105
Returns:
@@ -93,8 +108,10 @@ Hash containing keys: {:yaml_file,:url}
93
108
def read_args ( )
94
109
options = { }
95
110
OptionParser . new do |opt |
111
+ opt . banner = 'Usage: validate-gitlab-ci [options]'
112
+
96
113
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 }
98
115
end . parse!
99
116
100
117
options
@@ -104,11 +121,12 @@ end
104
121
Load yaml to send to GitLab API for linting
105
122
Display report of linting retrieved from api
106
123
Returns:
107
- Exits with 0 upon success and 1 when errors encountered
124
+ 0 upon success and 1 when errors encountered
108
125
=end
109
126
def main ( )
110
127
# try and parse the arguments
111
128
options = read_args ( )
129
+
112
130
unless !options . has_key? ( :yaml_file ) || !options . has_key? ( :url )
113
131
# try and load the yaml from path
114
132
puts "Loading file #{ options [ :yaml_file ] } "
@@ -125,7 +143,7 @@ def main()
125
143
puts "Something went wrong parsing the json response " + response_data
126
144
end
127
145
else
128
- abort ( "Missing required arguments yaml_file and url, use -h for usage " )
146
+ abort ( "Missing required options --yaml and --base-url " )
129
147
end
130
148
end
131
149
0 commit comments