feat: initial

This commit is contained in:
Kasper Juul Hermansen 2023-04-07 01:12:51 +02:00
commit f3b497d183
Signed by: kjuulh
GPG Key ID: 57B6E1465221F912
6 changed files with 2363 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
/target
.env
target/

2197
Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

40
Cargo.toml Normal file
View File

@ -0,0 +1,40 @@
[workspace]
members = ["."]
[workspace.dependencies]
clap = "4.2.1"
color-eyre = "0.6.2"
dagger-sdk = "0.2.20"
eyre = "0.6.8"
serde = { version = "1.0.159", features = ["derive"] }
tokio = { version = "1.27.0", features = ["full"] }
tracing = { version = "0.1.37", features = ["log"] }
tracing-subscriber = { version = "0.3.16", features = [
"serde",
"serde_json",
"env-filter",
] }
[package]
name = "releaser"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap.workspace = true
color-eyre.workspace = true
dagger-sdk.workspace = true
dotenv = "0.15.0"
eyre.workspace = true
regex = "1.7.3"
reqwest = "0.11.16"
serde.workspace = true
serde_json = "1.0.95"
tokio.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
[dev-dependencies]
tracing-test = "0.2.4"

View File

@ -0,0 +1,7 @@
pub fn is_conventional_commit(commit_message: &str) -> eyre::Result<bool> {
let re = regex::Regex::new(
r"(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\([a-z ]+\))?: .{1,72}",
)?;
Ok(re.is_match(commit_message))
}

79
src/gitea.rs Normal file
View File

@ -0,0 +1,79 @@
use reqwest::Client;
use serde_json::{json, Value};
#[derive(serde::Deserialize, serde::Serialize)]
pub struct GitCommit {
commit: GitCommitDetails,
}
#[derive(serde::Deserialize, serde::Serialize)]
pub struct GitCommitDetails {
message: String,
}
pub async fn get_pull_request_commits(
base_url: &str,
owner: &str,
repo: &str,
pull_request_id: u32,
access_token: &str,
) -> eyre::Result<Vec<String>> {
let client = Client::new();
let api_url = format!(
"{}/api/v1/repos/{}/{}/pulls/{}/commits",
base_url, owner, repo, pull_request_id
);
let response = client
.get(&api_url)
.bearer_auth(access_token)
.send()
.await?;
let commits: Vec<GitCommit> = response.json().await?;
let mut commit_titles = Vec::new();
tracing::debug!(
commits = serde_json::to_string(&commits)?,
"fetched commits"
);
for entry in commits {
commit_titles.push(entry.commit.message);
}
Ok(commit_titles)
}
#[cfg(test)]
mod tests {
use tracing_test::traced_test;
use super::*;
#[tokio::test]
#[traced_test]
async fn test_get_pull_request_commits() {
dotenv::dotenv().unwrap();
let base_url = "https://git.front.kjuulh.io";
let owner = "kjuulh";
let repo = "test-repo";
let pull_request_id = 3;
let access_token = &std::env::var("GITEA_ACCESS_TOKEN").unwrap();
let commits =
get_pull_request_commits(base_url, owner, repo, pull_request_id, access_token)
.await
.unwrap();
assert!(
!commits.is_empty(),
"Expected at least one commit in the pull request"
);
// You can add more assertions here to check the contents of the commits vector
}
}

37
src/main.rs Normal file
View File

@ -0,0 +1,37 @@
mod conventional_commits;
mod gitea;
use clap::Command;
use tracing_subscriber::EnvFilter;
const VERSION: &'static str = "<dev>";
#[tokio::main]
async fn main() -> eyre::Result<()> {
color_eyre::install().unwrap();
tracing_subscriber::fmt()
.pretty()
.with_env_filter(EnvFilter::from_default_env())
.init();
let matches = clap::Command::new("releaser")
.version(VERSION)
.author("kjuulh <contact@kjuulh.io>")
.about("A CLI app for releasing software and managing versions")
.subcommand(Command::new("release").about("Release the software"))
.subcommand(
Command::new("validate").about("Validate the semantic versioning of the software"),
)
.subcommand(Command::new("bump").about("Bump the semantic versioning in git tags"))
.get_matches();
match matches.subcommand() {
Some(("release", sub_matches)) => {}
Some(("validate", sub_matches)) => {}
Some(("bump", sub_matches)) => {}
_ => unreachable!(),
}
Ok(())
}