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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
use anyhow::Result;
use std::collections::HashMap;
use std::path::Path;
use std::process::ExitCode;
use crate::auth;
use crate::github::GitHubClient;
use crate::output::{PinReport, PinResult, PinSkip};
use crate::workflow::{self, RefType};
pub async fn run(repo_root: &Path, json: bool, apply: bool) -> Result<ExitCode> {
let token = auth::require_token().await?;
let client = GitHubClient::new(token);
let files = workflow::find_workflows(repo_root)?;
let mut report = PinReport {
pinned: Vec::new(),
skipped: Vec::new(),
applied: apply,
};
let mut resolve_cache: HashMap<String, (String, String)> = HashMap::new();
for file in &files {
let display_name = workflow::display_path(file, repo_root);
if !json {
eprintln!("Scanning {display_name}...");
}
let actions = workflow::scan_workflow(file)?;
let mut replacements: Vec<(usize, String)> = Vec::new();
for action in &actions {
match action.ref_type {
RefType::Sha => {}
RefType::Branch => {
report.skipped.push(PinSkip {
file: workflow::display_path(file, repo_root),
action: format!("{}@{}", action.full_name(), action.ref_string),
reason: "branch ref — pin to a SHA manually".to_string(),
line: action.line_number,
});
}
RefType::SlidingTag | RefType::Tag => {
let cache_key =
format!("{}/{}@{}", action.owner, action.repo, action.ref_string);
if !json {
eprint!(
" Resolving {}@{}...",
action.full_name(),
action.ref_string
);
}
let resolved = if let Some(hit) = resolve_cache.get(&cache_key) {
if !json {
eprintln!(" cached");
}
Ok(hit.clone())
} else {
match client
.resolve_tag(&action.owner, &action.repo, &action.ref_string)
.await
{
Ok(sha) => {
let tag = client
.find_exact_tag(
&action.owner,
&action.repo,
&sha,
&action.ref_string,
)
.await;
if !json {
eprintln!(" done");
}
resolve_cache.insert(cache_key, (sha.clone(), tag.clone()));
Ok((sha, tag))
}
Err(e) => {
if !json {
eprintln!(" failed");
}
Err(e)
}
}
};
match resolved {
Ok((sha, tag)) => {
if action.ref_type == RefType::SlidingTag {
report.skipped.push(PinSkip {
file: workflow::display_path(file, repo_root),
action: format!("{}@{}", action.full_name(), action.ref_string),
reason: format!("sliding tag, resolved to {tag}"),
line: action.line_number,
});
}
if let Some(new_line) =
workflow::build_pinned_line(&action.raw_line, &sha, &tag)
{
replacements.push((action.line_number, new_line));
report.pinned.push(PinResult {
file: workflow::display_path(file, repo_root),
action: action.full_name(),
old_ref: action.ref_string.clone(),
sha,
tag,
line: action.line_number,
});
}
}
Err(e) => {
report.skipped.push(PinSkip {
file: workflow::display_path(file, repo_root),
action: format!("{}@{}", action.full_name(), action.ref_string),
reason: format!("{e}"),
line: action.line_number,
});
}
}
}
}
}
if apply && !replacements.is_empty() {
workflow::rewrite_actions(file, &replacements)?;
}
}
if json {
report.print_json();
} else {
report.print_human();
}
if !apply && !report.pinned.is_empty() {
Ok(ExitCode::from(1))
} else {
Ok(ExitCode::SUCCESS)
}
}