Skip to content

Commit

Permalink
feat: add option for github remote repository
Browse files Browse the repository at this point in the history
Adds new option `--github-repository-url <github repository url>`.
If set, necessary commit preprocessors are added.
These preprocessors replace github issue (#i) and pull request (@i)
refrences with the correct markdown links to the corresponding
issues & pull requests.
  • Loading branch information
FlrnFrmm committed Oct 16, 2022
1 parent 0f38960 commit f799ff8
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 33 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

52 changes: 40 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,18 +173,20 @@ git-cliff [FLAGS] [OPTIONS] [--] [RANGE]
**Options:**

```
-c, --config <PATH> Sets the configuration file [env: GIT_CLIFF_CONFIG=] [default: cliff.toml]
-w, --workdir <PATH> Sets the working directory [env: GIT_CLIFF_WORKDIR=]
-r, --repository <PATH> Sets the git repository [env: GIT_CLIFF_REPOSITORY=]
--include-path <PATTERN>... Sets the path to include related commits [env: GIT_CLIFF_INCLUDE_PATH=]
--exclude-path <PATTERN>... Sets the path to exclude related commits [env: GIT_CLIFF_EXCLUDE_PATH=]
--with-commit <MSG>... Sets custom commit messages to include in the changelog [env: GIT_CLIFF_WITH_COMMIT=]
-p, --prepend <PATH> Prepends entries to the given changelog file [env: GIT_CLIFF_PREPEND=]
-o, --output <PATH> Writes output to the given file [env: GIT_CLIFF_OUTPUT=]
-t, --tag <TAG> Sets the tag for the latest version [env: GIT_CLIFF_TAG=]
-b, --body <TEMPLATE> Sets the template for the changelog body [env: GIT_CLIFF_TEMPLATE=]
-s, --strip <PART> Strips the given parts from the changelog [possible values: header, footer, all]
--sort <SORT> Sets sorting of the commits inside sections [default: oldest] [possible values: oldest, newest]
-c, --config <PATH> Sets the configuration file [env: GIT_CLIFF_CONFIG=] [default: cliff.toml]
-w, --workdir <PATH> Sets the working directory [env: GIT_CLIFF_WORKDIR=]
-r, --repository <PATH> Sets the git repository [env: GIT_CLIFF_REPOSITORY=]
--include-path <PATTERN>... Sets the path to include related commits [env: GIT_CLIFF_INCLUDE_PATH=]
--exclude-path <PATTERN>... Sets the path to exclude related commits [env: GIT_CLIFF_EXCLUDE_PATH=]
--with-commit <MSG>... Sets custom commit messages to include in the changelog [env: GIT_CLIFF_WITH_COMMIT=]
-p, --prepend <PATH> Prepends entries to the given changelog file [env: GIT_CLIFF_PREPEND=]
-o, --output <PATH> Writes output to the given file [env: GIT_CLIFF_OUTPUT=]
-t, --tag <TAG> Sets the tag for the latest version [env: GIT_CLIFF_TAG=]
-b, --body <TEMPLATE> Sets the template for the changelog body [env: GIT_CLIFF_TEMPLATE=]
-s, --strip <PART> Strips the given parts from the changelog [possible values: header, footer, all]
--sort <SORT> Sets sorting of the commits inside sections [default: oldest] [possible values: oldest, newest]
--github-repository-url <GITHUB_REPOSITORY_URL> Adds [CommitPreprocessor] steps for github issues (#i) & pull requests (@i) [env: GIT_CLIFF_GITHUB_REPOSITORY_URL=]
```

**Args:**
Expand Down Expand Up @@ -309,6 +311,32 @@ git cliff --body $template --strip footer

Also, see the [release script](./release.sh) of this project which sets the changelog as a message of an annotated tag.

Add commit preprocessors to create markdown links for github issues & pull requests:

Assuming we have a git commit like ...

```sh
feat: @1 adds a new feature as suggested by #1
```

```sh
git-cliff --github-repository-url github.com/example-repo
```

Will generate the following changelog entry for the above commit:

- [Pull Request 1](https://github.com/example-repo/pull/1) adds a new feature as suggested by [Issue 1](https://github.com/example-repo/issues/1)

<details>
<summary>Raw Output</summary>

```
- [Pull Request 1](https://github.com/example-repo/pull/1) adds a new feature as suggested by [Issue 1](https://github.com/example-repo/issues/1)
```

</details>


## Docker

The easiest way of running **git-cliff** (in the git root directory with [configuration file](#configuration-file) present) is to use the [available tags](https://hub.docker.com/repository/docker/orhunp/git-cliff/tags) from [Docker Hub](https://hub.docker.com/repository/docker/orhunp/git-cliff):
Expand Down
1 change: 1 addition & 0 deletions git-cliff/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ log = "0.4.17"
dirs-next = "2.0.0"
clap_complete = "3.2.5"
clap_mangen = "0.1.11"
regex = "1.6.0"

[dependencies.git-cliff-core]
version = "0.9.2" # managed by release.sh
Expand Down
44 changes: 24 additions & 20 deletions git-cliff/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,46 +36,46 @@ pub enum Sort {
pub struct Opt {
/// Increases the logging verbosity.
#[clap(short, long, parse(from_occurrences), alias = "debug", help_heading = Some("FLAGS"))]
pub verbose: u8,
pub verbose: u8,
/// Sets the configuration file.
#[clap(short, long, env = "GIT_CLIFF_CONFIG", value_name = "PATH", default_value = DEFAULT_CONFIG)]
pub config: PathBuf,
pub config: PathBuf,
/// Sets the working directory.
#[clap(short, long, env = "GIT_CLIFF_WORKDIR", value_name = "PATH")]
pub workdir: Option<PathBuf>,
pub workdir: Option<PathBuf>,
/// Sets the git repository.
#[clap(short, long, env = "GIT_CLIFF_REPOSITORY", value_name = "PATH")]
pub repository: Option<PathBuf>,
pub repository: Option<PathBuf>,
/// Sets the path to include related commits.
#[clap(
long,
env = "GIT_CLIFF_INCLUDE_PATH",
value_name = "PATTERN",
multiple_values = true
)]
pub include_path: Option<Vec<Pattern>>,
pub include_path: Option<Vec<Pattern>>,
/// Sets the path to exclude related commits.
#[clap(
long,
env = "GIT_CLIFF_EXCLUDE_PATH",
value_name = "PATTERN",
multiple_values = true
)]
pub exclude_path: Option<Vec<Pattern>>,
pub exclude_path: Option<Vec<Pattern>>,
/// Sets custom commit messages to include in the changelog.
#[clap(
long,
env = "GIT_CLIFF_WITH_COMMIT",
value_name = "MSG",
multiple_values = true
)]
pub with_commit: Option<Vec<String>>,
pub with_commit: Option<Vec<String>>,
/// Prepends entries to the given changelog file.
#[clap(short, long, env = "GIT_CLIFF_PREPEND", value_name = "PATH")]
pub prepend: Option<PathBuf>,
pub prepend: Option<PathBuf>,
/// Writes output to the given file.
#[clap(short, long, env = "GIT_CLIFF_OUTPUT", value_name = "PATH")]
pub output: Option<PathBuf>,
pub output: Option<PathBuf>,
/// Sets the tag for the latest version.
#[clap(
short,
Expand All @@ -84,7 +84,7 @@ pub struct Opt {
value_name = "TAG",
allow_hyphen_values = true
)]
pub tag: Option<String>,
pub tag: Option<String>,
/// Sets the template for the changelog body.
#[clap(
short,
Expand All @@ -93,36 +93,40 @@ pub struct Opt {
value_name = "TEMPLATE",
allow_hyphen_values = true
)]
pub body: Option<String>,
pub body: Option<String>,
/// Writes the default configuration file to cliff.toml
#[clap(short, long, help_heading = Some("FLAGS"))]
pub init: bool,
pub init: bool,
/// Processes the commits starting from the latest tag.
#[clap(short, long, help_heading = Some("FLAGS"))]
pub latest: bool,
pub latest: bool,
/// Processes the commits that belong to the current tag.
#[clap(long, help_heading = Some("FLAGS"))]
pub current: bool,
pub current: bool,
/// Processes the commits that do not belong to a tag.
#[clap(short, long, help_heading = Some("FLAGS"))]
pub unreleased: bool,
pub unreleased: bool,
/// Sorts the tags chronologically.
#[clap(long, help_heading = Some("FLAGS"))]
pub date_order: bool,
pub date_order: bool,
/// Prints changelog context as JSON.
#[clap(long, help_heading = Some("FLAGS"))]
pub context: bool,
pub context: bool,
/// Strips the given parts from the changelog.
#[clap(short, long, value_name = "PART", arg_enum)]
pub strip: Option<Strip>,
pub strip: Option<Strip>,
/// Sets sorting of the commits inside sections.
#[clap(
long,
arg_enum,
default_value_t = Sort::Oldest
)]
pub sort: Sort,
pub sort: Sort,
/// Sets the commit range to process.
#[clap(value_name = "RANGE", help_heading = Some("ARGS"))]
pub range: Option<String>,
pub range: Option<String>,
/// Adds [CommitPreprocessor] steps for github issues (#i) & pull requests
/// (@i)
#[clap(long, env = "GIT_CLIFF_GITHUB_REPOSITORY_URL")]
pub github_repository_url: Option<String>,
}
37 changes: 36 additions & 1 deletion git-cliff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ use args::{
use changelog::Changelog;
use clap::ArgEnum;
use git_cliff_core::commit::Commit;
use git_cliff_core::config::Config;
use git_cliff_core::config::{
CommitPreprocessor,
Config,
};
use git_cliff_core::embed::EmbeddedConfig;
use git_cliff_core::error::{
Error,
Expand Down Expand Up @@ -101,6 +104,12 @@ pub fn run(mut args: Opt) -> Result<()> {
config.changelog.body = EmbeddedConfig::parse()?.changelog.body;
}

// Add custom git_cliff_core::config::LinkParser for github issues & pull
// requests
if let Some(ref url) = args.github_repository_url {
add_github_link_preprocessors(&mut config, url.clone())?;
}

// Update the configuration based on command line arguments and vice versa.
match args.strip {
Some(Strip::Header) => {
Expand Down Expand Up @@ -301,3 +310,29 @@ pub fn run(mut args: Opt) -> Result<()> {
changelog.generate(&mut io::stdout())
}
}

fn add_github_link_preprocessors(
config: &mut Config,
repository_url: String,
) -> Result<()> {
let github_link_parsers = [
CommitPreprocessor {
pattern: regex::Regex::new("#(\\d+)")?,
replace: Some(format!(
"[Issue ${{1}}](https://{repository_url}/issues/${{1}})"
)),
replace_command: None,
},
CommitPreprocessor {
pattern: regex::Regex::new("@(\\d+)")?,
replace: Some(format!(
"[Pull Request ${{1}}](https://{repository_url}/pull/${{1}})"
)),
replace_command: None,
},
]
.into_iter()
.collect();
config.git.commit_preprocessors = Some(github_link_parsers);
Ok(())
}

0 comments on commit f799ff8

Please sign in to comment.