-
Notifications
You must be signed in to change notification settings - Fork 20
/
git_pr_files_changed.rs
83 lines (71 loc) · 2.7 KB
/
git_pr_files_changed.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// git_pr_files_changed.rs
// Getting all the files changed in a PR example.
use anyhow::Result;
use azure_devops_rust_api::git;
use std::collections::HashSet;
use std::env;
mod utils;
#[tokio::main]
async fn main() -> Result<()> {
// Get authentication credential
let credential = utils::get_credential()?;
// Get ADO server configuration via environment variables
let organization = env::var("ADO_ORGANIZATION").expect("Must define ADO_ORGANIZATION");
let repository_name = env::args()
.nth(1)
.expect("Usage: git_pr_files_changed <repository-name> <pull_request_id>");
let pull_request_id: i32 = env::args()
.nth(2)
.expect("Usage: git_pr_files_changed <repository-name> <pull_request_id>")
.parse()
.unwrap();
let project = env::var("ADO_PROJECT").expect("Must define ADO_PROJECT");
// Set the max number of commits to get, default is 100
let top_commits: i32 = 500;
// Create a git client
let git_client = git::ClientBuilder::new(credential).build();
// Get Commits for the PR
let pr_commits = git_client
.pull_request_commits_client()
.get_pull_request_commits(&organization, &repository_name, pull_request_id, &project)
.top(top_commits)
.await?
.value;
// Record unique filenames which are changed in the PR
let mut files_changed = HashSet::<String>::new();
// Get each commit in the PR
println!("\nCommits:");
for commit in pr_commits.iter() {
let commit_id = commit.commit_id.clone().unwrap_or("".to_string());
let comment = match &commit.comment {
Some(comment) => comment.clone(),
_ => "".to_string(),
};
println!("{} {}", commit_id, comment);
// Get the commit changes in a commit
let pr_commits_changes = git_client
.commits_client()
.get_changes(&organization, commit_id, &repository_name, &project)
.await?
.changes;
// Get files changed in the commit
for change in pr_commits_changes.iter() {
let item = &change.change.item;
// We are only interested in files not directories.
// files are "blob" type, directories are "folder" type.
if let (Some("blob"), Some(filename)) =
(item["git_object_type"].as_str(), item["path"].as_str())
{
files_changed.insert(filename.to_string());
}
}
}
println!("\nChanged files:");
// Unique files changed in the PR
for filename in files_changed.iter() {
println!("{}", filename)
}
Ok(())
}