From 6c7ceeac92db402be94a14d135023141e25951ba Mon Sep 17 00:00:00 2001 From: Martin Madsen Date: Fri, 17 Mar 2023 09:02:52 +0100 Subject: [PATCH 1/2] Declare workflowDispatch with inputs --- packages/cdkactions/API.md | 68 +++++++++++++++- packages/cdkactions/src/workflow.ts | 119 ++++++++++++++++++++++++++-- 2 files changed, 178 insertions(+), 9 deletions(-) diff --git a/packages/cdkactions/API.md b/packages/cdkactions/API.md index 4fb7b30..6acde34 100644 --- a/packages/cdkactions/API.md +++ b/packages/cdkactions/API.md @@ -45,10 +45,20 @@ Name|Description [StrategyProps](#cdkactions-strategyprops)|Strategy configuration block. [StringMap](#cdkactions-stringmap)|A generic string to string map. [WatchTypes](#cdkactions-watchtypes)|Configuration for the Watch event. +[WorkflowDispatchEvent](#cdkactions-workflowdispatchevent)|Configuration for the WorkflowDispatch event. +[WorkflowDispatchEventInputProps](#cdkactions-workflowdispatcheventinputprops)|Configuration for a manually dispatched workflow input. +[WorkflowDispatchEventProps](#cdkactions-workflowdispatcheventprops)|Configuration for the WorkflowDispatch event properties. [WorkflowProps](#cdkactions-workflowprops)|Configuration for a single GitHub Action workflow. [WorkflowRunEvent](#cdkactions-workflowrunevent)|Configuration for the WorkflowRun event. +**Enums** + +Name|Description +----|----------- +[WorkflowDispatchInputType](#cdkactions-workflowdispatchinputtype)|Workflow dispatch input types. + + ## class App @@ -274,7 +284,7 @@ new Workflow(scope: Construct, id: string, config: WorkflowProps) * **id** (string) The name of this workflow. * **config** ([WorkflowProps](#cdkactions-workflowprops)) *No description* * **name** (string) Name of the workflow. - * **on** ([ScheduleEvent](#cdkactions-scheduleevent) | [WorkflowRunEvent](#cdkactions-workflowrunevent) | [EventMap](#cdkactions-eventmap) | string | Array) When to run this workflow. + * **on** ([ScheduleEvent](#cdkactions-scheduleevent) | [WorkflowRunEvent](#cdkactions-workflowrunevent) | [WorkflowDispatchEvent](#cdkactions-workflowdispatchevent) | [EventMap](#cdkactions-eventmap) | string | Array) When to run this workflow. * **defaults** ([DefaultsProps](#cdkactions-defaultsprops)) A map of default settings to apply to all steps in this job. __*Optional*__ * **env** ([StringMap](#cdkactions-stringmap)) A map of environment variables to provide to the job. __*Optional*__ @@ -734,6 +744,48 @@ Name | Type | Description +## struct WorkflowDispatchEvent + + +Configuration for the WorkflowDispatch event. + + + +Name | Type | Description +-----|------|------------- +**workflowDispatch** | [WorkflowDispatchEventProps](#cdkactions-workflowdispatcheventprops) | + + + +## struct WorkflowDispatchEventInputProps + + +Configuration for a manually dispatched workflow input. + + + +Name | Type | Description +-----|------|------------- +**required** | boolean | Whether the input is required. +**type** | [WorkflowDispatchInputType](#cdkactions-workflowdispatchinputtype) | The type that identifies this input. +**default**? | string | boolean | The default value for the input if not supplied.
__*Optional*__ +**description**? | string | A description for this particular input.
__*Optional*__ + + + +## struct WorkflowDispatchEventProps + + +Configuration for the WorkflowDispatch event properties. + + + +Name | Type | Description +-----|------|------------- +**inputs**? | Map | __*Optional*__ + + + ## struct WorkflowProps @@ -744,7 +796,7 @@ Configuration for a single GitHub Action workflow. Name | Type | Description -----|------|------------- **name** | string | Name of the workflow. -**on** | [ScheduleEvent](#cdkactions-scheduleevent) | [WorkflowRunEvent](#cdkactions-workflowrunevent) | [EventMap](#cdkactions-eventmap) | string | Array | When to run this workflow. +**on** | [ScheduleEvent](#cdkactions-scheduleevent) | [WorkflowRunEvent](#cdkactions-workflowrunevent) | [WorkflowDispatchEvent](#cdkactions-workflowdispatchevent) | [EventMap](#cdkactions-eventmap) | string | Array | When to run this workflow. **defaults**? | [DefaultsProps](#cdkactions-defaultsprops) | A map of default settings to apply to all steps in this job.
__*Optional*__ **env**? | [StringMap](#cdkactions-stringmap) | A map of environment variables to provide to the job.
__*Optional*__ @@ -766,3 +818,15 @@ Name | Type | Description +## enum WorkflowDispatchInputType + +Workflow dispatch input types. + +Name | Description +-----|----- +**CHOICE** | +**BOOLEAN** | +**ENVIRONMENT** | +**STRING** | + + diff --git a/packages/cdkactions/src/workflow.ts b/packages/cdkactions/src/workflow.ts index ab6834d..64d3443 100644 --- a/packages/cdkactions/src/workflow.ts +++ b/packages/cdkactions/src/workflow.ts @@ -40,7 +40,24 @@ export interface IssuesTypes { /** * Supported types. */ - readonly types: Array<'opened' | 'edited' | 'deleted' | 'transferred' | 'pinned' | 'unpinned' | 'closed' | 'reopened' | 'assigned' | 'unassigned' | 'labeled' | 'unlabeled' | 'locked' | 'unlocked' | 'milestoned' | 'demilestoned'>; + readonly types: Array< + | 'opened' + | 'edited' + | 'deleted' + | 'transferred' + | 'pinned' + | 'unpinned' + | 'closed' + | 'reopened' + | 'assigned' + | 'unassigned' + | 'labeled' + | 'unlabeled' + | 'locked' + | 'unlocked' + | 'milestoned' + | 'demilestoned' + >; } /** @@ -100,7 +117,22 @@ export interface PullRequestTypes extends PushTypes { /** * Supported types. */ - readonly types?: Array<'assigned' | 'unassigned' | 'labeled' | 'unlabeled' | 'opened' | 'edited' | 'closed' | 'reopened' | 'synchronize' | 'ready_for_review' | 'locked' | 'unlocked' | 'review_requested' | 'review_request_removed'>; + readonly types?: Array< + | 'assigned' + | 'unassigned' + | 'labeled' + | 'unlabeled' + | 'opened' + | 'edited' + | 'closed' + | 'reopened' + | 'synchronize' + | 'ready_for_review' + | 'locked' + | 'unlocked' + | 'review_requested' + | 'review_request_removed' + >; } /** @@ -130,7 +162,22 @@ export interface PullRequestTargetTypes { /** * Supported types. */ - readonly types: Array<'assigned' | 'unassigned' | 'labeled' | 'unlabeled' | 'opened' | 'edited' | 'closed' | 'reopened' | 'synchronize' | 'ready_for_review' | 'locked' | 'unlocked' | 'review_requested' | 'review_request_removed'>; + readonly types: Array< + | 'assigned' + | 'unassigned' + | 'labeled' + | 'unlabeled' + | 'opened' + | 'edited' + | 'closed' + | 'reopened' + | 'synchronize' + | 'ready_for_review' + | 'locked' + | 'unlocked' + | 'review_requested' + | 'review_request_removed' + >; } /** @@ -205,7 +252,7 @@ export interface ScheduleEvent { /** * A cron schedule to run the workflow on. */ - readonly schedule: [{ 'cron': string }]; + readonly schedule: [{ cron: string }]; } /** @@ -233,10 +280,69 @@ export interface WorkflowRunEvent { readonly types?: Array<'completed' | 'requested'>; } +/** + * Workflow dispatch input types. + */ +export enum WorkflowDispatchInputType { + CHOICE = 'choice', + BOOLEAN = 'boolean', + ENVIRONMENT = 'environment', + STRING = 'string', +} + +/** + * Configuration for a manually dispatched workflow input + */ +export interface WorkflowDispatchEventInputProps { + /** + * The default value for the input if not supplied. + */ + readonly default?: string | boolean; + + /** + * A description for this particular input. + */ + readonly description?: string; + + /** + * Whether the input is required. + */ + readonly required: boolean; + + /** + * The type that identifies this input. + */ + readonly type: WorkflowDispatchInputType; +} + +/** + * Configuration for the WorkflowDispatch event properties. + */ +export interface WorkflowDispatchEventProps { + readonly inputs?: Record; +} + +/** + * Configuration for the WorkflowDispatch event. + */ +export interface WorkflowDispatchEvent { + readonly workflowDispatch: WorkflowDispatchEventProps; +} + /** * Events without additional subtypes. */ -export type EventStrings = 'workflowDispatch' | 'repositoryDispatch' | 'create' | 'delete' | 'deployment' | 'deploymentStatus' | 'fork' | 'gollum' | 'pageBuild' | 'public' | 'status'; +export type EventStrings = + | 'repositoryDispatch' + | 'create' + | 'delete' + | 'deployment' + | 'deploymentStatus' + | 'fork' + | 'gollum' + | 'pageBuild' + | 'public' + | 'status'; /** * Events with additional subtypes. @@ -345,7 +451,7 @@ export interface WorkflowProps { /** * When to run this workflow. */ - readonly on: Events | Array | EventMap | ScheduleEvent | WorkflowRunEvent; + readonly on: Events | Array | EventMap | ScheduleEvent | WorkflowRunEvent | WorkflowDispatchEvent; /** * A map of environment variables to provide to the job. @@ -420,4 +526,3 @@ export class Workflow extends Construct { return ghaction; } } - From 2fd24cef0616f843a1c5fcd54001922185c95c73 Mon Sep 17 00:00:00 2001 From: Martin Madsen Date: Fri, 17 Mar 2023 09:17:32 +0100 Subject: [PATCH 2/2] Add snapshot test to cover workflowDispatch --- packages/cdkactions/src/workflow.ts | 88 +++++++++---------- .../test/__snapshots__/workflow.test.ts.snap | 19 ++++ packages/cdkactions/test/workflow.test.ts | 38 +++++--- 3 files changed, 91 insertions(+), 54 deletions(-) diff --git a/packages/cdkactions/src/workflow.ts b/packages/cdkactions/src/workflow.ts index 64d3443..15293d7 100644 --- a/packages/cdkactions/src/workflow.ts +++ b/packages/cdkactions/src/workflow.ts @@ -41,22 +41,22 @@ export interface IssuesTypes { * Supported types. */ readonly types: Array< - | 'opened' - | 'edited' - | 'deleted' - | 'transferred' - | 'pinned' - | 'unpinned' - | 'closed' - | 'reopened' - | 'assigned' - | 'unassigned' - | 'labeled' - | 'unlabeled' - | 'locked' - | 'unlocked' - | 'milestoned' - | 'demilestoned' + | 'opened' + | 'edited' + | 'deleted' + | 'transferred' + | 'pinned' + | 'unpinned' + | 'closed' + | 'reopened' + | 'assigned' + | 'unassigned' + | 'labeled' + | 'unlabeled' + | 'locked' + | 'unlocked' + | 'milestoned' + | 'demilestoned' >; } @@ -118,20 +118,20 @@ export interface PullRequestTypes extends PushTypes { * Supported types. */ readonly types?: Array< - | 'assigned' - | 'unassigned' - | 'labeled' - | 'unlabeled' - | 'opened' - | 'edited' - | 'closed' - | 'reopened' - | 'synchronize' - | 'ready_for_review' - | 'locked' - | 'unlocked' - | 'review_requested' - | 'review_request_removed' + | 'assigned' + | 'unassigned' + | 'labeled' + | 'unlabeled' + | 'opened' + | 'edited' + | 'closed' + | 'reopened' + | 'synchronize' + | 'ready_for_review' + | 'locked' + | 'unlocked' + | 'review_requested' + | 'review_request_removed' >; } @@ -163,20 +163,20 @@ export interface PullRequestTargetTypes { * Supported types. */ readonly types: Array< - | 'assigned' - | 'unassigned' - | 'labeled' - | 'unlabeled' - | 'opened' - | 'edited' - | 'closed' - | 'reopened' - | 'synchronize' - | 'ready_for_review' - | 'locked' - | 'unlocked' - | 'review_requested' - | 'review_request_removed' + | 'assigned' + | 'unassigned' + | 'labeled' + | 'unlabeled' + | 'opened' + | 'edited' + | 'closed' + | 'reopened' + | 'synchronize' + | 'ready_for_review' + | 'locked' + | 'unlocked' + | 'review_requested' + | 'review_request_removed' >; } diff --git a/packages/cdkactions/test/__snapshots__/workflow.test.ts.snap b/packages/cdkactions/test/__snapshots__/workflow.test.ts.snap index 881de9a..41aa599 100644 --- a/packages/cdkactions/test/__snapshots__/workflow.test.ts.snap +++ b/packages/cdkactions/test/__snapshots__/workflow.test.ts.snap @@ -1,5 +1,24 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP +exports[`manual workflow dispatch 1`] = ` +Object { + "jobs": Object {}, + "name": "Test", + "on": Object { + "workflowDispatch": Object { + "inputs": Object { + "one": Object { + "default": "foo", + "description": "input one", + "required": false, + "type": "string", + }, + }, + }, + }, +} +`; + exports[`toGHAction 1`] = ` Object { "defaults": Object { diff --git a/packages/cdkactions/test/workflow.test.ts b/packages/cdkactions/test/workflow.test.ts index c4ec68f..02e193f 100644 --- a/packages/cdkactions/test/workflow.test.ts +++ b/packages/cdkactions/test/workflow.test.ts @@ -1,5 +1,5 @@ import { Construct } from 'constructs'; -import { Job } from '../src'; +import { Job, WorkflowDispatchInputType } from '../src'; import { TestingWorkflow } from './utils'; test('toGHAction', () => { @@ -41,14 +41,14 @@ test('2 jobs with same key -> error', () => { runsOn: 'ubuntu-latest', steps: [], }); - expect(() => - new Job(workflow, 'job', { - runsOn: 'ubuntu-latest', - steps: [], - }), + expect( + () => + new Job(workflow, 'job', { + runsOn: 'ubuntu-latest', + steps: [], + }), ).toThrowError("There is already a Construct with name 'job' in Workflow [test]"); -}, -); +}); test('jobs kept in insertion order', () => { const workflow = TestingWorkflow(); @@ -70,11 +70,29 @@ test('jobs kept in insertion order', () => { const jobs = Object.keys(workflow.toGHAction().jobs); const expected = [job_one, job_two, job_three]; expect(jobs).toEqual(expected); -}, -); +}); test('non-job children are ignored', () => { const workflow = TestingWorkflow(); new Construct(workflow, 'not_job'); expect(workflow.toGHAction().jobs).toEqual({}); }); + +test('manual workflow dispatch', () => { + const workflow = TestingWorkflow({ + name: 'Test', + on: { + workflowDispatch: { + inputs: { + one: { + default: 'foo', + description: 'input one', + required: false, + type: WorkflowDispatchInputType.STRING, + }, + }, + }, + }, + }); + expect(workflow.toGHAction()).toMatchSnapshot(); +});