2021-10-29 20:23:07 +05:30
|
|
|
/*
|
2022-03-29 17:36:22 +05:30
|
|
|
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
2021-10-29 20:23:07 +05:30
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
use git2::{build::CheckoutBuilder, BranchType, Direction, ObjectType, Repository};
|
|
|
|
use log::info;
|
|
|
|
use serde::Deserialize;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Deserialize)]
|
|
|
|
pub struct Page {
|
|
|
|
pub secret: String,
|
|
|
|
pub repo: String,
|
|
|
|
pub path: String,
|
|
|
|
pub branch: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Page {
|
2022-03-29 18:02:32 +05:30
|
|
|
fn create_repo(&self) -> Repository {
|
2021-10-29 20:23:07 +05:30
|
|
|
let repo = Repository::open(&self.path);
|
|
|
|
|
2022-03-29 16:56:06 +05:30
|
|
|
if let Ok(repo) = repo {
|
|
|
|
return repo;
|
|
|
|
} else {
|
2021-10-29 20:23:07 +05:30
|
|
|
info!("Cloning repository {} at {}", self.repo, self.path);
|
|
|
|
Repository::clone(&self.repo, &self.path).unwrap()
|
|
|
|
};
|
|
|
|
|
2022-03-29 16:56:06 +05:30
|
|
|
let repo = Repository::open(&self.path).unwrap();
|
2022-04-09 11:04:44 +05:30
|
|
|
self._fetch_upstream(&repo, &self.branch);
|
|
|
|
self.deploy_branch(&repo);
|
|
|
|
repo
|
|
|
|
}
|
2021-10-29 20:23:07 +05:30
|
|
|
|
2022-04-09 11:04:44 +05:30
|
|
|
pub fn deploy_branch(&self, repo: &Repository) {
|
|
|
|
let branch = repo
|
|
|
|
.find_branch(&format!("origin/{}", &self.branch), BranchType::Remote)
|
|
|
|
.unwrap();
|
2021-10-29 20:23:07 +05:30
|
|
|
|
2022-04-09 11:04:44 +05:30
|
|
|
let mut checkout_options = CheckoutBuilder::new();
|
|
|
|
checkout_options.force();
|
2021-10-29 20:23:07 +05:30
|
|
|
|
2022-04-09 11:04:44 +05:30
|
|
|
let tree = branch.get().peel(ObjectType::Tree).unwrap();
|
2021-10-29 20:23:07 +05:30
|
|
|
|
2022-04-09 11:04:44 +05:30
|
|
|
repo.checkout_tree(&tree, Some(&mut checkout_options))
|
|
|
|
.unwrap();
|
|
|
|
repo.set_head(branch.get().name().unwrap()).unwrap();
|
|
|
|
info!("Deploying branch {}", self.branch);
|
2021-10-29 20:23:07 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
fn _fetch_upstream(&self, repo: &Repository, branch: &str) {
|
|
|
|
let mut remote = repo.find_remote("origin").unwrap();
|
|
|
|
remote.connect(Direction::Fetch).unwrap();
|
|
|
|
info!("Updating repository {}", self.repo);
|
|
|
|
remote.fetch(&[branch], None, None).unwrap();
|
|
|
|
remote.disconnect().unwrap();
|
|
|
|
}
|
|
|
|
|
2022-04-09 11:04:44 +05:30
|
|
|
pub fn update(&self) {
|
2021-10-29 20:23:07 +05:30
|
|
|
let repo = self.create_repo();
|
2022-04-09 11:04:44 +05:30
|
|
|
self._fetch_upstream(&repo, &self.branch);
|
|
|
|
self.deploy_branch(&repo);
|
2021-10-29 20:23:07 +05:30
|
|
|
}
|
|
|
|
}
|
2022-03-29 18:02:32 +05:30
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
2022-04-09 11:04:44 +05:30
|
|
|
use git2::Branch;
|
|
|
|
use git2::Repository;
|
2022-03-29 18:02:32 +05:30
|
|
|
use mktemp::Temp;
|
|
|
|
|
2022-04-09 11:04:44 +05:30
|
|
|
impl Page {
|
|
|
|
fn get_tree<'a>(&self, repo: &'a Repository) -> Branch<'a> {
|
|
|
|
repo.find_branch(&format!("origin/{}", &self.branch), BranchType::Remote)
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-29 18:02:32 +05:30
|
|
|
#[actix_rt::test]
|
|
|
|
async fn pages_works() {
|
|
|
|
let tmp_dir = Temp::new_dir().unwrap();
|
|
|
|
assert!(tmp_dir.exists(), "tmp directory successully created");
|
2022-04-09 11:04:44 +05:30
|
|
|
let mut page = Page {
|
2022-03-29 18:02:32 +05:30
|
|
|
secret: String::default(),
|
|
|
|
repo: "https://github.com/mcaptcha/website".to_owned(),
|
|
|
|
path: tmp_dir.to_str().unwrap().to_string(),
|
|
|
|
branch: "gh-pages".to_string(),
|
|
|
|
};
|
|
|
|
|
|
|
|
assert!(
|
|
|
|
Repository::open(tmp_dir.as_path()).is_err(),
|
|
|
|
"repository doesn't exist yet"
|
|
|
|
);
|
|
|
|
|
|
|
|
let repo = page.create_repo();
|
|
|
|
assert!(!repo.is_bare(), "repository isn't bare");
|
|
|
|
page.create_repo();
|
|
|
|
assert!(
|
|
|
|
Repository::open(tmp_dir.as_path()).is_ok(),
|
|
|
|
"repository exists yet"
|
|
|
|
);
|
2022-04-09 11:04:44 +05:30
|
|
|
|
|
|
|
let gh_pages = page.get_tree(&repo);
|
|
|
|
assert_eq!(
|
|
|
|
gh_pages.name().unwrap().as_ref().unwrap(),
|
|
|
|
&"origin/gh-pages"
|
|
|
|
);
|
|
|
|
page.branch = "master".to_string();
|
|
|
|
page.update();
|
|
|
|
let master = page.get_tree(&repo);
|
|
|
|
assert_eq!(master.name().unwrap().as_ref().unwrap(), &"origin/master");
|
2022-03-29 18:02:32 +05:30
|
|
|
}
|
|
|
|
}
|