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: 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
36            move |rt| {
37                let sh = xshell::Shell::new()?;
38                let repo_path = rt.read(repo_path);
39
40                sh.change_dir(repo_path);
41
42                xshell::cmd!(sh, "git fetch --unshallow").run()?;
43
44                xshell::cmd!(sh, "git fetch origin {base_branch}").run()?;
45                let commit = xshell::cmd!(sh, "git merge-base HEAD origin/{base_branch}").read()?;
46                rt.write(merge_commit, &commit);
47
48                Ok(())
49            }
50        });
51
52        Ok(())
53    }
54}