Skip to content

Commit

Permalink
Run event and job (#13)
Browse files Browse the repository at this point in the history
* added method to run by event and job

* updated readme. bump package version
  • Loading branch information
shubhbapna authored Feb 8, 2023
1 parent 1de9668 commit 299380b
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 5 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Installs [nektos/act](https://github.com/nektos/act) and provides access to it a
- [Run a job](#run-a-job)
- [Using job id](#using-job-id)
- [Using event name](#using-event-name)
- [Using event name and job id](#using-event-name-and-job-id)
- [Mocking apis during the run](#mocking-apis-during-the-run)
- [Mocking steps](#mocking-steps)
- [Run result](#run-result)
Expand Down Expand Up @@ -269,6 +270,18 @@ result = await act
.runJob("pull_request");
```

#### Using event name and job id

You can trigger a workflow using an event name and job id. Equivalent of running `act event_name -j job_id`.

It returns an array of `Job` outputs. Described [below](#run-result)

```typescript
const act = new Act();

let result = await act.runEventAndJob("pull_request", "jobId");
```

#### Mocking apis during the run

You can use [Mockapi](#mockapi) and [Moctokit](https://github.com/kiegroup/mock-github#moctokit) to mock any kind of HTTP and HTTPS requests during your workflow run provided that the client being used honours HTTP_PROXY and HTTPS_PROXY env variables. Depending on the client, for HTTPS they might issue a CONNECT request to open a secure TCP tunnel. In this case `Act` won't be able to mock the HTTPS request.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kie/act-js",
"version": "2.0.0",
"version": "2.0.1",
"description": "nodejs wrapper for nektos/act",
"main": "build/src/index.js",
"types": "build/src/index.d.ts",
Expand Down
8 changes: 8 additions & 0 deletions src/act/act.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ export class Act {
return this.run([event], opts);
}

async runEventAndJob(event: string, jobId: string, opts?: RunOpts): Promise<Step[]> {
await this.handleStepMocking(
workflow => workflow.events.includes(event) && workflow.jobId === jobId,
opts
);
return this.run([event, "-j", jobId], opts);
}

private async handleStepMocking(
filter: (workflow: Workflow) => boolean,
opts?: RunOpts
Expand Down
25 changes: 23 additions & 2 deletions test/unit/act/act.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ describe("run", () => {
]);
});

test("run with event json and input", async () => {
test("run with input", async () => {
const act = new Act();
const output = await act
.setInput("some_input", "some_input")
.runEvent("workflow_dispatch", { workflowFile: resources });
.runEventAndJob("workflow_dispatch", "input", { workflowFile: resources });

expect(output).toMatchObject([
{
Expand All @@ -290,6 +290,27 @@ describe("run", () => {
},
]);
});

test("run with input and event json", async () => {
const act = new Act();
const output = await act
.setInput("some_input", "some_input")
.setEvent({ some_event: "some_event" })
.runEventAndJob("workflow_dispatch", "event-and-input", { workflowFile: resources });

expect(output).toMatchObject([
{
name: "Main event",
status: 0,
output: "some_event",
},
{
name: "Main input",
status: 0,
output: "some_input",
},
]);
});
});

describe("initialization", () => {
Expand Down
20 changes: 20 additions & 0 deletions test/unit/resources/act/event-and-input.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Event and Input

on:
workflow_dispatch:
inputs:
some_input:
default: 'world'
required: false
type: string

jobs:
event-and-input:
runs-on: ubuntu-latest
steps:
- name: event
run: echo "$EVENT"
env:
EVENT: ${{ github.event.some_event }}
- name: input
run: echo "${{ inputs.some_input }}"

0 comments on commit 299380b

Please sign in to comment.