forgectl 0.5.0

Keep a GitHub account's repositories in line with one manifest
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
mod cargo;
mod days;
mod dependabot;
mod deps;
mod diff;
mod docker;
mod gh;
mod init;
mod manifest;
mod pins;
mod progress;
mod reconcile;
mod release;
mod stale;
mod workflow;

use std::fmt::Write as _;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::sync::Mutex;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::thread;

use anyhow::{Context, Result, bail};
use clap::{Parser, Subcommand};

use crate::gh::Gh;
use crate::manifest::{Manifest, Repo};
use crate::progress::Progress;
use crate::reconcile::{Mode, Outcome, reconcile};
use crate::workflow::SharedWorkflow;

#[derive(Parser)]
#[command(version, about)]
struct Cli {
    /// Manifest describing the managed repositories [default: ./forge.toml,
    /// else forge/forge.toml in the config directory, like ~/.config]
    #[arg(long, global = true, env = "FORGE_MANIFEST")]
    manifest: Option<PathBuf>,

    #[command(subcommand)]
    command: Command,
}

#[derive(Subcommand)]
enum Command {
    /// Report where repositories drift from the manifest.
    Check {
        /// Repositories to check (default: all managed).
        repos: Vec<String>,
    },
    /// Bring repositories in line with the manifest.
    Apply {
        /// Repositories to update (default: all managed).
        repos: Vec<String>,
        /// Also overwrite secrets that already exist, from their local files
        /// (after renewing a token, say). Normally only missing ones are set.
        #[arg(long)]
        update_secrets: bool,
    },
    /// List managed repositories, and owned ones the manifest doesn't cover.
    List,
    /// Write a starter manifest listing every repository you own.
    ///
    /// It goes to --manifest, or ./forge.toml. Review it, then `forge check`.
    Init {
        /// Account whose repositories to manage [default: the one gh is
        /// signed in as]
        #[arg(long)]
        owner: Option<String>,
    },
    /// Open a PR in each repository whose `[pins]` input is behind the
    /// release it pins, moving it there, with auto-merge on.
    Bump {
        /// Repositories to bump (default: all managed).
        repos: Vec<String>,
        /// Only say which PRs would be opened.
        #[arg(long)]
        dry_run: bool,
    },
    /// Update each Rust repo's Cargo.lock to the newest versions its
    /// Cargo.toml allows, and open a PR with it once clippy and the tests
    /// pass on this machine.
    ///
    /// Repos are cloned into forge's cache directory, and kept there with
    /// their build cache for the next run.
    Deps {
        /// Repositories to update (default: every managed one with a
        /// Cargo.lock, but those with a `[deps] alerts_only` tag only while
        /// they have open Dependabot alerts).
        repos: Vec<String>,
        /// Update and run the checks, but open no PRs.
        #[arg(long)]
        dry_run: bool,
    },
    /// List open issues and PRs in any owned repo that have gone quiet.
    Stale {
        /// Days without activity before one counts as quiet.
        #[arg(long, default_value_t = 90)]
        days: i64,
    },
}

fn main() -> ExitCode {
    match run() {
        Ok(code) => code,
        Err(err) => {
            eprintln!("error: {err:#}");
            ExitCode::FAILURE
        }
    }
}

fn run() -> Result<ExitCode> {
    let cli = Cli::parse();
    if let Command::Init { owner } = cli.command {
        let path = cli.manifest.unwrap_or_else(|| PathBuf::from(MANIFEST));
        return init::run(&path, owner).map(|()| ExitCode::SUCCESS);
    }

    let path = find_manifest(cli.manifest)?;
    let mut manifest = Manifest::load(&path)?;
    let gh = Gh::new(&manifest.owner)?;
    manifest.fetch_workflows(|caller, uses| SharedWorkflow::fetch(&gh, caller, uses))?;
    if matches!(cli.command, Command::Check { .. } | Command::Bump { .. }) {
        manifest.fetch_pins(&gh)?;
    }

    match cli.command {
        Command::Check { repos } => sync(&gh, &manifest, &repos, Mode::Check),
        Command::Apply {
            repos,
            update_secrets,
        } => sync(&gh, &manifest, &repos, Mode::Apply { update_secrets }),
        Command::List => list(&gh, &manifest),
        Command::Stale { days } => stale(&gh, &manifest, days),
        Command::Bump { repos, dry_run } => bump(&gh, &manifest, &repos, dry_run),
        Command::Deps { repos, dry_run } => {
            let cache = cache_dir()
                .context("no cache directory: set XDG_CACHE_HOME or HOME")?
                .join("forge");
            let selected = select(&manifest, &repos)?;
            Ok(deps::run(
                &gh,
                &manifest,
                &selected,
                !repos.is_empty(),
                dry_run,
                &cache,
            ))
        }
        Command::Init { .. } => unreachable!("handled before loading a manifest"),
    }
}

const MANIFEST: &str = "forge.toml";

/// The manifest to use: the one given, else `./forge.toml`, else
/// `forge/forge.toml` in the config directory. Symlinks are resolved, so
/// custom rulesets are found next to the real file.
fn find_manifest(given: Option<PathBuf>) -> Result<PathBuf> {
    let path = if let Some(path) = given {
        path
    } else {
        let config = config_dir().map(|dir| dir.join("forge").join(MANIFEST));
        match config {
            _ if Path::new(MANIFEST).exists() => PathBuf::from(MANIFEST),
            Some(config) if config.exists() => config,
            config => bail!(
                "no {MANIFEST} here{}; write one with `forge init`, or pass --manifest",
                config.map_or_else(String::new, |c| format!(" or at {}", c.display()))
            ),
        }
    };
    fs::canonicalize(&path).with_context(|| format!("reading {}", path.display()))
}

/// `$XDG_CONFIG_HOME`, else `~/.config`, else (on Windows) `%APPDATA%`.
fn config_dir() -> Option<PathBuf> {
    let var = |name| std::env::var_os(name).filter(|v| !v.is_empty());
    var("XDG_CONFIG_HOME")
        .map(PathBuf::from)
        .or_else(|| var("HOME").map(|home| Path::new(&home).join(".config")))
        .or_else(|| var("APPDATA").map(PathBuf::from))
}

/// `$XDG_CACHE_HOME`, else `~/.cache`, else (on Windows) `%LOCALAPPDATA%`.
fn cache_dir() -> Option<PathBuf> {
    let var = |name| std::env::var_os(name).filter(|v| !v.is_empty());
    var("XDG_CACHE_HOME")
        .map(PathBuf::from)
        .or_else(|| var("HOME").map(|home| Path::new(&home).join(".cache")))
        .or_else(|| var("LOCALAPPDATA").map(PathBuf::from))
}

/// How many repositories to work on at once.
const WORKERS: usize = 8;

fn sync(gh: &Gh, manifest: &Manifest, names: &[String], mode: Mode) -> Result<ExitCode> {
    let repos = select(manifest, names)?;
    let progress = Progress::new(&repos.iter().map(|r| r.name.as_str()).collect::<Vec<_>>());
    let mut failed = false;

    in_parallel(
        &repos,
        |i, repo| {
            progress.start(i);
            let outcome = reconcile(gh, manifest, repo, mode);
            progress.finish(i, mark(&outcome, mode));
            outcome
        },
        |i, outcome| {
            progress.report(i, &report(&repos[i].name, &outcome, mode));
            failed |= !outcome.errors.is_empty()
                || !outcome.manual.is_empty()
                || !outcome.behind.is_empty()
                || (mode == Mode::Check && !outcome.drift.is_empty());
        },
    );

    Ok(if failed {
        ExitCode::FAILURE
    } else {
        ExitCode::SUCCESS
    })
}

fn mark(outcome: &Outcome, mode: Mode) -> &'static str {
    if !outcome.errors.is_empty() {
        "!"
    } else if !outcome.manual.is_empty() || !outcome.behind.is_empty() {
        "✗"
    } else if outcome.drift.is_empty() {
        "✓"
    } else if mode == Mode::Check {
        "✗"
    } else {
        "↻"
    }
}

/// A repo's mark and name, then a line per finding.
fn report(name: &str, outcome: &Outcome, mode: Mode) -> String {
    let mut text = format!("{} {name}", mark(outcome, mode));
    let sections = [
        ("", "", &outcome.drift),
        ("", " (fix by hand)", &outcome.manual),
        ("pending: ", "", &outcome.pending),
        ("behind: ", " (`forge bump` opens a PR)", &outcome.behind),
        ("skipped ", "", &outcome.skipped),
        ("error: ", "", &outcome.errors),
    ];
    for (prefix, suffix, lines) in sections {
        for line in lines {
            write!(text, "\n    {prefix}{line}{suffix}").expect("writing to a String");
        }
    }
    text
}

/// Maps `items` through `f` on a few threads, handing each result to `emit`
/// as soon as it and every result before it are in, so output keeps the
/// items' order without waiting for all of them. `f` and `emit` get the
/// item's index too.
fn in_parallel<T: Sync, R: Send>(
    items: &[T],
    f: impl Fn(usize, &T) -> R + Sync,
    emit: impl FnMut(usize, R) + Send,
) {
    struct Done<R, E> {
        results: Vec<Option<R>>,
        emitted: usize,
        emit: E,
    }
    let next = AtomicUsize::new(0);
    let done = Mutex::new(Done {
        results: items.iter().map(|_| None).collect(),
        emitted: 0,
        emit,
    });
    thread::scope(|scope| {
        for _ in 0..WORKERS.min(items.len()) {
            scope.spawn(|| {
                loop {
                    let i = next.fetch_add(1, Ordering::Relaxed);
                    let Some(item) = items.get(i) else { break };
                    let result = f(i, item);
                    let mut guard = done.lock().expect("worker panicked");
                    let done = &mut *guard;
                    done.results[i] = Some(result);
                    while let Some(result) =
                        done.results.get_mut(done.emitted).and_then(Option::take)
                    {
                        let at = done.emitted;
                        (done.emit)(at, result);
                        done.emitted += 1;
                    }
                    drop(guard);
                }
            });
        }
    });
}

fn select<'a>(manifest: &'a Manifest, names: &[String]) -> Result<Vec<&'a Repo>> {
    if names.is_empty() {
        return Ok(manifest.repos.iter().collect());
    }
    names
        .iter()
        .map(
            |name| match manifest.repos.iter().find(|r| &r.name == name) {
                Some(repo) => Ok(repo),
                None => bail!("{name} is not in the manifest"),
            },
        )
        .collect()
}

fn list(gh: &Gh, manifest: &Manifest) -> Result<ExitCode> {
    let calls = |repo: &Repo| {
        manifest
            .workflows
            .iter()
            .filter_map(
                |workflow| match workflow.call(gh, &manifest.owner, &repo.name) {
                    Ok(Some(call)) => Some(format!("{}: {}", workflow.name, call.describe())),
                    Ok(None) => None,
                    Err(err) => Some(format!("error: {err:#}")),
                },
            )
            .collect::<Vec<_>>()
    };
    in_parallel(
        &manifest.repos,
        |_, repo| calls(repo),
        |i, calls| {
            let repo = &manifest.repos[i];
            let tags: Vec<_> = repo.tags.iter().map(|t| format!("#{t}")).collect();
            println!(
                "{:<20} {:<16} {}",
                repo.name,
                tags.join(" "),
                calls.join(" | ")
            );
        },
    );

    let unmanaged: Vec<_> = gh
        .source_repos(&manifest.owner)?
        .into_iter()
        .filter(|name| !manifest.repos.iter().any(|r| &r.name == name))
        .collect();
    if !unmanaged.is_empty() {
        println!("\nnot managed (excluding forks and archived):");
        for name in unmanaged {
            println!("  {name}");
        }
    }

    Ok(ExitCode::SUCCESS)
}

fn bump(gh: &Gh, manifest: &Manifest, names: &[String], dry_run: bool) -> Result<ExitCode> {
    let repos = select(manifest, names)?;
    let mut failed = false;
    for (file, pin, latest) in &manifest.pins {
        let Some(latest) = latest else { continue };
        for repo in &repos {
            let result =
                pins::behind(gh, &manifest.owner, &repo.name, file, pin, latest).and_then(|b| {
                    match b {
                        None => Ok(None),
                        Some(_) if dry_run => Ok(Some("would open a PR".to_owned())),
                        Some(b) => Ok(Some(
                            pins::bump(gh, &manifest.owner, pin, &b)?
                                .unwrap_or_else(|| "a PR is open already".to_owned()),
                        )),
                    }
                });
            match result {
                Ok(Some(what)) => println!(
                    "{} {}: {} → {}: {what}",
                    repo.name, file, pin.version, latest.tag
                ),
                Ok(None) => {}
                Err(err) => {
                    failed = true;
                    eprintln!("{} {file}: error: {err:#}", repo.name);
                }
            }
        }
    }
    Ok(if failed {
        ExitCode::FAILURE
    } else {
        ExitCode::SUCCESS
    })
}

fn stale(gh: &Gh, manifest: &Manifest, days: i64) -> Result<ExitCode> {
    let repos = stale::find(gh, &manifest.owner, days, &manifest.stale.ignore_labels)?;
    if repos.is_empty() {
        println!("nothing open has been quiet for {days} days");
    }
    for (repo, lines) in repos {
        println!("{repo}");
        for line in lines {
            println!("    {line}");
        }
    }
    Ok(ExitCode::SUCCESS)
}