feat: update customer specified branch

This commit is contained in:
Aravinth Manivannan 2022-09-08 00:59:39 +05:30
parent af76be0e40
commit 4d136f2407
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
3 changed files with 15 additions and 10 deletions

View file

@ -59,7 +59,7 @@ async fn update(payload: web::Json<DeployEvent>, ctx: AppCtx) -> ServiceResult<i
let (tx, rx) = oneshot::channel(); let (tx, rx) = oneshot::channel();
let page = page.clone(); let page = page.clone();
web::block(move || { web::block(move || {
tx.send(page.update()).unwrap(); tx.send(page.update(&payload.branch)).unwrap();
}) })
.await .await
.unwrap(); .unwrap();

View file

@ -73,10 +73,14 @@ impl Page {
Ok(()) Ok(())
} }
fn fetch<'a>(&self, repo: &'a git2::Repository) -> ServiceResult<git2::AnnotatedCommit<'a>> { fn fetch<'a>(
&self,
repo: &'a git2::Repository,
branch: &str,
) -> ServiceResult<git2::AnnotatedCommit<'a>> {
let mut remote = repo.find_remote("origin")?; let mut remote = repo.find_remote("origin")?;
log::info!("Fetching {} for repo", remote.name().unwrap()); log::info!("Fetching {} for repo", remote.name().unwrap());
remote.fetch(&[&self.branch], None, None)?; remote.fetch(&[branch], None, None)?;
let fetch_head = repo.find_reference("FETCH_HEAD")?; let fetch_head = repo.find_reference("FETCH_HEAD")?;
Ok(repo.reference_to_annotated_commit(&fetch_head)?) Ok(repo.reference_to_annotated_commit(&fetch_head)?)
} }
@ -85,6 +89,7 @@ impl Page {
&self, &self,
repo: &'a Repository, repo: &'a Repository,
fetch_commit: git2::AnnotatedCommit<'a>, fetch_commit: git2::AnnotatedCommit<'a>,
branch: &str,
) -> ServiceResult<()> { ) -> ServiceResult<()> {
// 1. do a merge analysis // 1. do a merge analysis
let analysis = repo.merge_analysis(&[&fetch_commit])?; let analysis = repo.merge_analysis(&[&fetch_commit])?;
@ -94,7 +99,7 @@ impl Page {
//log::debug!("Doing a fast forward"); //log::debug!("Doing a fast forward");
log::debug!("Doing a fast forward"); log::debug!("Doing a fast forward");
// do a fast forward // do a fast forward
let refname = format!("refs/heads/{}", &self.branch); let refname = format!("refs/heads/{}", branch);
match repo.find_reference(&refname) { match repo.find_reference(&refname) {
Ok(mut r) => { Ok(mut r) => {
log::debug!("fast forwarding"); log::debug!("fast forwarding");
@ -109,7 +114,7 @@ impl Page {
&refname, &refname,
fetch_commit.id(), fetch_commit.id(),
true, true,
&format!("Setting {} to {}", &self.branch, fetch_commit.id()), &format!("Setting {} to {}", branch, fetch_commit.id()),
) )
.unwrap(); .unwrap();
repo.set_head(&refname).unwrap(); repo.set_head(&refname).unwrap();
@ -209,10 +214,10 @@ impl Page {
Ok(()) Ok(())
} }
pub fn update(&self) -> ServiceResult<()> { pub fn update(&self, branch: &str) -> ServiceResult<()> {
let repo = self.create_repo()?; let repo = self.create_repo()?;
let fetch_commit = self.fetch(&repo)?; let fetch_commit = self.fetch(&repo, branch)?;
self.merge(&repo, fetch_commit)?; self.merge(&repo, fetch_commit, branch)?;
Ok(()) Ok(())
} }
@ -271,7 +276,7 @@ mod tests {
let gh_pages = page.get_deploy_branch(&repo).unwrap(); let gh_pages = page.get_deploy_branch(&repo).unwrap();
assert_eq!(gh_pages, "gh-pages"); assert_eq!(gh_pages, "gh-pages");
page.branch = "master".to_string(); page.branch = "master".to_string();
page.update().unwrap(); page.update(&page.branch).unwrap();
let master = page.get_deploy_branch(&repo).unwrap(); let master = page.get_deploy_branch(&repo).unwrap();
assert_eq!(master, "master"); assert_eq!(master, "master");

View file

@ -124,7 +124,7 @@ impl Settings {
error!("{}", ServiceError::PathTaken(page.clone(), page2.clone())); error!("{}", ServiceError::PathTaken(page.clone(), page2.clone()));
} }
} }
if let Err(e) = page.update() { if let Err(e) = page.update(&page.branch) {
error!("{e}"); error!("{e}");
} }
} }