Skip to content

Commit

Permalink
feat: refactor code and use helpers lib for create github actions
Browse files Browse the repository at this point in the history
  • Loading branch information
kamontat committed Jun 11, 2024
1 parent 09c72a7 commit b3f052b
Show file tree
Hide file tree
Showing 13 changed files with 291 additions and 45 deletions.
8 changes: 7 additions & 1 deletion actions/example-ts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@
"clean": "heft clean"
},
"dependencies": {
"@kcws/github-actions": "^0.2.1"
"@actions/cache": "3.2.4",
"@actions/core": "1.10.1",
"@actions/exec": "1.1.1",
"@actions/github": "6.0.0",
"@actions/http-client": "2.2.1",
"@actions/io": "1.1.3",
"@kcws/github-actions": "0.2.2"
},
"devDependencies": {
"@kcws/eslint-config": "1.12.1",
Expand Down
21 changes: 21 additions & 0 deletions actions/example-ts/src/app/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import actions, { context } from "."

describe("app", () => {
it("should executes runner", async () => {
const runner = jest.fn()
await actions.exec(runner)
expect(runner).toHaveBeenCalledTimes(1)
})
})

describe("app.context", () => {
it("should contains metadata", () => {
expect(context.name).not.toBe("")
expect(context.version).not.toBe("")
})

it("should contains plugins", () => {
expect(context.use("input")).not.toBeFalsy()
expect(context.use("log")).not.toBeFalsy()
})
})
21 changes: 21 additions & 0 deletions actions/example-ts/src/app/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {
ContextBuilder,
InputContextPlugin,
LogContextPlugin,
Actions,
ExecContextPlugin,
} from "@kcws/github-actions"

export const context = ContextBuilder.builder()
.setName("example-ts")
.setVersion("v1.0.0")
.addPlugin(new LogContextPlugin())
.addPlugin(new InputContextPlugin())
.addPlugin(new ExecContextPlugin())
.build()

export default Actions.builder(context, context => {
return {
name: context.use("input").optionalString("name") ?? "world",
}
})
8 changes: 0 additions & 8 deletions actions/example-ts/src/core/actions.ts

This file was deleted.

12 changes: 0 additions & 12 deletions actions/example-ts/src/core/context.ts

This file was deleted.

15 changes: 11 additions & 4 deletions actions/example-ts/src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { greeting } from "./next"
jest.mock("@actions/core")

describe("hello", () => {
test("naming", () => {
expect(greeting).toEqual("world")
import { info } from "@actions/core"

import { context } from "./app"
import runner from "./index"

describe("application runner", () => {
test("should print info logs", async () => {
await runner({ input: { name: "name" } }, context)
expect(info).toHaveBeenCalledTimes(1)
expect(info).toHaveBeenCalledWith("hello name")
})
})
12 changes: 9 additions & 3 deletions actions/example-ts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import actions from "./core/actions"
import { AppRunner } from "@kcws/github-actions"

actions.exec((data, context) => {
import actions from "./app"

const runner: AppRunner<typeof actions> = (data, context) => {
context.use("log").info("hello {name}", data.input)
})
}

actions.exec(runner)

export default runner
14 changes: 14 additions & 0 deletions actions/example-ts/src/post.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
jest.mock("@actions/core")

import { info } from "@actions/core"

import { context } from "./app"
import runner from "./post"

describe("[post-hook] application runner", () => {
test("should print info logs", async () => {
await runner({ input: { name: "name" } }, context)
expect(info).toHaveBeenCalledTimes(1)
expect(info).toHaveBeenCalledWith("hello post name")
})
})
12 changes: 9 additions & 3 deletions actions/example-ts/src/post.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import actions from "./core/actions"
import { AppRunner } from "@kcws/github-actions"

actions.exec((data, context) => {
import actions from "./app"

const runner: AppRunner<typeof actions> = (data, context) => {
context.use("log").info("hello post {name}", data.input)
})
}

actions.exec(runner)

export default runner
14 changes: 14 additions & 0 deletions actions/example-ts/src/pre.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
jest.mock("@actions/core")

import { info } from "@actions/core"

import { context } from "./app"
import runner from "./pre"

describe("[pre-hook] application runner", () => {
test("should print info logs", async () => {
await runner({ input: { name: "name" } }, context)
expect(info).toHaveBeenCalledTimes(1)
expect(info).toHaveBeenCalledWith("hello pre name")
})
})
12 changes: 9 additions & 3 deletions actions/example-ts/src/pre.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import actions from "./core/actions"
import { AppRunner } from "@kcws/github-actions"

actions.exec((data, context) => {
import actions from "./app"

const runner: AppRunner<typeof actions> = (data, context) => {
context.use("log").info("hello pre {name}", data.input)
})
}

actions.exec(runner)

export default runner
4 changes: 0 additions & 4 deletions github-actions.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
"name": "@kcws/actions",
"path": "actions",
},
{
"name": "@kcws/utils",
"path": "utils",
},
{
"name": "@common/resources",
"path": "common/resources",
Expand Down
Loading

0 comments on commit b3f052b

Please sign in to comment.