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 +4 -4
- data/lib/flaky/providers/semaphore.rb +95 -23
- data/lib/flaky/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d016af0bdc9c0fbadfe3671661682a1cdf85eb96b2f666dec28c1c1d69420757
|
|
4
|
+
data.tar.gz: 6983cdd0f2763185858bf3774f8ab4df10e503691f1f37480be65f31ea1ef4ae
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
19
|
-
|
|
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["
|
|
61
|
+
id: job["job_id"],
|
|
30
62
|
name: job["name"],
|
|
31
63
|
block_name: block_name,
|
|
32
|
-
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
|
-
|
|
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
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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
|
|
51
|
-
|
|
52
|
-
|
|
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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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