gergich 2.1.5 → 2.2.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: a633dec2c1eb9cec61cba56c145a1c31c4022c19c7c75e91e368b0a3bc3cf89a
4
- data.tar.gz: 0721f82a0c12ce4069a00bed04d4097d8d712b6289e3a7f16d29865bf5220055
3
+ metadata.gz: 4d3e834d58a03556ceb4fe3a5b8208614e4336d78d77005d75869f6d7fcd592d
4
+ data.tar.gz: dae0267c8271931345a8b7a6d1346c6542cdf7eef850f8312166d7d593fc200e
5
5
  SHA512:
6
- metadata.gz: 4ecba879accbb6c196d3bad117360a6469e47a252043d3b4d71e6aaa50ac265294188d078b9798087cc92a17a83b319885dbf1e85e661dc4bd82566e77e182a3
7
- data.tar.gz: 4c40b055c23de9019776720c41167fec8a031f3592896f11131935b7641cf9ff34487377f552388e936e5349fe284b30d74d1870d1e6b2122964da1a40811605
6
+ metadata.gz: d8dfb192bd53c153d6cef178e8580b0e7abfd59515deede69d941fb8d50b59cb256ba0898f3a17059980f70fa2d4c95cd33fe9453a43ff8344880b90f8f35d80
7
+ data.tar.gz: 6676d0e9d624cc845f3c99d43c7fd96558a17972a2d66177e6bc8debc367242de82722ba986b70eb6a12f98ad654112da37890135675f91528dbcd4520ccbbbf
@@ -92,7 +92,8 @@ commands["publish"] = {
92
92
  commands["status"] = {
93
93
  summary: "Show the current draft for this patchset",
94
94
  action: -> {
95
- Gergich::Review.new.status
95
+ commit = Gergich::Commit.new
96
+ Gergich::Review.new(commit).status
96
97
  },
97
98
  help: -> {
98
99
  <<~TEXT
@@ -9,6 +9,9 @@ MASTER_BOUNCER_REVIEW_LABEL = ENV.fetch("GERGICH_REVIEW_LABEL", "Code-Review")
9
9
  require_relative "../../gergich"
10
10
 
11
11
  PROJECT = ENV["GERRIT_PROJECT"] || error("no GERRIT_PROJECT set")
12
+ # on Jenkins GERRIT_MAIN_BRANCH env var will default to empty string if not set
13
+ MAIN_BRANCH = ENV["GERRIT_MAIN_BRANCH"].nil? || ENV["GERRIT_MAIN_BRANCH"].empty? ? "master" :
14
+ ENV["GERRIT_MAIN_BRANCH"]
12
15
  # TODO: time-based thresholds
13
16
  WARN_DISTANCE = ENV.fetch("MASTER_BOUNCER_WARN_DISTANCE", 50).to_i
14
17
  ERROR_DISTANCE = ENV.fetch("MASTER_BOUNCER_ERROR_DISTANCE", 100).to_i
@@ -17,7 +20,7 @@ def potentially_mergeable_changes
17
20
  url = "/changes/?q=status:open+" \
18
21
  "p:#{PROJECT}+" \
19
22
  "label:Verified=1+" \
20
- "branch:master" \
23
+ "branch:#{MAIN_BRANCH}" \
21
24
  "&o=CURRENT_REVISION"
22
25
  changes = Gergich::API.get(url)
23
26
  changes.reject { |c| c["subject"] =~ /\Awip($|\W)/i }
@@ -27,8 +30,8 @@ def maybe_bounce_commit!(commit)
27
30
  draft = Gergich::Draft.new(commit)
28
31
  draft.reset!
29
32
 
30
- distance = Gergich.git("rev-list origin/master ^#{commit.ref} --count").to_i
31
- detail = "#{distance} commits behind master"
33
+ distance = Gergich.git("rev-list origin/#{MAIN_BRANCH} ^#{commit.ref} --count").to_i
34
+ detail = "#{distance} commits behind #{MAIN_BRANCH}"
32
35
 
33
36
  score = 0
34
37
  message = nil
@@ -84,10 +87,18 @@ commands["check"] = {
84
87
  commands["check_all"] = {
85
88
  summary: "Check the age of all potentially mergeable changes",
86
89
  action: -> {
87
- Gergich.git("fetch")
88
90
  gerrit_host = ENV["GERRIT_HOST"] || error("No GERRIT_HOST set")
91
+ Gergich.git("fetch")
89
92
 
90
93
  changes = potentially_mergeable_changes
94
+ # if changes is empty or nil, we don't need to iterate over it
95
+ # log that we're skipping, and return
96
+ if changes.nil? || changes.empty?
97
+ Logging.logger.info "No changes to check"
98
+ return
99
+ end
100
+ Logging.logger.info "Checking #{changes.size} changes"
101
+
91
102
  next if ENV["DRY_RUN"]
92
103
 
93
104
  changes.each do |change|
data/lib/gergich.rb CHANGED
@@ -3,7 +3,8 @@
3
3
  require "erb"
4
4
  require "json"
5
5
  require "fileutils"
6
- require "base64"
6
+ require "open3"
7
+ require_relative "logging"
7
8
 
8
9
  GERGICH_REVIEW_LABEL = ENV.fetch("GERGICH_REVIEW_LABEL", "Code-Review")
9
10
  GERGICH_USER = ENV.fetch("GERGICH_USER", "gergich")
@@ -12,24 +13,35 @@ GERGICH_GIT_PATH = ENV.fetch("GERGICH_GIT_PATH", ".")
12
13
  GergichError = Class.new(StandardError)
13
14
 
14
15
  module Gergich
16
+ include Logging
15
17
  def self.use_git?
16
- Dir.chdir(GERGICH_GIT_PATH) do
17
- system "git rev-parse --show-toplevel >/dev/null 2>&1"
18
- end
18
+ !git("rev-parse --show-toplevel").nil?
19
19
  end
20
20
 
21
21
  def self.git(args)
22
22
  Dir.chdir(GERGICH_GIT_PATH) do
23
- `git #{args} 2>/dev/null`
23
+ raise(GergichError, "git not installed") unless system("which git > /dev/null 2>&1")
24
+
25
+ output, error, status = Open3.capture3("git #{args}")
26
+ if status.success?
27
+ Logging.logger.debug "git #{args}: #{output}"
28
+ output
29
+ else
30
+ Logging.logger.warn "Output: #{output}"
31
+ Logging.logger.warn "git #{args} failed: #{error}"
32
+ nil
33
+ end
24
34
  end
25
35
  end
26
36
 
27
37
  class Commit
38
+ include Logging
28
39
  attr_reader :ref
29
40
 
30
41
  def initialize(ref = ENV.fetch("GERRIT_PATCHSET_REVISION", "HEAD"), revision_number = nil)
31
42
  @ref = ref
32
43
  @revision_number = revision_number
44
+ logger.debug "Commit initialized with ref: #{ref}"
33
45
  end
34
46
 
35
47
  def info
@@ -55,7 +67,7 @@ module Gergich
55
67
  Gergich.git("diff-tree --no-commit-id --name-only -r #{ref}").split
56
68
  else
57
69
  raw = API.get("/changes/#{change_id}/revisions/#{revision_id}/patch", raw: true)
58
- Base64.decode64(raw)
70
+ raw.unpack1("m")
59
71
  .scan(%r{^diff --git a/.*? b/(.*?)$})
60
72
  .flatten
61
73
  end
@@ -89,7 +101,7 @@ module Gergich
89
101
  class Review
90
102
  attr_reader :commit, :draft
91
103
 
92
- def initialize(commit = Commit.new, draft = Draft.new)
104
+ def initialize(commit = Commit.new, draft = Draft.new(commit))
93
105
  @commit = commit
94
106
  @draft = draft
95
107
  end
data/lib/logging.rb ADDED
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: false
2
+
3
+ require "logger"
4
+
5
+ module Logging
6
+ def logger
7
+ Logging.logger
8
+ end
9
+
10
+ def self.logger
11
+ @logger ||= Logger.new($stdout)
12
+ @logger.formatter = proc do |severity, datetime, _progname, msg|
13
+ datefmt = datetime.strftime("%Y-%m-%d %H:%M:%S")
14
+ # ensure that there is only one newline at the end of the message
15
+ "#{severity} [#{datefmt}]: #{msg}\n".gsub!(/\n+/, "\n")
16
+ end
17
+ @logger
18
+ end
19
+
20
+ def self.log_level
21
+ # default to only showing errors
22
+ ENV.fetch("RUBY_LOG_LEVEL", "ERROR")
23
+ end
24
+
25
+ logger.level = log_level
26
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gergich
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.5
4
+ version: 2.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Jensen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-05-03 00:00:00.000000000 Z
11
+ date: 2024-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -169,6 +169,7 @@ files:
169
169
  - lib/gergich/cli.rb
170
170
  - lib/gergich/cli/gergich.rb
171
171
  - lib/gergich/cli/master_bouncer.rb
172
+ - lib/logging.rb
172
173
  homepage: https://github.com/instructure/gergich
173
174
  licenses:
174
175
  - MIT