hako-github_status 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 05bc0b4478a4a4b107b11c9fd74a4aadc25b7a2b523783b992213525d376fd4e
4
+ data.tar.gz: 3be63c9e1612c03ecaa594594f0b8aaa29bc5696d4cc493d817ae630e001acc3
5
+ SHA512:
6
+ metadata.gz: 71c30c92e4d16df4afd2ee6a86d8a8d18970f4fa8fc0cd48f2d745b422a10dd07dc4be112750199a68994547dcc8ddfd8aaca333b37428c7b1e2390b75bc4526
7
+ data.tar.gz: 0d5b1987a7fd5e009d952785dffc59c644416709e847ffbde6778cf507551472d05c7755d5fb02be40db3576c4df2c2d1fc80da0bbff74e3bf78e515266e78e4
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in hako-github_status.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "rspec", "~> 3.0"
@@ -0,0 +1,117 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ hako-github_status (0.0.1)
5
+ hako
6
+ jwt
7
+ octokit
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ addressable (2.7.0)
13
+ public_suffix (>= 2.0.2, < 5.0)
14
+ aws-eventstream (1.0.3)
15
+ aws-partitions (1.269.0)
16
+ aws-sdk-applicationautoscaling (1.35.0)
17
+ aws-sdk-core (~> 3, >= 3.71.0)
18
+ aws-sigv4 (~> 1.1)
19
+ aws-sdk-autoscaling (1.30.0)
20
+ aws-sdk-core (~> 3, >= 3.71.0)
21
+ aws-sigv4 (~> 1.1)
22
+ aws-sdk-cloudwatch (1.32.0)
23
+ aws-sdk-core (~> 3, >= 3.71.0)
24
+ aws-sigv4 (~> 1.1)
25
+ aws-sdk-cloudwatchlogs (1.28.0)
26
+ aws-sdk-core (~> 3, >= 3.71.0)
27
+ aws-sigv4 (~> 1.1)
28
+ aws-sdk-core (3.89.1)
29
+ aws-eventstream (~> 1.0, >= 1.0.2)
30
+ aws-partitions (~> 1, >= 1.239.0)
31
+ aws-sigv4 (~> 1.1)
32
+ jmespath (~> 1.0)
33
+ aws-sdk-ec2 (1.137.0)
34
+ aws-sdk-core (~> 3, >= 3.71.0)
35
+ aws-sigv4 (~> 1.1)
36
+ aws-sdk-ecs (1.57.0)
37
+ aws-sdk-core (~> 3, >= 3.71.0)
38
+ aws-sigv4 (~> 1.1)
39
+ aws-sdk-elasticloadbalancing (1.19.0)
40
+ aws-sdk-core (~> 3, >= 3.71.0)
41
+ aws-sigv4 (~> 1.1)
42
+ aws-sdk-elasticloadbalancingv2 (1.39.0)
43
+ aws-sdk-core (~> 3, >= 3.71.0)
44
+ aws-sigv4 (~> 1.1)
45
+ aws-sdk-kms (1.28.0)
46
+ aws-sdk-core (~> 3, >= 3.71.0)
47
+ aws-sigv4 (~> 1.1)
48
+ aws-sdk-s3 (1.60.1)
49
+ aws-sdk-core (~> 3, >= 3.83.0)
50
+ aws-sdk-kms (~> 1)
51
+ aws-sigv4 (~> 1.1)
52
+ aws-sdk-servicediscovery (1.19.0)
53
+ aws-sdk-core (~> 3, >= 3.71.0)
54
+ aws-sigv4 (~> 1.1)
55
+ aws-sdk-sns (1.21.0)
56
+ aws-sdk-core (~> 3, >= 3.71.0)
57
+ aws-sigv4 (~> 1.1)
58
+ aws-sdk-ssm (1.70.0)
59
+ aws-sdk-core (~> 3, >= 3.71.0)
60
+ aws-sigv4 (~> 1.1)
61
+ aws-sigv4 (1.1.0)
62
+ aws-eventstream (~> 1.0, >= 1.0.2)
63
+ diff-lcs (1.3)
64
+ faraday (1.0.0)
65
+ multipart-post (>= 1.2, < 3)
66
+ hako (2.13.0)
67
+ aws-sdk-applicationautoscaling
68
+ aws-sdk-autoscaling
69
+ aws-sdk-cloudwatch
70
+ aws-sdk-cloudwatchlogs
71
+ aws-sdk-ec2
72
+ aws-sdk-ecs (>= 1.54.0)
73
+ aws-sdk-elasticloadbalancing
74
+ aws-sdk-elasticloadbalancingv2
75
+ aws-sdk-s3
76
+ aws-sdk-servicediscovery
77
+ aws-sdk-sns
78
+ aws-sdk-ssm
79
+ jsonnet
80
+ jmespath (1.4.0)
81
+ jsonnet (0.3.0)
82
+ mini_portile2 (>= 2.2.0)
83
+ jwt (2.2.1)
84
+ mini_portile2 (2.4.0)
85
+ multipart-post (2.1.1)
86
+ octokit (4.15.0)
87
+ faraday (>= 0.9)
88
+ sawyer (~> 0.8.0, >= 0.5.3)
89
+ public_suffix (4.0.3)
90
+ rake (12.3.3)
91
+ rspec (3.9.0)
92
+ rspec-core (~> 3.9.0)
93
+ rspec-expectations (~> 3.9.0)
94
+ rspec-mocks (~> 3.9.0)
95
+ rspec-core (3.9.1)
96
+ rspec-support (~> 3.9.1)
97
+ rspec-expectations (3.9.0)
98
+ diff-lcs (>= 1.2.0, < 2.0)
99
+ rspec-support (~> 3.9.0)
100
+ rspec-mocks (3.9.1)
101
+ diff-lcs (>= 1.2.0, < 2.0)
102
+ rspec-support (~> 3.9.0)
103
+ rspec-support (3.9.2)
104
+ sawyer (0.8.2)
105
+ addressable (>= 2.3.5)
106
+ faraday (> 0.8, < 2.0)
107
+
108
+ PLATFORMS
109
+ ruby
110
+
111
+ DEPENDENCIES
112
+ hako-github_status!
113
+ rake (~> 12.0)
114
+ rspec (~> 3.0)
115
+
116
+ BUNDLED WITH
117
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Sorah Fukumori
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.
@@ -0,0 +1,58 @@
1
+ # hako-github_status
2
+
3
+ [hako](https://github.com/eagletmt/hako) script to update image tag of `app` container to the commit SHA of the latest check-succeeded commit in a specified GitHub repository and branch
4
+
5
+
6
+ ## Installation
7
+
8
+ ```ruby
9
+ gem 'hako-github_status'
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ``` jsonnet
15
+ {
16
+ scripts: [
17
+ {
18
+ type: 'github_status_tag',
19
+ repo: 'sorah/hello-container',
20
+ ref: 'master',
21
+ # at least either from checks or statuses must be set
22
+ checks: ['ci/circleci:build'], # Required status names on GitHub Checks API
23
+ statuses: ['legacy-ci'], # Required status contexts on GitHub Status API
24
+
25
+ client: {
26
+ access_token: 'your-access-token', # or environment variable $OCTOKIT_ACCESS_TOKEN
27
+ # login: 'github-login',
28
+ # password: 'github-password',
29
+ # api_endpoint: 'https://githubenterprise/api/v3/',
30
+ # web_endpoint: 'https://githubenterprise/',
31
+
32
+ # Or authenticating as a GitHub app
33
+ # github_app: { private_key: "... PEM ...", app_id: 123456 },
34
+ }
35
+ },
36
+ ],
37
+ }
38
+ ```
39
+
40
+ ```
41
+ hako deploy --tag github your-app.jsonnet
42
+ ```
43
+
44
+
45
+ ## Development
46
+
47
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
48
+
49
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
50
+
51
+ ## Contributing
52
+
53
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/hako-github_status.
54
+
55
+
56
+ ## License
57
+
58
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "hako/github_status"
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,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,29 @@
1
+ require_relative 'lib/hako-github_status/version'
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "hako-github_status"
5
+ spec.version = HakoGithubStatus::VERSION
6
+ spec.authors = ["Sorah Fukumori"]
7
+ spec.email = ["her@sorah.jp"]
8
+
9
+ spec.summary = %q{Hako Script to update app image tag with GitHub commit status}
10
+ spec.homepage = "https://github.com/sorah/hako-github_status"
11
+ spec.license = "MIT"
12
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
13
+
14
+ spec.metadata["homepage_uri"] = spec.homepage
15
+ spec.metadata["source_code_uri"] = spec.homepage
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency "hako"
27
+ spec.add_dependency "octokit"
28
+ spec.add_dependency "jwt"
29
+ end
@@ -0,0 +1,3 @@
1
+ module HakoGithubStatus
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,136 @@
1
+ require 'hako/script'
2
+ require 'octokit'
3
+
4
+ module Hako
5
+ module Scripts
6
+ class GithubStatusTag < Script
7
+ class NoSuccessfulBuildError < StandardError; end
8
+
9
+ TARGET_TAG = 'github'
10
+
11
+ GITHUB_APP_MEDIA = 'application/vnd.github.machine-man-preview+json'
12
+ GITHUB_CHECKS_MEDIA = 'application/vnd.github.antiope-preview+json'
13
+
14
+ def configure(options)
15
+ super
16
+ @repo = options.fetch('repo')
17
+ @ref = options.fetch('ref', 'master')
18
+ @checks = options.fetch('checks', [])
19
+ @statuses = options.fetch('statuses ', [])
20
+ @client_options = options.fetch('client', {})
21
+
22
+ if @checks.empty? && @statuses.empty?
23
+ raise ArgumentError, "at least 1 check or 1 status must be set to github_status_tag script"
24
+ end
25
+ end
26
+
27
+ def deploy_starting(containers)
28
+ app = containers.fetch('app')
29
+ if app.definition['tag'] == TARGET_TAG
30
+ rewrite_tag(app)
31
+ end
32
+ end
33
+
34
+ alias_method :oneshot_starting, :deploy_starting
35
+
36
+ private
37
+
38
+ def rewrite_tag(app)
39
+ tag = fetch_version
40
+ app.definition['tag'] = tag
41
+ Hako.logger.info("Rewrite tag to #{app.image_tag}")
42
+ end
43
+
44
+ def fetch_version
45
+ page = octokit.commits(@repo, sha: @ref)
46
+ num_commits = 0
47
+
48
+ loop do
49
+
50
+ page.each do |commit|
51
+ num_commits += 1
52
+ ok = true
53
+ ok &&= @checks.empty? || (@checks - succeeded_checks_for_commit(commit[:sha])).empty?
54
+ ok &&= @statuses.empty? || (@statuses - succeeded_statuses_for_commit(commit[:sha])).empty?
55
+ return commit[:sha] if ok
56
+ end
57
+
58
+ next_page = octokit.last_response.rels[:next]&.href
59
+ raise NoSuccessfulBuildError unless next_page
60
+ Hako.logger.warn("github_status_tag is still finding a succeeding commit (num_commits=#{num_commits})")
61
+ sleep 1
62
+ page = octokit.get next_page
63
+ end
64
+ end
65
+
66
+ def succeeded_checks_for_commit(sha)
67
+ resp = octokit.check_runs_for_ref(@repo, sha, filter: 'completed', accept: GITHUB_CHECKS_MEDIA)
68
+ resp[:check_runs]
69
+ .select { |_| _[:status] == 'completed' && _[:conclusion] == 'success'}
70
+ .map{ |_| _[:name] }
71
+ end
72
+
73
+ def succeeded_statuses_for_commit(sha)
74
+ resp = octokit.combined_status(@repo, sha)
75
+ resp[:statuses]
76
+ .select{ |_| _[:state] == 'success' }
77
+ .map{ |_| _[:context] }
78
+ end
79
+
80
+ def octokit
81
+ @octokit ||= if @client_options['github_app']
82
+ repo_octokit
83
+ else
84
+ Octokit::Client.new(octokit_options)
85
+ end
86
+ end
87
+
88
+ def repo_octokit
89
+ @octokit ||= Octokit::Client.new(
90
+ octokit_options.merge(
91
+ access_token: github_installation_token,
92
+ )
93
+ )
94
+ end
95
+
96
+ def octokit_options
97
+ {}.tap do |o|
98
+ o[:web_endpoint] = @client_options['web_endpoint'] if @client_options['web_endpoint']
99
+ o[:api_endpoint] = @client_options['api_endpoint'] if @client_options['api_endpoint']
100
+ o[:login] = @client_options['login'] if @client_options['login']
101
+ o[:password] = @client_options['password'] if @client_options['password']
102
+ o[:access_token] = @client_options['access_token'] if @client_options['access_token']
103
+ end
104
+ end
105
+
106
+ def app_octokit
107
+ @app_octokit ||= Octokit::Client.new(
108
+ octokit_options.merge(
109
+ bearer_token: github_jwt,
110
+ )
111
+ )
112
+ end
113
+
114
+ def github_installation_token
115
+ return @github_installation_token if defined? @github_installation_token
116
+
117
+ installation = app_octokit.find_repository_installation(@repo, accept: GITHUB_APP_MEDIA)
118
+ raise "no github app installation found for #{repo.name.inspect}" unless installation
119
+
120
+ issuance = app_octokit.create_app_installation_access_token(installation[:id], accept: GITHUB_APP_MEDIA)
121
+ @github_installation_token = issuance[:token]
122
+ end
123
+
124
+ def github_jwt
125
+ iat = Time.now.to_i
126
+ payload = {
127
+ iss: @client_options['github_app'].fetch('id'),
128
+ iat: iat,
129
+ exp: iat + (3*60),
130
+ }
131
+ JWT.encode(payload, @client_options['github_app'].fetch('id'), 'RS256')
132
+ end
133
+ end
134
+ end
135
+ end
136
+
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: hako-github_status
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sorah Fukumori
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-01-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hako
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: jwt
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
+ - her@sorah.jp
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - Gemfile
66
+ - Gemfile.lock
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - hako-github_status.gemspec
73
+ - lib/hako-github_status/version.rb
74
+ - lib/hako/scripts/github_status_tag.rb
75
+ homepage: https://github.com/sorah/hako-github_status
76
+ licenses:
77
+ - MIT
78
+ metadata:
79
+ homepage_uri: https://github.com/sorah/hako-github_status
80
+ source_code_uri: https://github.com/sorah/hako-github_status
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: 2.3.0
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.1.2
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: Hako Script to update app image tag with GitHub commit status
100
+ test_files: []