From 7040b59aa54df569f10752b96b9f1a2ba81f44e7 Mon Sep 17 00:00:00 2001
From: CrazyMax <1951866+crazy-max@users.noreply.github.com>
Date: Tue, 8 Dec 2020 00:01:39 +0100
Subject: [PATCH] Replace forbidden chars derived from branch name (#29)

Co-authored-by: CrazyMax <crazy-max@users.noreply.github.com>
---
 CHANGELOG.md                                  |  5 ++++
 .../fixtures/event_push_invalidchars.env      | 23 +++++++++++++++
 __tests__/meta.test.ts                        | 29 +++++++++++++++++++
 dist/index.js                                 |  2 +-
 src/meta.ts                                   |  2 +-
 5 files changed, 59 insertions(+), 2 deletions(-)
 create mode 100644 __tests__/fixtures/event_push_invalidchars.env

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 0f1c2ab..c9e65d8 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,10 @@
 # Changelog
 
+## 1.9.1 (2020/12/07)
+
+* Replace forbidden chars derived from branch name (#30)
+* Bump semver from 7.3.2 to 7.3.4 (#26)
+
 ## 1.9.0 (2020/12/04)
 
 * Allow to add custom tags (#24)
diff --git a/__tests__/fixtures/event_push_invalidchars.env b/__tests__/fixtures/event_push_invalidchars.env
new file mode 100644
index 0000000..a9cfb22
--- /dev/null
+++ b/__tests__/fixtures/event_push_invalidchars.env
@@ -0,0 +1,23 @@
+GITHUB_ACTION=crazy-maxghaction-dump-context
+GITHUB_ACTIONS=true
+GITHUB_ACTION_PATH=/home/runner/work/_actions/crazy-max/ghaction-dump-context/v1
+GITHUB_ACTOR=crazy-max
+GITHUB_API_URL=https://api.github.com
+GITHUB_BASE_REF=
+GITHUB_ENV=/home/runner/work/_temp/_runner_file_commands/set_env_89a016e8-e5b7-4039-a67e-c8da08f87a0c
+GITHUB_EVENT_NAME=push
+#GITHUB_EVENT_PATH=/home/runner/work/_temp/_github_workflow/event.json
+GITHUB_GRAPHQL_URL=https://api.github.com/graphql
+GITHUB_HEAD_REF=
+GITHUB_JOB=event
+GITHUB_PATH=/home/runner/work/_temp/_runner_file_commands/add_path_89a016e8-e5b7-4039-a67e-c8da08f87a0c
+GITHUB_REF="refs/heads/my/feature#1245"
+GITHUB_REPOSITORY=crazy-max/test-docker-action
+GITHUB_REPOSITORY_OWNER=crazy-max
+GITHUB_RETENTION_DAYS=90
+GITHUB_RUN_ID=325957516
+GITHUB_RUN_NUMBER=1
+GITHUB_SERVER_URL=https://github.com
+GITHUB_SHA=90dd6032fac8bda1b6c4436a2e65de27961ed071
+GITHUB_WORKFLOW=event
+GITHUB_WORKSPACE=/home/runner/work/test-docker-action/test-docker-action
diff --git a/__tests__/meta.test.ts b/__tests__/meta.test.ts
index 303ffbe..c1aa051 100644
--- a/__tests__/meta.test.ts
+++ b/__tests__/meta.test.ts
@@ -376,6 +376,35 @@ describe('push', () => {
         "org.opencontainers.image.licenses=MIT"
       ]
     ],
+    [
+      'event_push_invalidchars.env',
+      {
+        images: ['org/app', 'ghcr.io/user/app'],
+        tagSha: true,
+        tagEdge: true,
+      } as Inputs,
+      {
+        main: 'my-feature-1245',
+        partial: [],
+        latest: false
+      } as Version,
+      [
+        'org/app:my-feature-1245',
+        'org/app:sha-90dd603',
+        'ghcr.io/user/app:my-feature-1245',
+        'ghcr.io/user/app:sha-90dd603'
+      ],
+      [
+        "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=my-feature-1245",
+        "org.opencontainers.image.created=2020-01-10T00:30:00.000Z",
+        "org.opencontainers.image.revision=90dd6032fac8bda1b6c4436a2e65de27961ed071",
+        "org.opencontainers.image.licenses=MIT"
+      ]
+    ],
   ])('given %p event ', tagsLabelsTest);
 });
 
diff --git a/dist/index.js b/dist/index.js
index 3830a7b..67851da 100644
--- a/dist/index.js
+++ b/dist/index.js
@@ -242,7 +242,7 @@ class Meta {
             }
         }
         else if (/^refs\/heads\//.test(this.context.ref)) {
-            version.main = this.context.ref.replace(/^refs\/heads\//g, '').replace(/\//g, '-');
+            version.main = this.context.ref.replace(/^refs\/heads\//g, '').replace(/[^a-zA-Z0-9._-]+/g, '-');
             if (this.inputs.tagEdge && this.inputs.tagEdgeBranch === version.main) {
                 version.main = 'edge';
             }
diff --git a/src/meta.ts b/src/meta.ts
index 8414415..2377d16 100644
--- a/src/meta.ts
+++ b/src/meta.ts
@@ -83,7 +83,7 @@ export class Meta {
         version.latest = this.inputs.tagLatest;
       }
     } else if (/^refs\/heads\//.test(this.context.ref)) {
-      version.main = this.context.ref.replace(/^refs\/heads\//g, '').replace(/\//g, '-');
+      version.main = this.context.ref.replace(/^refs\/heads\//g, '').replace(/[^a-zA-Z0-9._-]+/g, '-');
       if (this.inputs.tagEdge && this.inputs.tagEdgeBranch === version.main) {
         version.main = 'edge';
       }