ghamma 0.1.1 → 0.2.0

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: ad500d312e1ec3068bcebd224b215cc53cefb71c70ec88f765d3dda38c1605fa
4
- data.tar.gz: 26265dd2d7e18b24d6649d535b2546c42ea41404238e15117f65045621da1f31
3
+ metadata.gz: 374a835681570e0824d2ee192361ed16dbfc11352cc8f8f0f69ad2a0cdaa8f7f
4
+ data.tar.gz: 702786830164573d586113f1f88b8c5e983e71111c9ce3226274e468a374fb61
5
5
  SHA512:
6
- metadata.gz: c7cd7a72afe17f051550b28fd8b3de74227d38c1ef02e24a62242d355efcff1bf183103188b365201cec19e2506d1a2c3d6cbfb7c3e288e78abd612d08fc1174
7
- data.tar.gz: 3749db0be1011a9998684e5f9d66a1d990a97233e892581655136b576916e3becee978bc17002b3617b2536401339ba0ed4ce6c0d107e5c5a65035f0f3f997af
6
+ metadata.gz: 3e01904c7804ae9636d06aaad5af029736b8286200f2791851b10bdd11f6552721f7cdb2ab1c4dfe9489bd27ff98374f7d7037a15e0fb04ad22c396429b18361
7
+ data.tar.gz: f2fb982a98a100f66072fbc80d0c4618f1281de82442dcf59c2b551ade25466303684d7fcbf55cf56eefded0a4d43ad35ce95eb7148764e71b2dc840c08c9b51
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  ## [Unreleased]
2
2
 
3
+ -
4
+
5
+ ## [0.2.0] - 2023-06-01
6
+
7
+ - Breaking: Add commands to the CLI: `version`, `list-worklows`, `duration-history`
8
+
3
9
  ## [0.1.1] - 2023-06-01
4
10
 
5
11
  - Actually make the gem executable
data/Gemfile.lock CHANGED
@@ -1,7 +1,8 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ghamma (0.1.1)
4
+ ghamma (0.2.0)
5
+ dry-cli
5
6
  http
6
7
 
7
8
  GEM
@@ -12,6 +13,7 @@ GEM
12
13
  ast (2.4.2)
13
14
  domain_name (0.5.20190701)
14
15
  unf (>= 0.0.5, < 1.0.0)
16
+ dry-cli (1.0.0)
15
17
  ffi (1.15.5)
16
18
  ffi-compiler (1.0.1)
17
19
  ffi (>= 1.0.0)
data/README.md CHANGED
@@ -13,17 +13,43 @@ If bundler is not being used to manage dependencies, install the gem by executin
13
13
 
14
14
  ## Usage
15
15
 
16
- TODO: Write usage instructions here
16
+ ### Authentication
17
+
18
+ The Github workflows API requires an authenticated user, even for public repos.
19
+ To use this tool, you will need a
20
+ [Github API Token](https://docs.github.com/en/rest/overview/authenticating-to-the-rest-api#authenticating-with-a-personal-access-token)
21
+ with at least `read` access to the repo you want to look at.
22
+
23
+ Once you have that, make it available.
24
+
25
+ ```
26
+ $ export GH_TOKEN="YOUR_API_TOKEN"
27
+ ```
28
+
29
+ Then fetch the list of available workflows for the repo. The name will be necessary in the next step.
30
+
31
+ ```
32
+ $ ghamma list-workflows tony-rowan ghamma
33
+ Found 1 workflows
34
+ Ruby
35
+ ```
36
+
37
+ Then fetch a CSV of the durations of the latest workflow runs.
38
+
39
+ ```
40
+ $ ghamma duration-history tony-rowan ghamma Ruby
41
+ Date,Duration
42
+ 2023-06-01T15:44:49Z,21000
43
+ 2023-06-01T15:37:21Z,17000
44
+ 2023-06-01T15:35:16Z,23000
45
+ 2023-06-01T15:31:33Z,19000
46
+ ```
17
47
 
18
48
  ## Development
19
49
 
20
50
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test-unit` to run the tests.
21
51
  You can also run `bin/console` for an interactive prompt that will allow you to experiment.
22
52
 
23
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update
24
- the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the
25
- version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
26
-
27
53
  ## Contributing
28
54
 
29
55
  Bug reports and pull requests are welcome on GitHub at https://github.com/tony-rowan/ghamma. This project is
data/exe/ghamma CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'ghamma'
3
+ require "ghamma"
4
4
 
5
- Ghamma::Cli.new.print_workflow_duration_metrics
5
+ Dry::CLI.new(Ghamma::CLI).call
data/lib/ghamma/cli.rb CHANGED
@@ -1,80 +1,60 @@
1
1
  require "csv"
2
+ require "dry/cli"
2
3
  require "http"
3
4
 
4
- module Ghamma
5
- class Cli
6
-
7
- BASE_URL = "https://api.github.com".freeze
8
- OWNER = ARGV[0].freeze
9
- REPOSITORY = ARGV[1].freeze
5
+ require_relative "./github_api_client"
6
+ require_relative "./version"
10
7
 
11
- def api_url(resource)
12
- "#{BASE_URL}#{resource}"
13
- end
8
+ module Ghamma
9
+ module CLI
10
+ extend Dry::CLI::Registry
14
11
 
15
- def get(resource, params = {})
16
- response = HTTP
17
- .auth("Bearer #{ENV["GH_TOKEN"]}")
18
- .get(api_url(resource), params: params)
19
- .body
20
- JSON.parse(response)
21
- end
12
+ class Version < Dry::CLI::Command
13
+ desc "Print version"
22
14
 
23
- def fetch_workflows
24
- response = get "/repos/#{OWNER}/#{REPOSITORY}/actions/workflows"
25
- response["workflows"].map { |json| {id: json["id"], name: json["name"]} }.tap do |workflows|
26
- puts "Found #{workflows.size} workflows"
15
+ def call(*)
16
+ puts VERSION
27
17
  end
28
18
  end
29
19
 
30
- def fetch_workflows_with_runs
31
- fetch_workflows.map do |workflow|
32
- response = get "/repos/#{OWNER}/#{REPOSITORY}/actions/workflows/#{workflow[:id]}/runs",
33
- {per_page: 100, status: "success", exclude_pull_requests: true, created: ">2023-05-01"}
34
- puts "Fetched runs for #{workflow[:name]}"
20
+ class ListWorkflows < Dry::CLI::Command
21
+ desc "List workflows on the given repo"
35
22
 
36
- next if response["total_count"].zero?
23
+ argument :owner, required: true
24
+ argument :repo, required: true
37
25
 
38
- {
39
- id: workflow[:id],
40
- name: workflow[:name],
41
- runs: response["workflow_runs"].map { |json| {id: json["id"], date: json["created_at"]} }
42
- }
43
- end.compact
26
+ def call(owner:, repo:)
27
+ workflows = GithubApiClient.new(owner: owner, repo: repo, token: ENV["GH_TOKEN"]).fetch_workflows
28
+ puts "Found #{workflows.size} workflows"
29
+ workflows.each { |workflow| puts workflow[:name] }
30
+ puts
31
+ end
44
32
  end
45
33
 
46
- def fetch_workflow_duration_metrics
47
- fetch_workflows_with_runs.map do |workflow|
48
- runs_with_timing = workflow[:runs].map do |workflow_run|
49
- response = get "/repos/#{OWNER}/#{REPOSITORY}/actions/runs/#{workflow_run[:id]}/timing"
34
+ class DurationHistory < Dry::CLI::Command
35
+ desc "List the duration of each successful workflow run"
50
36
 
51
- {
52
- duration: response["run_duration_ms"],
53
- date: workflow_run[:date]
54
- }
55
- end
37
+ argument :owner, required: true
38
+ argument :repo, required: true
39
+ argument :workflow, required: true
56
40
 
57
- puts "Fetched timings for #{workflow[:name]}"
41
+ def call(owner:, repo:, workflow:)
42
+ durations = GithubApiClient.new(owner: owner, repo: repo, token: ENV["GH_TOKEN"]).fetch_workflow_duration_history(workflow)
58
43
 
59
- {
60
- name: workflow[:name],
61
- runs: runs_with_timing
62
- }
63
- end
64
- end
65
-
66
- def print_workflow_duration_metrics
67
- fetch_workflow_duration_metrics.each do |workflow_metrics|
68
- puts workflow_metrics[:name]
69
- metrics_table = CSV.generate do |csv|
44
+ durations_output = CSV.generate do |csv|
70
45
  csv << ["Date", "Duration"]
71
- workflow_metrics[:runs].each do |metric_datum|
72
- csv << [metric_datum[:date], metric_datum[:duration]]
46
+ durations.each do |duration|
47
+ csv << duration
73
48
  end
74
49
  end
75
- puts metrics_table
50
+
51
+ puts durations_output
76
52
  puts
77
53
  end
78
54
  end
55
+
56
+ register "version", Version, aliases: ["v", "-v", "--version"]
57
+ register "list-workflows", ListWorkflows
58
+ register "duration-history", DurationHistory
79
59
  end
80
60
  end
@@ -0,0 +1,54 @@
1
+ require "http"
2
+
3
+ module Ghamma
4
+ class GithubApiClient
5
+ BASE_URL = "https://api.github.com".freeze
6
+
7
+ def initialize(owner:, repo:, token:)
8
+ @repo_base_url = "#{BASE_URL}/repos/#{owner}/#{repo}"
9
+ @authorization_header = "Bearer #{token}"
10
+ end
11
+
12
+ def fetch_workflows
13
+ get("/workflows")
14
+ .fetch("workflows")
15
+ .map do |workflow_json|
16
+ workflow_json.transform_keys(&:to_sym).slice(:id, :name)
17
+ end
18
+ end
19
+
20
+ def fetch_workflow_duration_history(workflow_name)
21
+ workflow_id = get("/workflows").fetch("workflows")
22
+ .find { |workflow| workflow["name"] == workflow_name }
23
+ &.fetch("id", nil)
24
+
25
+ if workflow_id.nil?
26
+ raise "Could not find workflow with name #{workflow_name}"
27
+ end
28
+
29
+ workflow_runs = get(
30
+ "/workflows/#{workflow_id}}/runs",
31
+ {per_page: 100, status: "success", exclude_pull_requests: true}
32
+ ).fetch("workflow_runs")
33
+ .map do |workflow_run|
34
+ run_timing = get("/runs/#{workflow_run["id"]}/timing")
35
+
36
+ [
37
+ workflow_run["created_at"],
38
+ run_timing["run_duration_ms"]
39
+ ]
40
+ end
41
+ end
42
+
43
+ private
44
+
45
+ attr_reader :repo_base_url, :authorization_header
46
+
47
+ def get(resource, params = {})
48
+ HTTP
49
+ .auth(authorization_header)
50
+ .get("#{repo_base_url}/actions#{resource}", params: params)
51
+ .parse
52
+ end
53
+ end
54
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Ghamma
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ghamma
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tony Rowan
@@ -10,6 +10,20 @@ bindir: exe
10
10
  cert_chain: []
11
11
  date: 2023-06-01 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dry-cli
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: http
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -85,6 +99,7 @@ files:
85
99
  - exe/ghamma
86
100
  - lib/ghamma.rb
87
101
  - lib/ghamma/cli.rb
102
+ - lib/ghamma/github_api_client.rb
88
103
  - lib/ghamma/version.rb
89
104
  - sig/ghamma.rbs
90
105
  homepage: https://github.com/tony-rowan/ghamma