From bd52dc932fd990e9309a0287c13f645bd8dd9ff7 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Fri, 12 Jun 2020 07:04:09 -0400 Subject: [PATCH] Allow users to customize the default branch name Closes #2 --- CHANGELOG.md | 25 +++++++++++ Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 15 ++++++- VERSION | 2 +- shell.nix | 1 + src/cmd/drone_plugin.rs | 14 +++++- src/cmd/mod.rs | 28 +++++++----- src/gitea.rs | 97 +++++++++++++++++++++++++++++++++++++++++ 9 files changed, 169 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 356a3a1..ce172bc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,31 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## Unreleased +## 0.3.0 + +Allows for a customizable default branch name for the drone plugin using either +a hard-coded value or the Gitea api to fetch it. For compatibility's sake, the +default behavior is to fetch the default branch name from the Gitea api. If you +need to hard-code your default branch name, add the config like this: + +```yaml + - name: auto-release + image: xena/gitea-release:0.2.7 + settings: + auth_username: cadey + default_branch: trunk + gitea_server: https://tulpa.dev + gitea_token: + from_secret: GITEA_TOKEN + when: + event: + - push + branch: + - trunk +``` + +Also fixed a suggestion to fetch tags over git before trying to run this in Drone. + ## 0.2.7 ### FIXED diff --git a/Cargo.lock b/Cargo.lock index ea4690c..41a1f78 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -326,7 +326,7 @@ dependencies = [ [[package]] name = "gitea-release" -version = "0.2.7" +version = "0.3.0" dependencies = [ "anyhow", "byte-unit", diff --git a/Cargo.toml b/Cargo.toml index 2184370..e32acaa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gitea-release" -version = "0.2.7" +version = "0.3.0" authors = ["Christine Dodrill "] edition = "2018" diff --git a/README.md b/README.md index e6f59e5..b286e4e 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,8 @@ $ cargo install --git https://tulpa.dev/cadey/gitea-release.git ## Drone Plugin -To use this as a drone plugin, add the following to your `.drone.yml`: +To use this as a drone plugin, add the following to your `.drone.yml` under the +`steps` key: ```yaml - name: auto-release @@ -46,6 +47,18 @@ To use this as a drone plugin, add the following to your `.drone.yml`: Replace the values of the settings as makes sense for your gitea server. The `changelog_path` attribute is optional, and will be `./CHANGELOG.md` by default. +Note that Drone doesn't clone git tags by default. You will need to add a +tag-fetching step to your Drone config until +[#4](https://tulpa.dev/cadey/gitea-release/issues/4) is fixed. You can do that +by adding this before your auto-release step: + +``` + - name: fetch tags + image: alpine/git + commands: + - git fetch --tags +``` + ## CHANGELOG.md and VERSION files The `CHANGELOG.md` file is based on the [Keep a Changelog][kacl] format, but diff --git a/VERSION b/VERSION index b003284..0d91a54 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.7 +0.3.0 diff --git a/shell.nix b/shell.nix index 6953b40..38036e8 100644 --- a/shell.nix +++ b/shell.nix @@ -4,6 +4,7 @@ let in pkgs.mkShell { buildInputs = with pkgs; [ latest.rustChannels.stable.rust + cargo-watch openssl pkg-config libgit2 diff --git a/src/cmd/drone_plugin.rs b/src/cmd/drone_plugin.rs index a37a8e1..ab5759d 100644 --- a/src/cmd/drone_plugin.rs +++ b/src/cmd/drone_plugin.rs @@ -4,11 +4,21 @@ use git2::Repository; use url::Url; pub(crate) async fn run(env: DroneEnv) -> Result<()> { - if env.branch != "master" { + let common: Common = env.clone().into(); + + let default_branch = match &env.default_branch { + None => { + let cli = crate::client(&common)?; + let repo = crate::gitea::get_repo(&cli, &common.server, &common.owner, &common.repo).await?; + repo.default_branch + } + Some(branch) => branch.to_string(), + }; + + if env.branch != default_branch { return Ok(()); } - let common: Common = env.clone().into(); let repo = Repository::open(".")?; let mut u = Url::parse(&env.push_url)?; u.set_username(&env.auth_user).unwrap(); diff --git a/src/cmd/mod.rs b/src/cmd/mod.rs index 80d3dca..509299c 100644 --- a/src/cmd/mod.rs +++ b/src/cmd/mod.rs @@ -9,7 +9,7 @@ pub(crate) mod info; pub(crate) mod release; pub(crate) mod upload; -#[derive(StructOpt, Debug)] +#[derive(StructOpt, Debug, Clone)] pub(crate) struct Common { /// The gitea server to connect to #[structopt(short, long, env = "GITEA_SERVER")] @@ -30,30 +30,36 @@ pub(crate) struct Common { #[derive(StructOpt, Debug, Clone)] pub(crate) struct DroneEnv { + // Given by drone /// push URL - #[structopt(env="DRONE_GIT_HTTP_URL")] + #[structopt(long, env="DRONE_GIT_HTTP_URL")] pub push_url: String, /// repo owner - #[structopt(env="DRONE_REPO_OWNER")] + #[structopt(long, env="DRONE_REPO_OWNER")] pub owner: String, /// repo name - #[structopt(env="DRONE_REPO_NAME")] + #[structopt(long, env="DRONE_REPO_NAME")] pub repo: String, + /// branch + #[structopt(long, env="DRONE_REPO_BRANCH")] + pub branch: String, + + // Given by the user /// auth username - #[structopt(env="PLUGIN_AUTH_USERNAME")] + #[structopt(long, env="PLUGIN_AUTH_USERNAME")] pub auth_user: String, /// Gitea server - #[structopt(env="PLUGIN_GITEA_SERVER")] + #[structopt(long, env="PLUGIN_GITEA_SERVER")] pub server: String, /// Gitea token - #[structopt(env="PLUGIN_GITEA_TOKEN")] + #[structopt(long, env="PLUGIN_GITEA_TOKEN")] pub token: String, /// CHANGELOG path - #[structopt(env="PLUGIN_CHANGELOG_PATH", default_value="./CHANGELOG.md")] + #[structopt(long, env="PLUGIN_CHANGELOG_PATH", default_value="./CHANGELOG.md")] pub changelog_path: PathBuf, - /// branch - #[structopt(env="DRONE_REPO_BRANCH")] - pub branch: String, + /// Default branch name + #[structopt(long, env="PLUGIN_DEFAULT_BRANCH")] + pub default_branch: Option, } impl Into for DroneEnv { diff --git a/src/gitea.rs b/src/gitea.rs index 42ba116..c2e8999 100644 --- a/src/gitea.rs +++ b/src/gitea.rs @@ -116,6 +116,103 @@ impl Attachment { } } +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct Repo { + pub allow_merge_commits: bool, + pub allow_rebase: bool, + pub allow_rebase_explicit: bool, + pub allow_squash_merge: bool, + pub archived: bool, + pub avatar_url: String, + pub clone_url: String, + pub created_at: String, + pub default_branch: String, + pub description: String, + pub empty: bool, + pub external_tracker: ExternalTracker, + pub external_wiki: ExternalWiki, + pub fork: bool, + pub forks_count: i64, + pub full_name: String, + pub has_issues: bool, + pub has_pull_requests: bool, + pub has_wiki: bool, + pub html_url: String, + pub id: i64, + pub ignore_whitespace_conflicts: bool, + pub internal_tracker: InternalTracker, + pub mirror: bool, + pub name: String, + pub open_issues_count: i64, + pub open_pr_counter: i64, + pub original_url: String, + pub owner: Owner, + pub permissions: Permissions, + pub private: bool, + pub release_counter: i64, + pub size: i64, + pub ssh_url: String, + pub stars_count: i64, + pub template: bool, + pub updated_at: String, + pub watchers_count: i64, + pub website: String, +} + +pub(crate) async fn get_repo( + cli: &reqwest::Client, + server: &String, + owner: &String, + repo: &String, +) -> Result { + Ok(cli + .get(&format!("{}/api/v1/repos/{}/{}", server, owner, repo)) + .send() + .await? + .error_for_status()? + .json() + .await?) +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct ExternalTracker { + pub external_tracker_format: String, + pub external_tracker_style: String, + pub external_tracker_url: String, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct ExternalWiki { + pub external_wiki_url: String, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct InternalTracker { + pub allow_only_contributors_to_track_time: bool, + pub enable_issue_dependencies: bool, + pub enable_time_tracker: bool, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct Owner { + pub avatar_url: String, + pub created: String, + pub email: String, + pub full_name: String, + pub id: i64, + pub is_admin: bool, + pub language: String, + pub last_login: String, + pub login: String, +} + +#[derive(Default, Debug, Clone, PartialEq, Serialize, Deserialize)] +pub struct Permissions { + pub admin: bool, + pub pull: bool, + pub push: bool, +} + pub(crate) async fn get_attachments_for_release( cli: &reqwest::Client, server: &String,