introduce a new ForgeIdentifier type to be persisted

This commit is contained in:
Kiril Videlov 2024-10-29 21:21:09 +01:00
parent 0721250234
commit 22310e018c
4 changed files with 50 additions and 0 deletions

View File

@ -45,6 +45,7 @@ pub fn create_series(
target: target_patch,
name: req.name,
description: req.description,
forge_ids: Default::default(),
},
req.preceding_head,
)

View File

@ -17,6 +17,25 @@ pub struct PatchReference {
pub name: String,
/// Optional description of the series. This could be markdown or anything our hearts desire.
pub description: Option<String>,
/// A list of identifiers for the review unit at possible forges (eg. Pull Request).
/// The list is empty if there is no review units, eg. no Pull Request has been created.
#[serde(default)]
pub forge_ids: Vec<ForgeIdentifier>,
}
/// Represents identifiers for the series at possible forges, eg. GitHub PR numbers.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(tag = "type", content = "subject")]
pub enum ForgeIdentifier {
GitHub(GitHubIdentifier),
}
/// Represents a GitHub Pull Request identifier.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GitHubIdentifier {
/// Pull Request number.
pub pr_number: usize,
}
/// A patch identifier which is either `CommitId` or a `ChangeId`.

View File

@ -229,6 +229,7 @@ impl Stack {
generate_branch_name(author)?
},
description: None,
forge_ids: Default::default(),
};
let state = branch_state(ctx);
@ -302,6 +303,7 @@ impl Stack {
target: current_top_head.target.clone(),
name,
description,
forge_ids: Default::default(),
};
self.add_series(ctx, new_head, Some(current_top_head.name.clone()))
}

View File

@ -58,6 +58,7 @@ fn add_series_success() -> Result<()> {
name: "asdf".into(),
target: CommitOrChangeId::ChangeId(test_ctx.commits[1].change_id().unwrap()),
description: Some("my description".into()),
forge_ids: Default::default(),
};
let result = test_ctx.branch.add_series(&ctx, reference, None);
assert!(result.is_ok());
@ -112,6 +113,7 @@ fn add_series_top_base() -> Result<()> {
name: "asdf".into(),
target: CommitOrChangeId::CommitId(merge_base.id().to_string()),
description: Some("my description".into()),
forge_ids: Default::default(),
};
let result = test_ctx.branch.add_series(&ctx, reference, None);
println!("{:?}", result);
@ -137,6 +139,7 @@ fn add_multiple_series() -> Result<()> {
name: "head_4".into(),
target: CommitOrChangeId::ChangeId(test_ctx.commits.last().unwrap().change_id().unwrap()),
description: None,
forge_ids: Default::default(),
};
let result = test_ctx
.branch
@ -148,6 +151,7 @@ fn add_multiple_series() -> Result<()> {
name: "head_2".into(),
target: CommitOrChangeId::ChangeId(test_ctx.commits.last().unwrap().change_id().unwrap()),
description: None,
forge_ids: Default::default(),
};
let result = test_ctx.branch.add_series(&ctx, head_2, None);
assert!(result.is_ok());
@ -160,6 +164,7 @@ fn add_multiple_series() -> Result<()> {
name: "head_1".into(),
target: CommitOrChangeId::ChangeId(test_ctx.commits.first().unwrap().change_id().unwrap()),
description: None,
forge_ids: Default::default(),
};
let result = test_ctx.branch.add_series(&ctx, head_1, None);
@ -185,6 +190,7 @@ fn add_series_commit_id_when_change_id_available() -> Result<()> {
name: "asdf".into(),
target: CommitOrChangeId::CommitId(test_ctx.commits[1].id().to_string()),
description: None,
forge_ids: Default::default(),
};
let result = test_ctx.branch.add_series(&ctx, reference, None);
assert_eq!(
@ -206,6 +212,7 @@ fn add_series_invalid_name_fails() -> Result<()> {
name: "name with spaces".into(),
target: CommitOrChangeId::CommitId(test_ctx.commits[0].id().to_string()),
description: None,
forge_ids: Default::default(),
};
let result = test_ctx.branch.add_series(&ctx, reference, None);
assert_eq!(result.err().unwrap().to_string(), "Invalid branch name");
@ -221,6 +228,7 @@ fn add_series_duplicate_name_fails() -> Result<()> {
name: "asdf".into(),
target: CommitOrChangeId::ChangeId(test_ctx.commits[1].change_id().unwrap()),
description: None,
forge_ids: Default::default(),
};
let result = test_ctx.branch.add_series(&ctx, reference.clone(), None);
assert!(result.is_ok());
@ -241,6 +249,7 @@ fn add_series_matching_git_ref_is_ok() -> Result<()> {
name: "existing-branch".into(),
target: test_ctx.commits[0].clone().into(),
description: None,
forge_ids: Default::default(),
};
let result = test_ctx.branch.add_series(&ctx, reference.clone(), None);
assert!(result.is_ok()); // allow this
@ -256,6 +265,7 @@ fn add_series_including_refs_head_fails() -> Result<()> {
name: "refs/heads/my-branch".into(),
target: CommitOrChangeId::CommitId(test_ctx.commits[0].id().to_string()),
description: None,
forge_ids: Default::default(),
};
let result = test_ctx.branch.add_series(&ctx, reference.clone(), None);
assert_eq!(
@ -274,6 +284,7 @@ fn add_series_target_commit_doesnt_exist() -> Result<()> {
name: "my-branch".into(),
target: CommitOrChangeId::CommitId("30696678319e0fa3a20e54f22d47fc8cf1ceaade".into()), // does not exist
description: None,
forge_ids: Default::default(),
};
let result = test_ctx.branch.add_series(&ctx, reference.clone(), None);
assert!(result
@ -293,6 +304,7 @@ fn add_series_target_change_id_doesnt_exist() -> Result<()> {
name: "my-branch".into(),
target: CommitOrChangeId::ChangeId("does-not-exist".into()), // does not exist
description: None,
forge_ids: Default::default(),
};
let result = test_ctx.branch.add_series(&ctx, reference.clone(), None);
assert_eq!(
@ -312,6 +324,7 @@ fn add_series_target_commit_not_in_stack() -> Result<()> {
name: "my-branch".into(),
target: CommitOrChangeId::CommitId(other_commit_id.clone()), // does not exist
description: None,
forge_ids: Default::default(),
};
let result = test_ctx.branch.add_series(&ctx, reference.clone(), None);
assert_eq!(
@ -368,6 +381,7 @@ fn remove_series_with_multiple_last_heads() -> Result<()> {
name: "to_stay".into(),
target: CommitOrChangeId::ChangeId(test_ctx.commits.last().unwrap().change_id().unwrap()),
description: None,
forge_ids: Default::default(),
};
let result = test_ctx.branch.add_series(&ctx, to_stay.clone(), None);
assert!(result.is_ok());
@ -399,6 +413,7 @@ fn remove_series_no_orphan_commits() -> Result<()> {
name: "to_stay".into(),
target: CommitOrChangeId::ChangeId(test_ctx.commits.first().unwrap().change_id().unwrap()),
description: None,
forge_ids: Default::default(),
}; // references the oldest commit
let result = test_ctx.branch.add_series(&ctx, to_stay.clone(), None);
assert!(result.is_ok());
@ -561,6 +576,7 @@ fn update_series_target_success() -> Result<()> {
name: "series_1".into(),
target: commit_0_change_id.clone(),
description: None,
forge_ids: Default::default(),
};
let result = test_ctx.branch.add_series(&ctx, series_1, None);
assert!(result.is_ok());
@ -662,6 +678,7 @@ fn list_series_two_heads_same_commit() -> Result<()> {
name: "head_before".into(),
target: CommitOrChangeId::ChangeId(test_ctx.commits.last().unwrap().change_id().unwrap()),
description: None,
forge_ids: Default::default(),
};
// add `head_before` before the initial head
let result = test_ctx.branch.add_series(&ctx, head_before, None);
@ -697,6 +714,7 @@ fn list_series_two_heads_different_commit() -> Result<()> {
// point to the first commit
target: CommitOrChangeId::ChangeId(test_ctx.commits.first().unwrap().change_id().unwrap()),
description: None,
forge_ids: Default::default(),
};
// add `head_before` before the initial head
let result = test_ctx.branch.add_series(&ctx, head_before, None);
@ -761,6 +779,7 @@ fn replace_head_single() -> Result<()> {
name: "from_head".into(),
target: CommitOrChangeId::ChangeId(test_ctx.commits[1].change_id().unwrap()),
description: None,
forge_ids: Default::default(),
};
test_ctx.branch.add_series(&ctx, from_head, None)?;
// replace with previous head
@ -792,6 +811,7 @@ fn replace_head_single_with_merge_base() -> Result<()> {
name: "from_head".into(),
target: CommitOrChangeId::ChangeId(test_ctx.commits[1].change_id().unwrap()),
description: None,
forge_ids: Default::default(),
};
test_ctx.branch.add_series(&ctx, from_head, None)?;
// replace with merge base
@ -827,6 +847,7 @@ fn replace_head_with_invalid_commit_error() -> Result<()> {
name: "from_head".into(),
target: CommitOrChangeId::ChangeId(test_ctx.commits[1].change_id().unwrap()),
description: None,
forge_ids: Default::default(),
};
test_ctx.branch.add_series(&ctx, from_head, None)?;
let stack = test_ctx.branch.clone();
@ -853,6 +874,7 @@ fn replace_head_with_same_noop() -> Result<()> {
name: "from_head".into(),
target: CommitOrChangeId::ChangeId(test_ctx.commits[1].change_id().unwrap()),
description: None,
forge_ids: Default::default(),
};
test_ctx.branch.add_series(&ctx, from_head, None)?;
let stack = test_ctx.branch.clone();
@ -938,11 +960,13 @@ fn replace_head_multiple() -> Result<()> {
name: "from_head_1".into(),
target: CommitOrChangeId::ChangeId(test_ctx.commits[1].change_id().unwrap()),
description: None,
forge_ids: Default::default(),
};
let from_head_2 = PatchReference {
name: "from_head_2".into(),
target: CommitOrChangeId::ChangeId(test_ctx.commits[1].change_id().unwrap()),
description: None,
forge_ids: Default::default(),
};
// both references point to the same commit
test_ctx.branch.add_series(&ctx, from_head_1, None)?;
@ -982,6 +1006,7 @@ fn replace_head_top_of_stack_multiple() -> Result<()> {
name: "extra_head".into(),
target: CommitOrChangeId::ChangeId(test_ctx.commits[1].change_id().unwrap()),
description: None,
forge_ids: Default::default(),
};
// an extra head just beneath the top of the stack
test_ctx.branch.add_series(&ctx, extra_head, None)?;
@ -1047,6 +1072,7 @@ fn set_legacy_refname_multiple_heads() -> Result<()> {
name: "extra_head".into(),
target: CommitOrChangeId::ChangeId(test_ctx.commits[1].change_id().unwrap()),
description: None,
forge_ids: Default::default(),
};
// an extra head just beneath the top of the stack
test_ctx.branch.add_series(&ctx, extra_head, None)?;
@ -1117,6 +1143,7 @@ fn prune_heads_success() -> Result<()> {
target: test_ctx.other_commits.first().cloned().unwrap().into(),
name: "foo".to_string(),
description: None,
forge_ids: Default::default(),
},
);
assert_eq!(test_ctx.branch.heads.len(), 2);
@ -1146,6 +1173,7 @@ fn does_not_prune_head_on_merge_base() -> Result<()> {
target: merge_base.into(),
name: "bottom".to_string(),
description: None,
forge_ids: Default::default(),
},
None,
)?;