Skip to content

Commit

Permalink
Merge pull request #13 from TicClick/fix-merge-rc
Browse files Browse the repository at this point in the history
  • Loading branch information
TicClick authored Oct 6, 2023
2 parents a167270 + e0ee093 commit a4e7233
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 61 deletions.
41 changes: 38 additions & 3 deletions src/controller/controller_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,30 @@ impl<T: GitHubInterface> Controller<T> {
reply_to.send(self.init().await).unwrap();
}

ControllerRequest::PullRequestCreated {
full_repo_name,
pull_request,
trigger_updates,
} => {
let pull_number = pull_request.number;
self.upsert_pull(&full_repo_name, *pull_request, trigger_updates)
.await
.unwrap_or_else(|e| {
log::error!(
"Pull #{}: failed to add information and trigger comments: {:?}",
pull_number,
e
);
})
}

ControllerRequest::PullRequestUpdated {
full_repo_name,
pull_request,
trigger_updates,
} => {
let pull_number = pull_request.number;
self.add_pull(&full_repo_name, *pull_request, trigger_updates)
self.update_pull(&full_repo_name, *pull_request, trigger_updates)
.await
.unwrap_or_else(|e| {
log::error!(
Expand Down Expand Up @@ -178,7 +195,7 @@ impl<T: GitHubInterface> Controller<T> {
/// Add a repository and fetch its pull requests.
async fn add_repository(&self, r: &Repository) -> Result<()> {
for p in self.github.pulls(&r.full_name).await? {
self.add_pull(&r.full_name, p, false).await?;
self.upsert_pull(&r.full_name, p, false).await?;
}
Ok(())
}
Expand Down Expand Up @@ -214,12 +231,30 @@ impl<T: GitHubInterface> Controller<T> {
.remove_conflicts_by_pull(full_repo_name, closed_pull.number);
}

async fn update_pull(
&self,
full_repo_name: &str,
new_pull: PullRequest,
trigger_updates: bool,
) -> Result<()> {
if self.memory.contains(full_repo_name, &new_pull) {
self.upsert_pull(full_repo_name, new_pull, trigger_updates)
.await
} else {
log::info!(
"Pull #{} can't be updated because it wasn't added in the first place",
new_pull.number
);
Ok(())
}
}

/// Handle pull request changes. This includes fetching a `.diff` file from another GitHub domain,
/// which may have its own rate limits.
///
/// If `trigger_updates` is set, check if the update conflicts with existing pull requests,
/// and make its author aware (or other PRs' owners, in rare cases). For details, see [`helpers::conflicts::Storage`].
async fn add_pull(
async fn upsert_pull(
&self,
full_repo_name: &str,
mut new_pull: PullRequest,
Expand Down
38 changes: 37 additions & 1 deletion src/controller/controller_impl/tests/tests_base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,42 @@ async fn test_handle_message_pull_request_updated() {
.0
.unwrap();

assert!(ctrl.lock().await.memory.pulls("test/repo-name").is_none());
}

#[tokio::test]
async fn test_handle_message_pull_request_created_and_updated() {
let (request_tx, c) = make_controller(true).await;
let ctrl = Arc::new(Mutex::new(c));

let pr = ctrl
.lock()
.await
.github
.test_add_pull("test/repo-name", &["wiki/Article/en.md"]);
let _ = request_tx
.send(ControllerRequest::PullRequestCreated {
full_repo_name: "test/repo-name".into(),
pull_request: Box::new(pr.clone()),
trigger_updates: false,
})
.await;
let _ = request_tx
.send(ControllerRequest::PullRequestUpdated {
full_repo_name: "test/repo-name".into(),
pull_request: Box::new(pr),
trigger_updates: false,
})
.await;

drop(request_tx);
let cloned = ctrl.clone();
tokio::join!(tokio::spawn(async move {
cloned.lock().await.run_forever().await;
}))
.0
.unwrap();

assert_eq!(
ctrl.lock()
.await
Expand All @@ -127,7 +163,7 @@ async fn test_handle_message_pull_request_closed() {
.github
.test_add_pull("test/repo-name", &["wiki/Article/en.md"]);
let _ = request_tx
.send(ControllerRequest::PullRequestUpdated {
.send(ControllerRequest::PullRequestCreated {
full_repo_name: "test/repo-name".into(),
pull_request: Box::new(pr.clone()),
trigger_updates: false,
Expand Down
36 changes: 18 additions & 18 deletions src/controller/controller_impl/tests/tests_comments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::helpers::{comments::CommentHeader, conflicts::ConflictType};
async fn test_no_conflict_no_comment() {
let c = new_controller(true).await;
let p1 = c.github.test_add_pull("test/repo", &["wiki/Article/en.md"]);
c.add_pull(
c.upsert_pull(
"test/repo",
c.github.fetch_pull("test/repo", p1.number),
true,
Expand All @@ -27,14 +27,14 @@ async fn test_one_conflict_one_comment() {
let p1 = c.github.test_add_pull("test/repo", &["wiki/Article/en.md"]);
let p2 = c.github.test_add_pull("test/repo", &["wiki/Article/en.md"]);

c.add_pull(
c.upsert_pull(
"test/repo",
c.github.fetch_pull("test/repo", p1.number),
true,
)
.await
.unwrap();
c.add_pull(
c.upsert_pull(
"test/repo",
c.github.fetch_pull("test/repo", p2.number),
true,
Expand Down Expand Up @@ -65,7 +65,7 @@ async fn test_one_conflict_one_valid_header() {
c.github.test_add_pull("test/repo", &["wiki/Article/en.md"]),
];
for p in pulls.iter() {
c.add_pull(
c.upsert_pull(
"test/repo",
c.github.fetch_pull("test/repo", p.number),
true,
Expand Down Expand Up @@ -100,7 +100,7 @@ async fn test_one_pull_and_conflict_one_comment() {
),
];
for p in pulls.iter() {
c.add_pull(
c.upsert_pull(
"test/repo",
c.github.fetch_pull("test/repo", p.number),
true,
Expand All @@ -114,7 +114,7 @@ async fn test_one_pull_and_conflict_one_comment() {
pulls[0].number,
&["wiki/Article/en.md", "wiki/Other_article/en.md"],
);
c.add_pull(
c.upsert_pull(
"test/repo",
c.github.fetch_pull("test/repo", pulls[0].number),
true,
Expand All @@ -124,7 +124,7 @@ async fn test_one_pull_and_conflict_one_comment() {

c.github
.test_update_pull("test/repo", pulls[0].number, &["wiki/Other_article/en.md"]);
c.add_pull(
c.upsert_pull(
"test/repo",
c.github.fetch_pull("test/repo", pulls[0].number),
true,
Expand All @@ -151,7 +151,7 @@ async fn test_one_pull_and_conflict_one_comment_updated() {
),
];
for p in pulls.iter() {
c.add_pull(
c.upsert_pull(
"test/repo",
c.github.fetch_pull("test/repo", p.number),
true,
Expand All @@ -165,7 +165,7 @@ async fn test_one_pull_and_conflict_one_comment_updated() {
pulls[0].number,
&["wiki/Article/en.md", "wiki/Other_article/en.md"],
);
c.add_pull(
c.upsert_pull(
"test/repo",
c.github.fetch_pull("test/repo", pulls[0].number),
true,
Expand All @@ -175,7 +175,7 @@ async fn test_one_pull_and_conflict_one_comment_updated() {

c.github
.test_update_pull("test/repo", pulls[0].number, &["wiki/Other_article/en.md"]);
c.add_pull(
c.upsert_pull(
"test/repo",
c.github.fetch_pull("test/repo", pulls[0].number),
true,
Expand Down Expand Up @@ -223,7 +223,7 @@ async fn test_post_comment_per_pull_and_conflict_combination() {
.test_add_pull("test/repo", &["wiki/New_article/en.md"]),
];
for p in pulls.iter() {
c.add_pull(
c.upsert_pull(
"test/repo",
c.github.fetch_pull("test/repo", p.number),
true,
Expand Down Expand Up @@ -275,7 +275,7 @@ async fn test_obsolete_comment_is_removed() {
c.github.test_add_pull("test/repo", &["wiki/Article/en.md"]),
];
for p in pulls.iter() {
c.add_pull(
c.upsert_pull(
"test/repo",
c.github.fetch_pull("test/repo", p.number),
true,
Expand All @@ -286,7 +286,7 @@ async fn test_obsolete_comment_is_removed() {

c.github
.test_update_pull("test/repo", 1, &["wiki/Article_2/ru.md"]);
c.add_pull("test/repo", c.github.fetch_pull("test/repo", 1), true)
c.upsert_pull("test/repo", c.github.fetch_pull("test/repo", 1), true)
.await
.unwrap();
assert!(c
Expand All @@ -311,7 +311,7 @@ async fn test_only_target_comment_is_removed() {
),
];
for p in pulls.iter() {
c.add_pull(
c.upsert_pull(
"test/repo",
c.github.fetch_pull("test/repo", p.number),
true,
Expand All @@ -327,7 +327,7 @@ async fn test_only_target_comment_is_removed() {

c.github
.test_update_pull("test/repo", 1, &["wiki/Article/Other_article/en.md"]);
c.add_pull("test/repo", c.github.fetch_pull("test/repo", 1), true)
c.upsert_pull("test/repo", c.github.fetch_pull("test/repo", 1), true)
.await
.unwrap();

Expand All @@ -351,7 +351,7 @@ async fn test_new_comment_is_posted_after_removal_in_different_pull() {
c.github.test_add_pull("test/repo", &["wiki/Article/ru.md"]),
];
for p in pulls.iter() {
c.add_pull(
c.upsert_pull(
"test/repo",
c.github.fetch_pull("test/repo", p.number),
true,
Expand All @@ -369,7 +369,7 @@ async fn test_new_comment_is_posted_after_removal_in_different_pull() {

c.github
.test_update_pull("test/repo", 1, &["wiki/Article/Other_article/en.md"]);
c.add_pull("test/repo", c.github.fetch_pull("test/repo", 1), true)
c.upsert_pull("test/repo", c.github.fetch_pull("test/repo", 1), true)
.await
.unwrap();

Expand All @@ -382,7 +382,7 @@ async fn test_new_comment_is_posted_after_removal_in_different_pull() {

c.github
.test_update_pull("test/repo", 1, &["wiki/Article/ru.md"]);
c.add_pull("test/repo", c.github.fetch_pull("test/repo", 1), true)
c.upsert_pull("test/repo", c.github.fetch_pull("test/repo", 1), true)
.await
.unwrap();

Expand Down
Loading

0 comments on commit a4e7233

Please sign in to comment.