gitlab-lint-client 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.pre-commit-hooks.yaml +7 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/CHANGELOG.md +21 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +53 -0
- data/LICENSE.txt +21 -0
- data/README.md +151 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/exe/glab-lint +5 -0
- data/gitlab-lint-client.gemspec +36 -0
- data/lib/gitlab/lint/client.rb +37 -0
- data/lib/gitlab/lint/client/api.rb +52 -0
- data/lib/gitlab/lint/client/args.rb +85 -0
- data/lib/gitlab/lint/client/summaryreport.rb +40 -0
- data/lib/gitlab/lint/client/version.rb +7 -0
- data/lib/gitlab/lint/client/yml.rb +50 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 45ab9cc8b2f31dad25d707bf6e3f71f70ed239d711f7eb130830a33803030e59
|
4
|
+
data.tar.gz: cbfe0c7d6555635835a991f254d46faba558f782ffe64af82af3f7c23b1039f0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: c51ad05e4a44138f44e345130b004c2f227d52db91f5d0f2e0bbb76485d4a3ccaff05ed55555cb45c35f0e4c9b4bbe0633c78e5a39d9fcb20db7bdab62c3f94d
|
7
|
+
data.tar.gz: 6dd59d0029349c0761df3434405446324a33ba19bd44fa1a9a1eaf877a98ec9ee70f936a15dc779827d8e37ad6a9d3bfeed50e11239614c920d5b633d174a26a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# CHANGELOG
|
2
|
+
|
3
|
+
## 0.0.1-alpha - Core Project - [08-07-2020]
|
4
|
+
|
5
|
+
### Added
|
6
|
+
* Create a CLI to get values from bash.
|
7
|
+
* Create client request to post JSON escaped yaml content to Gitlab API for linting.
|
8
|
+
* Evaluate response and display summary.
|
9
|
+
* Pre-commit hook for linting Gitlab yml file staged for changes.
|
10
|
+
|
11
|
+
## 0.0.1 - Refactored Project Structure - [13-07-2020]
|
12
|
+
|
13
|
+
### Added
|
14
|
+
* Entrypoint:
|
15
|
+
* Gitlab::Lint::Client.entry
|
16
|
+
* Class:
|
17
|
+
* Gitlab::Lint::Client::Api
|
18
|
+
* Gitlab::Lint::Client::Args
|
19
|
+
* Gitlab::Lint::Client::SummaryReport.
|
20
|
+
* GitLab::CI::Lint::YamlFile.
|
21
|
+
* Added unit and integration tests
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
gitlab-lint-client (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
addressable (2.7.0)
|
10
|
+
public_suffix (>= 2.0.2, < 5.0)
|
11
|
+
coderay (1.1.3)
|
12
|
+
crack (0.4.3)
|
13
|
+
safe_yaml (~> 1.0.0)
|
14
|
+
diff-lcs (1.4.4)
|
15
|
+
hashdiff (1.0.1)
|
16
|
+
method_source (1.0.0)
|
17
|
+
pry (0.13.1)
|
18
|
+
coderay (~> 1.1)
|
19
|
+
method_source (~> 1.0)
|
20
|
+
public_suffix (4.0.5)
|
21
|
+
rake (12.3.3)
|
22
|
+
rspec (3.9.0)
|
23
|
+
rspec-core (~> 3.9.0)
|
24
|
+
rspec-expectations (~> 3.9.0)
|
25
|
+
rspec-mocks (~> 3.9.0)
|
26
|
+
rspec-core (3.9.2)
|
27
|
+
rspec-support (~> 3.9.3)
|
28
|
+
rspec-expectations (3.9.2)
|
29
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
30
|
+
rspec-support (~> 3.9.0)
|
31
|
+
rspec-mocks (3.9.1)
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
33
|
+
rspec-support (~> 3.9.0)
|
34
|
+
rspec-support (3.9.3)
|
35
|
+
safe_yaml (1.0.5)
|
36
|
+
webmock (3.8.3)
|
37
|
+
addressable (>= 2.3.6)
|
38
|
+
crack (>= 0.3.2)
|
39
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
40
|
+
|
41
|
+
PLATFORMS
|
42
|
+
ruby
|
43
|
+
|
44
|
+
DEPENDENCIES
|
45
|
+
bundler (~> 2.1.4)
|
46
|
+
gitlab-lint-client!
|
47
|
+
pry (~> 0.13.1)
|
48
|
+
rake (~> 12.0)
|
49
|
+
rspec (~> 3.9)
|
50
|
+
webmock (~> 3.8.3)
|
51
|
+
|
52
|
+
BUNDLED WITH
|
53
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 spears
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
# Gitlab Lint Client :: A Ruby CLI And Pre-Commit Hook For Validating GitLab CI YAML Files
|
2
|
+
|
3
|
+
The author has some [repositories](https://gitlab.com/groups/sppears_grp/-/shared) at GitLab.
|
4
|
+
Occasionaly *.gitlab-ci.yml* files containing syntax errors would be committed accidentally,
|
5
|
+
breaking the CI build.
|
6
|
+
|
7
|
+
GitLab provide a web form interface, available per project for linting. However, this started
|
8
|
+
to be cumbersome and became a candidate for automation via the GitLab API and git hooks.
|
9
|
+
Unfortunately, the free plan for gitlab.com does not feature *pre-receive* git server hooks which
|
10
|
+
could prevent pushes containing invalid *.gitlab-ci.yml* files.
|
11
|
+
|
12
|
+
Git provides, a *pre-commit* hook that runs for staged files on the local development environment.
|
13
|
+
While use of local git hooks are not easily enforced, it is better than the alternative in this case.
|
14
|
+
The [pre-commit](https://pre-commit.com/) project faciliates deployment and installation of
|
15
|
+
client side git hooks.
|
16
|
+
|
17
|
+
|
18
|
+
## 🔎 What Does This Repository Offer?
|
19
|
+
|
20
|
+
The author developed a Ruby CLI and library to lint GitLab yaml files containing CI
|
21
|
+
Pipeline specifications. Linting is performed using the GitLab API.
|
22
|
+
|
23
|
+
The gem is served on [rubygems.org](https://rubygems.org) as *gitlab-lint-client* and the
|
24
|
+
source code, with accompanying unit and functional tests, are held within this repository.
|
25
|
+
It can be installed by issuing the following command:
|
26
|
+
```gem install gitlab-lint-client```.
|
27
|
+
|
28
|
+
Usage details are provided with the *help* option of the CLI, <kbd>glab-lint --help</kbd>
|
29
|
+
Examples are:
|
30
|
+
|
31
|
+
``` bash
|
32
|
+
# mandatory options
|
33
|
+
glab-lint --yaml=.gitlab-ci.yml --base-url=https://gitlab.com
|
34
|
+
glab-lint -f .gitlab-ci.yml -u https://gitlab.com
|
35
|
+
|
36
|
+
# configure timeout in seconds, for API request
|
37
|
+
glab-lint --yaml=.gitlab-ci.yml --base-url=https://gitlab.com --timeout=10
|
38
|
+
glab-lint -f .gitlab-ci.yml -u https://gitlab.com -t 10
|
39
|
+
|
40
|
+
# display program version number and exit
|
41
|
+
glab-lint --version
|
42
|
+
glab-lint -v
|
43
|
+
```
|
44
|
+
|
45
|
+
The author is using the gem within a client side pre-commit hook to automate GitLab CI linting.
|
46
|
+
|
47
|
+
|
48
|
+
## 🔐 Usage As A Pre-Commit Hook
|
49
|
+
|
50
|
+
This repository specifies a pre-commit hook in the *.pre-commit-hooks.yml* file. Developers can
|
51
|
+
configure their GitLab repositories to use this hook by creating a *.pre-commit-config.yml* file
|
52
|
+
in the root of their repository.
|
53
|
+
|
54
|
+
``` yaml
|
55
|
+
repos:
|
56
|
+
- repo: https://github.com/dcs3spp/validate-gitlab-ci
|
57
|
+
rev: v0.0.1
|
58
|
+
hooks:
|
59
|
+
- id: validate-gitlab-ci
|
60
|
+
args: [--yaml=.gitlab-ci.yml, --base-url=https://gitlab.com]
|
61
|
+
pass_filenames: false
|
62
|
+
types: [yaml]
|
63
|
+
files: .gitlab-ci.yml
|
64
|
+
stages: [commit]
|
65
|
+
```
|
66
|
+
|
67
|
+
Subsequently, [install](https://pre-commit.com/#installation) the pre-commit tool by issuing
|
68
|
+
the following command:
|
69
|
+
|
70
|
+
``` bash
|
71
|
+
pip install pre-commit
|
72
|
+
```
|
73
|
+
|
74
|
+
Instruct pre-commit to download and configure the hooks defined in the *.pre-commit-config.yml* file:
|
75
|
+
|
76
|
+
``` bash
|
77
|
+
pre-commit install
|
78
|
+
```
|
79
|
+
|
80
|
+
This will create a Ruby environment and automatically download and install the gem held within this
|
81
|
+
repository. The environment is setup on first time use only.
|
82
|
+
|
83
|
+
Subsequently, whenever an attempt is made to commit the GitLab CI yaml file, the pre-commit hook
|
84
|
+
will automatically send it for linting to the GitLab API. If a failed response is received from the API,
|
85
|
+
then the commit is declined.
|
86
|
+
|
87
|
+
|
88
|
+
## 🔧 Quick Start For Development
|
89
|
+
|
90
|
+
Perform a *git clone*:
|
91
|
+
``` bash
|
92
|
+
git clone --depth 1 https://github.com/dcs3spp/validate-gitlab-ci.git
|
93
|
+
```
|
94
|
+
|
95
|
+
* This will download the source to local machine.
|
96
|
+
* Ruby >=2.3.6 is required with the following development dependencies:
|
97
|
+
* bundler
|
98
|
+
* pry
|
99
|
+
* rake
|
100
|
+
* rspec
|
101
|
+
* webmock
|
102
|
+
|
103
|
+
* The gemspec file lists specific dependencies for development.
|
104
|
+
* This gem is available publically as *gitlab-lint-client*
|
105
|
+
|
106
|
+
|
107
|
+
## 🏭 Building And Installing The Gem Locally
|
108
|
+
|
109
|
+
``` bash
|
110
|
+
gem build gitlab-lint-client.gemspec
|
111
|
+
gem install gitlab-lint-client-0.0.1.gem
|
112
|
+
```
|
113
|
+
|
114
|
+
## ⛑ Running Tests Locally
|
115
|
+
|
116
|
+
``` bash
|
117
|
+
bundle exec rspec
|
118
|
+
```
|
119
|
+
|
120
|
+
## 📁 Environment Variables
|
121
|
+
|
122
|
+
**Name** | **Description**
|
123
|
+
:---: | :---:
|
124
|
+
**GITLAB_API_TOKEN** | GitLab API Token for use with private GitLab servers (other than https://gitlab.com) that may require an authorization header
|
125
|
+
|
126
|
+
|
127
|
+
## 📦 Using Rake For Performing A Release
|
128
|
+
|
129
|
+
``` bash
|
130
|
+
# build the gem
|
131
|
+
rake build
|
132
|
+
|
133
|
+
# install the gem
|
134
|
+
rake install
|
135
|
+
|
136
|
+
# run the tests
|
137
|
+
rake spec
|
138
|
+
|
139
|
+
# release the gem
|
140
|
+
rake release
|
141
|
+
```
|
142
|
+
|
143
|
+
|
144
|
+
## 📋 Versioning
|
145
|
+
|
146
|
+
- [CHANGELOG](CHANGELOG.md)
|
147
|
+
|
148
|
+
|
149
|
+
## 🔑 License
|
150
|
+
|
151
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "gitlab/lint/client"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/exe/glab-lint
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require_relative 'lib/gitlab/lint/client/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "gitlab-lint-client"
|
5
|
+
spec.version = Gitlab::Lint::Client::VERSION
|
6
|
+
spec.authors = ["spears"]
|
7
|
+
|
8
|
+
spec.description = %q{A CLI and library to validate GitLab CI pipeline yaml files via the GitLab API}
|
9
|
+
spec.summary = %q{Use as a standalone CLI, from within a git pre-commit hook. The repository provides a pre-commit.com hook with identifier 'validate-gitlab-ci'}
|
10
|
+
spec.homepage = "https://github.com/dcs3spp/validate-gitlab-ci"
|
11
|
+
spec.license = "MIT"
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.6.3")
|
13
|
+
|
14
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
15
|
+
|
16
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
17
|
+
spec.metadata["source_code_uri"] = "https://github.com/dcs3spp/validate-gitlab-ci"
|
18
|
+
spec.metadata["changelog_uri"] = "https://github.com/dcs3spp/validate-gitlab-ci/blob/master/CHANGELOG.md"
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables << 'glab-lint'
|
28
|
+
spec.extra_rdoc_files = ["README.md"]
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
spec.add_development_dependency "bundler", "~> 2.1.4"
|
32
|
+
spec.add_development_dependency "pry", "~> 0.13.1"
|
33
|
+
spec.add_development_dependency "rake", "~> 12.0"
|
34
|
+
spec.add_development_dependency "rspec", "~> 3.9"
|
35
|
+
spec.add_development_dependency "webmock", "~> 3.8.3"
|
36
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require "gitlab/lint/client/api"
|
2
|
+
require "gitlab/lint/client/args"
|
3
|
+
require "gitlab/lint/client/version"
|
4
|
+
require "gitlab/lint/client/summaryreport"
|
5
|
+
|
6
|
+
module Gitlab
|
7
|
+
module Lint
|
8
|
+
module Client
|
9
|
+
def self.entry(cliArgs)
|
10
|
+
|
11
|
+
args = Gitlab::Lint::Client::Args.new
|
12
|
+
args.get(cliArgs)
|
13
|
+
|
14
|
+
api = Gitlab::Lint::Client::Api.new
|
15
|
+
|
16
|
+
headers = ENV.has_key?('GITLAB_API_TOKEN') ? {
|
17
|
+
"Accept" => "application/json",
|
18
|
+
"Content-Type" => "application/json",
|
19
|
+
"Private-Token" => ENV['GITLAB_API_TOKEN']
|
20
|
+
} : {
|
21
|
+
"Accept" => "application/json",
|
22
|
+
"Content-Type" => "application/json"
|
23
|
+
}
|
24
|
+
|
25
|
+
puts "The arguments have been initialised as follows::"
|
26
|
+
puts "Base url => #{args.baseUrl}"
|
27
|
+
puts "Yaml file => #{args.pathToYamlFile}"
|
28
|
+
|
29
|
+
response = api.lint(args.url, args.pathToYamlFile, headers, args.timeout)
|
30
|
+
|
31
|
+
puts "reponse from server is :: #{response}"
|
32
|
+
report = Gitlab::Lint::Client::SummaryReport.new(response['status'], response['errors'])
|
33
|
+
report.summary()
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require "gitlab/lint/client/yml"
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
require 'net/http'
|
5
|
+
|
6
|
+
module Gitlab
|
7
|
+
module Lint
|
8
|
+
module Client
|
9
|
+
|
10
|
+
class Api
|
11
|
+
|
12
|
+
def lint (url, path, headers={ "Content-Type" => "application/json" }, timeout=10)
|
13
|
+
puts "Making lint request to GitLab with path => #{path} and url => #{url}"
|
14
|
+
if yamlContent = Gitlab::Lint::Client::YamlFile.new(path).get_json_content()
|
15
|
+
|
16
|
+
uri = URI.parse(url)
|
17
|
+
|
18
|
+
req = Net::HTTP::Post.new(uri, headers)
|
19
|
+
req.body = { content: yamlContent }.to_json
|
20
|
+
|
21
|
+
https = Net::HTTP.new(uri.host, uri.port)
|
22
|
+
https.open_timeout = timeout
|
23
|
+
https.read_timeout = timeout
|
24
|
+
https.use_ssl = true
|
25
|
+
https.verify_mode = OpenSSL::SSL::VERIFY_PEER
|
26
|
+
|
27
|
+
response = https.request(req)
|
28
|
+
|
29
|
+
case response
|
30
|
+
when Net::HTTPSuccess
|
31
|
+
return JSON.parse(response.body)
|
32
|
+
when Net::HTTPUnauthorized
|
33
|
+
abort("#{response.message}: invalid token in api request?")
|
34
|
+
when Net::HTTPServerError
|
35
|
+
abort("#{response.message}: server error, try again later?")
|
36
|
+
when Net::HTTPBadRequest
|
37
|
+
puts "Bad request..." + req.body
|
38
|
+
abort("#{response.message}: bad api request?")
|
39
|
+
when Net::HTTPNotFound
|
40
|
+
abort("#{response.message}: api request not found?")
|
41
|
+
else
|
42
|
+
abort("#{response.message}: failed api request?")
|
43
|
+
end
|
44
|
+
else
|
45
|
+
abort("\nLint request failed, problem encountered reading yaml file")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
@@ -0,0 +1,85 @@
|
|
1
|
+
require 'optparse'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
require 'gitlab/lint/client/version'
|
5
|
+
|
6
|
+
module Gitlab
|
7
|
+
module Lint
|
8
|
+
module Client
|
9
|
+
|
10
|
+
class Args
|
11
|
+
API_PATH="/api/v4/ci/lint"
|
12
|
+
|
13
|
+
attr_reader :baseUrl
|
14
|
+
attr_reader :pathToYamlFile
|
15
|
+
attr_reader :timeout
|
16
|
+
attr_reader :url
|
17
|
+
|
18
|
+
def initialize()
|
19
|
+
@version = Gitlab::Lint::Client::VERSION
|
20
|
+
end
|
21
|
+
|
22
|
+
def get(args)
|
23
|
+
options = {}
|
24
|
+
optparse = OptionParser.new do|opt|
|
25
|
+
opt.banner = 'Usage: validate-gitlab-ci [options]'
|
26
|
+
|
27
|
+
opt.on('-f', '--yaml=YAML-PATH', 'Path to .gitlab-ci.yml') { |o| options[:yamlFile] = o }
|
28
|
+
opt.on('-u', '--base-url=BASE_URL', 'GitLab API url') { |o| options[:baseUrl] = o }
|
29
|
+
|
30
|
+
options[:timeout] = 10
|
31
|
+
opt.on('-t', '--timeout[=TIMEOUT]', Integer, 'Api timeout in seconds') { |o| options[:timeout] = o }
|
32
|
+
|
33
|
+
opt.on('-v', '--version', 'Program version') { |o| options[:version] = version() }
|
34
|
+
end
|
35
|
+
|
36
|
+
begin
|
37
|
+
optparse.parse!(args)
|
38
|
+
|
39
|
+
if options[:version].nil?
|
40
|
+
mandatory = [:yamlFile, :baseUrl]
|
41
|
+
missing = mandatory.select{ |param| options[param].nil? }
|
42
|
+
if not missing.empty?
|
43
|
+
STDERR.puts "Required options #{missing[0]} are missing: #{missing.join(", ")}"
|
44
|
+
puts optparse.help
|
45
|
+
abort("Exiting due to error encountered while parsing arguments")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
rescue OptionParser::InvalidOption, OptionParser::MissingArgument => error
|
50
|
+
STDERR.puts error
|
51
|
+
puts optparse
|
52
|
+
abort("Exiting due to error encountered while parsing arguments...")
|
53
|
+
end
|
54
|
+
|
55
|
+
self.validateUrl!(options[:baseUrl])
|
56
|
+
self.validateYamlFile!(options[:yamlFile])
|
57
|
+
|
58
|
+
@baseUrl = options[:baseUrl]
|
59
|
+
@url = options[:baseUrl] + API_PATH
|
60
|
+
@pathToYamlFile = options[:yamlFile]
|
61
|
+
@timeout = options[:timeout]
|
62
|
+
end
|
63
|
+
|
64
|
+
def validateUrl!(url)
|
65
|
+
uri = URI.parse(url)
|
66
|
+
valid = uri.is_a?(URI::HTTPS) && !uri.host.nil?
|
67
|
+
|
68
|
+
raise URI::InvalidURIError unless valid
|
69
|
+
end
|
70
|
+
|
71
|
+
def validateYamlFile!(path)
|
72
|
+
raise ArgumentError unless path.chars.last(4).join == ".yml" or path.chars.last(5).join == ".yaml"
|
73
|
+
raise IOError unless ::File.exist?(path)
|
74
|
+
raise RuntimeError unless ::File.readable?(path)
|
75
|
+
end
|
76
|
+
|
77
|
+
def version()
|
78
|
+
string = "GitLab Lint Client Version: #{@version}"
|
79
|
+
puts string
|
80
|
+
exit 0
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
|
2
|
+
|
3
|
+
module Gitlab
|
4
|
+
module Lint
|
5
|
+
module Client
|
6
|
+
|
7
|
+
class SummaryReport
|
8
|
+
|
9
|
+
def initialize(status, errors)
|
10
|
+
@status = status
|
11
|
+
@errors = errors
|
12
|
+
end
|
13
|
+
|
14
|
+
def summary()
|
15
|
+
case @status
|
16
|
+
when "valid"
|
17
|
+
success()
|
18
|
+
when "invalid"
|
19
|
+
failure()
|
20
|
+
else
|
21
|
+
abort("A problem was encountered parsing status : " + @status)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def failure()
|
28
|
+
abort("Gitlab yaml file is invalid with errors:\n\n" + @errors.join("\n"))
|
29
|
+
end
|
30
|
+
|
31
|
+
def success()
|
32
|
+
puts "GitLab yaml file passed linting"
|
33
|
+
exit(0)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "yaml"
|
2
|
+
require "json"
|
3
|
+
|
4
|
+
module Gitlab
|
5
|
+
module Lint
|
6
|
+
module Client
|
7
|
+
|
8
|
+
class YamlFile
|
9
|
+
attr_reader :file
|
10
|
+
|
11
|
+
def initialize(file)
|
12
|
+
@file = file
|
13
|
+
validate!
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate!
|
17
|
+
raise NameError unless @file
|
18
|
+
raise ArgumentError unless @file.chars.last(4).join == ".yml" or @file.chars.last(5).join == ".yaml"
|
19
|
+
raise IOError unless ::File.exist?(@file)
|
20
|
+
raise RuntimeError unless ::File.readable?(@file)
|
21
|
+
end
|
22
|
+
|
23
|
+
def get_content
|
24
|
+
begin
|
25
|
+
return YAML.load_file(@file)
|
26
|
+
rescue Psych::SyntaxError => error
|
27
|
+
puts "Failed to parse the YAML File: #{error.message}"
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def get_json_content
|
32
|
+
content = JSON.generate(get_content())
|
33
|
+
return json_ok?(content) ? content : nil
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def json_ok? json
|
39
|
+
begin
|
40
|
+
JSON.parse(json)
|
41
|
+
return true
|
42
|
+
rescue
|
43
|
+
return false
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gitlab-lint-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- spears
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 2.1.4
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 2.1.4
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.13.1
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.13.1
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '12.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '12.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.9'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.9'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: webmock
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.8.3
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.8.3
|
83
|
+
description: A CLI and library to validate GitLab CI pipeline yaml files via the GitLab
|
84
|
+
API
|
85
|
+
email:
|
86
|
+
executables:
|
87
|
+
- glab-lint
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files:
|
90
|
+
- README.md
|
91
|
+
files:
|
92
|
+
- ".gitignore"
|
93
|
+
- ".pre-commit-hooks.yaml"
|
94
|
+
- ".rspec"
|
95
|
+
- ".travis.yml"
|
96
|
+
- CHANGELOG.md
|
97
|
+
- Gemfile
|
98
|
+
- Gemfile.lock
|
99
|
+
- LICENSE.txt
|
100
|
+
- README.md
|
101
|
+
- Rakefile
|
102
|
+
- bin/console
|
103
|
+
- bin/setup
|
104
|
+
- exe/glab-lint
|
105
|
+
- gitlab-lint-client.gemspec
|
106
|
+
- lib/gitlab/lint/client.rb
|
107
|
+
- lib/gitlab/lint/client/api.rb
|
108
|
+
- lib/gitlab/lint/client/args.rb
|
109
|
+
- lib/gitlab/lint/client/summaryreport.rb
|
110
|
+
- lib/gitlab/lint/client/version.rb
|
111
|
+
- lib/gitlab/lint/client/yml.rb
|
112
|
+
homepage: https://github.com/dcs3spp/validate-gitlab-ci
|
113
|
+
licenses:
|
114
|
+
- MIT
|
115
|
+
metadata:
|
116
|
+
allowed_push_host: https://rubygems.org
|
117
|
+
homepage_uri: https://github.com/dcs3spp/validate-gitlab-ci
|
118
|
+
source_code_uri: https://github.com/dcs3spp/validate-gitlab-ci
|
119
|
+
changelog_uri: https://github.com/dcs3spp/validate-gitlab-ci/blob/master/CHANGELOG.md
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: 2.6.3
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubygems_version: 3.0.8
|
136
|
+
signing_key:
|
137
|
+
specification_version: 4
|
138
|
+
summary: Use as a standalone CLI, from within a git pre-commit hook. The repository
|
139
|
+
provides a pre-commit.com hook with identifier 'validate-gitlab-ci'
|
140
|
+
test_files: []
|