Skip to content

Add support for adding .cff-file variable in version_variables #962

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

Closed
geddy11 opened this issue Jun 18, 2024 · 6 comments · Fixed by #1028
Closed

Add support for adding .cff-file variable in version_variables #962

geddy11 opened this issue Jun 18, 2024 · 6 comments · Fixed by #1028
Labels

Comments

@geddy11
Copy link

geddy11 commented Jun 18, 2024

When you have a CITATION.cff file in a python project, the package version is stored in a variable version in this file which needs to be updated for each release, i.e.:

cff-version: 1.2.0
message: "If you use this software, please cite it as below."
authors:
  - family-names: Druskat
    given-names: Stephan
    orcid: https://orcid.org/1234-5678-9101-1121
title: "My Research Software"
version: 2.0.4
identifiers:
  - type: doi
    value: 10.5281/zenodo.1234
date-released: 2021-08-11

Request support for adding CITATION.cff to version_variables list.

@codejedi365 codejedi365 added feature A new feature or a feature request confirmed Prevent from becoming stale labels Jun 21, 2024
@codejedi365
Copy link
Contributor

@geddy11, the version variables specification is fairly flexible, you should be able to use filename:variable like this:

[tool.semantic_release]
version_variables = [
    "CITATION.cff:version",
]

Since the citation.cff is valid yaml, it should work as it did over in #601. The minor nuance is that you likely will need to wrap the version value in quotes which is still valid yaml but it makes the regex match properly.

@codejedi365 codejedi365 added awaiting-reply Waiting for response and removed confirmed Prevent from becoming stale labels Jun 21, 2024
@geddy11
Copy link
Author

geddy11 commented Jun 21, 2024

Wrapping version value in quotes works. GitHub "Cite this repository"-function also handles quotes apparently, so this is acceptable solution.

@codejedi365 codejedi365 added question and removed awaiting-reply Waiting for response feature A new feature or a feature request labels Jun 21, 2024
codejedi365 added a commit to codejedi365/python-semantic-release that referenced this issue Sep 23, 2024
… (ie. json, yaml, etc)

Previously json would not work due to the key being wrapped in quotes, yaml also has issues
when it does not usually use quotes. The regex we created originally only wrapped the version
to be replaced in quotes but now both the key and version can optionally be wrapped in
different kind of quotations.

Resolves: python-semantic-release#601, python-semantic-release#706, python-semantic-release#962, python-semantic-release#1026
codejedi365 added a commit to codejedi365/python-semantic-release that referenced this issue Sep 23, 2024
… (ie. json, yaml, etc)

Previously json would not work due to the key being wrapped in quotes, yaml also has issues
when it does not usually use quotes. The regex we created originally only wrapped the version
to be replaced in quotes but now both the key and version can optionally be wrapped in
different kind of quotations.

Resolves: python-semantic-release#601, python-semantic-release#706, python-semantic-release#962, python-semantic-release#1026
codejedi365 added a commit to codejedi365/python-semantic-release that referenced this issue Sep 27, 2024
… (ie. json, yaml, etc)

Previously json would not work due to the key being wrapped in quotes, yaml also has issues
when it does not usually use quotes. The regex we created originally only wrapped the version
to be replaced in quotes but now both the key and version can optionally be wrapped in
different kind of quotations.

Resolves: python-semantic-release#601, python-semantic-release#706, python-semantic-release#962, python-semantic-release#1026
codejedi365 added a commit that referenced this issue Sep 27, 2024
…ie. json, yaml, etc) (#1028)

* fix(version-cmd): increase `version_variable` flexibility with quotations (ie. json, yaml, etc)

  Previously json would not work due to the key being wrapped in quotes, yaml also has issues
  when it does not usually use quotes. The regex we created originally only wrapped the version
  to be replaced in quotes but now both the key and version can optionally be wrapped in
  different kind of quotations.

  Resolves: #601, #706, #962, #1026

* docs(configuration): add clarity to `version_variables` usage & limitations

  Ref: #941

* fix(version-cmd): ensure `version_variables` do not match partial variable names

* build(deps-test): add `PyYAML` as a test dependency

* test(fixtures): refactor location of fixture for global use of cli runner

* test(stamp-version): add test cases to stamp json, python, & yaml files
@codejedi365
Copy link
Contributor

🎉 This resolution has been included in version 9.8.9 🎉

The release is available on:


@geddy11, I know we resolved this question earlier but I wanted to let you know of the latest release which has a more flexible regular expression now that you will no longer need quotes around your version number inside your yaml file.

@afuetterer
Copy link
Contributor

This is great. Thanks for the addition.

Is there a way to update date-released: 2021-08-11 (see example above) with the current date, when the version in citation.cff is bumped? I am still using the proposed sed workflow from #912.

@codejedi365
Copy link
Contributor

codejedi365 commented Nov 12, 2024

@afuetterer, there isn't a way through a build command variable unless you calculate your own date in the shell script. I think I have a sed line in the other PR that would update the date in option 1 if that's the way you want to go.

I'm not sure why I didn't think of this before but it might be a better alternative to utilize the custom changelog templating engine. There would be lot more variables and context available to update your files.

  1. Create a folder templates
  2. Copy the directory from inside PSR (where you installed PSR, or here on GitHub) data/templates/angular/md into templates.
  3. Create a CITATION.cff.j2 in the templates folder. Add your default contents and then some jinja replacement variables.
{%- set release = ctx.history.released.values() | first -%}
cff-version: 1.2.0
message: "If you use this software, please cite it as below."
authors:
  - family-names: Druskat
    given-names: Stephan
    orcid: https://orcid.org/1234-5678-9101-1121
title: "My Research Software"
version: {{ release.version | string }}
identifiers:
  - type: doi
    value: 10.5281/zenodo.1234
date-released: {{ release.tagged_date.strftime("%Y-%m-%d") }}

WARNING: Not tested but should get you close.

With this solution the file will be generated/updated through the changelog generation activities either with the changelog command or during the version command.

The downside to this is that you will have to maintain your own changelog templates instead of PSR maintaining the default ones. If the release notes file doesn't exist however, it will fall back to PSRs internal default template.

@afuetterer
Copy link
Contributor

Thank you @codejedi365. I will try that. I already have my own templates in place, so another one is not a big deal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants