ci_toolkit 1.4.12 → 1.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: cf57d831af4f6cf35645bf6badfa46e4e5c4a248b71073e913c7b00d916d9e29
4
- data.tar.gz: '0178662a1ea410b9dd1b653a5da8c7afd9230825e51b3f8da3f57e857d925907'
3
+ metadata.gz: a76550025b344c5539c79e2002b47c2e622ce90e1f1dd4dcf9710e148a41174f
4
+ data.tar.gz: 182d9df1e68adb7f825ba57a1b3ad61e39d42e1b0a42f2011302f0559113e0b9
5
5
  SHA512:
6
- metadata.gz: 37b8173921e346aaeb9cf254ed372d239abd5a23b53f0357139ad85876c3f5d9f832d60cde27046a343302e3d1eca766c8a819580af8c2c5cc1235c9d8a68716
7
- data.tar.gz: 2abe3d539e171cafdea9fce063300ef331e7be7c96fa50c56b437d51588019a8c30ece24c5bb5b4236176814e81481317c9adbf291f9cac18d115c522c5557fb
6
+ metadata.gz: bca4a5115ea7fcfbf351d961cd5390c8235068306d1480337d06b6a6177cd6617d4f9132976e980789ef853531ba9d67ba61cc876f2895fdc041796ed105affc
7
+ data.tar.gz: 333c32e5bd377d2947f3c705bd8ffb96af4d7a5c5720282eaf6264e5211342fb52e0851dfe5e0fe8bed6e9b341269f5e5509c264b06e1285f635673b5bad7ca3
data/Gemfile.lock CHANGED
@@ -1,9 +1,10 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ci_toolkit (1.4.12)
4
+ ci_toolkit (1.5.1)
5
5
  faraday
6
6
  faraday_middleware
7
+ gitlab
7
8
  jwt
8
9
  octokit
9
10
  openssl
@@ -15,7 +16,7 @@ GEM
15
16
  addressable (2.8.0)
16
17
  public_suffix (>= 2.0.2, < 5.0)
17
18
  ast (2.4.2)
18
- date (3.2.0)
19
+ date (3.2.1)
19
20
  diff-lcs (1.4.4)
20
21
  docile (1.4.0)
21
22
  faraday (1.8.0)
@@ -39,9 +40,19 @@ GEM
39
40
  faraday-rack (1.0.0)
40
41
  faraday_middleware (1.2.0)
41
42
  faraday (~> 1.0)
43
+ gitlab (4.18.0)
44
+ httparty (~> 0.18)
45
+ terminal-table (>= 1.5.1)
46
+ httparty (0.20.0)
47
+ mime-types (~> 3.0)
48
+ multi_xml (>= 0.5.2)
42
49
  ipaddr (1.2.3)
43
50
  json (2.5.1)
44
51
  jwt (2.3.0)
52
+ mime-types (3.4.1)
53
+ mime-types-data (~> 3.2015)
54
+ mime-types-data (3.2022.0105)
55
+ multi_xml (0.6.0)
45
56
  multipart-post (2.1.1)
46
57
  octokit (4.21.0)
47
58
  faraday (>= 0.9)
@@ -98,6 +109,8 @@ GEM
98
109
  json
99
110
  simplecov
100
111
  simplecov_json_formatter (0.1.3)
112
+ terminal-table (3.0.2)
113
+ unicode-display_width (>= 1.1.1, < 3)
101
114
  time (0.2.0)
102
115
  date
103
116
  unicode-display_width (2.1.0)
@@ -118,4 +131,4 @@ DEPENDENCIES
118
131
  simplecov-json
119
132
 
120
133
  BUNDLED WITH
121
- 2.2.30
134
+ 2.3.16
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # CiToolkit [![Lint & run tests](https://github.com/crvshlab/ci_toolkit/actions/workflows/lint_test_and_release.yml/badge.svg)](https://github.com/crvshlab/ci_toolkit/actions/workflows/lint_and_run_tests.yml) [![CodeQL](https://github.com/crvshlab/ci_toolkit/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/crvshlab/ci_toolkit/actions/workflows/codeql-analysis.yml.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)
1
+ # CiToolkit [![Lint & run tests](https://github.com/crvshlab/ci_toolkit/actions/workflows/lint_test_and_release.yml/badge.svg)](https://github.com/crvshlab/ci_toolkit/actions/workflows/lint_and_run_tests.yml) [![CodeQL](https://github.com/crvshlab/ci_toolkit/actions/workflows/codeql-analysis.yml/badge.svg)](https://github.com/crvshlab/ci_toolkit/actions/workflows/codeql-analysis.yml.yml)
2
2
 
3
- A collection of utility classes used to glue information between Github and CI (Bitrise primarily)
3
+ A collection of utility classes used to glue information between Github, Gitlab and CI (Bitrise primarily)
4
4
  ## Installation
5
5
 
6
6
  Add this line to your application's Gemfile:
@@ -4,7 +4,11 @@ module CiToolkit
4
4
  # allows to have a combined build status for all builds that are triggered for a pull request
5
5
  # it uses the description of the status check on Github to parse the number of builds remaining and total
6
6
  class BuildStatus
7
- def initialize(context = "Builds", github = CiToolkit::GithubPr.new, env = CiToolkit::BitriseEnv.new)
7
+ def initialize(
8
+ context = "Builds",
9
+ github = CiToolkit::DvcsPrFactory.create(CiToolkit::BitriseEnv.new),
10
+ env = CiToolkit::BitriseEnv.new
11
+ )
8
12
  @context = context
9
13
  @github = github
10
14
  @env = env
@@ -0,0 +1,101 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Credits: https://metabates.com/2011/02/07/building-interfaces-and-abstract-classes-in-ruby/
4
+ module AbstractInterface
5
+ # Use this if you want to enforce some form of contract for an implementing class
6
+ class InterfaceNotImplementedError < NoMethodError
7
+ end
8
+
9
+ def self.included(klass)
10
+ klass.send(:include, AbstractInterface::Methods)
11
+ klass.send(:extend, AbstractInterface::Methods)
12
+ end
13
+
14
+ # Use this to enforce that implementing class do implemented the needed contract
15
+ module Methods
16
+ def api_not_implemented(klass, method_name = nil)
17
+ if method_name.nil?
18
+ caller.first.match(/in `(.+)'/)
19
+ method_name = Regexp.last_match(1)
20
+ end
21
+ raise AbstractInterface::InterfaceNotImplementedError,
22
+ "#{klass.class.name} needs to implement '#{method_name}' for interface #{name}!"
23
+ end
24
+ end
25
+ end
26
+
27
+ module CiToolkit
28
+ # This needs to be implemented if you want to implement a distribution version control system be used by
29
+ # ci_toolkit. Similar to GithubPr and GitlabPr classes
30
+ class DvcsPr
31
+ include AbstractInterface
32
+
33
+ def title
34
+ CiToolkit::DvcsPr.api_not_implemented(self)
35
+ end
36
+
37
+ def number
38
+ CiToolkit::DvcsPr.api_not_implemented(self)
39
+ end
40
+
41
+ def lines_of_code_changed
42
+ CiToolkit::DvcsPr.api_not_implemented(self)
43
+ end
44
+
45
+ def comments
46
+ CiToolkit::DvcsPr.api_not_implemented(self)
47
+ end
48
+
49
+ def comment(_text)
50
+ CiToolkit::DvcsPr.api_not_implemented(self)
51
+ end
52
+
53
+ def delete_comments_including_text(_text)
54
+ CiToolkit::DvcsPr.api_not_implemented(self)
55
+ end
56
+
57
+ def delete_comment(_comment_id)
58
+ CiToolkit::DvcsPr.api_not_implemented(self)
59
+ end
60
+
61
+ def find_comments_including_text(_text)
62
+ CiToolkit::DvcsPr.api_not_implemented(self)
63
+ end
64
+
65
+ def labels
66
+ CiToolkit::DvcsPr.api_not_implemented(self)
67
+ end
68
+
69
+ def files
70
+ CiToolkit::DvcsPr.api_not_implemented(self)
71
+ end
72
+
73
+ def create_status(_state, _context, _target_url, _description)
74
+ CiToolkit::DvcsPr.api_not_implemented(self)
75
+ end
76
+
77
+ def get_status(_context)
78
+ CiToolkit::DvcsPr.api_not_implemented(self)
79
+ end
80
+
81
+ def build_types
82
+ CiToolkit::DvcsPr.api_not_implemented(self)
83
+ end
84
+
85
+ def infrastructure_work?
86
+ CiToolkit::DvcsPr.api_not_implemented(self)
87
+ end
88
+
89
+ def work_in_progress?
90
+ CiToolkit::DvcsPr.api_not_implemented(self)
91
+ end
92
+
93
+ def big?
94
+ CiToolkit::DvcsPr.api_not_implemented(self)
95
+ end
96
+
97
+ def realm_module_modified?
98
+ CiToolkit::DvcsPr.api_not_implemented(self)
99
+ end
100
+ end
101
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CiToolkit
4
+ # Use this to create an instance of Dvcs implementation based on the service type
5
+ # set in an environment value called DVCS_SERVICW with value of gitlab or github
6
+ class DvcsPrFactory
7
+ SERVICES = {
8
+ "gitlab" => CiToolkit::GitlabPr,
9
+ "github" => CiToolkit::GithubPr
10
+ }.freeze
11
+
12
+ private_constant :SERVICES
13
+
14
+ def self.create(bitrise_env = CiToolkit::BitriseEnv.new)
15
+ service = ENV["DVCS_SERVICE"]
16
+ (SERVICES[service.to_s.downcase] || CiToolkit::DvcsPr).new bitrise_env
17
+ end
18
+ end
19
+ end
@@ -4,13 +4,14 @@ require "octokit"
4
4
 
5
5
  module CiToolkit
6
6
  # Can be used to retrieve information about a PR on Github via the Github API
7
- class GithubPr
7
+ class GithubPr < DvcsPr
8
8
  def initialize(
9
9
  env = CiToolkit::BitriseEnv.new,
10
10
  build_types = ENV["BUILD_TYPES"]&.split(/,/) || ["BluetoothDemo", "Acceptance PreProd", "Acceptance Prod",
11
11
  "Latest Prod", "Latest PreProd", "Mock", "Design System"],
12
12
  client = Octokit::Client.new
13
13
  )
14
+ super()
14
15
  @pr_number = env.pull_request_number
15
16
  @repo_slug = env.repository_path
16
17
  @commit_sha = env.git_commit
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "gitlab"
4
+
5
+ module CiToolkit
6
+ # Can be used to retrieve information about a PR on Github via the Github API
7
+ class GitlabPr < DvcsPr
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", "Design System"],
12
+
13
+ client = Gitlab.client(endpoint: ENV["GITLAB_API_URL"] || "",
14
+ private_token: ENV["GITLAB_USER_PRIVATE_TOKEN"] || "")
15
+ )
16
+ super()
17
+ @pr_number = env.pull_request_number
18
+ @repo_slug = env.repository_path
19
+ @commit_sha = env.git_commit
20
+ @_client = client
21
+ @build_types = build_types
22
+ @bot = CiToolkit::GithubBot.new
23
+ end
24
+
25
+ def title
26
+ client.merge_request(@repo_slug, @pr_number).title || ""
27
+ end
28
+
29
+ def number
30
+ @pr_number
31
+ end
32
+
33
+ def lines_of_code_changed
34
+ pr = client.merge_request(@repo_slug, @pr_number)
35
+ pr.changes_count
36
+ end
37
+
38
+ def comments
39
+ client.merge_request_notes(@repo_slug, @pr_number).map(&:body)
40
+ end
41
+
42
+ def comment(text)
43
+ # github comment character limit is 65536
44
+ client.create_merge_request_note(@repo_slug, @pr_number, text[0...65_500])
45
+ end
46
+
47
+ def delete_comments_including_text(text)
48
+ comments = find_comments_including_text(text)
49
+ comments.each { |comment| delete_comment(comment.id) }
50
+ end
51
+
52
+ def delete_comment(comment_id)
53
+ client.delete_merge_request_note(@repo_slug, @pr_number, comment_id)
54
+ end
55
+
56
+ def find_comments_including_text(text)
57
+ comments = []
58
+ client.merge_request_notes(@repo_slug, @pr_number).map do |item|
59
+ comments << item if item.body&.include? text
60
+ end
61
+ comments
62
+ end
63
+
64
+ def labels
65
+ client.merge_request(@repo_slug, @pr_number).labels.map { |item| item }
66
+ end
67
+
68
+ def files
69
+ client.merge_request_changes(@repo_slug, @pr_number)
70
+ end
71
+
72
+ def create_status(state, context, target_url, description)
73
+ client.update_commit_status(
74
+ @repo_slug,
75
+ @commit_sha,
76
+ state,
77
+ { context: context, target_url: target_url, description: description }
78
+ )
79
+ end
80
+
81
+ def get_status(context)
82
+ client.commit_status(@repo_slug, @commit_sha).each do |status|
83
+ return status if status.name == context
84
+ end
85
+ nil
86
+ end
87
+
88
+ def build_types
89
+ types = []
90
+ @build_types.each do |type|
91
+ if comments.include?("#{type} build") || labels.include?("#{type} build") ||
92
+ comments.include?(type) || labels.include?(type)
93
+ types.push(type)
94
+ end
95
+ end
96
+ types
97
+ end
98
+
99
+ def infrastructure_work?
100
+ !(title =~ /\[infra\]/i).nil? || labels.include?("Infra")
101
+ end
102
+
103
+ def work_in_progress?
104
+ title.include?("[WIP]") || labels.include?("WIP")
105
+ end
106
+
107
+ def big?
108
+ lines_of_code_changed > 500
109
+ end
110
+
111
+ def realm_module_modified?
112
+ modified_files = files.select { |file| file&.old_path&.start_with? "cache/" }
113
+ modified_files.length.positive?
114
+ end
115
+
116
+ private
117
+
118
+ def client
119
+ @_client = GitLab::Client.new if @_client.nil?
120
+ # @_client.access_token = @bot.create_token if @_client.access_token.nil?
121
+
122
+ @_client
123
+ end
124
+ end
125
+ end
@@ -4,7 +4,7 @@ module CiToolkit
4
4
  # Sends messages to a Github PR for predefined build events
5
5
  class PrMessenger
6
6
  def initialize(
7
- github_pr = CiToolkit::GithubPr.new,
7
+ github_pr = CiToolkit::DvcsPrFactory.create(CiToolkit::BitriseEnv.new),
8
8
  messenger_text = CiToolkit::PrMessengerText.new
9
9
  )
10
10
  @github_pr = github_pr
data/lib/ci_toolkit.rb CHANGED
@@ -4,8 +4,11 @@ require "ci_toolkit/bitrise_env"
4
4
  require "ci_toolkit/build"
5
5
  require "ci_toolkit/build_status"
6
6
  require "ci_toolkit/duplicate_files_finder"
7
+ require "ci_toolkit/dvcs_pr"
7
8
  require "ci_toolkit/github_bot"
8
9
  require "ci_toolkit/github_pr"
10
+ require "ci_toolkit/gitlab_pr"
11
+ require "ci_toolkit/dvcs_pr_factory"
9
12
  require "ci_toolkit/git"
10
13
  require "ci_toolkit/jira"
11
14
  require "ci_toolkit/pr_messenger"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ci_toolkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.12
4
+ version: 1.5.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gero Keller
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-11-12 00:00:00.000000000 Z
11
+ date: 2022-06-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: gitlab
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'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: jwt
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -208,9 +222,12 @@ files:
208
222
  - lib/ci_toolkit/build.rb
209
223
  - lib/ci_toolkit/build_status.rb
210
224
  - lib/ci_toolkit/duplicate_files_finder.rb
225
+ - lib/ci_toolkit/dvcs_pr.rb
226
+ - lib/ci_toolkit/dvcs_pr_factory.rb
211
227
  - lib/ci_toolkit/git.rb
212
228
  - lib/ci_toolkit/github_bot.rb
213
229
  - lib/ci_toolkit/github_pr.rb
230
+ - lib/ci_toolkit/gitlab_pr.rb
214
231
  - lib/ci_toolkit/jira.rb
215
232
  - lib/ci_toolkit/pr_messenger.rb
216
233
  - lib/ci_toolkit/pr_messenger_text.rb