Repository: crazy-max/ghaction-docker-meta
Pull request #21 (2020-12-01 04:33:41)

Avoid unnecessary calls to version

codecov[bot]: # [Codecov](https://codecov.io/gh/crazy-max/ghaction-docker-meta/pull/21?src=pr&el=h1) Report
> Merging [#21](https://codecov.io/gh/crazy-max/ghaction-docker-meta/pull/21?src=pr&el=desc) (548cd71) into [master](https://codecov.io/gh/crazy-max/ghaction-docker-meta/commit/d48c7d2917ed833a332355e756f58053ba6782d5?el=desc) (d48c7d2) will **not change** coverage.
> The diff coverage is `60.00%`.

[![Impacted file tree graph](https://codecov.io/gh/crazy-max/ghaction-docker-meta/pull/21/graphs/tree.svg?width=650&height=150&src=pr&token=NgkCTCjmB1)](https://codecov.io/gh/crazy-max/ghaction-docker-meta/pull/21?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##           master      #21   +/-   ##
=======================================
  Coverage   73.17%   73.17%           
=======================================
  Files           3        3           
  Lines          82       82           
  Branches       17       17           
=======================================
  Hits           60       60           
  Misses          6        6           
  Partials       16       16           
```


| [Impacted Files](https://codecov.io/gh/crazy-max/ghaction-docker-meta/pull/21?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [src/meta.ts](https://codecov.io/gh/crazy-max/ghaction-docker-meta/pull/21/diff?src=pr&el=tree#diff-c3JjL21ldGEudHM=) | `75.00% <60.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/crazy-max/ghaction-docker-meta/pull/21?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/crazy-max/ghaction-docker-meta/pull/21?src=pr&el=footer). Last update [c53f885...548cd71](https://codecov.io/gh/crazy-max/ghaction-docker-meta/pull/21?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/dist/index.js b/dist/index.js
index 8dfd39f..686da3e 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -131,7 +131,7 @@ function run() {
             core.info(`runId: ${context.runId}`);
             core.endGroup();
             const meta = new meta_1.Meta(inputs, context, repo);
-            const version = meta.version();
+            const version = meta.version;
             core.startGroup(`Docker image version`);
             core.info(version.main || '');
             core.endGroup();
@@ -180,8 +180,9 @@ class Meta {
         this.context = context;
         this.repo = repo;
         this.date = new Date();
+        this.version = this.getVersion();
     }
-    version() {
+    getVersion() {
         const currentDate = this.date;
         const version = {
             main: undefined,
@@ -247,18 +248,17 @@ class Meta {
         return version;
     }
     tags() {
-        const version = this.version();
-        if (!version.main) {
+        if (!this.version.main) {
             return [];
         }
         let tags = [];
         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) {
@@ -274,7 +274,7 @@ 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=${((_a = this.repo.license) === null || _a === void 0 ? void 0 : _a.spdx_id) || ''}`
diff --git a/src/main.ts b/src/main.ts
index 16819d6..c476072 100644
--- a/src/main.ts
+++ b/src/main.ts
@@ -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();
diff --git a/src/meta.ts b/src/meta.ts
index 9683439..9f838fd 100644
--- a/src/meta.ts
+++ b/src/meta.ts
@@ -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;
@@ -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,
@@ -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) {
@@ -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 || ''}`


Test patch:
diff --git a/__tests__/meta.test.ts b/__tests__/meta.test.ts
index 3d58406..d465011 100644
--- a/__tests__/meta.test.ts
+++ b/__tests__/meta.test.ts
@@ -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);