From 604a0840b113a819f8cdcd971ceeaba94fff3fb6 Mon Sep 17 00:00:00 2001 From: Eduardo Aguilar Date: Fri, 17 May 2024 10:17:00 -0600 Subject: [PATCH] fix: input default values --- .eslintrc.json => .eslintrc | 0 .github/workflows/release.yml | 44 +++++++++++++++-------- .prettierrc.json => .prettierrc | 0 CHANGELOG.md | 41 --------------------- Dockerfile | 4 +-- README.md | 2 +- action.yml | 3 -- index.js | 32 ++++++----------- package-lock.json | 4 +-- src/action/Action.js | 2 ++ src/utils.js | 64 +++++++++++++++++++++++++++++++++ 11 files changed, 111 insertions(+), 85 deletions(-) rename .eslintrc.json => .eslintrc (100%) rename .prettierrc.json => .prettierrc (100%) delete mode 100644 CHANGELOG.md create mode 100644 src/utils.js diff --git a/.eslintrc.json b/.eslintrc similarity index 100% rename from .eslintrc.json rename to .eslintrc diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 15cfa9c..6182248 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -22,25 +22,25 @@ jobs: version: ${{ steps.cz.outputs.version }} steps: - name: Check out - uses: actions/checkout@v4 + uses: actions/checkout@v4.1.5 with: fetch-depth: 0 token: "${{ secrets.ACCESS_TOKEN }}" ref: "main" - - name: Set up Python - uses: actions/setup-python@v5.1.0 - with: - python-version: 3.11 - - name: Config Git User run: | git config --local user.email "$GIT_USER_EMAIL" git config --local user.name "$GIT_USER_NAME" git config --local pull.ff only - - id: cz - name: Create bump and changelog + - name: Set up Python + uses: actions/setup-python@v5.1.0 + with: + python-version: 3.11 + + - name: Create bump and changelog + id: cz run: | python -m pip install -U commitizen cz bump --yes @@ -48,7 +48,7 @@ jobs: echo "version=\"v$REV\"" >> $GITHUB_OUTPUT - name: Push changes - uses: ad-m/github-push-action@v0.6.0 + uses: ad-m/github-push-action@v0.8.0 with: github_token: ${{ secrets.ACCESS_TOKEN }} repository: "Drafteame/sync-secrets-manager" @@ -61,13 +61,29 @@ jobs: build: runs-on: ubuntu-latest + needs: + - bump_version steps: - - name: 🛎 Checkout + - name: Checkout uses: actions/checkout@v4 - - name: Set up buildx + + - name: Setup buildx uses: docker/setup-buildx-action@v3 - - name: Login to ghcr.io - run: echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u ${{ github.actor }} --password-stdin + + - name: Log in to GitHub container registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ github.token }} + logout: false + + - name: Set repo name + run: | + repo=$(echo "ghcr.io/${{ github.repository }}" | tr '[:upper:]' '[:lower:]') + echo "REPO=$repo" >> $GITHUB_ENV + - name: Build and push run: | - docker buildx build --platform linux/amd64,linux/arm64 -t $(echo "ghcr.io/${{ github.repository }}:latest" | tr '[:upper:]' '[:lower:]') --push . + docker buildx build --platform linux/amd64,linux/arm64 -t ${{ env.REPO }}:latest --push . + docker buildx build --platform linux/amd64,linux/arm64 -t ${{ env.REPO }}:${{ needs.bump_version.outputs.version }} --push . diff --git a/.prettierrc.json b/.prettierrc similarity index 100% rename from .prettierrc.json rename to .prettierrc diff --git a/CHANGELOG.md b/CHANGELOG.md deleted file mode 100644 index 1122f32..0000000 --- a/CHANGELOG.md +++ /dev/null @@ -1,41 +0,0 @@ -## v0.4.0 (2024-04-17) - - -- feat: add build and publish in registry in relaese job (#7) -- * feat: add build and publish in registry in relaese job - -* feat: add default inputs values for booleans - -* feat: remove workflow_dispatch added for testing - -## v0.3.0 (2023-11-02) - - -- feat: add secret creation flag and flow (#6) - -## v0.2.0 (2023-11-01) - - -- feat: makeing prive all action properties (#5) -- Chore/add exclude flag (#4) -- * chore: add exclude option to skip sync keys - -* chore: save - -* chore: save - -* chore: add colors to log messages - -* chore: add configuration to show or hide secret values - -## v0.1.1 (2023-10-06) - - -- chore: add branding (#3) - -## v0.1.0 (2023-10-06) - - -- chore: add testings for new messages (#2) -- feat: add first version of the sync action (#1) -- Initial commit diff --git a/Dockerfile b/Dockerfile index 8723704..c004ccf 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM node:20 +FROM node:20-alpine LABEL "com.github.actions.icon"="blue" LABEL "com.github.actions.color"="database" @@ -9,6 +9,6 @@ LABEL "org.opencontainers.image.source"="https://github.com/Drafteame/sync-secre COPY . /app WORKDIR /app -RUN npm install +RUN npm install --omit=dev ENTRYPOINT ["node", "/app/index.js"] diff --git a/README.md b/README.md index ae8d884..8782218 100644 --- a/README.md +++ b/README.md @@ -33,5 +33,5 @@ jobs: create_secret: false # If true it will check if the secret exists or not to create it before execute sync (default false) dry_run: true # Default false show_values: false # If true secret values will be displayed on action logs (default false) - exclude: '^_' # Regular expression that excludes the matching keys to be synced (default '^_') + exclude: "^_" # Regular expression that excludes the matching keys to be synced (default '^_') ``` diff --git a/action.yml b/action.yml index 483e2c3..a3ebbb4 100644 --- a/action.yml +++ b/action.yml @@ -24,15 +24,12 @@ inputs: create_secret: description: "Flag to create the given secret if not exist before execute sync" required: false - default: "false" dry_run: description: "Dry run mode (preview changes without modifying the secret)" required: false - default: "false" show_values: description: "Dry run mode (preview changes without modifying the secret)" required: false - default: "false" exclude: description: "List of regular expressions that determines if a secret key should be excluded from sync" required: false diff --git a/index.js b/index.js index ad39459..d0f51ce 100644 --- a/index.js +++ b/index.js @@ -1,26 +1,22 @@ -import core from "@actions/core"; +import { getInput, getBooleanInput } from "./src/utils.js"; import Action from "./src/action/Action.js"; const getAction = () => { return new Action( - core.getInput("aws_access_key_id"), - core.getInput("aws_secret_access_key"), - core.getInput("aws_region"), - core.getInput("secret_name"), - core.getInput("json_file_path"), - core.getInput("exclude"), - core.getBooleanInput("show_values"), - core.getBooleanInput("create_secret"), + getInput("aws_access_key_id"), + getInput("aws_secret_access_key"), + getInput("aws_region"), + getInput("secret_name"), + getInput("json_file_path"), + getInput("exclude"), + getBooleanInput("show_values", false), + getBooleanInput("create_secret", false), ); }; const run = async () => { try { - setDefault("dry_run", "false"); - setDefault("show_values", "false"); - setDefault("create_secret", "false"); - - const dryRun = core.getBooleanInput("dry_run"); + const dryRun = getBooleanInput("dry_run", false); const changeSet = await getAction().run(); @@ -37,12 +33,4 @@ const run = async () => { } }; -const setDefault = (name, value) => { - const envVarName = `INPUT_${name.replace(/ /g, '_').toUpperCase()}`; - const val = process.env[envVarName] || ''; - if (val === '') { - process.env[envVarName] = value; - } -} - run(); diff --git a/package-lock.json b/package-lock.json index e421f2a..1714861 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "sync-secrets-manager", - "version": "0.1.1", + "version": "0.4.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "sync-secrets-manager", - "version": "0.1.1", + "version": "0.4.0", "license": "MIT", "dependencies": { "@actions/core": "^1.10.1", diff --git a/src/action/Action.js b/src/action/Action.js index 23c5052..88cd3ab 100644 --- a/src/action/Action.js +++ b/src/action/Action.js @@ -1,4 +1,5 @@ import fs from "fs"; +import core from "@actions/core"; import SecretsManager from "../secrets-manager/SecretsManager.js"; import ChangeSet from "./ChangeSet.js"; @@ -119,6 +120,7 @@ export default class Action { */ async #createSecret() { if (!this.#createSecretFlag) { + core.info("secret creation skip..."); return; } diff --git a/src/utils.js b/src/utils.js new file mode 100644 index 0000000..29ac31c --- /dev/null +++ b/src/utils.js @@ -0,0 +1,64 @@ +import core from "@actions/core"; + +/** + * Checks if a given value is empty. + * + * @param {*} value - The value to check for emptiness. + * @returns {boolean} - Returns true if the value is empty, false otherwise. + */ +export function isEmpty(value) { + if (value == null || value == undefined) { + // Handles null and undefined + return true; + } + + if (typeof value === "boolean") { + // Boolean values are never empty + return false; + } + + if (typeof value === "number") { + // Number values are never empty + return false; + } + + if (typeof value === "string") { + // Check if the string is empty + return value.trim().length === 0; + } + + // For any other types, assume it's not empty + return false; +} + +/** + * Get action input with default value + * @param {string} input input name to retrieve + * @param {string} value string default value + * @returns {string} The value of input, if is empty it return the default one + */ +export function getInput(input, value = "") { + const inputValue = core.getInput(input); + + if (isEmpty(inputValue)) { + return value; + } + + return inputValue; +} + +/** + * Get action boolean input + * @param {string} input input name to retrieve + * @param {boolean} value boolean default value + * @returns {boolean} The value of the input, if it is empty ir return the default one + */ +export function getBooleanInput(input, value = false) { + const inputValue = core.getInput(input); + + if (isEmpty(inputValue)) { + return value; + } + + return inputValue.trim().toLowerCase() === "true"; +}