Skip to content

Commit 4b8759f

Browse files
committed
Add tag_prefix option
1 parent dcc8578 commit 4b8759f

File tree

4 files changed

+21
-13
lines changed

4 files changed

+21
-13
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ Sync your GitHub Release notes with your `CHANGELOG.md`.
44

55
This tool expects that you follow the [Keep a Changelog](https://keepachangelog.com/) format.
66

7-
Note that, per [SemVer](https://semver.org/spec/v1.0.0.html#tagging-specification-semvertag), this tool also expects that your git tags are prefixed with a "v". As in, "v3.0.0" and not "3.0.0".
8-
97
## Use the GitHub Action
108

119
```yml
@@ -25,6 +23,9 @@ jobs:
2523
- uses: dropseed/changerelease@v1
2624
with:
2725
github_token: ${{ secrets.GITHUB_TOKEN }}
26+
# optional
27+
tag_prefix: v
28+
changelog: CHANGELOG.md
2829
```
2930
3031
## Use the Python package

action.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ inputs:
55
description: 'Path to your CHANGELOG'
66
required: false
77
default: 'CHANGELOG.md'
8+
tag_prefix:
9+
description: 'The prefix that is on SemVer tags (i.e. "v" or nothing "")'
10+
default: 'v'
811
github_token:
912
description: "A GitHub token that can create and modify releases"
1013
required: true
1114
runs:
1215
using: docker
1316
image: Dockerfile
1417
args:
15-
- --changelog ${{ inputs.changelog }}
18+
- --changelog ${{ inputs.changelog }} --tag-prefix ${{ inputs.tag_prefix }}
1619
env:
1720
GITHUB_TOKEN: ${{ inputs.github_token }}

changerelease/cli.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ def cli():
2020
default="CHANGELOG.md",
2121
type=click.Path(exists=True, file_okay=True, dir_okay=False),
2222
)
23+
@click.option("--tag-prefix", default="v")
2324
@click.option("--repo", default=lambda: os.environ.get("GITHUB_REPOSITORY", ""))
2425
@click.option(
2526
"--api-url",
2627
default=lambda: os.environ.get("GITHUB_API_URL", "https://api.github.com"),
2728
)
2829
@click.option("--token", default=lambda: os.environ.get("GITHUB_TOKEN", ""))
29-
def sync(changelog, repo, api_url, token):
30+
def sync(changelog, tag_prefix, repo, api_url, token):
3031
requests_session = APISession(base_url=api_url)
3132
requests_session.headers.update(
3233
{
@@ -44,9 +45,10 @@ def sync(changelog, repo, api_url, token):
4445
click.secho(f"No content found for version {version}", fg="yellow")
4546
continue
4647

47-
release = Release(repo, version, requests_session)
48+
release = Release(repo, tag_prefix, version, requests_session)
4849
release.sync(version_contents)
4950

51+
# TODO fail if tag doesn't exist yet? (continue w/ others though)
5052
click.secho(f"\nGitHub releases on {repo} synced with {cl.path}", fg="green")
5153

5254

changerelease/release.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33

44
class Release:
5-
def __init__(self, repo, version, requests_session):
5+
def __init__(self, repo, tag_prefix, version, requests_session):
66
self.repo = repo
7+
self.tag_prefix = tag_prefix
78
self.version = version
9+
self.version_tag = tag_prefix + version
810

911
self.requests_session = requests_session
1012

@@ -15,7 +17,7 @@ def exists(self):
1517

1618
def tag_exists(self):
1719
response = self.requests_session.get(
18-
f"/repos/{self.repo}/git/refs/tags/v{self.version}"
20+
f"/repos/{self.repo}/git/refs/tags/{self.version_tag}"
1921
)
2022
if response.status_code == 404:
2123
return False
@@ -27,19 +29,19 @@ def body_matches(self, contents):
2729

2830
def get(self):
2931
response = self.requests_session.get(
30-
f"/repos/{self.repo}/releases/tags/v{self.version}"
32+
f"/repos/{self.repo}/releases/tags/{self.version_tag}"
3133
)
3234
if response.status_code == 404:
3335
return {}
3436
response.raise_for_status()
3537
return response.json()
3638

37-
def create(self, name, contents):
39+
def create(self, contents):
3840
response = self.requests_session.post(
3941
f"/repos/{self.repo}/releases",
4042
json={
41-
"tag_name": "v" + name,
42-
"name": name,
43+
"tag_name": self.version_tag,
44+
"name": self.version,
4345
"body": contents,
4446
# TODO prerelease if semver prerelease
4547
},
@@ -61,11 +63,11 @@ def sync(self, contents):
6163
f"Release for {self.version} does not exist. Creating it...",
6264
fg="green",
6365
)
64-
release_data = self.create(self.version, contents)
66+
release_data = self.create(contents)
6567
click.echo(release_data["html_url"])
6668
else:
6769
click.secho(
68-
f'Git tag "v{self.version}" not found. A tag needs to be pushed before we can create a release for it.',
70+
f'Git tag "{self.version_tag}" not found. A tag needs to be pushed before we can create a release for it.',
6971
fg="red",
7072
)
7173
elif not self.body_matches(contents):

0 commit comments

Comments
 (0)