aboutsummaryrefslogtreecommitdiff
path: root/src/forge/gitlab
diff options
context:
space:
mode:
authorMax Audron <audron@cocaine.farm>2022-06-07 12:28:18 +0200
committerMaximilian Manz <maximilian.manz@de.clara.net>2022-06-20 11:33:04 +0200
commitf869c7f52d8fd1f1ef61e218bbec4d0dac27673d (patch)
tree8029b33976fc897a4ae81785e622a7e61ad2eb67 /src/forge/gitlab
init
Diffstat (limited to 'src/forge/gitlab')
-rw-r--r--src/forge/gitlab/config.rs26
-rw-r--r--src/forge/gitlab/mod.rs73
2 files changed, 99 insertions, 0 deletions
diff --git a/src/forge/gitlab/config.rs b/src/forge/gitlab/config.rs
new file mode 100644
index 0000000..37186e8
--- /dev/null
+++ b/src/forge/gitlab/config.rs
@@ -0,0 +1,26 @@
+use serde::{Deserialize, Serialize};
+use std::path::PathBuf;
+
+use crate::config::ForgeConfigTrait;
+
+#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Deserialize, Serialize)]
+pub struct Gitlab {
+ // pub url: url::Url,
+ pub host: String,
+ pub token: String,
+ pub directory: PathBuf,
+ #[serde(default = "default_tls")]
+ pub tls: bool,
+ #[serde(default)]
+ pub auto_create_branches: bool,
+}
+
+const fn default_tls() -> bool {
+ true
+}
+
+impl ForgeConfigTrait for Gitlab {
+ fn root(&self) -> &str {
+ self.directory.to_str().unwrap()
+ }
+}
diff --git a/src/forge/gitlab/mod.rs b/src/forge/gitlab/mod.rs
new file mode 100644
index 0000000..a32c367
--- /dev/null
+++ b/src/forge/gitlab/mod.rs
@@ -0,0 +1,73 @@
+use anyhow::{bail, Context, Result};
+use gitlab::AsyncGitlab;
+
+use graphql_client::GraphQLQuery;
+use tracing::{debug, trace};
+
+pub mod config;
+
+#[derive(Clone, Debug)]
+pub struct Gitlab {
+ client: AsyncGitlab,
+}
+
+impl Gitlab {
+ pub async fn new(host: &str, token: &str, tls: bool) -> Result<Gitlab> {
+ let mut gitlab = gitlab::GitlabBuilder::new(host, token);
+
+ if !tls {
+ gitlab.insecure();
+ }
+
+ let gitlab = gitlab.build_async().await?;
+
+ Ok(Gitlab { client: gitlab })
+ }
+
+ pub async fn from_config(forge: &config::Gitlab) -> Result<Gitlab> {
+ Gitlab::new(&forge.host, &forge.token, forge.tls).await
+ }
+}
+
+#[async_trait::async_trait]
+impl super::ForgeTrait for Gitlab {
+ async fn projects(&self, scope: &str) -> Result<Vec<super::Project>> {
+ let query = Projects::build_query(projects::Variables {
+ scope: scope.to_owned(),
+ });
+ debug!("query: {:#?}", query);
+ let res = self.client.graphql::<Projects>(&query).await?;
+
+ let res = res
+ .projects
+ .unwrap()
+ .nodes
+ .unwrap()
+ .into_iter()
+ .filter(|x| x.is_some())
+ .map(|x| x.unwrap().into())
+ .collect();
+
+ Ok(res)
+ }
+}
+
+#[derive(GraphQLQuery)]
+#[graphql(
+ query_path = "graphql/projects_query.graphql",
+ schema_path = "graphql/schema.graphql",
+ response_derives = "Clone, Debug"
+)]
+pub struct Projects;
+
+impl Into<super::Project> for projects::ProjectsProjectsNodes {
+ fn into(self) -> super::Project {
+ super::Project {
+ id: self.id,
+ name: self.name,
+ path: self.full_path,
+ ssh_clone_url: self.ssh_url_to_repo,
+ http_clone_url: self.http_url_to_repo,
+ }
+ }
+}