Avoid unnecessary calls to version (#21)

Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
This commit is contained in:
CrazyMax 2020-12-01 05:38:08 +01:00 committed by GitHub
parent c53f88523a
commit ef12c77b87
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 19 additions and 17 deletions

View File

@ -44,7 +44,7 @@ const tagsLabelsTest = async (envFile: string, inputs: Inputs, exVersion: Versio
const repo = await github.repo(process.env.GITHUB_TOKEN || ''); const repo = await github.repo(process.env.GITHUB_TOKEN || '');
const meta = new Meta({...getInputs(), ...inputs}, context, repo); const meta = new Meta({...getInputs(), ...inputs}, context, repo);
const version = meta.version(); const version = meta.version;
console.log('version', version); console.log('version', version);
expect(version).toEqual(exVersion); expect(version).toEqual(exVersion);

16
dist/index.js generated vendored
View File

@ -131,7 +131,7 @@ function run() {
core.info(`runId: ${context.runId}`); core.info(`runId: ${context.runId}`);
core.endGroup(); core.endGroup();
const meta = new meta_1.Meta(inputs, context, repo); const meta = new meta_1.Meta(inputs, context, repo);
const version = meta.version(); const version = meta.version;
core.startGroup(`Docker image version`); core.startGroup(`Docker image version`);
core.info(version.main || ''); core.info(version.main || '');
core.endGroup(); core.endGroup();
@ -180,8 +180,9 @@ class Meta {
this.context = context; this.context = context;
this.repo = repo; this.repo = repo;
this.date = new Date(); this.date = new Date();
this.version = this.getVersion();
} }
version() { getVersion() {
const currentDate = this.date; const currentDate = this.date;
const version = { const version = {
main: undefined, main: undefined,
@ -247,18 +248,17 @@ class Meta {
return version; return version;
} }
tags() { tags() {
const version = this.version(); if (!this.version.main) {
if (!version.main) {
return []; return [];
} }
let tags = []; let tags = [];
for (const image of this.inputs.images) { for (const image of this.inputs.images) {
const imageLc = image.toLowerCase(); const imageLc = image.toLowerCase();
tags.push(`${imageLc}:${version.main}`); tags.push(`${imageLc}:${this.version.main}`);
for (const partial of version.partial) { for (const partial of this.version.partial) {
tags.push(`${imageLc}:${partial}`); tags.push(`${imageLc}:${partial}`);
} }
if (version.latest) { if (this.version.latest) {
tags.push(`${imageLc}:latest`); tags.push(`${imageLc}:latest`);
} }
if (this.context.sha && this.inputs.tagSha) { if (this.context.sha && this.inputs.tagSha) {
@ -274,7 +274,7 @@ class Meta {
`org.opencontainers.image.description=${this.repo.description || ''}`, `org.opencontainers.image.description=${this.repo.description || ''}`,
`org.opencontainers.image.url=${this.repo.html_url || ''}`, `org.opencontainers.image.url=${this.repo.html_url || ''}`,
`org.opencontainers.image.source=${this.repo.html_url || ''}`, `org.opencontainers.image.source=${this.repo.html_url || ''}`,
`org.opencontainers.image.version=${this.version().main || ''}`, `org.opencontainers.image.version=${this.version.main || ''}`,
`org.opencontainers.image.created=${this.date.toISOString()}`, `org.opencontainers.image.created=${this.date.toISOString()}`,
`org.opencontainers.image.revision=${this.context.sha || ''}`, `org.opencontainers.image.revision=${this.context.sha || ''}`,
`org.opencontainers.image.licenses=${((_a = this.repo.license) === null || _a === void 0 ? void 0 : _a.spdx_id) || ''}` `org.opencontainers.image.licenses=${((_a = this.repo.license) === null || _a === void 0 ? void 0 : _a.spdx_id) || ''}`

View File

@ -27,7 +27,7 @@ async function run() {
const meta: Meta = new Meta(inputs, context, repo); const meta: Meta = new Meta(inputs, context, repo);
const version: Version = meta.version(); const version: Version = meta.version;
core.startGroup(`Docker image version`); core.startGroup(`Docker image version`);
core.info(version.main || ''); core.info(version.main || '');
core.endGroup(); core.endGroup();

View File

@ -12,6 +12,8 @@ export interface Version {
} }
export class Meta { export class Meta {
public readonly version: Version;
private readonly inputs: Inputs; private readonly inputs: Inputs;
private readonly context: Context; private readonly context: Context;
private readonly repo: ReposGetResponseData; private readonly repo: ReposGetResponseData;
@ -25,9 +27,10 @@ export class Meta {
this.context = context; this.context = context;
this.repo = repo; this.repo = repo;
this.date = new Date(); this.date = new Date();
this.version = this.getVersion();
} }
public version(): Version { private getVersion(): Version {
const currentDate = this.date; const currentDate = this.date;
const version: Version = { const version: Version = {
main: undefined, main: undefined,
@ -89,19 +92,18 @@ export class Meta {
} }
public tags(): Array<string> { public tags(): Array<string> {
const version: Version = this.version(); if (!this.version.main) {
if (!version.main) {
return []; return [];
} }
let tags: Array<string> = []; let tags: Array<string> = [];
for (const image of this.inputs.images) { for (const image of this.inputs.images) {
const imageLc = image.toLowerCase(); const imageLc = image.toLowerCase();
tags.push(`${imageLc}:${version.main}`); tags.push(`${imageLc}:${this.version.main}`);
for (const partial of version.partial) { for (const partial of this.version.partial) {
tags.push(`${imageLc}:${partial}`); tags.push(`${imageLc}:${partial}`);
} }
if (version.latest) { if (this.version.latest) {
tags.push(`${imageLc}:latest`); tags.push(`${imageLc}:latest`);
} }
if (this.context.sha && this.inputs.tagSha) { if (this.context.sha && this.inputs.tagSha) {
@ -117,7 +119,7 @@ export class Meta {
`org.opencontainers.image.description=${this.repo.description || ''}`, `org.opencontainers.image.description=${this.repo.description || ''}`,
`org.opencontainers.image.url=${this.repo.html_url || ''}`, `org.opencontainers.image.url=${this.repo.html_url || ''}`,
`org.opencontainers.image.source=${this.repo.html_url || ''}`, `org.opencontainers.image.source=${this.repo.html_url || ''}`,
`org.opencontainers.image.version=${this.version().main || ''}`, `org.opencontainers.image.version=${this.version.main || ''}`,
`org.opencontainers.image.created=${this.date.toISOString()}`, `org.opencontainers.image.created=${this.date.toISOString()}`,
`org.opencontainers.image.revision=${this.context.sha || ''}`, `org.opencontainers.image.revision=${this.context.sha || ''}`,
`org.opencontainers.image.licenses=${this.repo.license?.spdx_id || ''}` `org.opencontainers.image.licenses=${this.repo.license?.spdx_id || ''}`