From 86ee2c2d902cdc8a688c7043b6d6840707c803e5 Mon Sep 17 00:00:00 2001 From: Alexandru Mihai Date: Tue, 27 Apr 2021 15:22:26 +0300 Subject: [PATCH] first iteration --- .gitignore | 2 ++ README.md | 17 +++++++++++++++++ action.yml | 18 ++++++++++++++++++ index.js | 19 +++++++++++++++++++ package.json | 14 ++++++++++++++ 5 files changed, 70 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 action.yml create mode 100644 index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..25c8fdb --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules +package-lock.json \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..9dbb413 --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# Branch name normalizer + +Simple action that normalizez the current branch name into a string with only lowercase alphanumerical values, separated by hiphens. +Example: For the branch name: Alex/A-Simple-pr. Output: `alex-a-simple-pr` + +Inputs: + +- **branch_name**: + - default: `$GITHUB_HEAD_REF` + - required: `false` +- **max_length**: + - default: `40` + - required: `false` + +Output: + +- **normalized** \ No newline at end of file diff --git a/action.yml b/action.yml new file mode 100644 index 0000000..11bdf8d --- /dev/null +++ b/action.yml @@ -0,0 +1,18 @@ +name: "Normalize branch name" +description: "Normalizes the branch into an " +inputs: + branch: + description: "Branch name to normalize. Defaults to $GITHUB_HEAD_REF" + required: false + max_length: + description: "Max length of the branch name. If exceeds, string will be striped down to first max_length chars. Defaults to 40 chars" + required: false +outputs: + normalized: + description: "Normalized branch name" +runs: + using: "node12" + main: "index.js" +branding: + icon: 'terminal' + color: 'green' \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..64d9076 --- /dev/null +++ b/index.js @@ -0,0 +1,19 @@ +const core = require("@actions/core"); +const defaultMaxLength = 40 +try { + const input = core.getInput("branch") ? core.getInput("branch") : process.env.GITHUB_HEAD_REF; + const maxBranchLength = core.getInput("max_length") ? core.getInput("max_length") : defaultMaxLength; + let output = input + .trim() + .toLowerCase() + .replace(/([^0-9a-zA-Z-]+)/g, "-"); + if (output.length > maxBranchLength) { + output = output.substring(0, maxBranchLength); + } + core.setOutput("normalized", output); + if (output.charAt(output.length - 1) == '-') { + output = output.substring(0, output.length - 1); + } +} catch (err) { + core.setFailed(err); +} \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..f9c3b55 --- /dev/null +++ b/package.json @@ -0,0 +1,14 @@ +{ + "name": "branch-name-normalizer", + "version": "1.0.0", + "description": "Simple action that normalizez the current branch name into a string with only lowercase alphanumerical values, separated by hiphens. Example: For the branch name: Alex/A-Simple-pr. Output: `alex-a-simple-pr`", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "@actions/core": "^1.2.7" + } +}