Skip to content

Commit

Permalink
Avoid unnecessary calls to version (#21)
Browse files Browse the repository at this point in the history
Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
  • Loading branch information
2 people authored and GitHub committed Dec 1, 2020
1 parent c53f885 commit ef12c77
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion __tests__/meta.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const tagsLabelsTest = async (envFile: string, inputs: Inputs, exVersion: Versio
const repo = await github.repo(process.env.GITHUB_TOKEN || '');
const meta = new Meta({...getInputs(), ...inputs}, context, repo);

const version = meta.version();
const version = meta.version;
console.log('version', version);
expect(version).toEqual(exVersion);

Expand Down
16 changes: 8 additions & 8 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async function run() {

const meta: Meta = new Meta(inputs, context, repo);

const version: Version = meta.version();
const version: Version = meta.version;
core.startGroup(`Docker image version`);
core.info(version.main || '');
core.endGroup();
Expand Down
16 changes: 9 additions & 7 deletions src/meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export interface Version {
}

export class Meta {
public readonly version: Version;

private readonly inputs: Inputs;
private readonly context: Context;
private readonly repo: ReposGetResponseData;
Expand All @@ -25,9 +27,10 @@ export class Meta {
this.context = context;
this.repo = repo;
this.date = new Date();
this.version = this.getVersion();
}

public version(): Version {
private getVersion(): Version {
const currentDate = this.date;
const version: Version = {
main: undefined,
Expand Down Expand Up @@ -89,19 +92,18 @@ export class Meta {
}

public tags(): Array<string> {
const version: Version = this.version();
if (!version.main) {
if (!this.version.main) {
return [];
}

let tags: Array<string> = [];
for (const image of this.inputs.images) {
const imageLc = image.toLowerCase();
tags.push(`${imageLc}:${version.main}`);
for (const partial of version.partial) {
tags.push(`${imageLc}:${this.version.main}`);
for (const partial of this.version.partial) {
tags.push(`${imageLc}:${partial}`);
}
if (version.latest) {
if (this.version.latest) {
tags.push(`${imageLc}:latest`);
}
if (this.context.sha && this.inputs.tagSha) {
Expand All @@ -117,7 +119,7 @@ export class Meta {
`org.opencontainers.image.description=${this.repo.description || ''}`,
`org.opencontainers.image.url=${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.revision=${this.context.sha || ''}`,
`org.opencontainers.image.licenses=${this.repo.license?.spdx_id || ''}`
Expand Down

0 comments on commit ef12c77

Please sign in to comment.