Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass external variables #224

Closed
friday opened this issue Jul 13, 2023 · 4 comments
Closed

Pass external variables #224

friday opened this issue Jul 13, 2023 · 4 comments
Assignees
Labels
feature/request New feature or request

Comments

@friday
Copy link

friday commented Jul 13, 2023

I wanted to implement a changelog generator similar to the angular preset for conventional-changelog, as this is what we're migrating away from.

My first attempt was this:

[changelog]
header = "# Changelog\n\n"
# This is similar to the "angular" preset, but skips releases with no commits, uppercases the first letter of each commit message, sorts feat over bug fixes and includes 
body = """
{% if version %}\
  {% if previous %}\
    {% if previous.version %}\
      ## [{{ version | trim_start_matches(pat="v") }}](https://github.com/org/repo/compare/{{ previous.version }}...{{ version }}) ({{ timestamp | date(format="%Y-%m-%d") }})
    {% else %}\
      ## [{{ version | trim_start_matches(pat="v") }}](https://github.com/org/repo/releases/tag/{{ version }}) ({{ timestamp | date(format="%Y-%m-%d") }})
    {% endif %}\
  {% endif %}\
{% else %}\
  ## [unreleased]
{% endif %}\
{% for group, commits in commits | group_by(attribute="group") %}
  ### {{ group | striptags | upper_first }}
  {% for commit in commits %}
    * {% if commit.scope %}**{{commit.scope}}:** {% endif %}{{ commit.message | upper_first }} ([{{commit.id | truncate(length=7, end="")}}](https://github.com/org/repo/commit/{{commit.id}}))
  {%- endfor -%}
  {% raw %}\n{% endraw %}\
{% endfor %}\n
"""

[git]
# regex for preprocessing the commit messages
commit_preprocessors = [
  { pattern = '\((\w+\s)?#([0-9]+)\)', replace = "([#${2}](https://github.com/org/repo/issues/${2}))"}, # replace issue refs with links
]

This is fast and does what we want, but it's hard codes the repo URL in several places, and we have multiple repos we generate changelogs for, so it would be a lot of duplication. We would prefer to be able to reuse one config and pass the repo url/remote either as a variable, get it from the git remote or something in between (like #112).

I tried to use replace_command (thanks for pointing it out for me on Discord), and that kind of worked:

[git]
# regex for preprocessing the commit messages
commit_preprocessors = [
-  { pattern = '\((\w+\s)?#([0-9]+)\)', replace = "([#${2}](https://github.com/org/repo/issues/${2}))"},  # replace issue refs with links
layer/issues/${2}))"}, # replace issue numbers
+  { pattern = '.*', replace_command = 'sed "s|(#\\([[:digit:]]\\+\\))|([#\1]($REPO_URL/issues/\1))|g"' }  # replace issue refs with links
]

This way I could use the REPO_URL variable instead of hard coding the url in the config. However:

  1. git cliff runs 3-4x slower this way.
  2. This only works for the commit messages. I'm sure I could also get the commit issue link handled here, but I could not use it for the version links, could I? Those are not critical for our use case, but I still think replace_command isn't a great option for us.

I don't know if there are any needs for other variables than this, so maybe the #112 / #121 proposals are more reasonable than this, if you are willing to reconsider.

Thanks again for your awesome work!

@friday friday added the feature/request New feature or request label Jul 13, 2023
@welcome
Copy link

welcome bot commented Jul 13, 2023

Thanks for opening your first issue at git-cliff! Be sure to follow the issue template! ⛰️

@friday
Copy link
Author

friday commented Jul 13, 2023

Sorry welcome-bot :D I think I didn't strictly follow it, but I provided the answers to the questions in it.

@friday
Copy link
Author

friday commented Oct 21, 2023

I got around this problem by using a special variable-like token <REPO_URL> where I wanted my repo url and then post-process the output with sed. It's similar to #155, but that only works through the config (which I don't want, I want to share the same config for multiple repos).

git cliff | sed 's|<REPO_URL>|https://github.com/myorg/myrepo|g' > CHANGELOG.md

You can't use an actual variable syntax $REPO_URL as the token though. That will come out as nothing in many places.

@orhun
Copy link
Owner

orhun commented Dec 26, 2023

Hello! This is now possible via remote config and GitHub integration.

@orhun orhun closed this as completed Dec 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature/request New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants