Skip to content

Commit f1778ab

Browse files
authored
Add a validation to check if the branch already exists (#297)
1 parent 98ad9d4 commit f1778ab

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

fplus-lib/src/core/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4947,7 +4947,7 @@ impl LDNPullRequest {
49474947
application_id: application_id.clone(),
49484948
})
49494949
.await
4950-
.map_err(|e| LDNError::Load(format!("Failed to get list of pull requests: {e}")))?;
4950+
.map_err(|e| LDNError::Load(format!("Failed to create a pull request: {e}")))?;
49514951

49524952
if should_create_in_db {
49534953
let issue_number = issue_number

fplus-lib/src/external_services/github.rs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -341,15 +341,22 @@ impl GithubWrapper {
341341
}
342342

343343
pub async fn list_branches(&self) -> Result<Vec<Branch>, OctocrabError> {
344-
let iid = self
344+
let mut all_branches = Vec::new();
345+
let mut page = self
345346
.inner
346347
.repos(&self.owner, &self.repo)
347348
.list_branches()
348349
.send()
349350
.await?;
350-
Ok(iid.items)
351-
}
351+
all_branches.extend(page.items);
352+
353+
while let Some(next_page) = self.inner.get_page::<Branch>(&page.next).await? {
354+
all_branches.extend(next_page.clone().items);
355+
page = next_page;
356+
}
352357

358+
Ok(all_branches)
359+
}
353360
/// creates new branch under head on github
354361
/// you should use build_create_ref_request function to construct request
355362
pub async fn create_branch(&self, request: Request<String>) -> Result<bool, OctocrabError> {
@@ -611,6 +618,13 @@ impl GithubWrapper {
611618
file_sha,
612619
application_id,
613620
} = data;
621+
let branch_exists = self.check_if_branch_exists(&branch_name).await?;
622+
if branch_exists {
623+
return Err(OctocrabError::Other {
624+
source: format!("Branch {branch_name} already exists.").into(),
625+
backtrace: GenerateImplicitData::generate(),
626+
});
627+
}
614628
self.create_branch(ref_request).await?;
615629
let file_update = self
616630
.update_file_content(&file_name, &commit, &file_content, &branch_name, &file_sha)
@@ -762,6 +776,15 @@ impl GithubWrapper {
762776
Ok(pull_request.head.ref_field.clone())
763777
}
764778

779+
pub async fn check_if_branch_exists(&self, branch_name: &str) -> Result<bool, OctocrabError> {
780+
let branch_exists = self
781+
.list_branches()
782+
.await?
783+
.iter()
784+
.any(|branch| branch.name == branch_name);
785+
Ok(branch_exists)
786+
}
787+
765788
pub async fn get_files_from_public_repo(
766789
&self,
767790
owner: &str,

0 commit comments

Comments
 (0)