diff --git a/CHANGELOG.md b/CHANGELOG.md index 8cf9bff..a620b64 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,22 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). -## Unreleased +## 0.4.0 + +This is a functionality-breaking release that removes untested/extraneous parts +of this project. + +### ADDED + +- The gitea client embedded into this repo is now a more generic crate that can + be reused across other projects. +- Gitea types have been simplified and redundant types have been removed. +- Simple tests for the gitea crate. +- A name generator `elfs` was created for future use in integration testing. + +### REMOVED + +- All functionality but the drone plugin and release commands ## 0.3.2 diff --git a/Cargo.lock b/Cargo.lock index 3ac0f05..dc72d90 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -176,6 +176,13 @@ version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4358a9e11b9a09cf52383b451b49a169e8d797b68aa02301ff586d70d9661ea3" +[[package]] +name = "elfs" +version = "0.1.0" +dependencies = [ + "names", +] + [[package]] name = "encoding_rs" version = "0.8.23" @@ -218,6 +225,12 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b" +[[package]] +name = "fuchsia-cprng" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba" + [[package]] name = "fuchsia-zircon" version = "0.3.3" @@ -312,10 +325,12 @@ dependencies = [ name = "gitea" version = "0.1.0" dependencies = [ + "anyhow", "reqwest", "serde", "serde_json", "thiserror", + "tokio", ] [[package]] @@ -324,6 +339,7 @@ version = "0.3.2" dependencies = [ "anyhow", "comrak", + "elfs", "git2", "gitea", "http", @@ -633,6 +649,15 @@ dependencies = [ "ws2_32-sys", ] +[[package]] +name = "names" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef320dab323286b50fb5cdda23f61c796a72a89998ab565ca32525c5c556f2da" +dependencies = [ + "rand 0.3.23", +] + [[package]] name = "native-tls" version = "0.2.4" @@ -838,6 +863,29 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.3.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c" +dependencies = [ + "libc", + "rand 0.4.6", +] + +[[package]] +name = "rand" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293" +dependencies = [ + "fuchsia-cprng", + "libc", + "rand_core 0.3.1", + "rdrand", + "winapi 0.3.8", +] + [[package]] name = "rand" version = "0.7.3" @@ -847,7 +895,7 @@ dependencies = [ "getrandom", "libc", "rand_chacha", - "rand_core", + "rand_core 0.5.1", "rand_hc", ] @@ -858,9 +906,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" dependencies = [ "ppv-lite86", - "rand_core", + "rand_core 0.5.1", ] +[[package]] +name = "rand_core" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b" +dependencies = [ + "rand_core 0.4.2", +] + +[[package]] +name = "rand_core" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc" + [[package]] name = "rand_core" version = "0.5.1" @@ -876,7 +939,16 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" dependencies = [ - "rand_core", + "rand_core 0.5.1", +] + +[[package]] +name = "rdrand" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2" +dependencies = [ + "rand_core 0.3.1", ] [[package]] @@ -1125,7 +1197,7 @@ checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9" dependencies = [ "cfg-if", "libc", - "rand", + "rand 0.7.3", "redox_syscall", "remove_dir_all", "winapi 0.3.8", diff --git a/Cargo.toml b/Cargo.toml index ddb3eef..8b39d15 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,11 +23,13 @@ gitea = { path = "./gitea" } [dev-dependencies] tempfile = "3" +elfs = { path = "./elfs" } [profile.release] lto = true [workspace] members = [ + "./elfs", "./gitea" ] diff --git a/elfs/Cargo.toml b/elfs/Cargo.toml new file mode 100644 index 0000000..6783c44 --- /dev/null +++ b/elfs/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "elfs" +version = "0.1.0" +authors = ["Christine Dodrill "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +names = "0" diff --git a/elfs/src/lib.rs b/elfs/src/lib.rs new file mode 100644 index 0000000..4615cff --- /dev/null +++ b/elfs/src/lib.rs @@ -0,0 +1,75 @@ +/// This one-function crate generates names based on [Pokemon Vietnamese Crystal](https://tvtropes.org/pmwiki/pmwiki.php/VideoGame/PokemonVietnameseCrystal). + +use names::{Generator, Name}; + +fn moves() -> &'static [&'static str] { + &[ + "ABLE", "ABNORMA", "AGAIN", "AIREXPL", "ANG", "ANGER", "ASAIL", "ATTACK", "AURORA", "AWL", + "BAN", "BAND", "BARE", "BEAT", "BEATED", "BELLY", "BIND", "BITE", "BLOC", "BLOOD", "BODY", + "BOOK", "BREATH", "BUMP", "CAST", "CHAM", "CLAMP", "CLAP", "CLAW", "CLEAR", "CLI", "CLIP", + "CLOUD", "CONTRO", "CONVY", "COOLHIT", "CRASH", "CRY", "CUT", "DESCRI", "D-FIGHT", "DIG", + "DITCH", "DIV", "DOZ", "DRE", "DUL", "DU-PIN", "DYE", "EARTH", "EDU", "EG-BOMB", "EGG", + "ELEGY", "ELE-HIT", "EMBODY", "EMPLI", "ENGL", "ERUPT", "EVENS", "EXPLOR", "EYES", "FALL", + "FAST", "F-CAR", "F-DANCE", "FEARS", "F-FIGHT", "FIGHT", "FIR", "FIRE", "FIREHIT", "FLAME", + "FLAP", "FLASH", "FLEW", "FORCE", "FRA", "FREEZE", "FROG", "G-BIRD", "GENKISS", "GIFT", + "G-KISS", "G-MOUSE", "GRADE", "GROW", "HAMMER", "HARD", "HAT", "HATE", "H-BOMB", "HELL-R", + "HEMP", "HINT", "HIT", "HU", "HUNT", "HYPNOSI", "INHA", "IRO", "IRONBAR", "IR-WING", + "J-GUN", "KEE", "KICK", "KNIF", "KNIFE", "KNOCK", "LEVEL", "LIGH", "LIGHHIT", "LIGHT", + "LIVE", "L-WALL", "MAD", "MAJUS", "MEL", "MELO", "MESS", "MILK", "MIMI", "MISS", "MIXING", + "MOVE", "MUD", "NI-BED", "NOISY", "NOONLI", "NULL", "N-WAVE", "PAT", "PEACE", "PIN", + "PLAN", "PLANE", "POIS", "POL", "POWDE", "POWE", "POWER", "PRIZE", "PROTECT", "PROUD", + "RAGE", "RECOR", "REFLAC", "REFREC", "REGR", "RELIV", "RENEW", "R-FIGHT", "RING", "RKICK", + "ROCK", "ROUND", "RUS", "RUSH", "SAND", "SAW", "SCISSOR", "SCRA", "SCRIPT", "SEEN", + "SERVER", "SHADOW", "SHELL", "SHINE", "SHO", "SIGHT", "SIN", "SMALL", "SMELT", "SMOK", + "SNAKE", "SNO", "SNOW", "SOU", "SO-WAVE", "SPAR", "SPEC", "SPID", "S-PIN", "SPRA", "STAM", + "STARE", "STEA", "STONE", "STORM", "STRU", "STRUG", "STUDEN", "SUBS", "SUCID", "SUN-LIG", + "SUNRIS", "SUPLY", "S-WAVE", "TAILS", "TANGL", "TASTE", "TELLI", "THANK", "TONKICK", + "TOOTH", "TORL", "TRAIN", "TRIKICK", "TUNGE", "VOLT", "WA-GUN", "WATCH", "WAVE", "W-BOMB", + "WFALL", "WFING", "WHIP", "WHIRL", "WIND", "WOLF", "WOOD", "WOR", "YUJA", + ] +} + +fn names() -> &'static [&'static str] { + &[ + "SEED", "GRASS", "FLOWE", "SHAD", "CABR", "SNAKE", "GOLD", "COW", "GUIKI", "PEDAL", + "DELAN", "B-FLY", "BIDE", "KEYU", "FORK", "LAP", "PIGE", "PIJIA", "CAML", "LAT", "BIRD", + "BABOO", "VIV", "ABOKE", "PIKAQ", "RYE", "SAN", "BREAD", "LIDEL", "LIDE", "PIP", "PIKEX", + "ROK", "JUGEN", "PUD", "BUDE", "ZHIB", "GELU", "GRAS", "FLOW", "LAFUL", "ATH", "BALA", + "CORN", "MOLUF", "DESP", "DAKED", "MIMI", "BOLUX", "KODA", "GELUD", "MONK", "SUMOY", + "GEDI", "WENDI", "NILEM", "NILE", "NILEC", "KEZI", "YONGL", "HUDE", "WANLI", "GELI", + "GUAIL", "MADAQ", "WUCI", "WUCI", "MUJEF", "JELLY", "SICIB", "GELU", "NELUO", "BOLI", + "JIALE", "YED", "YEDE", "CLO", "SCARE", "AOCO", "DEDE", "DEDEI", "BAWU", "JIUG", "BADEB", + "BADEB", "HOLE", "BALUX", "GES", "FANT", "QUAR", "YIHE", "SWAB", "SLIPP", "CLU", "DEPOS", + "BILIY", "YUANO", "SOME", "NO", "YELA", "EMPT", "ZECUN", "XIAHE", "BOLEL", "DEJI", "MACID", + "XIHON", "XITO", "LUCK", "MENJI", "GELU", "DECI", "XIDE", "DASAJ", "DONGN", "RICUL", + "MINXI", "BALIY", "ZENDA", "LUZEL", "HELE5", "0FENB", "KAIL", "JIAND", "CARP", "JINDE", + "LAPU", "MUDE", "YIFU", "LINLI", "SANDI", "HUSI", "JINC", "OUMU", "OUMUX", "CAP", "KUIZA", + "PUD", "TIAO", "FRMAN", "CLAU", "SPARK", "DRAGO", "BOLIU", "GUAIL", "MIYOU", "MIY", + "QIAOK", "BEIL", "MUKEI", "RIDED", "MADAM", "BAGEP", "CROC", "ALIGE", "OUDAL", "OUD", + "DADA", "HEHE", "YEDEA", "NUXI", "NUXIN", "ROUY", "ALIAD", "STICK", "QIANG", "LAAND", + "PIQI", "PI", "PUPI", "DEKE", "DEKEJ", "NADI", "NADIO", "MALI", "PEA", "ELECT", "FLOWE", + "MAL", "MALI", "HUSHU", "NILEE", "YUZI", "POPOZ", "DUZI", "HEBA", "XIAN", "SHAN", "YEYEA", + "WUY", "LUO", "KEFE", "HULA", "CROW", "YADEH", "MOW", "ANNAN", "SUONI", "KYLI", "HULU", + "HUDEL", "YEHE", "GULAE", "YEHE", "BLU", "GELAN", "BOAT", "NIP", "POIT", "HELAK", "XINL", + "BEAR", "LINB", "MAGEH", "MAGEJ", "WULI", "YIDE", "RIVE", "FISH", "AOGU", "DELIE", "MANTE", + "KONMU", "DELU", "HELU", "HUAN", "HUMA", "DONGF", "JINCA", "HEDE", "DEFU", "LIBY", "JIAPA", + "MEJI", "HELE", "BUHU", "MILK", "HABI", "THUN", "GARD", "DON", "YANGQ", "SANAQ", "BANQ", + "LUJ", "PHIX", "SIEI", "EGG", + ] +} + +/// Generate a new name based on [Pokemon Vietnamese Crystal](https://tvtropes.org/pmwiki/pmwiki.php/VideoGame/PokemonVietnameseCrystal) +/// +/// ```rust +/// let name = elfs::next(); +/// ``` +pub fn next() -> String { + let mut generator = Generator::new(moves(), names(), Name::Numbered); + generator.next().unwrap() +} + +#[cfg(test)] +#[test] +fn name() { + assert_ne!(next(), "".to_string()); +} diff --git a/gitea/Cargo.toml b/gitea/Cargo.toml index 33b2d21..cfdd770 100644 --- a/gitea/Cargo.toml +++ b/gitea/Cargo.toml @@ -11,3 +11,7 @@ thiserror = "1" reqwest = { version = "0.10", features = ["json"] } serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } + +[dev-dependencies] +anyhow = "1" +tokio = { version = "0.2", features = ["macros"] } diff --git a/gitea/src/lib.rs b/gitea/src/lib.rs index 4b4a739..e08c6fb 100644 --- a/gitea/src/lib.rs +++ b/gitea/src/lib.rs @@ -3,6 +3,8 @@ use serde::{Deserialize, Serialize}; use std::result::Result as StdResult; use thiserror::Error; +/// Error represents all of the possible errors that can happen with the Gitea +/// API. Most of these errors boil down to user error. #[derive(Error, Debug)] pub enum Error { #[error("error from reqwest: {0:#?}")] @@ -18,22 +20,11 @@ pub enum Error { TagNotFound(String), } +/// A handy alias for Result like `anyhow::Result`. pub type Result = StdResult; -#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] -pub struct Author { - pub id: i64, - pub login: String, - pub full_name: String, - pub email: String, - pub avatar_url: String, - pub language: String, - pub is_admin: bool, - pub last_login: String, - pub created: String, - pub username: String, -} - +/// A repository release. +/// https://try.gitea.io/api/swagger#model-Release #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Release { pub id: i64, @@ -48,10 +39,12 @@ pub struct Release { pub prerelease: bool, pub created_at: String, pub published_at: String, - pub author: Author, + pub author: User, pub assets: Vec, } +/// The inputs to create a repository release. +/// https://try.gitea.io/api/swagger#model-CreateReleaseOption #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct CreateRelease { pub body: String, @@ -62,6 +55,8 @@ pub struct CreateRelease { pub target_commitish: String, } +/// An attachment to a release, such as a pre-built package. +/// https://try.gitea.io/api/swagger#model-Attachment #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Attachment { pub id: i64, @@ -73,6 +68,8 @@ pub struct Attachment { pub browser_download_url: String, } +/// A git repository. +/// https://try.gitea.io/api/swagger#model-Repository #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Repo { pub allow_merge_commits: bool, @@ -113,6 +110,8 @@ pub struct Repo { pub website: String, } +/// A user. +/// https://try.gitea.io/api/swagger#model-User #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct User { pub avatar_url: String, @@ -126,6 +125,8 @@ pub struct User { pub login: String, } +/// The permission set that a given user has on a Repo. +/// https://try.gitea.io/api/swagger#model-Permission #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Permissions { pub admin: bool, @@ -133,24 +134,40 @@ pub struct Permissions { pub push: bool, } +/// The version of Gitea. +/// https://try.gitea.io/api/swagger#model-ServerVersion #[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] pub struct Version { pub version: String, } +/// The gitea client that all gitea calls will go through. This wraps +/// [reqwest::Client](https://docs.rs/reqwest/0.10.6/reqwest/struct.Client.html) +/// and operates asyncronously. pub struct Client { cli: reqwest::Client, base_url: String, } impl Client { - pub fn new(base_url: String, token: String, user_agent: String) -> Result { + /// Create a new API client with the given base URL, token and user agent. + /// If you need inspiration for a user agent, try this: + /// + /// ```rust + /// const APP_USER_AGENT: &'static str = + /// concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION")); + /// gitea::Client::new("https://tulpa.dev".into(), "ayylmao".into(), APP_USER_AGENT).unwrap(); + /// ``` + pub fn new(base_url: String, token: String, user_agent: T) -> Result + where + T: Into, + { let mut headers = header::HeaderMap::new(); let auth = format!("token {}", token); let auth = auth.as_str(); headers.insert(header::AUTHORIZATION, header::HeaderValue::from_str(auth)?); let cli = reqwest::Client::builder() - .user_agent(user_agent) + .user_agent(user_agent.into()) .default_headers(headers) .build()?; Ok(Self { @@ -159,6 +176,18 @@ impl Client { }) } + /// Gets the current version of gitea. + /// + /// ```rust + /// use gitea::Result; + /// + /// #[tokio::main] + /// async fn main() -> Result<()> { + /// let cli = gitea::Client::new("https://tulpa.dev".into(), "ayylmao".into(), "test/test")?; + /// println!("{:?}", cli.version().await?); + /// Ok(()) + /// } + /// ``` pub async fn version(&self) -> Result { Ok(self .cli @@ -170,6 +199,18 @@ impl Client { .await?) } + /// Gets a release of a repo by its tag name. + /// + /// ```rust + /// use gitea::Result; + /// + /// #[tokio::main] + /// async fn main() -> Result<()> { + /// let cli = gitea::Client::new("https://tulpa.dev".into(), "ayylmao".into(), "test/test")?; + /// let release = cli.get_release_by_tag("cadey".into(), "gitea-release".into(), "0.3.2".into()).await; + /// Ok(()) + /// } + /// ``` pub async fn get_release_by_tag( &self, owner: String, @@ -191,6 +232,18 @@ impl Client { } } + /// Gets a gitea repo by name. + /// + /// ```rust + /// use gitea::Result; + /// + /// #[tokio::main] + /// async fn main() -> Result<()> { + /// let cli = gitea::Client::new("https://tulpa.dev".into(), "ayylmao".into(), "test/test")?; + /// let repo = cli.get_repo("cadey".into(), "gitea-release".into()).await; + /// Ok(()) + /// } + /// ``` pub async fn get_repo(&self, owner: String, repo: String) -> Result { Ok(self .cli @@ -205,6 +258,18 @@ impl Client { .await?) } + /// Gets all of the releases for a given gitea repo. + /// + /// ```rust + /// use gitea::Result; + /// + /// #[tokio::main] + /// async fn main() -> Result<()> { + /// let cli = gitea::Client::new("https://tulpa.dev".into(), "ayylmao".into(), "test/test")?; + /// let repo = cli.get_releases("cadey".into(), "gitea-release".into()).await; + /// Ok(()) + /// } + /// ``` pub async fn get_releases(&self, owner: String, repo: String) -> Result> { Ok(self .cli @@ -219,6 +284,29 @@ impl Client { .await?) } + /// Creates a new gitea release. + /// + /// ```rust + /// use gitea::{CreateRelease, Result}; + /// + /// #[tokio::main] + /// async fn main() -> Result<()> { + /// let cli = gitea::Client::new("https://tulpa.dev".into(), "ayylmao".into(), "test/test")?; + /// let repo = cli.create_release( + /// "cadey".into(), + /// "gitea-release".into(), + /// CreateRelease{ + /// body: "This is a cool release".into(), + /// draft: false, + /// name: "test".into(), + /// prerelease: false, + /// tag_name: "v4.2.0".into(), + /// target_commitish: "HEAD".into(), + /// }, + /// ).await; + /// Ok(()) + /// } + /// ``` pub async fn create_release( &self, owner: String, @@ -238,4 +326,33 @@ impl Client { .json() .await?) } + + /// Deletes a given release by tag name. + /// + /// ```rust + /// use gitea::Result; + /// + /// #[tokio::main] + /// async fn main() -> Result<()> { + /// let cli = gitea::Client::new("https://tulpa.dev".into(), "ayylmao".into(), "test/test")?; + /// let _ = cli.delete_release("cadey".into(), "gitea-release".into(), "4.2.0".into()).await; + /// Ok(()) + /// } + /// ``` + pub async fn delete_release(&self, owner: String, repo: String, tag: String) -> Result<()> { + let release = self + .get_release_by_tag(owner.clone(), repo.clone(), tag) + .await?; + + self.cli + .delete(&format!( + "{}/api/v1/repos/{}/{}/releases/{}", + self.base_url, owner, repo, release.id + )) + .send() + .await? + .error_for_status()?; + + Ok(()) + } } diff --git a/gitea/tests/version.rs b/gitea/tests/version.rs new file mode 100644 index 0000000..f4536c1 --- /dev/null +++ b/gitea/tests/version.rs @@ -0,0 +1,16 @@ +use anyhow::Result; +use gitea::Client; + +#[tokio::test] +async fn version() -> Result<()> { + let cli = Client::new( + std::env::var("GITEA_SERVER")?, + std::env::var("DOMO_GITEA_TOKEN")?, + "gitea/tests", + )?; + + let vers = cli.version().await?; + println!("gitea version {}", vers.version); + + Ok(()) +}