-
Notifications
You must be signed in to change notification settings - Fork 373
Expand file tree
/
Copy pathpin-cpflow-github-ref
More file actions
executable file
·72 lines (56 loc) · 1.97 KB
/
Copy pathpin-cpflow-github-ref
File metadata and controls
executable file
·72 lines (56 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
#!/usr/bin/env ruby
# frozen_string_literal: true
require "pathname"
USAGE = <<~USAGE
Usage: bin/pin-cpflow-github-ref [--allow-moving-ref] <control-plane-flow-ref>
Use a release tag for normal operation, e.g. v5.0.0.
Use a full 40-character commit SHA for temporary unreleased upstream testing.
Use --allow-moving-ref only for short-lived local branch/ref experiments.
USAGE
ALLOWED_OPTIONS = ["--allow-moving-ref"].freeze
FULL_COMMIT_SHA = /\A[0-9a-f]{40}\z/i
RELEASE_TAG = /\Av\d+\.\d+\.\d+(?:[-.][0-9A-Za-z][0-9A-Za-z.-]*)?\z/
options, positional = ARGV.partition { |arg| arg.start_with?("--") }
unknown_options = options - ALLOWED_OPTIONS
unless unknown_options.empty?
warn "Unknown option(s): #{unknown_options.join(', ')}"
warn USAGE
exit 1
end
unless positional.length == 1
warn USAGE
exit 1
end
ref = positional.first
allow_moving_ref = options.include?("--allow-moving-ref")
unless ref.match?(/\A[0-9A-Za-z._\/-]+\z/)
warn "Ref contains unsupported characters: #{ref.inspect}"
exit 1
end
unless ref.match?(FULL_COMMIT_SHA) || ref.match?(RELEASE_TAG) || allow_moving_ref
warn "Ref must be a full 40-character commit SHA or a v-prefixed release tag: #{ref.inspect}"
warn "Use --allow-moving-ref only for a short-lived branch/ref experiment."
exit 1
end
root = Pathname.new(__dir__).join("..").expand_path
workflow_paths = Dir[root.join(".github/workflows/cpflow-*.yml")].sort
if workflow_paths.empty?
warn "No cpflow workflow wrappers found."
exit 1
end
changed = []
workflow_paths.each do |path|
text = File.read(path)
updated = text
.gsub(%r{(uses:\s+shakacode/control-plane-flow/\.github/workflows/[^@\s]+@)[^\s]+}, "\\1#{ref}")
.gsub(/(\bcontrol_plane_flow_ref:\s*)\S+/, "\\1#{ref}")
next if updated == text
File.write(path, updated)
changed << Pathname.new(path).relative_path_from(root).to_s
end
puts "Pinned cpflow GitHub ref to #{ref}"
if changed.empty?
puts "No files changed."
else
changed.each { |path| puts "updated #{path}" }
end