terraspace_ci_gitlab 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b032f2c6bc8b2194f5b9947c9d136179575cb2b5a7569091170002c44e4fb32c
4
+ data.tar.gz: dab7b140333f75176c0518bcf84931df6fdaf30f927b0c8a2c95153fc3d6d836
5
+ SHA512:
6
+ metadata.gz: 5a294e4c11db5579841607da16062e48ea69540e7890e898b9a84b1b1a823d041236bd40d9c711b26271c7edefeab26fe58feb0c2138e176d8e91b348f226909
7
+ data.tar.gz: f6e5294f8d16af5d9097a30183fe9fda4a39e93937df89bcb10e9d92e0587d7a2d976079243813e223024dd5eb56a480a63fdd922dae9bfbb2a2478ad5dd5080
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,13 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.6
3
+
4
+ Style/StringLiterals:
5
+ Enabled: true
6
+ EnforcedStyle: double_quotes
7
+
8
+ Style/StringLiteralsInInterpolation:
9
+ Enabled: true
10
+ EnforcedStyle: double_quotes
11
+
12
+ Layout/LineLength:
13
+ Max: 120
data/CHANGELOG.md ADDED
@@ -0,0 +1,8 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ This project *tries* to adhere to [Semantic Versioning](http://semver.org/).
5
+
6
+ ## [0.1.0] - 2022-06-09
7
+
8
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in gemspec
6
+ gemspec
7
+
8
+ gem "rake"
9
+ gem "rspec"
10
+ gem "rubocop"
11
+
12
+ group :development, :test do
13
+ gem "terraspace"
14
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) <%= Time.now.year %>
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,25 @@
1
+ # terraspace_ci_gitlab
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/terraspace.png)](http://badge.fury.io/rb/terraspace)
4
+
5
+ [![BoltOps Badge](https://img.boltops.com/boltops/badges/boltops-badge.png)](https://www.boltops.com)
6
+
7
+ [![BoltOps Learn Badge](https://img.boltops.com/boltops-learn/boltops-learn.png)](https://learn.boltops.com)
8
+
9
+ Provides Terraspace CI support for GitLab.
10
+
11
+ ## Installation
12
+
13
+ Add gem to your Terraspace project
14
+
15
+ Gemfile
16
+
17
+ gem terraspace_ci_gitlab
18
+
19
+ Then to install run
20
+
21
+ bundle
22
+
23
+ ## Contributing
24
+
25
+ Bug reports and pull requests are welcome on GitHub at https://github.com/boltops-tools/terraspace_ci_gitlab/
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "rubocop/rake_task"
9
+
10
+ RuboCop::RakeTask.new
11
+
12
+ task default: %i[spec rubocop]
@@ -0,0 +1,54 @@
1
+ image: ruby:latest
2
+
3
+ stages:
4
+ - plan dev
5
+ - up dev
6
+ - plan prod
7
+ - up prod
8
+
9
+ before_script: |
10
+ # install terraform
11
+ git clone https://github.com/tfutils/tfenv.git ~/.tfenv
12
+ echo 'export PATH="$HOME/.tfenv/bin:$PATH"' >> ~/.bash_profile
13
+ export PATH="$HOME/.tfenv/bin:$PATH"
14
+ tfenv install latest
15
+ tfenv use latest
16
+ terraform --version
17
+ # install terraspace
18
+ bundle
19
+ bundle exec terraspace new shim
20
+ echo 'export PATH="/usr/local/bin:$PATH' >> ~/.bash_profile
21
+ export PATH="/usr/local/bin:$PATH"
22
+
23
+ # Runs on Merge Request Only
24
+ plan_dev:
25
+ stage: plan dev
26
+ only:
27
+ - merge_requests
28
+ script:
29
+ - terraspace plan demo
30
+
31
+ # Runs on Push Only
32
+ up_dev:
33
+ stage: up dev
34
+ rules:
35
+ - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "main"'
36
+ script:
37
+ - terraspace up demo -y
38
+
39
+ # Runs Manually Only (part 1 of 2)
40
+ plan_prod:
41
+ stage: plan prod
42
+ rules:
43
+ - if: '$CI_PIPELINE_SOURCE == "web"'
44
+ script:
45
+ - TS_ENV=prod terraspace plan demo
46
+
47
+ # Runs Manually Only (part 2 of 2)
48
+ up_prod:
49
+ stage: up prod
50
+ rules:
51
+ - if: '$CI_PIPELINE_SOURCE == "web"'
52
+ when: manual # manual approval
53
+ script:
54
+ - TS_ENV=prod terraspace up demo -y
@@ -0,0 +1,23 @@
1
+ require "zeitwerk"
2
+
3
+ module TerraspaceCiGitlab
4
+ class Autoloader
5
+ class Inflector < Zeitwerk::Inflector
6
+ def camelize(basename, _abspath)
7
+ map = { version: "VERSION" }
8
+ map[basename.to_sym] || super
9
+ end
10
+ end
11
+
12
+ class << self
13
+ def setup
14
+ loader = Zeitwerk::Loader.new
15
+ loader.inflector = Inflector.new
16
+ loader.push_dir(File.dirname(__dir__)) # lib
17
+ loader.log! if ENV["TERRASPACE_CI_GITLAB_AUTOLOAD_LOG"]
18
+ loader.setup
19
+ end
20
+ end
21
+ end
22
+ end
23
+
@@ -0,0 +1,12 @@
1
+ module TerraspaceCiGitlab
2
+ class Interface
3
+ # required interface
4
+ def vars
5
+ Vars.new.data
6
+ end
7
+
8
+ def comment(url)
9
+ Pr.new.comment(url)
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,55 @@
1
+ require "gitlab"
2
+
3
+ module TerraspaceCiGitlab
4
+ class Pr
5
+ extend Memoist
6
+
7
+ def comment(url)
8
+ return unless ENV['CI_PIPELINE_SOURCE'] == 'merge_request_event'
9
+ return unless gitlab_token?
10
+
11
+ repo = ENV['CI_PROJECT_PATH'] # org/repo
12
+ number = ENV['CI_MERGE_REQUEST_IID']
13
+ marker = "<!-- terraspace marker -->"
14
+ body = marker + "\n"
15
+ body << "Terraspace Cloud Url #{url}"
16
+
17
+ puts "Adding comment to repo #{repo} number #{number}"
18
+
19
+ project = client.project(ENV['CI_PROJECT_PATH'])
20
+ merge_request = ENV['CI_MERGE_REQUEST_IID']
21
+
22
+ # https://www.rubydoc.info/gems/gitlab/Gitlab/Client/Notes
23
+ # TODO: handle pagination
24
+ notes = client.merge_request_notes(project.id, number)
25
+ # TODO: handle not found. Examples:
26
+ # token is not valid
27
+ # token is not right repo
28
+ # todo are we allow to post comment on public repo without need the permission?
29
+ found_note = notes.find do |note|
30
+ note.body.starts_with?(marker)
31
+ end
32
+
33
+ if found_note
34
+ client.edit_merge_request_note(project.id, merge_request, found_note.id, body) unless found_note.body == body
35
+ else
36
+ client.create_merge_request_note(project.id, merge_request, body)
37
+ end
38
+ rescue Gitlab::Error::Unauthorized => e
39
+ puts "WARN: #{e.message}. Unable to create merge request comment. Please double check your gitlab token"
40
+ end
41
+
42
+ def client
43
+ Gitlab.configure do |config|
44
+ config.endpoint = 'https://gitlab.com/api/v4'
45
+ config.private_token = ENV['GITLAB_TOKEN']
46
+ end
47
+ Gitlab.client
48
+ end
49
+ memoize :client
50
+
51
+ def gitlab_token?
52
+ ENV['GITLAB_TOKEN']
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,48 @@
1
+ module TerraspaceCiGitlab
2
+ class Vars
3
+ # Interface method. Hash of properties to be stored
4
+ def data
5
+ {
6
+ build_system: "gitlab", # required
7
+ host: host,
8
+ full_repo: full_repo,
9
+ branch_name: branch_name,
10
+ # urls
11
+ pr_url: pr_url,
12
+ build_url: ENV['CI_PIPELINE_URL'],
13
+ # additional properties
14
+ build_type: ENV['CI_PIPELINE_SOURCE'], # IE: merge_request_event
15
+ pr_number: pr_number, # set when build_type=pull_request
16
+ sha: sha,
17
+ # additional properties
18
+ commit_message: ENV['CI_COMMIT_MESSAGE'],
19
+ build_id: ENV['CI_PIPELINE_ID'],
20
+ build_number: ENV['CI_PIPELINE_IID'],
21
+ }
22
+ end
23
+
24
+ def host
25
+ ENV['CI_SERVER_URL'] || 'https://gitlab.com'
26
+ end
27
+
28
+ def pr_url
29
+ "#{host}/#{full_repo}/-/merge_requests/#{pr_number}" if pr_number
30
+ end
31
+
32
+ def pr_number
33
+ ENV['CI_MERGE_REQUEST_IID']
34
+ end
35
+
36
+ def branch_name
37
+ ENV['CI_COMMIT_REF_NAME']
38
+ end
39
+
40
+ def sha
41
+ ENV['CI_COMMIT_SHA']
42
+ end
43
+
44
+ def full_repo
45
+ ENV['CI_PROJECT_PATH']
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TerraspaceCiGitlab
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "terraspace_ci_gitlab/autoloader"
4
+ TerraspaceCiGitlab::Autoloader.setup
5
+
6
+ require "json"
7
+ require "memoist"
8
+
9
+ module TerraspaceCiGitlab
10
+ class Error < StandardError; end
11
+ end
12
+
13
+ require "terraspace"
14
+ Terraspace::Cloud::Ci.register(
15
+ name: "gitlab",
16
+ env_key: "GITLAB_CI", # NOTE: probably have to change. Env var used for CI detection
17
+ root: __dir__,
18
+ )
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/terraspace_ci_gitlab/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "terraspace_ci_gitlab"
7
+ spec.version = TerraspaceCiGitlab::VERSION
8
+ spec.authors = ["Tung Nguyen"]
9
+ spec.email = ["tung@boltops.com"]
10
+
11
+ spec.summary = "Terraspace CI GitLab support"
12
+ spec.homepage = "https://github.com/boltops-tools/terraspace_ci_gitlab"
13
+ spec.license = "MIT"
14
+ spec.required_ruby_version = ">= 2.6.0"
15
+
16
+ spec.metadata["homepage_uri"] = spec.homepage
17
+ spec.metadata["source_code_uri"] = "https://github.com/boltops-tools/terraspace_ci_gitlab"
18
+ spec.metadata["changelog_uri"] = "https://github.com/boltops-tools/terraspace_ci_gitlab/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(__dir__) do
23
+ `git ls-files -z`.split("\x0").reject do |f|
24
+ (f == __FILE__) || f.match(%r{\A(?:(?:bin|test|spec|features)/|\.(?:git|travis|circleci)|appveyor)})
25
+ end
26
+ end
27
+ spec.bindir = "exe"
28
+ spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
29
+ spec.require_paths = ["lib"]
30
+
31
+ spec.add_dependency "gitlab"
32
+ spec.add_dependency "memoist"
33
+ spec.add_dependency "zeitwerk"
34
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: terraspace_ci_gitlab
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Tung Nguyen
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-06-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: gitlab
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: memoist
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: zeitwerk
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - tung@boltops.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".rspec"
63
+ - ".rubocop.yml"
64
+ - CHANGELOG.md
65
+ - Gemfile
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - lib/template/.gitlab-ci.yml
70
+ - lib/terraspace_ci_gitlab.rb
71
+ - lib/terraspace_ci_gitlab/autoloader.rb
72
+ - lib/terraspace_ci_gitlab/interface.rb
73
+ - lib/terraspace_ci_gitlab/pr.rb
74
+ - lib/terraspace_ci_gitlab/vars.rb
75
+ - lib/terraspace_ci_gitlab/version.rb
76
+ - terraspace_ci_gitlab.gemspec
77
+ homepage: https://github.com/boltops-tools/terraspace_ci_gitlab
78
+ licenses:
79
+ - MIT
80
+ metadata:
81
+ homepage_uri: https://github.com/boltops-tools/terraspace_ci_gitlab
82
+ source_code_uri: https://github.com/boltops-tools/terraspace_ci_gitlab
83
+ changelog_uri: https://github.com/boltops-tools/terraspace_ci_gitlab/blob/master/CHANGELOG.md
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 2.6.0
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubygems_version: 3.3.12
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Terraspace CI GitLab support
103
+ test_files: []