Skip to main content

flowey_lib_common/
git_merge_commit.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! Gets the merge commit of a PR to base branch
5
6use flowey::node::prelude::*;
7
8flowey_request! {
9    pub struct Request {
10        pub repo_path: ReadVar<PathBuf>,
11        pub merge_commit: WriteVar<String>,
12        pub base_branch: ReadVar<String>,
13    }
14}
15
16new_simple_flow_node!(struct Node);
17
18impl SimpleFlowNode for Node {
19    type Request = Request;
20
21    fn imports(ctx: &mut ImportCtx<'_>) {
22        ctx.import::<crate::install_git::Node>();
23    }
24
25    fn process_request(request: Self::Request, ctx: &mut NodeCtx<'_>) -> anyhow::Result<()> {
26        let Request {
27            repo_path,
28            merge_commit,
29            base_branch,
30        } = request;
31
32        ctx.emit_rust_step("get merge commit", move |ctx| {
33            let merge_commit = merge_commit.claim(ctx);
34            let repo_path = repo_path.claim(ctx);
35            let base_branch = base_branch.claim(ctx);
36
37            move |rt| {
38                let repo_path = rt.read(repo_path);
39                let base_branch = rt.read(base_branch);
40
41                rt.sh.change_dir(repo_path);
42
43                flowey::shell_cmd!(rt, "git fetch --unshallow").run()?;
44
45                flowey::shell_cmd!(rt, "git fetch origin {base_branch}").run()?;
46                let commit =
47                    flowey::shell_cmd!(rt, "git merge-base HEAD origin/{base_branch}").read()?;
48                rt.write(merge_commit, &commit);
49
50                Ok(())
51            }
52        });
53
54        Ok(())
55    }
56}