Skip to content

Commit

Permalink
Input (#11)
Browse files Browse the repository at this point in the history
* implemented inputs

* added test cases

* refactor action input

* readme update

* fix typo
  • Loading branch information
shubhbapna authored Feb 8, 2023
1 parent bc475e5 commit 1de9668
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 0 deletions.
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Installs [nektos/act](https://github.com/nektos/act) and provides access to it a
- [Current working directory](#current-working-directory)
- [Secrets](#secrets)
- [Env](#env)
- [Input](#input)
- [Event payload](#event-payload)
- [List workflows](#list-workflows)
- [Run a job](#run-a-job)
Expand Down Expand Up @@ -132,6 +133,29 @@ act.deleteEnv("env1");
act.clearEnv();
```

### Input

You can define github action input that will be used by `act` when you execute a run.

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

// setInput returns back the object
act = act.setInput("input1", "value1");

// you can chain your setInputs
act
.setInput("input1", "value1")
.setInput("input2", "value2")
.setInput("input3", "value3")

// you can delete an input
act.deleteInput("input1");

// you clear all the inputs that you had previously defined
act.clearInput();
```

### Event payload

You can pass an [event payload](https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads) during your workflow execution. It is equivalent to calling `act` with the `-e` flag set.
Expand Down
20 changes: 20 additions & 0 deletions src/act/act.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,23 @@ import { ActionEvent } from "@aj/action-event/action-event";
import { EventJSON } from "@aj/action-event/action-event.types";
import { writeFile } from "fs/promises";
import { OutputParser } from "@aj/output-parser/output-parser";
import { ActionInput } from "@aj/action-input/action-input";

export class Act {
private secrets: ArgumentMap;
private cwd: string;
private workflowFile: string;
private env: ArgumentMap;
private event: ActionEvent;
private input: ActionInput;

constructor(cwd?: string, workflowFile?: string, defaultImageSize?: string) {
this.secrets = new ArgumentMap("-s");
this.cwd = cwd ?? process.cwd();
this.workflowFile = workflowFile ?? this.cwd;
this.env = new ArgumentMap("--env");
this.event = new ActionEvent();
this.input = new ActionInput(this.event);
this.setDefaultImage(defaultImageSize);
this.setGithubStepSummary("/dev/stdout");
}
Expand Down Expand Up @@ -85,6 +88,21 @@ export class Act {
return this;
}

setInput(key: string, val: string) {
this.input.map.set(key, val);
return this;
}

deleteInput(key: string) {
this.input.map.delete(key);
return this;
}

clearInput() {
this.input.map.clear();
return this;
}

/**
* List available workflows.
* If working directory is not specified then node's current working directory is used
Expand Down Expand Up @@ -223,13 +241,15 @@ export class Act {
const { cwd, actArguments, proxy } = await this.parseRunOpts(opts);
const env = this.env.toActArguments();
const secrets = this.secrets.toActArguments();
const input = this.input.toActArguments();
const event = await this.event.toActArguments();

const { data, error } = await this.act(
cwd,
...cmd,
...secrets,
...env,
...input,
...event,
...actArguments
);
Expand Down
27 changes: 27 additions & 0 deletions src/action-input/action-input.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ArgumentMap } from "@aj/map/argument-map";
import { ActionEvent } from "@aj/action-event/action-event";

export class ActionInput {
private input: ArgumentMap;
private event: ActionEvent;

constructor(event: ActionEvent) {
this.input = new ArgumentMap("--input");
this.event = event;
}

get map() {
return this.input.map;
}

toActArguments() {
if (Object.keys(this.event.event).length > 0) {
const eventCopy = { ...this.event.event };
eventCopy.inputs = Object.fromEntries(this.input.map);
this.event.event = eventCopy;
return [];
} else {
return this.input.toActArguments();
}
}
}
15 changes: 15 additions & 0 deletions test/unit/act/act.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,21 @@ describe("run", () => {
},
]);
});

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

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

describe("initialization", () => {
Expand Down
43 changes: 43 additions & 0 deletions test/unit/action-input/action-input.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { ActionEvent } from "@aj/action-event/action-event";
import { ActionInput } from "@aj/action-input/action-input";

describe("toActArguments", () => {
test("event payload is defined", () => {
const event = new ActionEvent();
const input = new ActionInput(event);

event.event = {
pull_request: {
head: {
ref: "branch",
},
},
};

input.map.set("INPUT1", "value1");
expect(input.toActArguments()).toStrictEqual([]);
expect(event.event).toStrictEqual({
pull_request: {
head: {
ref: "branch",
},
},
inputs: {
INPUT1: "value1",
},
});
});

test("event payload is not defined", () => {
const event = new ActionEvent();
const input = new ActionInput(event);
input.map.set("INPUT1", "value1");
input.map.set("INPUT2", "value2");
expect(input.toActArguments()).toStrictEqual([
"--input",
"INPUT1=value1",
"--input",
"INPUT2=value2",
]);
});
});
16 changes: 16 additions & 0 deletions test/unit/resources/act/input.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Input

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

jobs:
input:
runs-on: ubuntu-latest
steps:
- name: input
run: echo "${{ inputs.some_input }}"

0 comments on commit 1de9668

Please sign in to comment.