terraspace_ci_github 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: 8d88b062ab17176d868d4f0b6f11a8a4b614c1e1396922186e4356264be0e069
4
+ data.tar.gz: 58b57b162da756f2e3aa63c055c8980ab58aa846489ac6378263fa72bfc1101c
5
+ SHA512:
6
+ metadata.gz: ccae205902e9717ca80ee2335238052217e9e4e553ebbe2c1afec8274c7aeda317ed73a04eeb611f9406af4964af5f12074a6da69f77614add44697613d34134
7
+ data.tar.gz: 2f24c1dd359f4856e4f67b5681777417647dc6f60d514d4b82400d631d34611a9b30f505ccb2b65dbb319800121c58a1920a8336e0c56b680aea27a84c5be3e1
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 terraspace_ci_github.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) 2022 Tung Nguyen
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_github
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 GitHub Actions CI support.
10
+
11
+ ## Installation
12
+
13
+ Add gem to your Terraspace project
14
+
15
+ Gemfile
16
+
17
+ gem terraspace_ci_github
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_github.
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,8 @@
1
+ #!/bin/bash
2
+ brew install tfenv
3
+ tfenv install latest
4
+ tfenv use latest
5
+ terraform --version
6
+ bundle install
7
+ bundle exec terraspace new shim
8
+ terraspace --version
@@ -0,0 +1,21 @@
1
+ name: Pull Request Plan
2
+
3
+ on:
4
+ - pull_request
5
+
6
+ jobs:
7
+ run:
8
+ runs-on: ubuntu-latest
9
+
10
+ env:
11
+ <%= plugin_env_vars(indent: 6) -%>
12
+ GH_TOKEN: ${{ secrets.GH_TOKEN }}
13
+ TS_ENV: dev
14
+ TS_TOKEN: ${{ secrets.TS_TOKEN }}
15
+
16
+ steps:
17
+ <%= partial "init_steps.yml" -%>
18
+
19
+ - name: Terraspace Command
20
+ run: |
21
+ terraspace plan demo
@@ -0,0 +1,23 @@
1
+ name: Push Up
2
+ on:
3
+ push:
4
+ branches:
5
+ - main
6
+ - master
7
+
8
+ jobs:
9
+ run:
10
+ runs-on: ubuntu-latest
11
+
12
+ env:
13
+ <%= plugin_env_vars(indent: 6) -%>
14
+ GH_TOKEN: ${{ secrets.GH_TOKEN }}
15
+ TS_ENV: dev
16
+ TS_TOKEN: ${{ secrets.TS_TOKEN }}
17
+
18
+ steps:
19
+ <%= partial "init_steps.yml" -%>
20
+
21
+ - name: Terraspace Command
22
+ run: |
23
+ terraspace up demo -y
@@ -0,0 +1,30 @@
1
+ name: Dispatch Terraspace
2
+ on:
3
+ workflow_dispatch:
4
+ inputs:
5
+ env:
6
+ description: "TS_ENV"
7
+ type: choice
8
+ default: prod
9
+ options: # Add more environment if needed
10
+ - dev
11
+ - prod
12
+ command:
13
+ description: "Terraspace command to run"
14
+ default: "plan demo"
15
+ jobs:
16
+ run:
17
+ runs-on: ubuntu-latest
18
+
19
+ env:
20
+ <%= plugin_env_vars(indent: 6) -%>
21
+ GH_TOKEN: ${{ secrets.GH_TOKEN }}
22
+ TS_ENV: ${{ github.event.inputs.env }}
23
+ TS_TOKEN: ${{ secrets.TS_TOKEN }}
24
+
25
+ steps:
26
+ <%= partial "init_steps.yml" -%>
27
+
28
+ - name: Terraspace Command
29
+ run: |
30
+ terraspace ${{ github.event.inputs.command }} -y
@@ -0,0 +1,12 @@
1
+ - name: Checkout
2
+ uses: actions/checkout@v3
3
+
4
+ - name: Install Ruby
5
+ uses: ruby/setup-ruby@v1
6
+ with:
7
+ ruby-version: 3.1
8
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
9
+
10
+ - name: Install
11
+ run:
12
+ .github/bin/install
@@ -0,0 +1,23 @@
1
+ require "zeitwerk"
2
+
3
+ module TerraspaceCiGithub
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_GITHUB_AUTOLOAD_LOG"]
18
+ loader.setup
19
+ end
20
+ end
21
+ end
22
+ end
23
+
@@ -0,0 +1,21 @@
1
+ require "octokit"
2
+
3
+ module TerraspaceCiGithub
4
+ class Base
5
+ extend Memoist
6
+
7
+ def client
8
+ Octokit::Client.new(access_token: ENV['GH_TOKEN'])
9
+ end
10
+ memoize :client
11
+
12
+ def github_token?
13
+ if ENV['GH_TOKEN']
14
+ true
15
+ else
16
+ puts "WARN: The env var GH_TOKEN is not configured. Will not post PR comment"
17
+ false
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ module TerraspaceCiGithub
2
+ class Interface
3
+ # required interface
4
+ def vars
5
+ Vars.new.data
6
+ end
7
+
8
+ # optional interface
9
+ def comment(url)
10
+ Pr.new.comment(url)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ module TerraspaceCiGithub
2
+ class Pr < Base
3
+ def comment(url)
4
+ return unless ENV['GITHUB_EVENT_NAME'] == 'pull_request'
5
+ return unless github_token?
6
+
7
+ repo = ENV['GITHUB_REPOSITORY'] # org/repo
8
+ number = ENV['GITHUB_REF_NAME'].split('/').first # IE: 2/merge
9
+ marker = "<!-- terraspace marker -->"
10
+ body = marker + "\n"
11
+ body << "Terraspace Cloud Url #{url}"
12
+
13
+ puts "Adding comment to repo #{repo} number #{number}"
14
+
15
+ comments = client.issue_comments(repo, number)
16
+ found_comment = comments.find do |comment|
17
+ comment.body.starts_with?(marker)
18
+ end
19
+
20
+ if found_comment
21
+ client.update_comment(repo, found_comment.id, body) unless found_comment.body == body
22
+ else
23
+ client.add_comment(repo, number, body)
24
+ end
25
+ rescue Octokit::Unauthorized => e
26
+ puts "WARN: #{e.message}. Unable to create pull request comment. Please double check your github token"
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,80 @@
1
+ module TerraspaceCiGithub
2
+ class Vars < Base
3
+ # Hash of properties to store
4
+ def data
5
+ {
6
+ build_system: "github", # required
7
+ host: host,
8
+ full_repo: full_repo,
9
+ branch_name: branch_name,
10
+ # urls
11
+ pr_url: pr_url,
12
+ build_url: build_url,
13
+ # additional properties
14
+ build_type: build_type, # required IE: pull_request or push
15
+ pr_number: pr['number'], # set when build_type=pull_request
16
+ sha: sha,
17
+ # additional properties
18
+ commit_message: commit_message,
19
+ build_id: build_id,
20
+ build_number: ENV['GITHUB_RUN_NUMBER'],
21
+ }
22
+ end
23
+
24
+ def host
25
+ ENV['GITHUB_SERVER_URL'] || 'https://github.com'
26
+ end
27
+
28
+ def pr_url
29
+ "#{host}/#{full_repo}/pull/#{pr['number']}" if pr['number']
30
+ end
31
+
32
+ def build_url
33
+ "#{host}/#{full_repo}/actions/runs/#{build_id}"
34
+ end
35
+
36
+ def build_id
37
+ ENV['GITHUB_RUN_ID']
38
+ end
39
+
40
+ # https://github.com/octokit/octokit.rb/blob/4-stable/lib/octokit/client/commits.rb#L150
41
+ # https://docs.github.com/en/rest/commits/commits#get-a-commit
42
+ def commit_message
43
+ return unless github_token?
44
+ resp = client.commit(full_repo, sha)
45
+ resp['commit']['message']
46
+ rescue Octokit::Unauthorized => e
47
+ puts "WARN: #{e.message}. Error getting commit message. Please double check your github token"
48
+ end
49
+
50
+ def full_repo
51
+ ENV['GITHUB_REPOSITORY']
52
+ end
53
+
54
+ def sha
55
+ if build_type == "pull_request"
56
+ pr.dig('pull_request','head','sha')
57
+ else # push
58
+ ENV['GITHUB_SHA']
59
+ end
60
+ end
61
+
62
+ def build_type
63
+ ENV['GITHUB_EVENT_NAME']
64
+ end
65
+
66
+ def branch_name
67
+ if build_type == "pull_request"
68
+ pr.dig('pull_request','head','ref')
69
+ else # push
70
+ ENV['GITHUB_REF']
71
+ end
72
+ end
73
+
74
+ # GitHub webhook JSON payload in file and path is set in GITHUB_EVENT_PATH
75
+ def pr
76
+ return {} unless ENV['GITHUB_EVENT_PATH']
77
+ JSON.load(IO.read(ENV['GITHUB_EVENT_PATH']))
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module TerraspaceCiGithub
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "terraspace_ci_github/autoloader"
4
+ TerraspaceCiGithub::Autoloader.setup
5
+
6
+ require "json"
7
+ require "memoist"
8
+
9
+ module TerraspaceCiGithub
10
+ class Error < StandardError; end
11
+ end
12
+
13
+ require "terraspace"
14
+ Terraspace::Cloud::Ci.register(
15
+ name: "github",
16
+ env_key: "GITHUB_ACTIONS",
17
+ root: __dir__,
18
+ exe: ".github/bin",
19
+ )
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/terraspace_ci_github/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "terraspace_ci_github"
7
+ spec.version = TerraspaceCiGithub::VERSION
8
+ spec.authors = ["Tung Nguyen"]
9
+ spec.email = ["tung@boltops.com"]
10
+
11
+ spec.summary = "Terraspace CI GitHub support"
12
+ spec.homepage = "https://github.com/boltops-tools/terraspace_ci_github"
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_github"
18
+ spec.metadata["changelog_uri"] = "https://github.com/boltops-tools/terraspace_ci_github/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 "memoist"
32
+ spec.add_dependency "octokit"
33
+ spec.add_dependency "zeitwerk"
34
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: terraspace_ci_github
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: memoist
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: octokit
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/.github/bin/install
70
+ - lib/template/.github/workflows/pull_request.yml.tt
71
+ - lib/template/.github/workflows/push.yml.tt
72
+ - lib/template/.github/workflows/workflow_dispatch.yml.tt
73
+ - lib/template/partials/init_steps.yml
74
+ - lib/terraspace_ci_github.rb
75
+ - lib/terraspace_ci_github/autoloader.rb
76
+ - lib/terraspace_ci_github/base.rb
77
+ - lib/terraspace_ci_github/interface.rb
78
+ - lib/terraspace_ci_github/pr.rb
79
+ - lib/terraspace_ci_github/vars.rb
80
+ - lib/terraspace_ci_github/version.rb
81
+ - terraspace_ci_github.gemspec
82
+ homepage: https://github.com/boltops-tools/terraspace_ci_github
83
+ licenses:
84
+ - MIT
85
+ metadata:
86
+ homepage_uri: https://github.com/boltops-tools/terraspace_ci_github
87
+ source_code_uri: https://github.com/boltops-tools/terraspace_ci_github
88
+ changelog_uri: https://github.com/boltops-tools/terraspace_ci_github/blob/master/CHANGELOG.md
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: 2.6.0
98
+ required_rubygems_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ requirements: []
104
+ rubygems_version: 3.3.12
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Terraspace CI GitHub support
108
+ test_files: []