Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
OR13 committed Aug 10, 2024
1 parent 77f3df7 commit d67658a
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 53 deletions.
6 changes: 3 additions & 3 deletions dist/index.js

Large diffs are not rendered by default.

40 changes: 26 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
},
"homepage": "https://github.com/transmute-industries/transmute#readme",
"dependencies": {
"@actions/core": "^1.10.1"
"@actions/core": "^1.10.1",
"jose": "^5.6.3"
},
"devDependencies": {
"@types/jest": "^29.2.6",
Expand All @@ -62,4 +63,4 @@
"ts-jest": "^29.0.3",
"typescript": "^4.9.4"
}
}
}
28 changes: 28 additions & 0 deletions src/action/args.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

import { parseArgs } from "node:util";

import { getInput } from '@actions/core'

import { env } from "./env";

export const args = (prompt: string) => {
// https://stackoverflow.com/questions/29655760/convert-a-string-into-shell-arguments
const re = /"[^"]+"|'[^']+'|\S+/g
if (env.github() && !env.mock()) {
prompt = getInput("transmute")
}
return parseArgs({
allowPositionals: true,
args: prompt.match(re) || [],
options: {
verbose: {
type: 'boolean' as "boolean",
short: 'v'
},
alg: {
type: 'string' as "string",
short: 'a'
},
},
})
}
5 changes: 5 additions & 0 deletions src/action/env.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const github = () => !!process.env.GITHUB_ACTION

const mock = () => process.env.GITHUB_ACTION === 'jest-mock'

export const env = { github, mock }
30 changes: 7 additions & 23 deletions src/action/facade.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
import { setFailed } from '@actions/core'
import { args } from "./args"

import { parseArgs } from "node:util";
import { handler } from './handler'

import * as core from '@actions/core'

const getArgs = (prompt: string) => {
// https://stackoverflow.com/questions/29655760/convert-a-string-into-shell-arguments
const re = /"[^"]+"|'[^']+'|\S+/g
if (process.env.GITHUB_ACTION) {
prompt = core.getInput("transmute")
}
return parseArgs({
allowPositionals: true,
args: prompt.match(re) || [],
options: {
alg: {
type: 'string' as "string",
},
},
})
}
import { env } from './env'

export async function facade(prompt: string = process.argv.slice(2).join(' ')) {
try {
const parsed = getArgs(prompt)
console.log(parsed)
await handler(args(prompt))
} catch (error) {
// swallow error to prevent leaking
const message = '💀 Internal Error.'
if (process.env.GITHUB_ACTION) {
core.setFailed(message)
if (env.github()) {
setFailed(message)
} else {
console.error(message)
}
Expand Down
16 changes: 16 additions & 0 deletions src/action/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { PositionalArguments } from '../types'

import * as jose from '../jose'

const commands = { jose }

export const handler = async (args: PositionalArguments) => {
const [command] = args.positionals
if (commands[command]) {
await commands[command].handler(args)
} else {
const message = `😕 Unknown Command`
console.error(message)
throw new Error(message)
}
}
3 changes: 3 additions & 0 deletions src/action/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './env'
export * from './facade'
export * from './handler'
1 change: 1 addition & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './action'
6 changes: 2 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { facade } from './action/facade'

const cli = { facade }

export default cli
export * from './types'
export * from './cli'
44 changes: 44 additions & 0 deletions src/jose/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as jose from 'jose'

import { PositionalArguments } from "../types"

import { setSecret, setOutput, debug } from '@actions/core'

import { env } from '../action'

const prettyKey = (k: jose.JWK) => {
const { kid, kty, crv, alg, x, y, d } = k
return JSON.stringify({ kid, kty, crv, alg, x, y, d }, null, 2)
}

export const handler = async function ({ positionals, values }: PositionalArguments) {
const [resource, _action] = positionals.slice(1)
switch (resource) {
case 'keygen': {
const alg = values.alg || 'ES256'
const crv = values.crv || 'Ed25519'
const verbose = values.verbose || false
const k = await jose.generateKeyPair(alg, { crv })
const privateKey = await jose.exportJWK(k.privateKey)
privateKey.kid = await jose.calculateJwkThumbprint(privateKey)
if (verbose) {
const message = `🔑 ${privateKey.kid}`
debug(message)
}
const output = prettyKey(privateKey)
if (env.github()) {
setSecret(output)
setOutput('json', output)
} else {
console.log(output)
}
break
}
default: {
const message = `😕 Unknown Command`
console.error(message)
throw new Error(message)
}
}

}
1 change: 1 addition & 0 deletions src/jose/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './handler'
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type PositionalArguments = { positionals: string[], values: Record<string, any> }
7 changes: 0 additions & 7 deletions tests/cli.test.ts

This file was deleted.

22 changes: 22 additions & 0 deletions tests/jose.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as core from '@actions/core'

import { facade } from '../src'

let debug: jest.SpiedFunction<typeof core.debug>
let output: jest.SpiedFunction<typeof core.setOutput>
let secret: jest.SpiedFunction<typeof core.setSecret>

beforeEach(() => {
process.env.GITHUB_ACTION = 'jest-mock'
jest.clearAllMocks()
debug = jest.spyOn(core, 'debug').mockImplementation()
output = jest.spyOn(core, 'setOutput').mockImplementation()
secret = jest.spyOn(core, 'setSecret').mockImplementation()
})

it('keygen', async () => {
await facade(`jose keygen --alg ES256 --verbose`)
expect(debug).toHaveBeenCalledTimes(1)
expect(secret).toHaveBeenCalledTimes(1)
expect(output).toHaveBeenCalledTimes(1)
})

0 comments on commit d67658a

Please sign in to comment.