Allow users to customize the default branch name #5

Merged
cadey merged 3 commits from customizable-branch-name into master 2020-06-12 11:18:14 +00:00
11 changed files with 178 additions and 24 deletions

View File

@ -6,6 +6,31 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased ## 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.3.0
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 ## 0.2.7
### FIXED ### FIXED

2
Cargo.lock generated
View File

@ -326,7 +326,7 @@ dependencies = [
[[package]] [[package]]
name = "gitea-release" name = "gitea-release"
version = "0.2.7" version = "0.3.0"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"byte-unit", "byte-unit",

View File

@ -1,6 +1,6 @@
[package] [package]
name = "gitea-release" name = "gitea-release"
version = "0.2.7" version = "0.3.0"
authors = ["Christine Dodrill <me@christine.website>"] authors = ["Christine Dodrill <me@christine.website>"]
edition = "2018" edition = "2018"

View File

@ -25,7 +25,8 @@ $ cargo install --git https://tulpa.dev/cadey/gitea-release.git
## Drone Plugin ## 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 ```yaml
- name: auto-release - 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 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. `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 ## CHANGELOG.md and VERSION files
The `CHANGELOG.md` file is based on the [Keep a Changelog][kacl] format, but The `CHANGELOG.md` file is based on the [Keep a Changelog][kacl] format, but

View File

@ -1 +1 @@
0.2.7 0.3.0

View File

@ -4,6 +4,7 @@ let
in pkgs.mkShell { in pkgs.mkShell {
buildInputs = with pkgs; [ buildInputs = with pkgs; [
latest.rustChannels.stable.rust latest.rustChannels.stable.rust
cargo-watch
openssl openssl
pkg-config pkg-config
libgit2 libgit2

View File

@ -74,6 +74,9 @@ mod tests {
let res = super::read("testdata/basic.md".into(), "0.1.0".into()); let res = super::read("testdata/basic.md".into(), "0.1.0".into());
assert!(res.is_ok()); assert!(res.is_ok());
let delta = res.unwrap(); let delta = res.unwrap();
assert_eq!(delta, "Hi there this is a test\\!\n### ADDED\n - something\n") assert_eq!(
delta,
"Hi there this is a test\\!\n### ADDED\n - something\n"
)
} }
} }

View File

@ -4,11 +4,22 @@ use git2::Repository;
use url::Url; use url::Url;
pub(crate) async fn run(env: DroneEnv) -> Result<()> { 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(()); return Ok(());
} }
let common: Common = env.clone().into();
let repo = Repository::open(".")?; let repo = Repository::open(".")?;
let mut u = Url::parse(&env.push_url)?; let mut u = Url::parse(&env.push_url)?;
u.set_username(&env.auth_user).unwrap(); u.set_username(&env.auth_user).unwrap();

View File

@ -32,12 +32,10 @@ pub(crate) async fn run(
cr.prerelease = rm.pre_release; cr.prerelease = rm.pre_release;
let resp = cli let resp = cli
.post( .post(&format!(
&format!( "{}/api/v1/repos/{}/{}/releases/{}",
"{}/api/v1/repos/{}/{}/releases/{}", common.server, common.owner, common.repo, release.id
common.server, common.owner, common.repo, release.id ))
),
)
.json(&cr) .json(&cr)
.send() .send()
.await?; .await?;

View File

@ -9,7 +9,7 @@ pub(crate) mod info;
pub(crate) mod release; pub(crate) mod release;
pub(crate) mod upload; pub(crate) mod upload;
#[derive(StructOpt, Debug)] #[derive(StructOpt, Debug, Clone)]
pub(crate) struct Common { pub(crate) struct Common {
/// The gitea server to connect to /// The gitea server to connect to
#[structopt(short, long, env = "GITEA_SERVER")] #[structopt(short, long, env = "GITEA_SERVER")]
@ -30,30 +30,36 @@ pub(crate) struct Common {
#[derive(StructOpt, Debug, Clone)] #[derive(StructOpt, Debug, Clone)]
pub(crate) struct DroneEnv { pub(crate) struct DroneEnv {
// Given by drone
/// push URL /// push URL
#[structopt(env="DRONE_GIT_HTTP_URL")] #[structopt(long, env = "DRONE_GIT_HTTP_URL")]
pub push_url: String, pub push_url: String,
/// repo owner /// repo owner
#[structopt(env="DRONE_REPO_OWNER")] #[structopt(long, env = "DRONE_REPO_OWNER")]
pub owner: String, pub owner: String,
/// repo name /// repo name
#[structopt(env="DRONE_REPO_NAME")] #[structopt(long, env = "DRONE_REPO_NAME")]
pub repo: String, pub repo: String,
/// branch
#[structopt(long, env = "DRONE_REPO_BRANCH")]
pub branch: String,
// Given by the user
/// auth username /// auth username
#[structopt(env="PLUGIN_AUTH_USERNAME")] #[structopt(long, env = "PLUGIN_AUTH_USERNAME")]
pub auth_user: String, pub auth_user: String,
/// Gitea server /// Gitea server
#[structopt(env="PLUGIN_GITEA_SERVER")] #[structopt(long, env = "PLUGIN_GITEA_SERVER")]
pub server: String, pub server: String,
/// Gitea token /// Gitea token
#[structopt(env="PLUGIN_GITEA_TOKEN")] #[structopt(long, env = "PLUGIN_GITEA_TOKEN")]
pub token: String, pub token: String,
/// CHANGELOG path /// 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, pub changelog_path: PathBuf,
/// branch /// Default branch name
#[structopt(env="DRONE_REPO_BRANCH")] #[structopt(long, env = "PLUGIN_DEFAULT_BRANCH")]
pub branch: String, pub default_branch: Option<String>,
} }
impl Into<Common> for DroneEnv { impl Into<Common> for DroneEnv {

View File

@ -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<Repo> {
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( pub(crate) async fn get_attachments_for_release(
cli: &reqwest::Client, cli: &reqwest::Client,
server: &String, server: &String,