From 59d7dda32e5b019411d8c1f6c43987caebc3886a Mon Sep 17 00:00:00 2001 From: Volodymyr Shelmuk Date: Tue, 17 Sep 2024 17:46:41 +0300 Subject: [PATCH 1/7] feat: add static-analysis-js.yml --- .github/workflows/static-analysis-js.yml | 104 +++++++++++++++++++++++ docs/js.md | 53 ++++++++++++ 2 files changed, 157 insertions(+) create mode 100644 .github/workflows/static-analysis-js.yml diff --git a/.github/workflows/static-analysis-js.yml b/.github/workflows/static-analysis-js.yml new file mode 100644 index 00000000..c2b4166b --- /dev/null +++ b/.github/workflows/static-analysis-js.yml @@ -0,0 +1,104 @@ +name: Static code analysis Javascript + +on: + workflow_call: + inputs: + NPM_REGISTRY_DOMAIN: + description: Domain of the private npm registry. + default: https://npm.pkg.github.com/ + required: false + type: string + NODE_VERSION: + description: Node version with which the static analysis is to be executed. + default: 18 + required: false + type: string + NODE_OPTIONS: + description: Space-separated list of command-line Node options. + type: string + default: '' + required: false + secrets: + NPM_REGISTRY_TOKEN: + description: Authentication for the private npm registry. + required: false + GITHUB_USER_EMAIL: + description: Email address for the GitHub user configuration. + required: false + GITHUB_USER_NAME: + description: Username for the GitHub user configuration. + required: false + GITHUB_USER_SSH_KEY: + description: Private SSH key associated with the GitHub user passed as `GITHUB_USER_NAME`. + required: false + ENV_VARS: + description: Additional environment variables as a JSON formatted object. + required: false + +jobs: + static-analysis-js: + timeout-minutes: 5 + runs-on: ubuntu-latest + env: + NODE_OPTIONS: ${{ inputs.NODE_OPTIONS }} + NODE_AUTH_TOKEN: ${{ secrets.NPM_REGISTRY_TOKEN }} + NODE_CACHE_MODE: '' + GITHUB_USER_EMAIL: ${{ secrets.GITHUB_USER_EMAIL }} + GITHUB_USER_NAME: ${{ secrets.GITHUB_USER_NAME }} + GITHUB_USER_SSH_KEY: ${{ secrets.GITHUB_USER_SSH_KEY }} + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Set up custom environment variables + env: + ENV_VARS: ${{ secrets.ENV_VARS }} + if: ${{ env.ENV_VARS }} + uses: actions/github-script@v7 + with: + script: | + JSON + .parse(process.env.ENV_VARS) + .forEach(envVar => core.exportVariable(envVar.name, envVar.value)); + + - name: Set up SSH + if: ${{ env.GITHUB_USER_SSH_KEY != '' }} + uses: webfactory/ssh-agent@v0.9.0 + with: + ssh-private-key: ${{ env.GITHUB_USER_SSH_KEY }} + + - name: Set up Git + run: | + git config --global user.email "${{ env.GITHUB_USER_EMAIL }}" + git config --global user.name "${{ env.GITHUB_USER_NAME }}" + + - name: Set up node cache mode + run: | + if [ { [ -f "${GITHUB_WORKSPACE}/package-lock.json" ] || [ -f "${GITHUB_WORKSPACE}/npm-shrinkwrap.json" ]; }; then + echo "NODE_CACHE_MODE=npm" >> $GITHUB_ENV + else + echo "No lock files found" + fi + + - name: Set up node + uses: actions/setup-node@v4 + with: + node-version: ${{ inputs.NODE_VERSION }} + registry-url: ${{ inputs.NPM_REGISTRY_DOMAIN }} + cache: ${{ env.NODE_CACHE_MODE }} + + - name: Install dependencies + env: + ARGS: ${{ env.NODE_CACHE_MODE == 'npm' && 'ci' || 'install' }} + run: ${{ format('npm {0}', env.ARGS) }} + + - name: Run Jest + run: | + ./node_modules/.bin/tsc --noEmit --skipLibCheck --pretty false | + while read -r line + do + if [[ $line =~ ([0-9a-z\/\.]+)[\(]([0-9]+)[\,]([0-9]+)[\)].*(TS.*) ]]; then + echo "::error file=${BASH_REMATCH[1]},line=${BASH_REMATCH[2]},col=${BASH_REMATCH[3]}::${BASH_REMATCH[4]}" + fi + done + exit "${PIPESTATUS[0]}" diff --git a/docs/js.md b/docs/js.md index 67cadb43..a552193b 100644 --- a/docs/js.md +++ b/docs/js.md @@ -56,3 +56,56 @@ jobs: NODE_VERSION: 14 JEST_ARGS: 'my-test --reporters=jest-junit --coverage' ``` + +## Static analysis Javascript + +This workflow runs [Typescript compiler](https://www.typescriptlang.org/docs/handbook/compiler-options.html) +with `--noEmit` argument. It does so by executing the `tsc` binary in the `./node_modules/.bin/` folder. + +**Simplest possible example:** + +```yml +name: Static code analysis Javascript +on: + push: +jobs: + static-analysis-js: + uses: inpsyde/reusable-workflows/.github/workflows/static-analysis-js.yml@main +``` + +### Configuration parameters + +#### Inputs + +| Name | Default | Description | +|-------------------------|---------------------------------|-----------------------------------------------------------------------------------| +| `NODE_OPTIONS` | `''` | Space-separated list of command-line Node options | +| `NODE_VERSION` | `18` | Node version with which the assets will be compiled | +| `NPM_REGISTRY_DOMAIN` | `'https://npm.pkg.github.com/'` | Domain of the private npm registry | + +#### Secrets + +| Name | Description | +|-----------------------|------------------------------------------------------------------------------| +| `NPM_REGISTRY_TOKEN` | Authentication for the private npm registry | +| `GITHUB_USER_EMAIL` | Email address for the GitHub user configuration | +| `GITHUB_USER_NAME` | Username for the GitHub user configuration | +| `GITHUB_USER_SSH_KEY` | Private SSH key associated with the GitHub user passed as `GITHUB_USER_NAME` | +| `ENV_VARS` | Additional environment variables as a JSON formatted object | + +**Example with configuration parameters:** + +```yml +name: Static code analysis Javascript +on: + pull_request: +jobs: + static-analysis-js: + uses: inpsyde/reusable-workflows/.github/workflows/static-analysis-js.yml@main + secrets: + NPM_REGISTRY_TOKEN: ${{ secrets.NPM_REGISTRY_TOKEN }} + ENV_VARS: >- + [{"name":"EXAMPLE_USERNAME", "value":"${{ secrets.USERNAME }}"}] + with: + NODE_VERSION: 18 +``` From 874ef214b61da30803a413eed2a64349fd4d0523 Mon Sep 17 00:00:00 2001 From: Volodymyr Shelmuk Date: Tue, 17 Sep 2024 17:53:26 +0300 Subject: [PATCH 2/7] fix: cache mode detection --- .github/workflows/static-analysis-js.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/static-analysis-js.yml b/.github/workflows/static-analysis-js.yml index c2b4166b..3d8b6543 100644 --- a/.github/workflows/static-analysis-js.yml +++ b/.github/workflows/static-analysis-js.yml @@ -74,7 +74,7 @@ jobs: - name: Set up node cache mode run: | - if [ { [ -f "${GITHUB_WORKSPACE}/package-lock.json" ] || [ -f "${GITHUB_WORKSPACE}/npm-shrinkwrap.json" ]; }; then + if [ -f "${GITHUB_WORKSPACE}/package-lock.json" ] || [ -f "${GITHUB_WORKSPACE}/npm-shrinkwrap.json" ]; then echo "NODE_CACHE_MODE=npm" >> $GITHUB_ENV else echo "No lock files found" From 72a1c185ad96f690f8d88bafce4120f560a86ae4 Mon Sep 17 00:00:00 2001 From: Volodymyr Shelmuk Date: Tue, 17 Sep 2024 17:57:14 +0300 Subject: [PATCH 3/7] fix: step name --- .github/workflows/static-analysis-js.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/static-analysis-js.yml b/.github/workflows/static-analysis-js.yml index 3d8b6543..f60cca0e 100644 --- a/.github/workflows/static-analysis-js.yml +++ b/.github/workflows/static-analysis-js.yml @@ -92,7 +92,7 @@ jobs: ARGS: ${{ env.NODE_CACHE_MODE == 'npm' && 'ci' || 'install' }} run: ${{ format('npm {0}', env.ARGS) }} - - name: Run Jest + - name: Run tsc run: | ./node_modules/.bin/tsc --noEmit --skipLibCheck --pretty false | while read -r line From 8efd4f109d928a4883a62df6943e754bbdec37a6 Mon Sep 17 00:00:00 2001 From: Volodymyr Shelmuk Date: Tue, 17 Sep 2024 18:14:31 +0300 Subject: [PATCH 4/7] style: suppress MD error --- docs/js.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/js.md b/docs/js.md index a552193b..bcc1be3c 100644 --- a/docs/js.md +++ b/docs/js.md @@ -1,3 +1,5 @@ + + # Reusable workflows – JavaScript ## Unit tests JavaScript From 519ab18d4956755e90c84fd6512552e74ea50ffc Mon Sep 17 00:00:00 2001 From: Volodymyr Shelmuk Date: Wed, 18 Sep 2024 12:49:10 +0300 Subject: [PATCH 5/7] style: format markdown --- docs/js.md | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/docs/js.md b/docs/js.md index bcc1be3c..efeae948 100644 --- a/docs/js.md +++ b/docs/js.md @@ -61,13 +61,15 @@ jobs: ## Static analysis Javascript -This workflow runs [Typescript compiler](https://www.typescriptlang.org/docs/handbook/compiler-options.html) -with `--noEmit` argument. It does so by executing the `tsc` binary in the `./node_modules/.bin/` folder. +This workflow runs +the [TypeScript compiler](https://www.typescriptlang.org/docs/handbook/compiler-options.html) with +the `--noEmit` argument. It does so by executing the `tsc` binary in the `./node_modules/.bin/` +folder. **Simplest possible example:** ```yml -name: Static code analysis Javascript +name: Static code analysis JavaScript on: push: jobs: @@ -79,11 +81,11 @@ jobs: #### Inputs -| Name | Default | Description | -|-------------------------|---------------------------------|-----------------------------------------------------------------------------------| -| `NODE_OPTIONS` | `''` | Space-separated list of command-line Node options | -| `NODE_VERSION` | `18` | Node version with which the assets will be compiled | -| `NPM_REGISTRY_DOMAIN` | `'https://npm.pkg.github.com/'` | Domain of the private npm registry | +| Name | Default | Description | +|-----------------------|---------------------------------|-----------------------------------------------------| +| `NODE_OPTIONS` | `''` | Space-separated list of command-line Node options | +| `NODE_VERSION` | `18` | Node version with which the assets will be compiled | +| `NPM_REGISTRY_DOMAIN` | `'https://npm.pkg.github.com/'` | Domain of the private npm registry | #### Secrets @@ -98,7 +100,7 @@ jobs: **Example with configuration parameters:** ```yml -name: Static code analysis Javascript +name: Static code analysis JavaScript on: pull_request: jobs: From 96e20862ee223ccbb2167157e108b79257c823fd Mon Sep 17 00:00:00 2001 From: Volodymyr Shelmuk Date: Wed, 18 Sep 2024 12:51:26 +0300 Subject: [PATCH 6/7] style: format markdown --- .github/workflows/static-analysis-js.yml | 2 +- docs/js.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/static-analysis-js.yml b/.github/workflows/static-analysis-js.yml index f60cca0e..948003e9 100644 --- a/.github/workflows/static-analysis-js.yml +++ b/.github/workflows/static-analysis-js.yml @@ -1,4 +1,4 @@ -name: Static code analysis Javascript +name: Static code analysis JavaScript on: workflow_call: diff --git a/docs/js.md b/docs/js.md index efeae948..e663b2cd 100644 --- a/docs/js.md +++ b/docs/js.md @@ -59,7 +59,7 @@ jobs: JEST_ARGS: 'my-test --reporters=jest-junit --coverage' ``` -## Static analysis Javascript +## Static analysis JavaScript This workflow runs the [TypeScript compiler](https://www.typescriptlang.org/docs/handbook/compiler-options.html) with From 960ec5fc89b2fdc01abf6c34989e0b3fcab6370c Mon Sep 17 00:00:00 2001 From: Volodymyr Shelmuk Date: Wed, 18 Sep 2024 13:19:52 +0300 Subject: [PATCH 7/7] feat: use `actions/setup-node` Typescript problem matcher --- .github/workflows/static-analysis-js.yml | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/.github/workflows/static-analysis-js.yml b/.github/workflows/static-analysis-js.yml index 948003e9..bf0dc626 100644 --- a/.github/workflows/static-analysis-js.yml +++ b/.github/workflows/static-analysis-js.yml @@ -93,12 +93,4 @@ jobs: run: ${{ format('npm {0}', env.ARGS) }} - name: Run tsc - run: | - ./node_modules/.bin/tsc --noEmit --skipLibCheck --pretty false | - while read -r line - do - if [[ $line =~ ([0-9a-z\/\.]+)[\(]([0-9]+)[\,]([0-9]+)[\)].*(TS.*) ]]; then - echo "::error file=${BASH_REMATCH[1]},line=${BASH_REMATCH[2]},col=${BASH_REMATCH[3]}::${BASH_REMATCH[4]}" - fi - done - exit "${PIPESTATUS[0]}" + run: ./node_modules/.bin/tsc --noEmit --skipLibCheck --pretty false