From 1a678de43d82fbc865fe8fe6b8586fa82d9596aa Mon Sep 17 00:00:00 2001 From: CrazyMax <1951866+crazy-max@users.noreply.github.com> Date: Wed, 7 Apr 2021 20:54:35 +0200 Subject: [PATCH] Allow to override flavor (#63) Co-authored-by: CrazyMax --- __tests__/meta.test.ts | 49 +++++++++++++++++++++++++++++++--- __tests__/tag.test.ts | 60 +++--------------------------------------- dist/index.js | 10 ++----- src/meta.ts | 4 +-- src/tag.ts | 6 ----- 5 files changed, 53 insertions(+), 76 deletions(-) diff --git a/__tests__/meta.test.ts b/__tests__/meta.test.ts index 2439173..e165297 100644 --- a/__tests__/meta.test.ts +++ b/__tests__/meta.test.ts @@ -1169,6 +1169,47 @@ describe('tag', () => { "org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071", "org.opencontainers.image.licenses=MIT" ] + ], + [ + 'tag19', + 'event_tag_p1-v1.0.0.env', + { + images: ['org/app', 'ghcr.io/user/app'], + tags: [ + `type=match,pattern=p1-v(\\d.\\d.\\d),group=1`, + `type=match,pattern=p1-v(\\d.\\d),group=1,suffix=`, + `type=ref,event=pr`, + `type=sha` + ], + flavor: [ + `suffix=-dev` + ] + } as Inputs, + { + main: '1.0.0-dev', + partial: ['1.0', 'sha-90dd603-dev'], + latest: true + } as Version, + [ + 'org/app:1.0.0-dev', + 'org/app:1.0', + 'org/app:sha-90dd603-dev', + 'org/app:latest', + 'ghcr.io/user/app:1.0.0-dev', + 'ghcr.io/user/app:1.0', + 'ghcr.io/user/app:sha-90dd603-dev', + 'ghcr.io/user/app:latest' + ], + [ + "org.opencontainers.image.title=Hello-World", + "org.opencontainers.image.description=This your first repo!", + "org.opencontainers.image.url=https://github.com/octocat/Hello-World", + "org.opencontainers.image.source=https://github.com/octocat/Hello-World", + "org.opencontainers.image.version=1.0.0-dev", + "org.opencontainers.image.created=2020-01-10T00:30:00.000Z", + "org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071", + "org.opencontainers.image.licenses=MIT" + ] ] ])('given %p with %p event', tagsLabelsTest); }); @@ -1625,20 +1666,20 @@ describe('pr', () => { ] } as Inputs, { - main: 'glo-2-bal', + main: '2-bal', partial: [], latest: false } as Version, [ - 'org/app:glo-2-bal', - 'ghcr.io/user/app:glo-2-bal' + 'org/app:2-bal', + 'ghcr.io/user/app:2-bal' ], [ "org.opencontainers.image.title=Hello-World", "org.opencontainers.image.description=This your first repo!", "org.opencontainers.image.url=https://github.com/octocat/Hello-World", "org.opencontainers.image.source=https://github.com/octocat/Hello-World", - "org.opencontainers.image.version=glo-2-bal", + "org.opencontainers.image.version=2-bal", "org.opencontainers.image.created=2020-01-10T00:30:00.000Z", "org.opencontainers.image.revision=1e9249f05bfc090e0688b8fb9c1b347586add504", "org.opencontainers.image.licenses=MIT" diff --git a/__tests__/tag.test.ts b/__tests__/tag.test.ts index 8032af8..6c83efd 100644 --- a/__tests__/tag.test.ts +++ b/__tests__/tag.test.ts @@ -21,8 +21,6 @@ describe('transform', () => { attrs: { "priority": DefaultPriorities[Type.Schedule], "enable": "true", - "prefix": "", - "suffix": "", "pattern": "nightly" } }, @@ -31,8 +29,6 @@ describe('transform', () => { attrs: { "priority": DefaultPriorities[Type.Semver], "enable": "true", - "prefix": "", - "suffix": "", "pattern": "{{version}}", "value": "" } @@ -42,8 +38,6 @@ describe('transform', () => { attrs: { "priority": DefaultPriorities[Type.Match], "enable": "true", - "prefix": "", - "suffix": "", "pattern": "\\d.\\d.\\d", "group": "0", "value": "" @@ -54,8 +48,6 @@ describe('transform', () => { attrs: { "priority": DefaultPriorities[Type.Edge], "enable": "true", - "prefix": "", - "suffix": "", "branch": "" } }, @@ -64,8 +56,6 @@ describe('transform', () => { attrs: { "priority": DefaultPriorities[Type.Ref], "enable": "true", - "prefix": "", - "suffix": "", "event": RefEvent.Branch } }, @@ -74,8 +64,6 @@ describe('transform', () => { attrs: { "priority": DefaultPriorities[Type.Ref], "enable": "true", - "prefix": "", - "suffix": "", "event": RefEvent.Tag } }, @@ -85,7 +73,6 @@ describe('transform', () => { "priority": DefaultPriorities[Type.Ref], "enable": "true", "prefix": "pr-", - "suffix": "", "event": RefEvent.PR } }, @@ -94,8 +81,6 @@ describe('transform', () => { attrs: { "priority": DefaultPriorities[Type.Raw], "enable": "true", - "prefix": "", - "suffix": "", "value": "foo" } }, @@ -104,8 +89,7 @@ describe('transform', () => { attrs: { "priority": DefaultPriorities[Type.Sha], "enable": "true", - "prefix": "sha-", - "suffix": "" + "prefix": "sha-" } } ] as Tag[], @@ -135,8 +119,6 @@ describe('parse', () => { attrs: { "priority": DefaultPriorities[Type.Schedule], "enable": "true", - "prefix": "", - "suffix": "", "pattern": "{{date 'YYYYMMDD'}}" } } as Tag, @@ -149,8 +131,6 @@ describe('parse', () => { attrs: { "priority": DefaultPriorities[Type.Semver], "enable": "true", - "prefix": "", - "suffix": "", "pattern": "{{version}}", "value": "" } @@ -164,8 +144,6 @@ describe('parse', () => { attrs: { "priority": "1", "enable": "true", - "prefix": "", - "suffix": "", "pattern": "{{version}}", "value": "" } @@ -179,8 +157,6 @@ describe('parse', () => { attrs: { "priority": "1", "enable": "true", - "prefix": "", - "suffix": "", "pattern": "{{version}}", "value": "v1.0.0" } @@ -194,8 +170,6 @@ describe('parse', () => { attrs: { "priority": DefaultPriorities[Type.Match], "enable": "true", - "prefix": "", - "suffix": "", "pattern": "v(.*)", "group": "1", "value": "" @@ -210,8 +184,6 @@ describe('parse', () => { attrs: { "priority": DefaultPriorities[Type.Match], "enable": "true", - "prefix": "", - "suffix": "", "pattern": "^v(\\d.\\d.\\d)$", "group": "1", "value": "" @@ -226,8 +198,6 @@ describe('parse', () => { attrs: { "priority": "700", "enable": "true", - "prefix": "", - "suffix": "", "pattern": "v(.*)", "group": "1", "value": "" @@ -242,8 +212,6 @@ describe('parse', () => { attrs: { "priority": DefaultPriorities[Type.Match], "enable": "true", - "prefix": "", - "suffix": "", "pattern": "v(.*)", "group": "1", "value": "v1.2.3" @@ -263,8 +231,6 @@ describe('parse', () => { attrs: { "priority": DefaultPriorities[Type.Edge], "enable": "true", - "prefix": "", - "suffix": "", "branch": "" } } as Tag, @@ -277,8 +243,6 @@ describe('parse', () => { attrs: { "priority": DefaultPriorities[Type.Edge], "enable": "true", - "prefix": "", - "suffix": "", "branch": "master" } } as Tag, @@ -291,8 +255,6 @@ describe('parse', () => { attrs: { "priority": DefaultPriorities[Type.Ref], "enable": "true", - "prefix": "", - "suffix": "", "event": RefEvent.Tag } } as Tag, @@ -305,8 +267,6 @@ describe('parse', () => { attrs: { "priority": DefaultPriorities[Type.Ref], "enable": "true", - "prefix": "", - "suffix": "", "event": RefEvent.Branch } } as Tag, @@ -320,7 +280,6 @@ describe('parse', () => { "priority": DefaultPriorities[Type.Ref], "enable": "true", "prefix": "pr-", - "suffix": "", "event": RefEvent.PR } } as Tag, @@ -343,8 +302,6 @@ describe('parse', () => { attrs: { "priority": DefaultPriorities[Type.Raw], "enable": "true", - "prefix": "", - "suffix": "", "value": "acustomtag" } } as Tag, @@ -362,8 +319,6 @@ describe('parse', () => { attrs: { "priority": DefaultPriorities[Type.Raw], "enable": "true", - "prefix": "", - "suffix": "", "value": "acustomtag2" } } as Tag, @@ -376,8 +331,6 @@ describe('parse', () => { attrs: { "priority": DefaultPriorities[Type.Raw], "enable": "true", - "prefix": "", - "suffix": "", "value": "acustomtag4" } } as Tag, @@ -390,8 +343,6 @@ describe('parse', () => { attrs: { "priority": DefaultPriorities[Type.Raw], "enable": "false", - "prefix": "", - "suffix": "", "value": "acustomtag5" } } as Tag, @@ -404,8 +355,7 @@ describe('parse', () => { attrs: { "priority": DefaultPriorities[Type.Sha], "enable": "true", - "prefix": "sha-", - "suffix": "" + "prefix": "sha-" } } as Tag, false @@ -417,8 +367,7 @@ describe('parse', () => { attrs: { "priority": DefaultPriorities[Type.Sha], "enable": "true", - "prefix": "", - "suffix": "" + "prefix": "" } } as Tag, false @@ -430,8 +379,7 @@ describe('parse', () => { attrs: { "priority": DefaultPriorities[Type.Sha], "enable": "false", - "prefix": "sha-", - "suffix": "" + "prefix": "sha-" } } as Tag, false diff --git a/dist/index.js b/dist/index.js index da2f1b0..55d5ab0 100644 --- a/dist/index.js +++ b/dist/index.js @@ -634,13 +634,13 @@ class Meta { return version; } setFlavor(val, tag) { - if (tag.attrs['prefix'].length > 0) { + if (tag.attrs.hasOwnProperty('prefix')) { val = `${tag.attrs['prefix']}${val}`; } else if (this.flavor.prefix.length > 0) { val = `${this.flavor.prefix}${val}`; } - if (tag.attrs['suffix'].length > 0) { + if (tag.attrs.hasOwnProperty('suffix')) { val = `${val}${tag.attrs['suffix']}`; } else if (this.flavor.suffix.length > 0) { @@ -912,12 +912,6 @@ function Parse(s) { if (!tag.attrs.hasOwnProperty('priority')) { tag.attrs['priority'] = exports.DefaultPriorities[tag.type]; } - if (!tag.attrs.hasOwnProperty('prefix')) { - tag.attrs['prefix'] = ''; - } - if (!tag.attrs.hasOwnProperty('suffix')) { - tag.attrs['suffix'] = ''; - } if (!['true', 'false'].includes(tag.attrs['enable'])) { throw new Error(`Invalid value for enable attribute: ${tag.attrs['enable']}`); } diff --git a/src/meta.ts b/src/meta.ts index ca6b633..2e5b978 100644 --- a/src/meta.ts +++ b/src/meta.ts @@ -315,12 +315,12 @@ export class Meta { } private setFlavor(val: string, tag: tcl.Tag): string { - if (tag.attrs['prefix'].length > 0) { + if (tag.attrs.hasOwnProperty('prefix')) { val = `${tag.attrs['prefix']}${val}`; } else if (this.flavor.prefix.length > 0) { val = `${this.flavor.prefix}${val}`; } - if (tag.attrs['suffix'].length > 0) { + if (tag.attrs.hasOwnProperty('suffix')) { val = `${val}${tag.attrs['suffix']}`; } else if (this.flavor.suffix.length > 0) { val = `${val}${this.flavor.suffix}`; diff --git a/src/tag.ts b/src/tag.ts index b27e726..474ca6d 100644 --- a/src/tag.ts +++ b/src/tag.ts @@ -185,12 +185,6 @@ export function Parse(s: string): Tag { if (!tag.attrs.hasOwnProperty('priority')) { tag.attrs['priority'] = DefaultPriorities[tag.type]; } - if (!tag.attrs.hasOwnProperty('prefix')) { - tag.attrs['prefix'] = ''; - } - if (!tag.attrs.hasOwnProperty('suffix')) { - tag.attrs['suffix'] = ''; - } if (!['true', 'false'].includes(tag.attrs['enable'])) { throw new Error(`Invalid value for enable attribute: ${tag.attrs['enable']}`); }