Skip to content

Commit 7e1a2ce

Browse files
dcs3sppdcs3spp
andauthored
Feature/refactor (#1)
* version(0.0.1) commit v0.0.1 * chore(gemspec): update summary and description Co-authored-by: dcs3spp <simonppears@googlemail.com>
1 parent c8389a8 commit 7e1a2ce

29 files changed

+1163
-217
lines changed

.gitignore

Lines changed: 9 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,15 @@
1-
*.gem
2-
*.rbc
3-
/.config
1+
/.bundle/
2+
/.yardoc
3+
/_yardoc/
44
/coverage/
5-
/InstalledFiles
5+
/doc/
66
/pkg/
77
/spec/reports/
8-
/spec/examples.txt
9-
/test/tmp/
10-
/test/version_tmp/
118
/tmp/
9+
/vendor/
1210

13-
# Used by dotenv library to load environment variables.
14-
# .env
15-
16-
# Ignore Byebug command history file.
17-
.byebug_history
18-
19-
## Specific to RubyMotion:
20-
.dat*
21-
.repl_history
22-
build/
23-
*.bridgesupport
24-
build-iPhoneOS/
25-
build-iPhoneSimulator/
26-
27-
## Specific to RubyMotion (use of CocoaPods):
28-
#
29-
# We recommend against adding the Pods directory to your .gitignore. However
30-
# you should judge for yourself, the pros and cons are mentioned at:
31-
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32-
#
33-
# vendor/Pods/
34-
35-
## Documentation cache and generated files:
36-
/.yardoc/
37-
/_yardoc/
38-
/doc/
39-
/rdoc/
40-
41-
## Environment normalization:
42-
/.bundle/
43-
/vendor/bundle
44-
/lib/bundler/man/
45-
46-
# for a library or gem, you might want to ignore these files since the code is
47-
# intended to run in multiple environments; otherwise, check them in:
48-
# Gemfile.lock
49-
# .ruby-version
50-
# .ruby-gemset
51-
52-
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53-
.rvmrc
11+
# rspec failure tracking
12+
.rspec_status
13+
*.gem
5414

55-
# Used by RuboCop. Remote config files pulled in from inherit_from directive.
56-
# .rubocop-https?--*
15+
*.DS_Store

.pre-commit-hooks.yaml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
- id: validate-gitlab-ci
2-
name: Validate GitLab CI
3-
description: This hook validates the Gitlab CI yaml file
4-
entry: validate-gitlab-ci
5-
language: ruby
6-
language_version: 2.6.3
7-
verbose: true
1+
- id: validate-gitlab-ci
2+
name: Validate GitLab CI
3+
description: This hook validates the Gitlab CI yaml file
4+
entry: glab-lint
5+
language: ruby
6+
language_version: 2.6.3
7+
verbose: true

.rspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--format documentation
2+
--color
3+
--require spec_helper

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
language: ruby
3+
cache: bundler
4+
rvm:
5+
- 2.3.6
6+
before_install: gem install bundler -v 2.1.4

CHANGELOG.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 - [12-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

Gemfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source "https://rubygems.org"
2+
3+
gemspec
4+

LICENSE.txt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2020 simon pears
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.

README.md

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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 can be found [here]() and the source code, with accompanying unit and functional tests,
24+
are held within this repository. It can be installed by issuing the following command:
25+
```gem install gitlab-lint-client```.
26+
27+
Usage details are provided with the *help* option of the CLI, <kbd>glab-lint --help</kbd>
28+
Examples are:
29+
30+
``` bash
31+
# mandatory options
32+
glab-lint --yaml=.gitlab-ci.yml --base-url=https://gitlab.com
33+
glab-lint -f .gitlab-ci.yml -u https://gitlab.com
34+
35+
# configure timeout in seconds, for API request
36+
glab-lint --yaml=.gitlab-ci.yml --base-url=https://gitlab.com --timeout=10
37+
glab-lint -f .gitlab-ci.yml -u https://gitlab.com -t 10
38+
39+
# display program version number and exit
40+
glab-lint --version
41+
glab-lint -v
42+
```
43+
44+
The author is using the gem within a client side pre-commit hook to automate GitLab CI linting.
45+
46+
47+
## 🔐 Usage As A Pre-Commit Hook
48+
49+
This repository specifies a pre-commit hook in the *.pre-commit-hooks.yml* file. Developers can
50+
configure their GitLab repositories to use this hook by creating a *.pre-commit-config.yml* file
51+
in the root of their repository.
52+
53+
``` yaml
54+
repos:
55+
- repo: https://github.com/dcs3spp/validate-gitlab-ci
56+
rev: v0.0.1
57+
hooks:
58+
- id: validate-gitlab-ci
59+
args: [--yaml=.gitlab-ci.yml, --base-url=https://gitlab.com]
60+
pass_filenames: false
61+
types: [yaml]
62+
files: .gitlab-ci.yml
63+
stages: [commit]
64+
```
65+
66+
Subsequently, [install](https://pre-commit.com/#installation) the pre-commit tool by issuing
67+
the following command:
68+
69+
``` bash
70+
pip install pre-commit
71+
```
72+
73+
Instruct pre-commit to download and configure the hooks defined in the *.pre-commit-config.yml* file:
74+
75+
``` bash
76+
pre-commit install
77+
```
78+
79+
This will create a Ruby environment and automatically download and install the gem held within this
80+
repository. The environment is setup on first time use only.
81+
82+
Subsequently, whenever an attempt is made to commit the GitLab CI yaml file, the pre-commit hook
83+
will automatically send it for linting to the GitLab API. If a failed response is received from the API,
84+
then the commit is declined.
85+
86+
87+
## 🔧 Quick Start For Development
88+
89+
Perform a *git clone*:
90+
``` bash
91+
git clone --depth 1 https://github.com/dcs3spp/validate-gitlab-ci.git
92+
```
93+
94+
* This will download the source to local machine.
95+
* Ruby >=2.3.6 is required with the following development dependencies:
96+
* bundler
97+
* pry
98+
* rake
99+
* rspec
100+
* webmock
101+
102+
* The gemspec file lists specific dependencies for development.
103+
* This gem is available publically as *gitlab-lint-client*
104+
105+
106+
## 🏭 Building And Installing The Gem Locally
107+
108+
``` bash
109+
gem build gitlab-lint-client.gemspec
110+
gem install gitlab-lint-client-0.0.1.gem
111+
```
112+
113+
## ⛑ Running Tests Locally
114+
115+
``` bash
116+
bundle exec rspec
117+
```
118+
119+
## 📁 Environment Variables
120+
121+
**Name** | **Description**
122+
:---: | :---:
123+
**GITLAB_API_TOKEN** | GitLab API Token for use with private GitLab servers (other than https://gitlab.com) that may require an authorization header
124+
125+
126+
## 📦 Using Rake For Performing A Release
127+
128+
``` bash
129+
# build the gem
130+
rake build
131+
132+
# install the gem
133+
rake install
134+
135+
# run the tests
136+
rake spec
137+
138+
# release the gem
139+
rake release
140+
```
141+
142+
143+
## 📋 Versioning
144+
145+
- [CHANGELOG](CHANGELOG.md)
146+
147+
148+
## 🔑 License
149+
150+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

Rakefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require "bundler/gem_tasks"
2+
require "rspec/core/rake_task"
3+
4+
RSpec::Core::RakeTask.new(:spec)
5+
6+
task :default => :spec

bin/console

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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__)

0 commit comments

Comments
 (0)