Skip to content

Commit 2792fed

Browse files
Migrate action to Typescript (#36)
* 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>
1 parent b575844 commit 2792fed

File tree

13 files changed

+4187
-713
lines changed

13 files changed

+4187
-713
lines changed

.github/workflows/test.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@ on: push
22
name: Run acceptance tests
33

44
jobs:
5+
unit-tests:
6+
runs-on: ubuntu-latest
7+
steps:
8+
- uses: actions/checkout@v3
9+
- uses: actions/setup-node@v3
10+
with:
11+
node-version: 20
12+
- run: npm ci
13+
- run: npm test
14+
515
test-with-output-secrets:
616
strategy:
717
matrix:

action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ inputs:
1212
description: Export the secrets as environment variables
1313
default: "true"
1414
runs:
15-
using: "node16"
15+
using: "node20"
1616
main: "dist/index.js"

config/jest.config.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,16 @@ const jestConfig = {
1111
testEnvironment: "node",
1212
testRegex: "(/__tests__/.*|(\\.|/)test)\\.ts",
1313
transform: {
14-
".ts": ["ts-jest"],
14+
".ts": [
15+
"ts-jest",
16+
{
17+
// Note: We shouldn't need to include `isolatedModules` here because it's a deprecated config option in TS 5,
18+
// but setting it to `true` fixes the `ESM syntax is not allowed in a CommonJS module when
19+
// 'verbatimModuleSyntax' is enabled` error that we're seeing when running our Jest tests.
20+
isolatedModules: true,
21+
useESM: true,
22+
},
23+
],
1524
},
1625
verbose: true,
1726
};

dist/index.js

Lines changed: 3549 additions & 384 deletions
Large diffs are not rendered by default.

entrypoint.sh

Lines changed: 0 additions & 172 deletions
This file was deleted.

install_cli.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Install op-cli
5+
install_op_cli() {
6+
# Create a temporary directory where the CLI is installed
7+
OP_INSTALL_DIR="$(mktemp -d)"
8+
if [[ ! -d "$OP_INSTALL_DIR" ]]; then
9+
echo "Install dir $OP_INSTALL_DIR not found"
10+
exit 1
11+
fi
12+
echo "::debug::OP_INSTALL_DIR: ${OP_INSTALL_DIR}"
13+
14+
# Get the latest stable version of the CLI
15+
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]+')"
16+
17+
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
18+
# Get runner's architecture
19+
ARCH=$(uname -m)
20+
if [[ "$(getconf LONG_BIT)" = 32 ]]; then
21+
ARCH="386"
22+
elif [[ "$ARCH" == "x86_64" ]]; then
23+
ARCH="amd64"
24+
elif [[ "$ARCH" == "aarch64" ]]; then
25+
ARCH="arm64"
26+
fi
27+
28+
if [[ "$ARCH" != "386" ]] && [[ "$ARCH" != "amd64" ]] && [[ "$ARCH" != "arm" ]] && [[ "$ARCH" != "arm64" ]]; then
29+
echo "Unsupported architecture for the 1Password CLI: $ARCH."
30+
exit 1
31+
fi
32+
33+
curl -sSfLo op.zip "https://cache.agilebits.com/dist/1P/op2/pkg/${CLI_VERSION}/op_linux_${ARCH}_${CLI_VERSION}.zip"
34+
unzip -od "$OP_INSTALL_DIR" op.zip && rm op.zip
35+
elif [[ "$OSTYPE" == "darwin"* ]]; then
36+
curl -sSfLo op.pkg "https://cache.agilebits.com/dist/1P/op2/pkg/${CLI_VERSION}/op_apple_universal_${CLI_VERSION}.pkg"
37+
pkgutil --expand op.pkg temp-pkg
38+
tar -xvf temp-pkg/op.pkg/Payload -C "$OP_INSTALL_DIR"
39+
rm -rf temp-pkg && rm op.pkg
40+
else
41+
echo "Operating system not supported yet for this GitHub Action: $OSTYPE."
42+
exit 1
43+
fi
44+
}
45+
46+
install_op_cli

0 commit comments

Comments
 (0)