Skip to content

Commit

Permalink
Adding helpers for semver usage
Browse files Browse the repository at this point in the history
Signed-off-by: Mehdi Benadel <mehdi.benadel@gmail.com>
  • Loading branch information
Murazaki committed Feb 13, 2024
1 parent 8e5442c commit 9ee6203
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 16 deletions.
30 changes: 30 additions & 0 deletions __tests__/fixtures/event_tag_v9.5.7-alpha.2+fork.3.1.8.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
GITHUB_ACTION=__crazy-max_ghaction-dump-context
GITHUB_ACTIONS=true
GITHUB_ACTION_PATH=/home/runner/work/_actions/crazy-max/ghaction-dump-context/v1
GITHUB_ACTION_REF=
GITHUB_ACTION_REPOSITORY=
GITHUB_ACTOR=crazy-max
GITHUB_API_URL=https://api.github.com
GITHUB_BASE_REF=
GITHUB_ENV=/home/runner/work/_temp/_runner_file_commands/set_env_7703d3cb-84db-438f-9f97-46e159388a55
GITHUB_EVENT_NAME=push
#GITHUB_EVENT_PATH=./__tests__/fixtures/event_tag_v9.5.7-alpha.2+fork.3.1.8.json
GITHUB_GRAPHQL_URL=https://api.github.com/graphql
GITHUB_HEAD_REF=
GITHUB_JOB=test
GITHUB_PATH=/home/runner/work/_temp/_runner_file_commands/add_path_7703d3cb-84db-438f-9f97-46e159388a55
GITHUB_REF=refs/tags/v9.5.7-alpha.2+fork.3.1.8
GITHUB_REF_NAME=v9.5.7-alpha.2+fork.3.1.8
GITHUB_REF_PROTECTED=false
GITHUB_REF_TYPE=tag
GITHUB_REPOSITORY=docker/test-docker-action
GITHUB_REPOSITORY_OWNER=docker
GITHUB_RETENTION_DAYS=90
GITHUB_RUN_ATTEMPT=1
GITHUB_RUN_ID=2188815933
GITHUB_RUN_NUMBER=21
GITHUB_SERVER_URL=https://github.com
GITHUB_SHA=860c1904a1ce19322e91ac35af1ab07466440c37
GITHUB_STEP_SUMMARY=/home/runner/work/_temp/_runner_file_commands/step_summary_7703d3cb-84db-438f-9f97-46e159388a55
GITHUB_WORKFLOW=metadata
GITHUB_WORKSPACE=/home/runner/work/test-docker-action/test-docker-action
72 changes: 67 additions & 5 deletions __tests__/meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1201,13 +1201,15 @@ describe('tag', () => {
]
} as Inputs,
{
main: '2.0.8-beta.67',
partial: [],
main: '2.0',
partial: ["2"],
latest: false
} as Version,
[
'org/app:2.0.8-beta.67',
'ghcr.io/user/app:2.0.8-beta.67'
'org/app:2.0',
'org/app:2',
'ghcr.io/user/app:2.0',
'ghcr.io/user/app:2'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
Expand All @@ -1217,7 +1219,7 @@ describe('tag', () => {
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=2.0.8-beta.67"
"org.opencontainers.image.version=2.0"
],
undefined
],
Expand Down Expand Up @@ -1292,6 +1294,66 @@ describe('tag', () => {
],
undefined
],
[
'tag15.1',
'event_tag_v9.5.7-alpha.2+fork.3.1.8.env',
{
images: ['org/app', 'ghcr.io/user/app'],
tags: [
`type=semver,pattern={{> completeVersion}}`
]
} as Inputs,
{
main: '9.5.7-alpha.2+fork.3.1.8',
partial: [],
latest: false
} as Version,
[
'org/app:9.5.7-alpha.2+fork.3.1.8',
'ghcr.io/user/app:9.5.7-alpha.2+fork.3.1.8'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.licenses=MIT",
"org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=9.5.7-alpha.2+fork.3.1.8",
],
undefined
],
[
'tag15.1',
'event_tag_v9.5.7-alpha.2+fork.3.1.8.env',
{
images: ['org/app', 'ghcr.io/user/app'],
tags: [
`type=semver,pattern={{> buildMetadata}}{{> prereleaseMetadata}}.{{major}}.{{minor}}.{{patch}}`
]
} as Inputs,
{
main: '+fork.3.1.8-alpha.2.9.5.7',
partial: [],
latest: false
} as Version,
[
'org/app:+fork.3.1.8-alpha.2.9.5.7',
'ghcr.io/user/app:+fork.3.1.8-alpha.2.9.5.7'
],
[
"org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
"org.opencontainers.image.description=This your first repo!",
"org.opencontainers.image.licenses=MIT",
"org.opencontainers.image.revision=860c1904a1ce19322e91ac35af1ab07466440c37",
"org.opencontainers.image.source=https://github.com/octocat/Hello-World",
"org.opencontainers.image.title=Hello-World",
"org.opencontainers.image.url=https://github.com/octocat/Hello-World",
"org.opencontainers.image.version=+fork.3.1.8-alpha.2.9.5.7",
],
undefined
],
[
'tag16',
'event_tag_v1.1.1.env',
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

30 changes: 21 additions & 9 deletions src/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,29 @@ export class Meta {
const sver = semver.parse(vraw, {
loose: true
});
if (semver.prerelease(vraw)) {
if (Meta.isRawStatement(tag.attrs['pattern'])) {
vraw = this.setValue(handlebars.compile(tag.attrs['pattern'])(sver), tag);
} else {
vraw = this.setValue(handlebars.compile('{{version}}')(sver), tag);
}
} else {
vraw = this.setValue(handlebars.compile(tag.attrs['pattern'])(sver), tag);

const hbEnv = handlebars.create();

hbEnv.registerPartial('prereleaseMetadata', (data: semver.SemVer) => {
return data.prerelease.length > 0 ? '-' + data.prerelease.join('.') : '';
});

hbEnv.registerPartial('buildMetadata', (data: semver.SemVer) => {
return data.build.length > 0 ? '+' + data.build.join('.') : '';
});

hbEnv.registerPartial('completeVersion', (data: semver.SemVer) => {
return [data.major, data.minor, data.patch].join('.') + (data.prerelease.length > 0 ? '-' + data.prerelease.join('.') : '') + (data.build.length > 0 ? '+' + data.build.join('.') : '');
});

if (!semver.prerelease(vraw)) {
latest = true;
}

const hbTemplate = hbEnv.compile(tag.attrs['pattern']);

vraw = this.setValue(hbTemplate(sver), tag);

return Meta.setVersion(version, vraw, this.flavor.latest == 'auto' ? latest : this.flavor.latest == 'true');
}

Expand Down Expand Up @@ -591,6 +603,6 @@ export class Meta {
}

private static sanitizeTag(tag: string): string {
return tag.replace(/[^a-zA-Z0-9._-]+/g, '-');
return tag.replace(/[^a-zA-Z0-9+._-]+/g, '-');
}
}

0 comments on commit 9ee6203

Please sign in to comment.