add anyhow
This commit is contained in:
parent
319e9375b2
commit
379eef484c
4 changed files with 115 additions and 25 deletions
70
Cargo.lock
generated
70
Cargo.lock
generated
|
@ -2,6 +2,21 @@
|
|||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "addr2line"
|
||||
version = "0.17.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b9ecd88a8c8378ca913a680cd98f0f13ac67383d35993f86c90a70e3f137816b"
|
||||
dependencies = [
|
||||
"gimli",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "adler"
|
||||
version = "1.0.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
|
||||
|
||||
[[package]]
|
||||
name = "android_system_properties"
|
||||
version = "0.1.5"
|
||||
|
@ -11,6 +26,15 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anyhow"
|
||||
version = "1.0.66"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "216261ddc8289130e551ddcd5ce8a064710c0d064a4d2895c67151c92b5443f6"
|
||||
dependencies = [
|
||||
"backtrace",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ascii"
|
||||
version = "0.9.3"
|
||||
|
@ -45,6 +69,21 @@ version = "1.1.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "backtrace"
|
||||
version = "0.3.66"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cab84319d616cfb654d03394f38ab7e6f0919e181b1b57e1fd15e7fb4077d9a7"
|
||||
dependencies = [
|
||||
"addr2line",
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"miniz_oxide",
|
||||
"object",
|
||||
"rustc-demangle",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "base64"
|
||||
version = "0.13.1"
|
||||
|
@ -360,6 +399,12 @@ dependencies = [
|
|||
"slab",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gimli"
|
||||
version = "0.26.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "22030e2c5a68ec659fde1e949a745124b48e6fa8b045b7ed5bd1fe4ccc5c4e5d"
|
||||
|
||||
[[package]]
|
||||
name = "gitlab"
|
||||
version = "0.1505.0"
|
||||
|
@ -389,6 +434,7 @@ dependencies = [
|
|||
name = "gitlab-helper"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"clap",
|
||||
"dotenv",
|
||||
"gitlab",
|
||||
|
@ -686,6 +732,15 @@ version = "0.3.16"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d"
|
||||
|
||||
[[package]]
|
||||
name = "miniz_oxide"
|
||||
version = "0.5.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "96590ba8f175222643a85693f33d26e9c8a015f599c216509b1a6894af675d34"
|
||||
dependencies = [
|
||||
"adler",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "mio"
|
||||
version = "0.8.5"
|
||||
|
@ -727,6 +782,15 @@ dependencies = [
|
|||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "object"
|
||||
version = "0.29.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "21158b2c33aa6d4561f1c0a6ea283ca92bc54802a93b263e910746d679a7eb53"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "once_cell"
|
||||
version = "1.16.0"
|
||||
|
@ -853,6 +917,12 @@ dependencies = [
|
|||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "rustc-demangle"
|
||||
version = "0.1.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342"
|
||||
|
||||
[[package]]
|
||||
name = "rustls"
|
||||
version = "0.20.7"
|
||||
|
|
|
@ -10,3 +10,4 @@ gitlab = "0.1505"
|
|||
dotenv = "0.15"
|
||||
clap = { version = "4", features = ["derive", "env"] }
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
anyhow = { version = "1", features = ["backtrace"] }
|
||||
|
|
10
src/main.rs
10
src/main.rs
|
@ -3,6 +3,8 @@ use gitlab::Gitlab;
|
|||
|
||||
mod projects;
|
||||
|
||||
use anyhow::Result;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
struct Args {
|
||||
/// Gitlab host
|
||||
|
@ -38,16 +40,18 @@ enum SubCommand {
|
|||
},
|
||||
}
|
||||
|
||||
fn main() {
|
||||
fn main() -> Result<()> {
|
||||
dotenv::dotenv().ok();
|
||||
let args = Args::parse();
|
||||
|
||||
let client = Gitlab::new(args.host, args.token).unwrap();
|
||||
|
||||
match args.cmd {
|
||||
SubCommand::Projects { group_id} => projects::list(&client, group_id),
|
||||
SubCommand::Projects { group_id } => projects::list(&client, group_id)?,
|
||||
SubCommand::Unprotect { group_id, branch } => {
|
||||
projects::unprotect(&client, group_id, &branch);
|
||||
projects::unprotect(&client, group_id, &branch)?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
use anyhow::Result;
|
||||
use gitlab::{
|
||||
api::{
|
||||
groups::projects::GroupProjects, ignore, paged,
|
||||
projects::protected_branches::UnprotectBranch, Query,
|
||||
groups::projects::GroupProjects,
|
||||
ignore, paged,
|
||||
projects::protected_branches::{ProtectedBranches, UnprotectBranch},
|
||||
Query,
|
||||
},
|
||||
Gitlab, ProjectId,
|
||||
};
|
||||
|
@ -14,40 +17,52 @@ struct Project {
|
|||
ssh_url_to_repo: String,
|
||||
}
|
||||
|
||||
fn get_projects_by_group(client: &Gitlab, id: u64) -> Vec<Project> {
|
||||
let endpoint = GroupProjects::builder()
|
||||
.group(id)
|
||||
.archived(false)
|
||||
.build()
|
||||
.unwrap();
|
||||
fn get_projects_by_group(client: &Gitlab, id: u64) -> Result<Vec<Project>> {
|
||||
let endpoint = GroupProjects::builder().group(id).archived(false).build()?;
|
||||
|
||||
paged(endpoint, gitlab::api::Pagination::All)
|
||||
.query(client)
|
||||
.unwrap()
|
||||
Ok(paged(endpoint, gitlab::api::Pagination::All).query(client)?)
|
||||
}
|
||||
|
||||
pub fn list(client: &Gitlab, id: u64) {
|
||||
let projects = get_projects_by_group(client, id);
|
||||
pub fn list(client: &Gitlab, id: u64) -> Result<()> {
|
||||
let projects = get_projects_by_group(client, id)?;
|
||||
|
||||
for project in projects {
|
||||
let name = project.name.replace(' ', "-").to_lowercase();
|
||||
println!("{name} {}", project.ssh_url_to_repo);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn unprotect(client: &Gitlab, group: u64, branch: &str) {
|
||||
let projects = get_projects_by_group(client, group);
|
||||
let n = projects.len();
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct Branch {
|
||||
id: u64,
|
||||
name: String,
|
||||
}
|
||||
|
||||
pub fn unprotect(client: &Gitlab, group: u64, branch: &str) -> Result<()> {
|
||||
let projects = get_projects_by_group(client, group)?;
|
||||
let mut n = 0;
|
||||
|
||||
for project in projects {
|
||||
let endpoint = UnprotectBranch::builder()
|
||||
let endpoint = ProtectedBranches::builder()
|
||||
.project(project.id.value())
|
||||
.name(branch)
|
||||
.build()
|
||||
.unwrap();
|
||||
.build()?;
|
||||
|
||||
// TODO: Ignore if branch already unprotected / doesn't exist
|
||||
ignore(endpoint).query(client).unwrap();
|
||||
let branches: Vec<Branch> = endpoint.query(client)?;
|
||||
|
||||
if branches.iter().any(|b| b.name == branch) {
|
||||
let endpoint = UnprotectBranch::builder()
|
||||
.project(project.id.value())
|
||||
.name(branch)
|
||||
.build()?;
|
||||
|
||||
ignore(endpoint).query(client)?;
|
||||
|
||||
n += 1;
|
||||
}
|
||||
}
|
||||
println!("Unprotected {branch} on {n} projects successfully");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Reference in a new issue