Skip to content

Commit

Permalink
Updates vsts-api-patcher to reflect valid use of 'Change' (#539)
Browse files Browse the repository at this point in the history
* Removes modification of vsts-api-patcher which causes 'item' to be required

* Updates Change struct in azure_devops_rust_api

* Fix up examples to handle optional Change::item. Update README.

---------

Co-authored-by: John Batty <[email protected]>
  • Loading branch information
emagers and johnbatty authored Dec 20, 2024
1 parent db3cbad commit 85a61d7
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 18 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
- Clippy fixes for `needless_lifetime` warnings.

### Breaking change
- Change git 'Change::item' field to be optional.
- Discovered that in some returned values this field is not present.
Any code accessing this field now needs to wrap it in an `Option`, or deal with the returned value being wrapped in an `Option`.

## [0.24.0]

### Breaking change
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,12 @@ async fn main() -> Result<()> {

// Get files name which are present in the target branch
for diff in diffs.iter() {
let git_object_type = diff.change.item["gitObjectType"].as_str().unwrap();
if git_object_type == "blob" {
let file_name = diff.change.item["path"].as_str().unwrap();
files_diff_between_branches.insert(file_name.to_string());
if let Some(item) = &diff.change.item {
let git_object_type = item["gitObjectType"].as_str().unwrap();
if git_object_type == "blob" {
let file_name = item["path"].as_str().unwrap();
files_diff_between_branches.insert(file_name.to_string());
}
}
}

Expand Down
15 changes: 8 additions & 7 deletions azure_devops_rust_api/examples/git_pr_files_changed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,14 @@ async fn main() -> Result<()> {

// 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());
if let Some(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());
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions azure_devops_rust_api/examples/git_push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ fn new_git_change_with_content(
GitChange {
change: Change {
change_type,
item: json!({
item: Some(json!({
"path": filename
}),
})),
new_content: Some(ItemContent {
content: file_content,
content_type: ContentType::RawText,
Expand Down
7 changes: 4 additions & 3 deletions azure_devops_rust_api/src/git/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ pub struct Change {
#[doc = "The type of change that was made to the item."]
#[serde(rename = "changeType")]
pub change_type: change::ChangeType,
pub item: serde_json::Value,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub item: Option<serde_json::Value>,
#[serde(
rename = "newContent",
default,
Expand All @@ -372,10 +373,10 @@ pub struct Change {
pub url: Option<String>,
}
impl Change {
pub fn new(change_type: change::ChangeType, item: serde_json::Value) -> Self {
pub fn new(change_type: change::ChangeType) -> Self {
Self {
change_type,
item,
item: None,
new_content: None,
source_server_item: None,
url: None,
Expand Down
3 changes: 1 addition & 2 deletions vsts-api-patcher/src/patcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1186,8 +1186,7 @@ impl Patcher {
"git.json",
"Change",
r#"[
"changeType",
"item"
"changeType"
]"#,
),
(
Expand Down

0 comments on commit 85a61d7

Please sign in to comment.