From 6fe5b3f6bb6a0e4f4de0a874cefe8ce707bb632c Mon Sep 17 00:00:00 2001
From: CrazyMax <crazy-max@users.noreply.github.com>
Date: Tue, 27 Oct 2020 02:32:26 +0100
Subject: [PATCH] Add tag-match-group input to choose group to get if tag-match
 matches Check tag-match is a valid regex

---
 .github/workflows/ci.yml | 11 ++++++++---
 CHANGELOG.md             |  5 +++++
 README.md                | 20 +++++++++++---------
 __tests__/meta.test.ts   | 26 ++++++++++++++++++++++++++
 action.yml               |  4 ++++
 dist/index.js            | 12 ++++++++++--
 src/context.ts           |  2 ++
 src/meta.ts              | 10 ++++++++--
 8 files changed, 74 insertions(+), 16 deletions(-)

diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 6a3d4b9..36bd58c 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -58,9 +58,13 @@ jobs:
     strategy:
       fail-fast: false
       matrix:
-        tag-match:
-          - \\d{1,3}.\\d{1,3}.\\d{1,3}
-          - \\d{1,3}.\\d{1,3}
+        include:
+          - tag-match: '\d{1,3}.\d{1,3}.\d{1,3}'
+            tag-match-group: '0'
+          - tag-match: '\d{1,3}.\d{1,3}'
+            tag-match-group: '0'
+          - tag-match: 'v(.*)'
+            tag-match-group: '1'
     steps:
       -
         name: Checkout
@@ -74,6 +78,7 @@ jobs:
             ghcr.io/name/app
           tag-sha: true
           tag-match: ${{ matrix.tag-match }}
+          tag-match-group: ${{ matrix.tag-match-group }}
 
   docker-push:
     runs-on: ubuntu-latest
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 9d44e9c..fa32ca1 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,10 @@
 # Changelog
 
+## 1.5.0 (2020/10/27)
+
+* Add `tag-match-group` input to choose group to get if `tag-match` matches
+* Check `tag-match` is a valid regex 
+
 ## 1.4.0 (2020/10/27)
 
 * Use RegExp to match against a Git tag instead of coerce
diff --git a/README.md b/README.md
index 81600e8..9661487 100644
--- a/README.md
+++ b/README.md
@@ -76,7 +76,8 @@ jobs:
         with:
           images: name/app
           tag-sha: true
-          tag-match: \\d{1,3}.\\d{1,3}.\\d{1,3}
+          tag-match: v(.*)
+          tag-match-group: 1
       -
         name: Set up QEMU
         uses: docker/setup-qemu-action@v1
@@ -114,7 +115,8 @@ Following inputs can be used as `step.with` keys
 | `tag-sha`           | Bool     | Add git short SHA as Docker tag (default `false`) |
 | `tag-edge`          | Bool     | Enable edge branch tagging (default `false`) |
 | `tag-edge-branch`   | String   | Branch that will be tagged as edge (default `repo.default_branch`) |
-| `tag-match`         | String   | RegExp to match against a Git tag and use match group as Docker tag |
+| `tag-match`         | String   | RegExp to match against a Git tag and use first match as Docker tag |
+| `tag-match-group`   | Number   | Group to get if `tag-match` matches (default `0`) |
 | `tag-match-latest`  | Bool     | Set `latest` Docker tag if `tag-match` matches (default `true`) |
 | `tag-schedule`      | String   | [Template](#schedule-tag) to apply to schedule tag (default `nightly`) |
 | `sep-tags`          | String   | Separator to use for tags output (default `\n`) |
@@ -136,13 +138,13 @@ Following outputs are available
 
 ### `tag-match` examples
 
-| Git tag                 | `tag-match`                    | Docker tag
-|-------------------------|--------------------------------|-------------------|
-| `v1.2.3`                | `\\d{1,3}.\\d{1,3}.\\d{1,3}`   | `1.2.3`           |
-| `v2.0.8-beta.67`        | `\\d{1,3}.\\d{1,3}.\\d{1,3}.*` | `2.0.8-beta.67`   |
-| `v2.0.8-beta.67`        | `\\d{1,3}.\\d{1,3}`            | `2.0`             |
-| `release1`              | `\\d{1,3}.\\d{1,3}`            | `release1`        |
-| `20200110-RC2`          | `\\d+`                         | `20200110`        |
+| Git tag                 | `tag-match`                        | `tag-match-group` | Docker tag
+|-------------------------|------------------------------------|-------------------|------------------|
+| `v1.2.3`                | `\\d{1,3}.\\d{1,3}.\\d{1,3}`       | `0`               | `1.2.3`          |
+| `v2.0.8-beta.67`        | `v(.*)`                            | `1`               | `2.0.8-beta.67`  |
+| `v2.0.8-beta.67`        | `v(\\d.\\d)`                       | `1`               | `2.0`            |
+| `release1`              | `\\d{1,3}.\\d{1,3}`                | `0`               | `release1`       |
+| `20200110-RC2`          | `\\d+`                             | `0`               | `20200110`       |
 
 ### Schedule tag
 
diff --git a/__tests__/meta.test.ts b/__tests__/meta.test.ts
index 620ede8..3406388 100644
--- a/__tests__/meta.test.ts
+++ b/__tests__/meta.test.ts
@@ -441,6 +441,32 @@ describe('push tag', () => {
         "org.opencontainers.image.licenses=MIT"
       ]
     ],
+    [
+      'event_tag_20200110-RC2.env',
+      {
+        images: ['user/app'],
+        tagMatch: `(.*)-RC`,
+        tagMatchGroup: 1,
+        tagMatchLatest: false,
+      } as Inputs,
+      {
+        version: '20200110',
+        latest: false
+      } as Version,
+      [
+        'user/app:20200110'
+      ],
+      [
+        "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.git",
+        "org.opencontainers.image.version=20200110",
+        "org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
+        "org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071",
+        "org.opencontainers.image.licenses=MIT"
+      ]
+    ],
     [
       'event_tag_v1.1.1.env',
       {
diff --git a/action.yml b/action.yml
index 2b683ef..dfc3ae8 100644
--- a/action.yml
+++ b/action.yml
@@ -24,6 +24,10 @@ inputs:
   tag-match:
     description: 'RegExp to match against a Git tag and use match group as Docker tag'
     required: false
+  tag-match-group:
+    description: 'Group to get if tag-match matches (default 0)'
+    default: '0'
+    required: false
   tag-match-latest:
     description: 'Set latest Docker tag if tag-match matches'
     default: 'true'
diff --git a/dist/index.js b/dist/index.js
index 5e389ab..116eabf 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -26,6 +26,7 @@ function getInputs() {
         tagEdge: /true/i.test(core.getInput('tag-edge') || 'false'),
         tagEdgeBranch: core.getInput('tag-edge-branch'),
         tagMatch: core.getInput('tag-match'),
+        tagMatchGroup: Number(core.getInput('tag-match-group')) || 0,
         tagMatchLatest: /true/i.test(core.getInput('tag-match-latest') || 'true'),
         tagSchedule: core.getInput('tag-schedule') || 'nightly',
         sepTags: core.getInput('sep-tags') || `\n`,
@@ -194,9 +195,16 @@ class Meta {
         else if (/^refs\/tags\//.test(this.context.ref)) {
             version.version = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
             if (this.inputs.tagMatch) {
-                const tagMatch = version.version.match(this.inputs.tagMatch);
+                let tagMatch;
+                const isRegEx = this.inputs.tagMatch.match(/^\/(.+)\/(.*)$/);
+                if (isRegEx) {
+                    tagMatch = version.version.match(new RegExp(isRegEx[1], isRegEx[2]));
+                }
+                else {
+                    tagMatch = version.version.match(this.inputs.tagMatch);
+                }
                 if (tagMatch) {
-                    version.version = tagMatch[0];
+                    version.version = tagMatch[this.inputs.tagMatchGroup];
                     version.latest = this.inputs.tagMatchLatest;
                 }
             }
diff --git a/src/context.ts b/src/context.ts
index cb301a6..a5a7eb6 100644
--- a/src/context.ts
+++ b/src/context.ts
@@ -6,6 +6,7 @@ export interface Inputs {
   tagEdge: boolean;
   tagEdgeBranch: string;
   tagMatch: string;
+  tagMatchGroup: number;
   tagMatchLatest: boolean;
   tagSchedule: string;
   sepTags: string;
@@ -20,6 +21,7 @@ export function getInputs(): Inputs {
     tagEdge: /true/i.test(core.getInput('tag-edge') || 'false'),
     tagEdgeBranch: core.getInput('tag-edge-branch'),
     tagMatch: core.getInput('tag-match'),
+    tagMatchGroup: Number(core.getInput('tag-match-group')) || 0,
     tagMatchLatest: /true/i.test(core.getInput('tag-match-latest') || 'true'),
     tagSchedule: core.getInput('tag-schedule') || 'nightly',
     sepTags: core.getInput('sep-tags') || `\n`,
diff --git a/src/meta.ts b/src/meta.ts
index d5f8386..8d85431 100644
--- a/src/meta.ts
+++ b/src/meta.ts
@@ -41,9 +41,15 @@ export class Meta {
     } else if (/^refs\/tags\//.test(this.context.ref)) {
       version.version = this.context.ref.replace(/^refs\/tags\//g, '').replace(/\//g, '-');
       if (this.inputs.tagMatch) {
-        const tagMatch = version.version.match(this.inputs.tagMatch);
+        let tagMatch;
+        const isRegEx = this.inputs.tagMatch.match(/^\/(.+)\/(.*)$/);
+        if (isRegEx) {
+          tagMatch = version.version.match(new RegExp(isRegEx[1], isRegEx[2]));
+        } else {
+          tagMatch = version.version.match(this.inputs.tagMatch);
+        }
         if (tagMatch) {
-          version.version = tagMatch[0];
+          version.version = tagMatch[this.inputs.tagMatchGroup];
           version.latest = this.inputs.tagMatchLatest;
         }
       }