-
Notifications
You must be signed in to change notification settings - Fork 20
/
git_pr_create.rs
59 lines (48 loc) · 2.08 KB
/
git_pr_create.rs
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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// git_pr_create.rs
// Create Pull Request example.
use anyhow::Result;
use azure_devops_rust_api::git;
use git::models::{GitPullRequestCreateOptions, WebApiCreateTagRequestData};
use std::env;
mod utils;
#[tokio::main]
async fn main() -> Result<()> {
// Get authentication credential
let credential = utils::get_credential()?;
const USAGE: &str =
"Usage: git_pr_create <repository-name> <src_branch> <target_branch> <title> <description>";
// Get ADO server configuration via environment variables
let organization = env::var("ADO_ORGANIZATION").expect("Must define ADO_ORGANIZATION");
let project = env::var("ADO_PROJECT").expect("Must define ADO_PROJECT");
let repo_name = env::args().nth(1).expect(USAGE);
let src_branch: String = env::args().nth(2).expect(USAGE);
let target_branch: String = env::args().nth(3).expect(USAGE);
let title: String = env::args().nth(4).expect(USAGE);
let description: String = env::args().nth(5).expect(USAGE);
// Create a git client
let git_client = git::ClientBuilder::new(credential).build();
// Create GitPullRequestCreateOptions with all the mandatory parameters
println!("Create PR to merge {} => {}", src_branch, target_branch);
let mut pr_create_options = GitPullRequestCreateOptions::new(
// Need to specify full git refs path
format!("refs/heads/{src_branch}"),
format!("refs/heads/{target_branch}"),
title,
);
// Set any additional optional parameters
pr_create_options.description = Some(description);
// Label creation is unfortunately currently not very ergonomic...
pr_create_options.labels = vec![
WebApiCreateTagRequestData::new("example_label1".to_string()),
WebApiCreateTagRequestData::new("example_label2".to_string()),
];
// Define the new PR
let pr = git_client
.pull_requests_client()
.create(organization, repo_name, project, pr_create_options)
.await?;
println!("Created PR:\n{:#?}", pr);
Ok(())
}