Module: Kettle::Dev::CIHelpers

Defined in:
lib/kettle/dev/ci_helpers.rb,
sig/kettle/dev.rbs

Overview

CI-related helper functions used by Rake tasks and release tooling.

This module only exposes module-functions (no instance state) and is intentionally small so it can be required by both Rake tasks and the kettle-release executable.

Class Method Summary collapse

Class Method Details

.ci_project_rootString

Return the repository root used by release CI monitoring. Monorepo member releases execute from a subdirectory but monitor workflows stored at the shared repository root.

Returns:

  • (String)

    absolute CI project root



35
36
37
# File 'lib/kettle/dev/ci_helpers.rb', line 35

def ci_project_root
  ENV.fetch("K_RELEASE_CI_ROOT", project_root)
end

.current_branchString?

Current git branch name, or nil when not in a repository.

Returns:

  • (String, nil)


51
52
53
54
# File 'lib/kettle/dev/ci_helpers.rb', line 51

def current_branch
  out, status = Open3.capture2("git", "rev-parse", "--abbrev-ref", "HEAD")
  status.success? ? out.strip : nil
end

.current_head_shaString?

Current git commit SHA, or nil when unavailable.

Returns:

  • (String, nil)


58
59
60
61
# File 'lib/kettle/dev/ci_helpers.rb', line 58

def current_head_sha
  out, status = Open3.capture2("git", "rev-parse", "HEAD")
  status.success? ? out.strip : nil
end

.default_gitlab_tokenString?

Default GitLab token from environment

Returns:

  • (String, nil)


217
218
219
# File 'lib/kettle/dev/ci_helpers.rb', line 217

def default_gitlab_token
  ENV["GITLAB_TOKEN"] || ENV["GL_TOKEN"]
end

.default_tokenString?

Default GitHub token sourced from environment.

Returns:

  • (String, nil)


193
194
195
# File 'lib/kettle/dev/ci_helpers.rb', line 193

def default_token
  ENV["GITHUB_TOKEN"] || ENV["GH_TOKEN"]
end

.exclusionsArray<String>

List of workflow files to exclude from interactive menus and checks.

Returns:

  • (Array<String>)


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/kettle/dev/ci_helpers.rb', line 81

def exclusions
  %w[
    auto-assign.yml
    codeql-analysis.yml
    danger.yml
    dependency-review.yml
    discord-notifier.yml
    opencollective.yml
    scorecard-analysis.yml
  ]
end

.failed?(run) ⇒ Boolean

Whether a run has completed with a non-success conclusion.

Parameters:

  • run (Hash, nil)
  • ({ "status" => String, "conclusion" => String? }, nil)

Returns:

  • (Boolean)


187
188
189
# File 'lib/kettle/dev/ci_helpers.rb', line 187

def failed?(run)
  run && run["status"] == "completed" && run["conclusion"] && run["conclusion"] != "success"
end

.github_get_json(url, token:) ⇒ Object



166
167
168
169
170
171
172
173
174
175
# File 'lib/kettle/dev/ci_helpers.rb', line 166

def github_get_json(url, token:)
  uri = URI(url)
  req = Net::HTTP::Get.new(uri)
  req["User-Agent"] = "kettle-dev/ci-helpers"
  req["Authorization"] = "token #{token}" if token && !token.empty?
  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) }
  return unless res.is_a?(Net::HTTPSuccess)

  JSON.parse(res.body)
end

.gitlab_failed?(pipeline) ⇒ Boolean

Whether a GitLab pipeline has failed

Parameters:

  • pipeline (Hash, nil)
  • ({ "status" => String }, nil)

Returns:

  • (Boolean)


288
289
290
# File 'lib/kettle/dev/ci_helpers.rb', line 288

def gitlab_failed?(pipeline)
  pipeline && pipeline["status"] == "failed"
end

.gitlab_latest_pipeline(owner:, repo:, branch: nil, host: "gitlab.com", token: default_gitlab_token) ⇒ Hash{String=>String,Integer}?

Fetch the latest pipeline for a branch on GitLab

Parameters:

  • owner (String)
  • repo (String)
  • branch (String, nil) (defaults to: nil)
  • host (String) (defaults to: "gitlab.com")
  • token (String, nil) (defaults to: default_gitlab_token)

Returns:

  • (Hash{String=>String,Integer}, nil)


228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# File 'lib/kettle/dev/ci_helpers.rb', line 228

def gitlab_latest_pipeline(owner:, repo:, branch: nil, host: "gitlab.com", token: default_gitlab_token)
  return unless owner && repo

  b = branch || current_branch
  return unless b

  project = URI.encode_www_form_component("#{owner}/#{repo}")
  uri = URI("https://#{host}/api/v4/projects/#{project}/pipelines?ref=#{URI.encode_www_form_component(b)}&per_page=1")
  req = Net::HTTP::Get.new(uri)
  req["User-Agent"] = "kettle-dev/ci-helpers"
  req["PRIVATE-TOKEN"] = token if token && !token.empty?
  res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) { |http| http.request(req) }
  return unless res.is_a?(Net::HTTPSuccess)

  data = JSON.parse(res.body)
  return unless data.is_a?(Array)

  pipe = data.first
  return unless pipe.is_a?(Hash)

  # Attempt to enrich with failure_reason by querying the single pipeline endpoint
  begin
    if pipe["id"]
      detail_uri = URI("https://#{host}/api/v4/projects/#{project}/pipelines/#{pipe["id"]}")
      dreq = Net::HTTP::Get.new(detail_uri)
      dreq["User-Agent"] = "kettle-dev/ci-helpers"
      dreq["PRIVATE-TOKEN"] = token if token && !token.empty?
      dres = Net::HTTP.start(detail_uri.hostname, detail_uri.port, use_ssl: true) { |http| http.request(dreq) }
      if dres.is_a?(Net::HTTPSuccess)
        det = JSON.parse(dres.body)
        pipe["failure_reason"] = det["failure_reason"] if det.is_a?(Hash)
        pipe["status"] = det["status"] if det["status"]
        pipe["web_url"] = det["web_url"] if det["web_url"]
      end
    end
  rescue => e
    Kettle::Dev.debug_error(e, __method__)
    # ignore enrichment errors; fall back to basic fields
  end
  {
    "status" => pipe["status"],
    "web_url" => pipe["web_url"],
    "id" => pipe["id"],
    "failure_reason" => pipe["failure_reason"]
  }
rescue => e
  Kettle::Dev.debug_error(e, __method__)
  nil
end

.gitlab_success?(pipeline) ⇒ Boolean

Whether a GitLab pipeline has succeeded

Parameters:

  • pipeline (Hash, nil)
  • ({ "status" => String }, nil)

Returns:

  • (Boolean)


281
282
283
# File 'lib/kettle/dev/ci_helpers.rb', line 281

def gitlab_success?(pipeline)
  pipeline && pipeline["status"] == "success"
end

.latest_repository_workflow_run(owner:, repo:, workflow_file:, branch:, head_sha:, token:) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/kettle/dev/ci_helpers.rb', line 138

def latest_repository_workflow_run(owner:, repo:, workflow_file:, branch:, head_sha:, token:)
  data = github_get_json(
    "https://api.github.com/repos/#{owner}/#{repo}/actions/runs?branch=#{URI.encode_www_form_component(branch)}&per_page=100",
    token: token
  )
  return unless data

  workflow_path = ".github/workflows/#{workflow_file}"
  preferred_head_run(
    Array(data["workflow_runs"]).select do |run|
      run["head_sha"] == head_sha && run["path"] == workflow_path
    end
  )
end

.latest_run(owner:, repo:, workflow_file:, branch: nil, token: default_token, require_head: false, head_sha: nil) ⇒ Hash{String=>String,Integer}?

Fetch latest workflow run info for a given workflow and branch via GitHub API.

Parameters:

  • owner (String)
  • repo (String)
  • workflow_file (String)

    the workflow basename (e.g., "ci.yml")

  • branch (String, nil) (defaults to: nil)

    branch to query; defaults to #current_branch

  • token (String, nil) (defaults to: default_token)

    OAuth token for higher rate limits; defaults to #default_token

Returns:

  • (Hash{String=>String,Integer}, nil)

    minimal run info or nil on error/none



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/kettle/dev/ci_helpers.rb', line 100

def latest_run(owner:, repo:, workflow_file:, branch: nil, token: default_token, require_head: false, head_sha: nil)
  return unless owner && repo

  b = branch || current_branch
  return unless b

  # Scope to the exact commit SHA when available to avoid picking up a previous run on the same branch.
  sha = head_sha || current_head_sha
  data = github_get_json(
    "https://api.github.com/repos/#{owner}/#{repo}/actions/workflows/#{workflow_file}/runs?branch=#{URI.encode_www_form_component(b)}&per_page=100",
    token: token
  )
  return unless data

  runs = Array(data["workflow_runs"]) || []
  # Match by head_sha first. There may be multiple builds for one commit;
  # always select the most recent build rather than trusting API order.
  run = if sha
    match = preferred_head_run(runs.select { |r| r["head_sha"] == sha })
    match ||= latest_repository_workflow_run(owner: owner, repo: repo, workflow_file: workflow_file, branch: b, head_sha: sha, token: token)
    require_head ? match : (match || runs.first)
  else
    runs.first unless require_head
  end
  return unless run

  {
    "status" => run["status"],
    "conclusion" => run["conclusion"],
    "html_url" => run["html_url"],
    "id" => run["id"],
    "head_sha" => run["head_sha"]
  }
rescue => e
  Kettle::Dev.debug_error(e, __method__)
  nil
end

.origin_urlString?

GitLab

Returns:

  • (String, nil)


201
202
203
204
# File 'lib/kettle/dev/ci_helpers.rb', line 201

def origin_url
  out, status = Open3.capture2("git", "config", "--get", "remote.origin.url")
  status.success? ? out.strip : nil
end

.parse_hosted_repo(url, host) ⇒ Array(String, String)?

Parse owner/repo from common hosted Git remote URL shapes.

Parameters:

  • url (String, nil)
  • host (String)

Returns:

  • (Array(String, String), nil)


296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/kettle/dev/ci_helpers.rb', line 296

def parse_hosted_repo(url, host)
  return unless url

  if url =~ %r{\Agit@#{Regexp.escape(host)}:(.+?)/(.+?)(?:\.git)?\z}
    return [Regexp.last_match(1), Regexp.last_match(2).sub(/\.git\z/, "")]
  end

  uri = URI.parse(url)
  return unless uri.host == host

  path = uri.path.to_s.sub(%r{\A/}, "")
  owner, repo = path.split("/", 2)
  return unless owner && repo && !owner.empty? && !repo.empty?

  [owner, repo.sub(/\.git\z/, "")]
rescue URI::InvalidURIError
  nil
end

.preferred_head_run(runs) ⇒ Object

GitHub can return multiple builds for the same commit, including an older failed run before a later retry. Select by run recency so an old conclusion cannot mask the newest build. updated_at also matters for reruns, which retain the original run's creation timestamp.



157
158
159
160
161
162
163
164
# File 'lib/kettle/dev/ci_helpers.rb', line 157

def preferred_head_run(runs)
  runs.max_by do |run|
    created_at = run["created_at"].to_s
    updated_at = run["updated_at"].to_s
    effective_at = [created_at, updated_at].max
    [effective_at, run["run_number"].to_i, run["run_attempt"].to_i, run["id"].to_i]
  end
end

.project_rootString

singleton (module) methods

Returns:

  • (String)


21
22
23
24
25
26
27
28
29
# File 'lib/kettle/dev/ci_helpers.rb', line 21

def project_root
  # Too difficult to test every possible branch here, so ignoring
  # simplecov:disable
  dir = if defined?(Rake) && Rake&.application&.respond_to?(:original_dir)
    Rake.application.original_dir
  end
  # simplecov:enable
  dir || Dir.pwd
end

.repo_infoArray(String, String)?

Parse the GitHub owner/repo from the configured origin remote. Supports SSH and HTTPS remote URL forms.

Returns:

  • (Array(String, String), nil)

    [owner, repo] or nil when unavailable



42
43
44
45
46
47
# File 'lib/kettle/dev/ci_helpers.rb', line 42

def repo_info
  out, status = Open3.capture2("git", "config", "--get", "remote.origin.url")
  return unless status.success?

  parse_hosted_repo(out.strip, "github.com")
end

.repo_info_gitlabArray(String, String)?

Parse GitLab owner/repo from origin if pointing to gitlab.com

Returns:

  • (Array(String, String), nil)


208
209
210
211
212
213
# File 'lib/kettle/dev/ci_helpers.rb', line 208

def repo_info_gitlab
  url = origin_url
  return unless url

  parse_hosted_repo(url, "gitlab.com")
end

.success?(run) ⇒ Boolean

Whether a run has completed successfully.

Parameters:

  • run (Hash, nil)
  • ({ "status" => String, "conclusion" => String? }, nil)

Returns:

  • (Boolean)


180
181
182
# File 'lib/kettle/dev/ci_helpers.rb', line 180

def success?(run)
  run && run["status"] == "completed" && run["conclusion"] == "success"
end

.workflows_list(root = project_root) ⇒ Array<String>

List workflow YAML basenames under .github/workflows at the given root. Excludes maintenance workflows defined by #exclusions.

Parameters:

  • root (String) (defaults to: project_root)

    project root (defaults to #project_root)

Returns:

  • (Array<String>)

    sorted list of basenames (e.g., "ci.yml")



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/kettle/dev/ci_helpers.rb', line 67

def workflows_list(root = project_root)
  workflows_dir = File.join(root, ".github", "workflows")
  files = if Dir.exist?(workflows_dir)
    Dir[File.join(workflows_dir, "*.yml")] + Dir[File.join(workflows_dir, "*.yaml")]
  else
    []
  end
  basenames = files.map { |p| File.basename(p) }
  basenames = basenames.uniq - exclusions
  basenames.sort
end