ci_toolkit 1.0.19

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: 764a0232eaf62477c0c732c391473bba9f94f6e5300917e7f10d519cee6270c5
4
+ data.tar.gz: 48ec6e86b5a71d4b786c87fe20deb753441271a7746f05b3de9a4a3a1580aa6d
5
+ SHA512:
6
+ metadata.gz: 6ffe7c0fab3c34e5ba50b8da27e14ecff1b546549b8797a1eaa28ebd747937ef92c85819737e2aee36af36026b4cffb75e58c590f4c0ebbe0b55d98734023abd
7
+ data.tar.gz: f80b2a1b5cf8e5d4f611b31aace6811a8be1b956df8d7efaa3a3c5300230b2dd492c5c0c5e1b563fd354461f53e96532c8243ab1b1895a0298ccbe2287839bdf
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --require spec_helper
3
+ --format documentation
data/.rubocop.yml ADDED
@@ -0,0 +1,19 @@
1
+ require: rubocop-rspec
2
+
3
+ AllCops:
4
+ NewCops: enable
5
+ TargetRubyVersion: 2.6
6
+
7
+ Style/StringLiterals:
8
+ Enabled: true
9
+ EnforcedStyle: double_quotes
10
+
11
+ Style/StringLiteralsInInterpolation:
12
+ Enabled: true
13
+ EnforcedStyle: double_quotes
14
+
15
+ Layout/LineLength:
16
+ Max: 120
17
+
18
+ Metrics/BlockLength:
19
+ IgnoredMethods: ['describe']
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2021-09-21
4
+
5
+ - Initial release
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at gero.f.keller@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ci_toolkit.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
data/Gemfile.lock ADDED
@@ -0,0 +1,112 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ ci_toolkit (1.0.19)
5
+ jwt
6
+ octokit
7
+ openssl
8
+ time
9
+
10
+ GEM
11
+ remote: https://rubygems.org/
12
+ specs:
13
+ addressable (2.8.0)
14
+ public_suffix (>= 2.0.2, < 5.0)
15
+ ast (2.4.2)
16
+ diff-lcs (1.4.4)
17
+ docile (1.4.0)
18
+ faraday (1.8.0)
19
+ faraday-em_http (~> 1.0)
20
+ faraday-em_synchrony (~> 1.0)
21
+ faraday-excon (~> 1.1)
22
+ faraday-httpclient (~> 1.0.1)
23
+ faraday-net_http (~> 1.0)
24
+ faraday-net_http_persistent (~> 1.1)
25
+ faraday-patron (~> 1.0)
26
+ faraday-rack (~> 1.0)
27
+ multipart-post (>= 1.2, < 3)
28
+ ruby2_keywords (>= 0.0.4)
29
+ faraday-em_http (1.0.0)
30
+ faraday-em_synchrony (1.0.0)
31
+ faraday-excon (1.1.0)
32
+ faraday-httpclient (1.0.1)
33
+ faraday-net_http (1.0.1)
34
+ faraday-net_http_persistent (1.2.0)
35
+ faraday-patron (1.0.0)
36
+ faraday-rack (1.0.0)
37
+ json (2.5.1)
38
+ jwt (2.2.3)
39
+ multipart-post (2.1.1)
40
+ octokit (4.21.0)
41
+ faraday (>= 0.9)
42
+ sawyer (~> 0.8.0, >= 0.5.3)
43
+ openssl (2.2.0)
44
+ parallel (1.21.0)
45
+ parser (3.0.2.0)
46
+ ast (~> 2.4.1)
47
+ public_suffix (4.0.6)
48
+ rainbow (3.0.0)
49
+ rake (13.0.6)
50
+ regexp_parser (2.1.1)
51
+ rexml (3.2.5)
52
+ rspec (3.10.0)
53
+ rspec-core (~> 3.10.0)
54
+ rspec-expectations (~> 3.10.0)
55
+ rspec-mocks (~> 3.10.0)
56
+ rspec-core (3.10.1)
57
+ rspec-support (~> 3.10.0)
58
+ rspec-expectations (3.10.1)
59
+ diff-lcs (>= 1.2.0, < 2.0)
60
+ rspec-support (~> 3.10.0)
61
+ rspec-mocks (3.10.2)
62
+ diff-lcs (>= 1.2.0, < 2.0)
63
+ rspec-support (~> 3.10.0)
64
+ rspec-support (3.10.2)
65
+ rubocop (1.21.0)
66
+ parallel (~> 1.10)
67
+ parser (>= 3.0.0.0)
68
+ rainbow (>= 2.2.2, < 4.0)
69
+ regexp_parser (>= 1.8, < 3.0)
70
+ rexml
71
+ rubocop-ast (>= 1.9.1, < 2.0)
72
+ ruby-progressbar (~> 1.7)
73
+ unicode-display_width (>= 1.4.0, < 3.0)
74
+ rubocop-ast (1.11.0)
75
+ parser (>= 3.0.1.1)
76
+ rubocop-rake (0.6.0)
77
+ rubocop (~> 1.0)
78
+ rubocop-rspec (2.5.0)
79
+ rubocop (~> 1.19)
80
+ ruby-progressbar (1.11.0)
81
+ ruby2_keywords (0.0.5)
82
+ sawyer (0.8.2)
83
+ addressable (>= 2.3.5)
84
+ faraday (> 0.8, < 2.0)
85
+ simplecov (0.21.2)
86
+ docile (~> 1.1)
87
+ simplecov-html (~> 0.11)
88
+ simplecov_json_formatter (~> 0.1)
89
+ simplecov-html (0.12.3)
90
+ simplecov-json (0.2.3)
91
+ json
92
+ simplecov
93
+ simplecov_json_formatter (0.1.3)
94
+ time (0.1.0)
95
+ unicode-display_width (2.1.0)
96
+
97
+ PLATFORMS
98
+ universal-darwin-20
99
+ x86_64-linux
100
+
101
+ DEPENDENCIES
102
+ ci_toolkit!
103
+ rake (~> 13.0)
104
+ rspec (~> 3.2)
105
+ rubocop (~> 1.7)
106
+ rubocop-rake
107
+ rubocop-rspec
108
+ simplecov
109
+ simplecov-json
110
+
111
+ BUNDLED WITH
112
+ 2.2.27
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Gero Keller
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,40 @@
1
+ # CiToolkit [![Lint & run tests](https://github.com/crvshlab/ci_toolkit/actions/workflows/lint_and_run_tests.yml/badge.svg)](https://github.com/crvshlab/ci_toolkit/actions/workflows/lint_and_run_tests.yml) [![Quality Gate Status](https://sonarcloud.io/api/project_badges/measure?project=crvshlab_ci_toolkit&metric=alert_status&token=f10d3b754be5144c5acced94b8f2fe8705045db7)](https://sonarcloud.io/dashboard?id=crvshlab_ci_toolkit) [![Coverage](https://sonarcloud.io/api/project_badges/measure?project=crvshlab_ci_toolkit&metric=coverage&token=f10d3b754be5144c5acced94b8f2fe8705045db7)](https://sonarcloud.io/dashboard?id=crvshlab_ci_toolkit)
2
+
3
+ A collection of utility classes used to glue information between Github and CI (Bitrise primarily)
4
+ ## Installation
5
+
6
+ Add this line to your application's Gemfile:
7
+
8
+ ```ruby
9
+ gem 'ci_toolkit'
10
+ ```
11
+
12
+ And then execute:
13
+
14
+ $ bundle install
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install ci_toolkit
19
+
20
+ ## Usage
21
+
22
+ TODO: Write usage instructions here
23
+
24
+ ## Development
25
+
26
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec/test/test-unit` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
27
+
28
+ 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 the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
29
+
30
+ ## Contributing
31
+
32
+ Bug reports and pull requests are welcome on GitHub at https://github.com/crvshlab/ci_toolkit. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/ci_toolkit/blob/master/CODE_OF_CONDUCT.md).
33
+
34
+ ## License
35
+
36
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
37
+
38
+ ## Code of Conduct
39
+
40
+ Everyone interacting in the CiToolkit project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/ci_toolkit/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rubocop/rake_task"
5
+
6
+ RuboCop::RakeTask.new
7
+
8
+ task default: :rubocop
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ci_toolkit"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -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 @@
1
+ lib/whitelisted.txt
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CiToolkit
4
+ # Bitrise constants
5
+ class BitriseEnv
6
+ attr_reader :build_number, :build_url,
7
+ :pull_request_number,
8
+ :app_url,
9
+ :git_branch
10
+
11
+ def initialize(options = {
12
+ build_number: ENV["BITRISE_BUILD_NUMBER"],
13
+ build_url: ENV["BITRISE_BUILD_URL"],
14
+ pull_request_number: ENV["BITRISE_PULL_REQUEST"],
15
+ build_from_cron_job: !ENV["BITRISE_SCHEDULED_BUILD"].nil?,
16
+ repository_owner: ENV["BITRISEIO_GIT_REPOSITORY_OWNER"] || "crvshlab",
17
+ repository_slug: ENV["BITRISEIO_GIT_REPOSITORY_SLUG"],
18
+ app_url: ENV["BITRISE_APP_URL"],
19
+ git_branch: ENV["BITRISE_GIT_BRANCH"]
20
+ })
21
+ @build_number = options[:build_number]
22
+ @build_url = options[:build_url]
23
+ @pull_request_number = options[:pull_request_number]
24
+ @build_from_cron_job = options[:build_from_cron_job]
25
+ @repository_owner = options[:repository_owner]
26
+ @repository_slug = options[:repository_slug]
27
+ @app_url = options[:app_url]
28
+ @git_branch = options[:git_branch]
29
+ end
30
+
31
+ def build_from_pr?
32
+ !pull_request_number.nil?
33
+ end
34
+
35
+ def build_from_cron_job?
36
+ @build_from_cron_job
37
+ end
38
+
39
+ def repository_path
40
+ "#{@repository_owner}/#{@repository_slug}"
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CiToolkit
4
+ # Provides utility for a build
5
+ class Build
6
+ def initialize(
7
+ git = CiToolkit::Git.new,
8
+ env = CiToolkit::BitriseEnv.new
9
+ )
10
+ @git = git
11
+ @env = env
12
+ end
13
+
14
+ def url
15
+ @env.build_url
16
+ end
17
+
18
+ def number
19
+ @env.build_number || Time.now.to_i.to_s
20
+ end
21
+
22
+ def from_pull_request?
23
+ @env.build_from_pr?
24
+ end
25
+
26
+ def from_develop?
27
+ !!(@git.branch =~ /^develop$/)
28
+ end
29
+
30
+ def from_release?
31
+ !!(@git.branch =~ %r{^release/.*})
32
+ end
33
+
34
+ def from_master?
35
+ !!(@git.branch =~ /^master$/)
36
+ end
37
+
38
+ def from_cron_job?
39
+ @env.build_from_cron_job?
40
+ end
41
+
42
+ def version
43
+ version = @git.branch.split("/").last
44
+ return version if !version.nil? && Gem::Version.correct?(version)
45
+
46
+ return @git.latest_tag if Gem::Version.correct?(@git.latest_tag)
47
+
48
+ raise StandardError, "Incorrect version supplied. You need to build from a valid \
49
+ release branch with semantic versioning, eg. release/x.y.z"
50
+ end
51
+
52
+ def tag_name(build_number = nil)
53
+ return "#{version}-build.#{build_number}" if build_number
54
+
55
+ version.to_s
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "pathname"
4
+
5
+ module CiToolkit
6
+ # Finds duplicate files based on md5 hash
7
+ class DuplicateFilesFinder
8
+ def initialize(
9
+ relative_search_path = nil,
10
+ whitelist_file = nil,
11
+ exclusion_patterns = []
12
+ )
13
+ @base_dir = select_base_dir(relative_search_path)
14
+ @whitelisted_files = create_whitelist_from_file(whitelist_file) || []
15
+ @exclusion_patterns = exclusion_patterns || []
16
+ @duplicated_files = []
17
+ duplicate_map = files_mapped_to_md5_checksum
18
+ duplicate_map.each { |md5, duplicates| @duplicated_files << duplicates if duplicate_map[md5].count > 1 }
19
+ end
20
+
21
+ def duplicate_groups
22
+ @duplicated_files
23
+ end
24
+
25
+ private
26
+
27
+ def files_mapped_to_md5_checksum
28
+ md5_file_map = {}
29
+ all_files_in_project.each do |file|
30
+ md5 = Digest::MD5.hexdigest(File.read(file))
31
+ files = md5_file_map[md5] || (md5_file_map[md5] = [])
32
+ relative_path = Pathname.new(file).relative_path_from(Pathname.new(@base_dir)).to_s
33
+ files << relative_path unless @whitelisted_files.include?(relative_path)
34
+ end
35
+ md5_file_map
36
+ end
37
+
38
+ def all_files_in_project
39
+ files = Dir.glob("#{@base_dir}/**/*")
40
+ files.reject! do |f|
41
+ !File.exist?(f) || File.symlink?(f) || File.directory?(f) || File.size?(f).nil?
42
+ end
43
+ @exclusion_patterns.each do |pattern|
44
+ files.reject! { |f| f[/#{pattern}/] }
45
+ end
46
+ files
47
+ end
48
+
49
+ def select_base_dir(relative_search_path)
50
+ if !relative_search_path.nil? && Dir.exist?(relative_search_path)
51
+ File.expand_path(relative_search_path)
52
+ else
53
+ File.expand_path(Dir.pwd)
54
+ end
55
+ end
56
+
57
+ def create_whitelist_from_file(whitelist_file)
58
+ if !whitelist_file.nil? && File.exist?("#{@base_dir}/#{whitelist_file}")
59
+ File.readlines("#{@base_dir}/#{whitelist_file}", chomp: true)
60
+ elsif File.exist? "#{@base_dir}/duplicate_files_whitelist.txt"
61
+ File.readlines("#{@base_dir}/duplicate_files_whitelist.txt", chomp: true)
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CiToolkit
4
+ # Utility class to provide information about git related data
5
+ class Git
6
+ def initialize(dir = nil, env = CiToolkit::BitriseEnv.new)
7
+ @branch = env.git_branch
8
+ @dir = dir
9
+ end
10
+
11
+ def latest_tag
12
+ describe = "git describe --abbrev=0"
13
+ return `#{describe}`.gsub("\n", "") unless @dir
14
+
15
+ `cd #{@dir} && #{describe}`.gsub("\n", "")
16
+ end
17
+
18
+ def branch
19
+ return @branch unless @branch.nil?
20
+
21
+ git_branch_cmd = "git branch --show-current"
22
+ return `cd #{@dir} && #{git_branch_cmd}`.gsub(/\s+/, "") unless @dir.nil?
23
+
24
+ `#{git_branch_cmd}`.gsub(/\s+/, "")
25
+ end
26
+
27
+ def infrastructure_branch?
28
+ !(branch =~ %r{infra/}).nil?
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "octokit"
4
+ require "jwt"
5
+
6
+ module CiToolkit
7
+ # Utility class that provides an access token that can be used with the Github API
8
+ class GithubAccess
9
+ # stack = Faraday::RackBuilder.new do |builder|
10
+ # builder.response :logger
11
+ # builder.use Octokit::Response::RaiseError
12
+ # builder.adapter Faraday.default_adapter
13
+ # end
14
+ # Octokit.middleware = stack
15
+
16
+ def initialize(app_id = ENV["CRVSH_BOT_GITHUB_APP_ID"], private_key = ENV["CRVSH_BOT_GITHUB_APP_PRIVATE_KEY"])
17
+ @app_id = app_id.to_i
18
+ @private_key = private_key
19
+ @client = Octokit::Client.new(bearer_token: jwt_token, auto_paginate: true)
20
+ end
21
+
22
+ def create_token
23
+ return unless (installation_id = find_app_installation)
24
+
25
+ @client.create_app_installation_access_token(
26
+ installation_id,
27
+ { accept: Octokit::Preview::PREVIEW_TYPES[:integrations] }
28
+ )[:token]
29
+ end
30
+
31
+ private
32
+
33
+ def find_app_installation
34
+ @client.find_app_installations(
35
+ { accept: Octokit::Preview::PREVIEW_TYPES[:integrations] }
36
+ ).select { |installation| installation[:app_id] == @app_id }.first[:id]
37
+ end
38
+
39
+ def jwt_token
40
+ JWT.encode(
41
+ {
42
+ iat: Time.now.to_i,
43
+ exp: Time.now.to_i + (9 * 60),
44
+ iss: @app_id
45
+ },
46
+ OpenSSL::PKey::RSA.new(@private_key),
47
+ "RS256"
48
+ )
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "octokit"
4
+
5
+ module CiToolkit
6
+ # Can be used to retrieve information about a PR on Github via the Github API
7
+ class GithubPr
8
+ def initialize(
9
+ env = CiToolkit::BitriseEnv.new,
10
+ build_types = ENV["BUILD_TYPES"]&.split(/,/) || ["BluetoothDemo", "Acceptance PreProd", "Acceptance Prod",
11
+ "Latest Prod", "Latest PreProd", "Mock"],
12
+ client = Octokit::Client.new
13
+ )
14
+ @pr_number = env.pull_request_number
15
+ @repo_slug = env.repository_path
16
+ @_client = client
17
+ @build_types = build_types
18
+ @access = CiToolkit::GithubAccess.new
19
+ end
20
+
21
+ def title
22
+ client.pull_request(@repo_slug, @pr_number).[](:title)
23
+ end
24
+
25
+ def number
26
+ @pr_number
27
+ end
28
+
29
+ def lines_of_code_changed
30
+ pr = client.pull_request(@repo_slug, @pr_number)
31
+ pr[:additions] + pr[:deletions]
32
+ end
33
+
34
+ def comments
35
+ client.issue_comments(@repo_slug, @pr_number).map { |item| item[:body] }
36
+ end
37
+
38
+ def comment(text)
39
+ client.add_comment(@repo_slug, @pr_number, text[0...65_500]) # github comment character limit is 65536
40
+ end
41
+
42
+ def delete_comment_containing_text(text)
43
+ comment = find_comment_containing_text(text)
44
+ delete_comment(comment[:id]) unless comment.nil?
45
+ end
46
+
47
+ def delete_comment(comment_id)
48
+ client.delete_comment(@repo_slug, comment_id)
49
+ end
50
+
51
+ def find_comment_containing_text(text)
52
+ comment = nil
53
+ client.issue_comments(@repo_slug, @pr_number).map do |item|
54
+ comment = item if item[:body]&.include? text
55
+ end
56
+ comment
57
+ end
58
+
59
+ def labels
60
+ client.labels_for_issue(@repo_slug, @pr_number).map { |item| item[:name] }
61
+ end
62
+
63
+ def create_status(state, context, target_url, description)
64
+ ref = client.pull_request(@repo_slug, @pr_number).[](:head).[](:sha)
65
+ client.create_status(
66
+ @repo_slug,
67
+ ref,
68
+ state,
69
+ { context: context, target_url: target_url, description: description }
70
+ )
71
+ end
72
+
73
+ def build_types
74
+ types = []
75
+ @build_types.each do |type|
76
+ types.push(type) if comments.include?("#{type} build") || labels.include?("#{type} build")
77
+ end
78
+ types
79
+ end
80
+
81
+ def infrastructure_work?
82
+ !(title =~ /\[infra\]/i).nil? || labels.include?("Infra")
83
+ end
84
+
85
+ def work_in_progress?
86
+ title.include?("[WIP]") || labels.include?("WIP")
87
+ end
88
+
89
+ def big?
90
+ lines_of_code_changed > 500
91
+ end
92
+
93
+ private
94
+
95
+ def client
96
+ @_client = Octokit::Client.new if @_client.nil?
97
+ @_client.access_token = @access.create_token if @_client.access_token.nil?
98
+
99
+ @_client
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CiToolkit
4
+ # Utility class to parse a Jira ticket from a string
5
+ class Jira
6
+ attr_reader :ticket
7
+
8
+ def initialize(
9
+ github_pr = CiToolkit::GithubPr.new,
10
+ git = CiToolkit::Git.new,
11
+ ticket_regex_keys = ENV["SUPPORTED_JIRA_PROJECT_KEYS_REGEX"]
12
+ )
13
+ @ticket_regex_keys = ticket_regex_keys
14
+ @ticket = parse_ticket(github_pr.nil? ? "" : github_pr.title) || parse_ticket(git.nil? ? "" : git.branch)
15
+ end
16
+
17
+ private
18
+
19
+ def parse_ticket(string)
20
+ matches = string.match(ticket_regex)
21
+ key = matches&.[](:project_key)
22
+ number = matches&.[](:ticket_number)
23
+ "#{key}-#{number}" unless key.nil? || key.empty? || number.nil? || number.empty?
24
+ end
25
+
26
+ def ticket_regex
27
+ /.*?(?<project_key>#{@ticket_regex_keys})[- ]?(?<ticket_number>[0-9]+)/i
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CiToolkit
4
+ # Sends messages to a Github PR for predefined build events
5
+ class PrMessenger
6
+ def initialize(
7
+ github_pr = CiToolkit::GithubPr.new,
8
+ messenger_text = CiToolkit::PrMessengerText.new
9
+ )
10
+ @github_pr = github_pr
11
+ @messenger_text = messenger_text
12
+ @is_connected = !@github_pr.nil?
13
+ end
14
+
15
+ def send_build_deployed(name, version_name, tag)
16
+ send(@messenger_text.for_new_build(name, version_name, tag))
17
+ end
18
+
19
+ def send_ci_failed(reason)
20
+ send(@messenger_text.for_build_failure(reason))
21
+ end
22
+
23
+ def send_duplicate_files_report(finder)
24
+ delete_duplicate_files_report
25
+ report = @messenger_text.create_duplicate_files_report(finder)
26
+ send(@messenger_text.for_duplicated_files_report(report))
27
+ report
28
+ end
29
+
30
+ def delete_duplicate_files_report
31
+ delete(@messenger_text.duplicated_files_title)
32
+ end
33
+
34
+ def send_lint_report(report)
35
+ delete_lint_report
36
+ send(@messenger_text.for_lint_report(report))
37
+ report
38
+ end
39
+
40
+ def delete_lint_report
41
+ delete(@messenger_text.lint_report_title)
42
+ end
43
+
44
+ def send_big_pr_warning
45
+ delete_big_pr_warning
46
+ send(@messenger_text.big_pr_warning_title)
47
+ @messenger_text.big_pr_warning_title
48
+ end
49
+
50
+ def delete_big_pr_warning
51
+ delete(@messenger_text.big_pr_warning_title)
52
+ end
53
+
54
+ def send_work_in_progress
55
+ delete_work_in_progress
56
+ send(@messenger_text.work_in_progress_title)
57
+ @messenger_text.work_in_progress_title
58
+ end
59
+
60
+ def delete_work_in_progress
61
+ delete(@messenger_text.work_in_progress_title)
62
+ end
63
+
64
+ private
65
+
66
+ def send(message)
67
+ return unless @is_connected
68
+
69
+ @github_pr.comment("#{message}\n#{@messenger_text.footer}")
70
+ end
71
+
72
+ def delete(message)
73
+ return unless @is_connected
74
+
75
+ @github_pr.delete_comment_containing_text(message)
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CiToolkit
4
+ # Utilities to create markdown text from build messages
5
+ class PrMessengerText
6
+ def initialize(
7
+ build = CiToolkit::Build.new,
8
+ env = CiToolkit::BitriseEnv.new
9
+ )
10
+ @build = build
11
+ @app_url = env.app_url
12
+ end
13
+
14
+ def for_new_build(name, version_name, tag)
15
+ "#### New `#{name}` build deployed 🚀\nVersion **#{version_name}** with
16
+ build number **#{@build.number}** on tag **#{tag}** deployed
17
+ from [this](#{@build.url}) build"
18
+ end
19
+
20
+ def for_build_failure(reason)
21
+ "#### Build failed ⛔️\n#{body(reason.to_s)}"
22
+ end
23
+
24
+ def for_duplicated_files_report(report)
25
+ "#{duplicated_files_title}\n#{body(report)}"
26
+ end
27
+
28
+ def for_lint_report(report)
29
+ "#{lint_report_title}\n#{body(report)}"
30
+ end
31
+
32
+ def duplicated_files_title
33
+ warning_with_message("There are duplicated files found")
34
+ end
35
+
36
+ def lint_report_title
37
+ "#### Swiftlint report 🕵️‍♀️"
38
+ end
39
+
40
+ def big_pr_warning_title
41
+ warning_with_message("Big PR")
42
+ end
43
+
44
+ def work_in_progress_title
45
+ "PR is Work in Progress 🚧"
46
+ end
47
+
48
+ def warning_with_message(message)
49
+ "Warning: #{message} ⚠️"
50
+ end
51
+
52
+ def create_duplicate_files_report(finder)
53
+ report = ""
54
+ finder.duplicate_groups.each do |dups|
55
+ report = "#{report}\n#{dups.join("\n")}\n"
56
+ end
57
+ report
58
+ end
59
+
60
+ def footer
61
+ "###### 🔮 _Comment via [CI](#{@app_url}) on build [#{@build.number}](#{@build.url})_"
62
+ end
63
+
64
+ def body(text)
65
+ formatted_text = "```\n#{text.to_s[0...60_000]}\n```"
66
+ formatted_text = "<details>\n<summary>Details</summary>\n\n#{formatted_text}\n</details>" if text.lines.count > 6
67
+ "#{formatted_text}\n"
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CiToolkit
4
+ VERSION = "1.0.19"
5
+ end
data/lib/ci_toolkit.rb ADDED
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "ci_toolkit/version"
4
+
5
+ require "ci_toolkit/bitrise_env"
6
+ require "ci_toolkit/build"
7
+ require "ci_toolkit/duplicate_files_finder"
8
+ require "ci_toolkit/github_access"
9
+ require "ci_toolkit/github_pr"
10
+ require "ci_toolkit/git"
11
+ require "ci_toolkit/jira"
12
+ require "ci_toolkit/pr_messenger"
13
+ require "ci_toolkit/pr_messenger_text"
14
+
15
+ module CiToolkit
16
+ class Error < StandardError; end
17
+ # Your code goes here...
18
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require "pathname"
5
+
6
+ # The coverage data in resultset.json from SimpleCov contains absolute file paths
7
+ # As that can confuse the sonarcloud sonar-scanner if it's running in a Docker for example
8
+ # In the block below we are transforming the absolute file paths to relative paths
9
+ puts "Transforming coverage/.resultset.json to sonar-scanner valid format in coverage/.resultset.sonar.json"
10
+ json = JSON.parse(File.read("coverage/.resultset.json"))
11
+ puts "Fixing file paths…"
12
+ json["RSpec"]["coverage"].transform_keys! do |file|
13
+ path = Pathname.new(file).relative_path_from(Pathname.new(File.expand_path("."))).to_s
14
+ path.slice! ".."
15
+ path if ENV["CI"].nil?
16
+
17
+ "/github/workspace/#{path}"
18
+ end
19
+ puts "Fixing json line information…"
20
+ json["RSpec"]["coverage"].transform_values! do |value|
21
+ value["lines"]
22
+ end
23
+ File.write("coverage/.resultset.sonar.json", JSON.dump(json))
24
+ puts "Wrote new coverage json to coverage/.resultset.sonar.json"
metadata ADDED
@@ -0,0 +1,212 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ci_toolkit
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.19
5
+ platform: ruby
6
+ authors:
7
+ - Gero Keller
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-09-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: jwt
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: openssl
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
+ - !ruby/object:Gem::Dependency
56
+ name: time
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.2'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.2'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.7'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.7'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: simplecov
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: simplecov-json
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ description: Set of tools making it easier to interact between Github PRs and Bitrise
154
+ CI
155
+ email:
156
+ - gero.f.keller@gmail.com
157
+ executables: []
158
+ extensions: []
159
+ extra_rdoc_files: []
160
+ files:
161
+ - ".rspec"
162
+ - ".rubocop.yml"
163
+ - CHANGELOG.md
164
+ - CODE_OF_CONDUCT.md
165
+ - Gemfile
166
+ - Gemfile.lock
167
+ - LICENSE.txt
168
+ - README.md
169
+ - Rakefile
170
+ - bin/console
171
+ - bin/setup
172
+ - duplicate_files_whitelist.txt
173
+ - lib/ci_toolkit.rb
174
+ - lib/ci_toolkit/bitrise_env.rb
175
+ - lib/ci_toolkit/build.rb
176
+ - lib/ci_toolkit/duplicate_files_finder.rb
177
+ - lib/ci_toolkit/git.rb
178
+ - lib/ci_toolkit/github_access.rb
179
+ - lib/ci_toolkit/github_pr.rb
180
+ - lib/ci_toolkit/jira.rb
181
+ - lib/ci_toolkit/pr_messenger.rb
182
+ - lib/ci_toolkit/pr_messenger_text.rb
183
+ - lib/ci_toolkit/version.rb
184
+ - transform_coverage_data.rb
185
+ homepage: https://github.com/crvshlab/ci_toolkit
186
+ licenses:
187
+ - MIT
188
+ metadata:
189
+ allowed_push_host: https://rubygems.org
190
+ homepage_uri: https://github.com/crvshlab/ci_toolkit
191
+ source_code_uri: https://github.com/crvshlab/ci_toolkit
192
+ changelog_uri: https://github.com/crvshlab/ci_toolkit/CHANGELOG.md
193
+ post_install_message:
194
+ rdoc_options: []
195
+ require_paths:
196
+ - lib
197
+ required_ruby_version: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: 2.6.0
202
+ required_rubygems_version: !ruby/object:Gem::Requirement
203
+ requirements:
204
+ - - ">="
205
+ - !ruby/object:Gem::Version
206
+ version: '0'
207
+ requirements: []
208
+ rubygems_version: 3.0.3
209
+ signing_key:
210
+ specification_version: 4
211
+ summary: Set of CI utilities
212
+ test_files: []