Class: Kettle::Gha::Pins::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/kettle/gha/pins/cli.rb

Overview

CLI to scan GitHub Action workflow files and pin mutable references in uses: to commit SHAs.

Constant Summary collapse

API_BASE =
Kettle::Gha::Pins::API_BASE
RELEASE_PATH =
Kettle::Gha::Pins::RELEASE_PATH
SHA_RE =
Kettle::Gha::Pins::SHA_RE
WEAK_SHA_RE =
Kettle::Gha::Pins::WEAK_SHA_RE
NON_SHA_REASON =
Kettle::Gha::Pins::NON_SHA_REASON
STALE_SHA_REASON =
Kettle::Gha::Pins::STALE_SHA_REASON
UPGRADE_REASON =
Kettle::Gha::Pins::UPGRADE_REASON
COMMENT_REASON =
"update_version_comment"
DEFAULT_UPGRADE_LEVEL =
Kettle::Gha::Pins::DEFAULT_UPGRADE_LEVEL
DEFAULT_CACHE_TTL_SECONDS =
Kettle::Gha::Pins::DEFAULT_CACHE_TTL_SECONDS
DEFAULT_HTTP_OPEN_TIMEOUT_SECONDS =
Kettle::Gha::Pins::DEFAULT_HTTP_OPEN_TIMEOUT_SECONDS
DEFAULT_HTTP_READ_TIMEOUT_SECONDS =
Kettle::Gha::Pins::DEFAULT_HTTP_READ_TIMEOUT_SECONDS
DEFAULT_HTTP_REFRESH_TIMEOUT_SECONDS =
Kettle::Gha::Pins::DEFAULT_HTTP_REFRESH_TIMEOUT_SECONDS
VALID_UPGRADE_LEVELS =
Kettle::Gha::Pins::VALID_UPGRADE_LEVELS
PersistentActionCache =
Kettle::Gha::Pins::PersistentActionCache
GitHubClient =
Kettle::Gha::Pins::GitHubClient
VERSION_COMMENT_SUFFIX_RE =
/\A\s+#\s*v?(?<version>\d+(?:\.\d+){0,2}(?:[-.]?[0-9A-Za-z.-]+)?)/
VERSION_COMMENT_REPLACEMENT_RE =
/\A(?<prefix>\s+#\s*)v?\d+(?:\.\d+){0,2}(?:[-.]?[0-9A-Za-z.-]+)?/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv, err: $stderr, clock: -> { Time.now }) ⇒ CLI

Returns a new instance of CLI.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/kettle/gha/pins/cli.rb', line 48

def initialize(argv, err: $stderr, clock: -> { Time.now })
  cooldown_days = begin
    Integer(ENV.fetch("KETTLE_GHA_PINS_COOLDOWN_DAYS", "0"))
  rescue ArgumentError, TypeError
    0
  end
  cooldown_days = 0 if cooldown_days.negative?
  @argv = argv
  @err = err
  @options = {
    root: File.join(Dir.pwd, ".github", "workflows"),
    dry_run: true,
    token: ENV["GITHUB_TOKEN"] || ENV["GH_TOKEN"],
    json: false,
    events: false,
    validate: true,
    write: false,
    check: false,
    api_base: API_BASE,
    user_agent: "kettle-gha-pins",
    upgrade: DEFAULT_UPGRADE_LEVEL,
    cache_path: ENV["KETTLE_GHA_SHA_PINS_CACHE"] || PersistentActionCache.default_path,
    ttl_days: DEFAULT_CACHE_TTL_SECONDS / 86_400.0,
    cooldown_days: cooldown_days,
    clock: clock,
    refresh_cache: false,
    offline: false,
    mode: :project,
    input: nil,
    reject_patterns: Set.new,
    progress: nil
  }
end

Class Method Details

.release_version_sort_key(entry) ⇒ Object



40
41
42
# File 'lib/kettle/gha/pins/cli.rb', line 40

def self.release_version_sort_key(entry)
  Kettle::Gha::Pins::VersionRubric.sort_key(entry)
end

.release_version_specificity(entry) ⇒ Object



44
45
46
# File 'lib/kettle/gha/pins/cli.rb', line 44

def self.release_version_specificity(entry)
  Kettle::Gha::Pins::VersionRubric.specificity(entry)
end

Instance Method Details

#run!Object



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kettle/gha/pins/cli.rb', line 82

def run!
  parse!

  return list_workflows! if @options[:mode] == :list

  client, persistent_cache = build_client
  return review_cache!(client: client, persistent_cache: persistent_cache) if @options[:mode] == :review

  run_project!(client: client)
rescue Error, GitHubClient::CacheMissError, GitHubClient::RefreshError => error
  @err.puts("kettle-gha-pins: #{error.message}")
  2
end