Skip to content

Commit

Permalink
Migrate action to Typescript (#36)
Browse files Browse the repository at this point in the history
* Make function for executing script

* Migrate auth validation

* Migrate load secret functionality

  We make use of the following in the migration:
  - `op-js` package (make direct calls to the CLI and nicely get the output of the commands)
  - `core.exportVariable` to nicely export a secret as an environment variable
  - `core.setOutput` to nicely export a secret a the step’s output.
  - `core.setSecret` to mask the value of the secret if logged on the action’s output.

  Note: `core.exportVariable` and `core.setOutput` work with multiline secrets without any additional work on our side.

  Also, we export the temporary path where the CLI is installed to make sure the `op-js` package can find it.

* Fix CLI installation process

* Fix conditional of appending protocol

  Fix conditional of appending `http://` to the Connect host.

* Update CLI version and improve script

* Use core.addPath

  This is a safer and nicer way to ensure the path to the CLI is included later in the pipeline (including this GitHub action).

* Use version from package.json

  This eliminates the duplication of version in the code

* Upgrade to Typescript 5

* Prettify test.yml

* Move constants to constants.ts

  This shows better what constants we use and they will be later used in both code and tests.

* Move 'validateAuth' to 'utils.ts'

* Add validate auth tests

* Extract functionality for extracting a secret

  This will enable us to easily test the functionality of the action regarding the extraction of secret and how it provides it to the rest of the pipeline based on user's input

* Add tests for extracting secret

* Move 'unsetPrevious' to 'utils.ts'

* Add unit test pipeline

* Add tests for 'unsetPrevious'

* Improve disabling eslint rules

  Disable the ES Lint rules only for the next line and add a comment explaining why it’s disabled.

* Improve code based on PR review feedback

  This contains code improvements that were easy to address based on PR review feedback.

* Improve CLI installation functionality

  Two key elements are improved:
  - The action will now automatically fetch the latest stable version of the CLI. There’s no longer the need to hardcode the version and manually update it.
  - The action will now perform a check if the CLI exists in the pipeline and install it if it’s not available.

* Simplify extractSecret functionality

  Eliminate the nested conditionals to have a cleaner and more readable code.

* Fix CLI version

  The curl would return the version number, but we forgot to append the `v` in the version (i.e. from 2.18.0 to v2.18.0). Now it should be fixed.

* Move loadSecrets function to utils.ts

  This is done to keep things modular and narrow down the scope and complexity of index.ts.

  `installCLI` will be kept in `index.ts` for the following reasons:
  - Moving it to utils brings complications (`import.meta.url` doesn’t work)
  - This code will be removed once the action will make use of the separate install CLI action

* Simplify code related to mocking

* Use semverToInt from op-js

  Version `0.1.9` of the `op-js` exports function `semverToInt`, therefore we no longer need to duplicate it in our code.

* Improve CLI installation script

  - Add architectures for Linux runners. Fail if the architecture is not supported.
  - Fail if the runner’s operating system is not supported.

* Change from debug messages to info

  In pre-TS GitHub Action, we’d print some messages to the output as info (e.g. authenticated as, populating variable, unsetting previous values). Therefore, we apply the same principle here since there’s useful info.

* use toHaveBeenCalled consistently in tests

  `toBeCalled` is an alias for `toHaveBeenCalled` and `toBeCalledWith` is an alias for `toHaveBeenCalledWith`. For consistency, we will use `toHaveBeenCalled` and `toHaveBeenCalledWith` consistently across our tests.

* Add warning if both configs are provided

  1Password CLI will prioritize Connect config (with `OP_CONNECT_HOST` and `OP_CONNECT_TOKEN`) over service account one (with `OP_SERVICE_ACCOUNT_TOKEN`). This shouldn’t happen, therefore we print a warning to the user if both are provided.

* Add comment about cli validation process

  The code itself seems a bit confusing, therefore we add a comment explaining how it works.

* test: assertions for loadSecrets function

* Improve loadSecrets function

  Return early if no env vars with valid secret references are found

* Update dependencies

* Upgrade action to use Node20

---------

Co-authored-by: Dustin Ruetz <dustin.ruetz@agilebits.com>
  • Loading branch information
edif2008 and dustin-ruetz authored Feb 21, 2024
1 parent b575844 commit 2792fed
Show file tree
Hide file tree
Showing 13 changed files with 4,187 additions and 713 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ on: push
name: Run acceptance tests

jobs:
unit-tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 20
- run: npm ci
- run: npm test

test-with-output-secrets:
strategy:
matrix:
Expand Down
2 changes: 1 addition & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ inputs:
description: Export the secrets as environment variables
default: "true"
runs:
using: "node16"
using: "node20"
main: "dist/index.js"
11 changes: 10 additions & 1 deletion config/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,16 @@ const jestConfig = {
testEnvironment: "node",
testRegex: "(/__tests__/.*|(\\.|/)test)\\.ts",
transform: {
".ts": ["ts-jest"],
".ts": [
"ts-jest",
{
// Note: We shouldn't need to include `isolatedModules` here because it's a deprecated config option in TS 5,
// but setting it to `true` fixes the `ESM syntax is not allowed in a CommonJS module when
// 'verbatimModuleSyntax' is enabled` error that we're seeing when running our Jest tests.
isolatedModules: true,
useESM: true,
},
],
},
verbose: true,
};
Expand Down
3,933 changes: 3,549 additions & 384 deletions dist/index.js

Large diffs are not rendered by default.

172 changes: 0 additions & 172 deletions entrypoint.sh

This file was deleted.

46 changes: 46 additions & 0 deletions install_cli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash
set -e

# Install op-cli
install_op_cli() {
# Create a temporary directory where the CLI is installed
OP_INSTALL_DIR="$(mktemp -d)"
if [[ ! -d "$OP_INSTALL_DIR" ]]; then
echo "Install dir $OP_INSTALL_DIR not found"
exit 1
fi
echo "::debug::OP_INSTALL_DIR: ${OP_INSTALL_DIR}"

# Get the latest stable version of the CLI
CLI_VERSION="v$(curl https://app-updates.agilebits.com/check/1/0/CLI2/en/2.0.0/N -s | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+')"

if [[ "$OSTYPE" == "linux-gnu"* ]]; then
# Get runner's architecture
ARCH=$(uname -m)
if [[ "$(getconf LONG_BIT)" = 32 ]]; then
ARCH="386"
elif [[ "$ARCH" == "x86_64" ]]; then
ARCH="amd64"
elif [[ "$ARCH" == "aarch64" ]]; then
ARCH="arm64"
fi

if [[ "$ARCH" != "386" ]] && [[ "$ARCH" != "amd64" ]] && [[ "$ARCH" != "arm" ]] && [[ "$ARCH" != "arm64" ]]; then
echo "Unsupported architecture for the 1Password CLI: $ARCH."
exit 1
fi

curl -sSfLo op.zip "https://cache.agilebits.com/dist/1P/op2/pkg/${CLI_VERSION}/op_linux_${ARCH}_${CLI_VERSION}.zip"
unzip -od "$OP_INSTALL_DIR" op.zip && rm op.zip
elif [[ "$OSTYPE" == "darwin"* ]]; then
curl -sSfLo op.pkg "https://cache.agilebits.com/dist/1P/op2/pkg/${CLI_VERSION}/op_apple_universal_${CLI_VERSION}.pkg"
pkgutil --expand op.pkg temp-pkg
tar -xvf temp-pkg/op.pkg/Payload -C "$OP_INSTALL_DIR"
rm -rf temp-pkg && rm op.pkg
else
echo "Operating system not supported yet for this GitHub Action: $OSTYPE."
exit 1
fi
}

install_op_cli
Loading

0 comments on commit 2792fed

Please sign in to comment.