flowey_lib_common/
gh_latest_completed_workflow_id.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Gets the latest completed Github workflow id for a pipeline and branch
5use flowey::node::prelude::*;
6
7flowey_request! {
8    pub struct Request {
9        pub repo: String,
10        pub pipeline_name: String,
11        pub branch: ReadVar<String>,
12        pub gh_workflow_id: WriteVar<String>,
13    }
14}
15new_simple_flow_node!(struct Node);
16
17impl SimpleFlowNode for Node {
18    type Request = Request;
19
20    fn imports(ctx: &mut ImportCtx<'_>) {
21        ctx.import::<crate::use_gh_cli::Node>();
22    }
23
24    fn process_request(request: Self::Request, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
25        let Request {
26            repo,
27            gh_workflow_id,
28            pipeline_name,
29            branch,
30        } = request;
31
32        let pipeline_name = pipeline_name.clone();
33
34        let gh_cli = ctx.reqv(crate::use_gh_cli::Request::Get);
35
36        ctx.emit_rust_step("get latest completed action id", |ctx| {
37            let pipeline_name = pipeline_name.clone();
38            let gh_cli = gh_cli.claim(ctx);
39            let gh_workflow_id = gh_workflow_id.claim(ctx);
40            let branch = branch.claim(ctx);
41
42            move |rt| {
43                let sh = xshell::Shell::new()?;
44                let gh_cli = rt.read(gh_cli);
45                let branch = rt.read(branch);
46
47                let id = xshell::cmd!(
48                    sh,
49                    "{gh_cli} run list -R {repo} -b {branch} -w {pipeline_name} -s completed --limit 1 --json databaseId -q .[0].databaseId"
50                )
51                .read()?;
52
53                log::info!("Got action id {id}");
54                rt.write(gh_workflow_id, &id);
55
56                Ok(())
57            }
58        });
59
60        Ok(())
61    }
62}