github_workflows_update/
processor.rs

1// Copyright (C) 2022 Leandro Lisboa Penz <lpenz@lpenz.org>
2// This file is subject to the terms and conditions defined in
3// file 'LICENSE', which is part of this source code package.
4
5//! Top level file processing function.
6
7use anyhow::Result;
8use std::path;
9use tracing::Level;
10use tracing::event;
11use tracing::instrument;
12
13use crate::cmd::OutputFormat;
14use crate::proxy;
15use crate::workflow::Workflow;
16
17/// Process the provided file.
18/// Returns true if any outdated entities were found, false otherwise.
19#[instrument(level="info", fields(filename = ?filename.as_ref().display()))]
20pub async fn process_file(
21    dryrun: bool,
22    output_format: OutputFormat,
23    proxy_server: &proxy::Server,
24    filename: impl AsRef<path::Path>,
25) -> Result<bool> {
26    let filename = filename.as_ref();
27    let mut workflow = match Workflow::new(filename).await {
28        Ok(entities) => entities,
29        Err(e) => {
30            event!(
31                Level::ERROR,
32                error = ?e,
33                filename = ?filename,
34            );
35            return Err(e);
36        }
37    };
38    workflow.fetch_latest_versions(proxy_server).await;
39    let dryrunmsg = if dryrun { " (dryrun)" } else { "" };
40    let mut any_outdated = false;
41    for (resource, current_version) in &workflow.uses {
42        if let Some(latest_version) = workflow.latest.get(resource) {
43            if current_version == latest_version {
44                continue;
45            }
46            any_outdated = true;
47            match output_format {
48                OutputFormat::Standard => {
49                    println!(
50                        "{}: update {} from {} to {}{}",
51                        filename.display(),
52                        resource,
53                        current_version,
54                        latest_version,
55                        dryrunmsg
56                    );
57                }
58                OutputFormat::GithubWarning => {
59                    println!(
60                        "::warning file={}::update {} from {} to {}",
61                        filename.display(),
62                        resource,
63                        current_version,
64                        latest_version,
65                    );
66                }
67            }
68        }
69    }
70    if !dryrun {
71        match workflow.update_file().await {
72            Ok(true) => {
73                event!(
74                    Level::INFO,
75                    filename = ?filename,
76                    "updated"
77                );
78            }
79            Ok(false) => {
80                event!(
81                    Level::INFO,
82                    filename = ?filename,
83                    "unchanged"
84                );
85            }
86            Err(e) => {
87                event!(
88                    Level::ERROR,
89                    error = ?e,
90                    filename = ?filename,
91                    "error writing updated file"
92                );
93            }
94        }
95    }
96    Ok(any_outdated)
97}