flaky-friend 0.1.1 → 0.1.2

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: 834bb11c965a69b372987383775645d140aceee6cd3fd4827597ec548d0ff4d9
4
- data.tar.gz: 99836f80168e30b658e04d695b8bbc831a0c9a44a7b6c50f6d5d07baf9921710
3
+ metadata.gz: d016af0bdc9c0fbadfe3671661682a1cdf85eb96b2f666dec28c1c1d69420757
4
+ data.tar.gz: 6983cdd0f2763185858bf3774f8ab4df10e503691f1f37480be65f31ea1ef4ae
5
5
  SHA512:
6
- metadata.gz: 98887d08017bb8f1e0bd864dc1af71498d505d87a29cdf7e32f69eea181af426bd457091761461087fd8c903688d01b54ea5b014f9acb6a96bd346288bff2027
7
- data.tar.gz: a195d7c85d1a2d683a13f8f11d4f317bc46048cd5c99faff279968ef22ac648b093da4ebd29b673b4bb52ab46d5d3df5426d739df165857f4630fe02a1b33826
6
+ metadata.gz: 2619613daf34a41ae20f86702e212fd4379fa738dfcc1fcf30e32e16558ad741152612559d8f50ab368c2538a80fd7ce357899962495bfa7d3e05d8bdda2e117
7
+ data.tar.gz: 32f4ca285cf88f32ff8b80eb082628063dd8c745ce39175ef96f41af5c825df7b0ae3cc8d7501cde240affeaa5c79d46214d21f9fdf6aa154a9380ecf78612ea
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "json"
4
+ require "net/http"
5
+ require "uri"
3
6
  require "yaml"
4
7
  require_relative "base"
5
8
 
@@ -9,16 +12,45 @@ module Flaky
9
12
  TEST_BLOCKS = ["Unit Tests", "System Tests"].freeze
10
13
 
11
14
  def fetch_workflows(age: "24h")
12
- output = run_cmd("sem get workflows -p #{config.project} --age #{age}")
13
- lines = output.lines.drop(1) # skip header
14
- lines.filter_map { |line| parse_workflow_line(line) }
15
+ cutoff = Time.now - parse_age_seconds(age)
16
+ project_id = resolve_project_id
17
+ branch = config.branch
18
+ workflows = []
19
+
20
+ page = 1
21
+ loop do
22
+ data = api_get("plumber-workflows", project_id: project_id, page: page)
23
+ break if data.empty?
24
+
25
+ data.each do |wf|
26
+ created_at = Time.at(wf.dig("created_at", "seconds").to_i)
27
+ next unless wf["branch_name"] == branch
28
+
29
+ if created_at < cutoff
30
+ return workflows # older than cutoff, done
31
+ end
32
+
33
+ workflows << {
34
+ id: wf["wf_id"],
35
+ pipeline_id: wf["initial_ppl_id"],
36
+ branch: wf["branch_name"],
37
+ created_at: created_at.strftime("%Y-%m-%d %H:%M:%S")
38
+ }
39
+ end
40
+
41
+ # If the oldest entry on this page is still within cutoff, keep paging
42
+ oldest = Time.at(data.last.dig("created_at", "seconds").to_i)
43
+ break if oldest < cutoff
44
+
45
+ page += 1
46
+ end
47
+
48
+ workflows
15
49
  end
16
50
 
17
51
  def fetch_jobs(pipeline_id:)
18
- output = run_cmd("sem get pipelines #{pipeline_id}")
19
- data = YAML.safe_load(output, permitted_classes: [Date, Time])
20
- blocks = data&.[]("blocks")
21
- return [] unless blocks
52
+ data = api_get("pipelines/#{pipeline_id}", detailed: true)
53
+ blocks = data["blocks"] || []
22
54
 
23
55
  blocks.flat_map do |block|
24
56
  block_name = block["name"]
@@ -26,37 +58,77 @@ module Flaky
26
58
 
27
59
  (block["jobs"] || []).map do |job|
28
60
  {
29
- id: job["jobid"],
61
+ id: job["job_id"],
30
62
  name: job["name"],
31
63
  block_name: block_name,
32
- result: block["result"]
64
+ result: job["result"]&.downcase == "passed" ? "passed" : "failed"
33
65
  }
34
66
  end
35
67
  end
36
68
  end
37
69
 
38
70
  def fetch_log(job_id:)
39
- run_cmd("sem logs #{job_id}")
71
+ data = api_get("logs/#{job_id}")
72
+ events = data["events"] || []
73
+ events
74
+ .select { |e| e["event"] == "cmd_output" }
75
+ .map { |e| e["output"] }
76
+ .join
40
77
  end
41
78
 
42
79
  private
43
80
 
44
- def run_cmd(cmd)
45
- output = `#{cmd} 2>/dev/null`
46
- raise Error, "Command failed (exit #{$?.exitstatus}): #{cmd}" unless $?.success?
47
- output
81
+ def api_get(path, **params)
82
+ query = params.map { |k, v| "#{k}=#{v}" }.join("&")
83
+ url = "#{api_host}/api/v1alpha/#{path}"
84
+ url += "?#{query}" unless query.empty?
85
+
86
+ uri = URI(url)
87
+ req = Net::HTTP::Get.new(uri)
88
+ req["Authorization"] = "Token #{api_token}"
89
+
90
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
91
+ http.request(req)
92
+ end
93
+
94
+ raise Error, "Semaphore API error (#{response.code}): #{response.body[0..200]}" unless response.is_a?(Net::HTTPSuccess)
95
+
96
+ JSON.parse(response.body)
97
+ end
98
+
99
+ def api_host
100
+ @api_host ||= begin
101
+ sem_config = YAML.load_file(File.expand_path("~/.sem.yaml"))
102
+ context_name = sem_config["active-context"]
103
+ host = sem_config.dig("contexts", context_name, "host")
104
+ "https://#{host}"
105
+ end
48
106
  end
49
107
 
50
- def parse_workflow_line(line)
51
- parts = line.strip.split(/\s{2,}/)
52
- return nil if parts.length < 4
108
+ def api_token
109
+ @api_token ||= begin
110
+ sem_config = YAML.load_file(File.expand_path("~/.sem.yaml"))
111
+ context_name = sem_config["active-context"]
112
+ sem_config.dig("contexts", context_name, "auth", "token")
113
+ end
114
+ end
115
+
116
+ def resolve_project_id
117
+ @project_id ||= begin
118
+ projects = api_get("projects")
119
+ project = projects.find { |p| p.dig("metadata", "name") == config.project }
120
+ raise Error, "Project '#{config.project}' not found in Semaphore" unless project
121
+ project.dig("metadata", "id")
122
+ end
123
+ end
53
124
 
54
- {
55
- id: parts[0],
56
- pipeline_id: parts[1],
57
- branch: parts[3],
58
- created_at: parts[2]
59
- }
125
+ def parse_age_seconds(age)
126
+ case age.to_s
127
+ when /\A(\d+)h\z/ then $1.to_i * 3600
128
+ when /\A(\d+)d\z/ then $1.to_i * 86400
129
+ when /\A(\d+)m\z/ then $1.to_i * 60
130
+ else 86400 # default 24h
131
+ end
60
132
  end
61
133
  end
62
134
 
data/lib/flaky/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Flaky
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: flaky-friend
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Flytedesk