From 23d3496da56cce1aaaf90f0b94bf4922d80b4ed9 Mon Sep 17 00:00:00 2001 From: George Lungley Date: Mon, 15 Jul 2024 15:56:58 +0100 Subject: [PATCH] feat: add support for secrets on workflow_call --- __tests__/action-docs-workflow.test.ts | 12 +++++ .../workflow/action_docs_workflow.yml | 5 ++ .../workflow/all_fields_one_annotation.output | 7 +++ .../workflow/all_fields_workflow.output | 7 +++ .../fixtures/workflow/all_fields_workflow.yml | 5 ++ .../fixtures/workflow/secrets_workflow.output | 53 +++++++++++++++++++ .../fixtures/workflow/secrets_workflow.yml | 46 ++++++++++++++++ __tests__/fixtures/workflow/workflow.yml | 1 + package.json | 2 +- src/action-docs.ts | 16 ++++++ 10 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 __tests__/fixtures/workflow/secrets_workflow.output create mode 100644 __tests__/fixtures/workflow/secrets_workflow.yml diff --git a/__tests__/action-docs-workflow.test.ts b/__tests__/action-docs-workflow.test.ts index e508f522..3f80eb12 100644 --- a/__tests__/action-docs-workflow.test.ts +++ b/__tests__/action-docs-workflow.test.ts @@ -28,6 +28,18 @@ describe("Test output", () => { expect(markdown).toEqual(expected); }); + test("With secrets.", async () => { + const markdown = await generateActionMarkdownDocs({ + sourceFile: path.join(fixtureDir, "secrets_workflow.yml"), + includeNameHeader: true, + }); + const expected = ( + readFileSync(path.join(fixtureDir, "secrets_workflow.output"), "utf-8") + ); + + expect(markdown).toEqual(expected); + }); + test("A minimal workflow definition.", async () => { const markdown = await generateActionMarkdownDocs({ sourceFile: path.join(fixtureDir, "minimal_workflow.yml"), diff --git a/__tests__/fixtures/workflow/action_docs_workflow.yml b/__tests__/fixtures/workflow/action_docs_workflow.yml index 6dc25a8f..f365d4d6 100644 --- a/__tests__/fixtures/workflow/action_docs_workflow.yml +++ b/__tests__/fixtures/workflow/action_docs_workflow.yml @@ -24,6 +24,11 @@ on: required: false default: LF + secrets: + notVerySecret: + description: "A secret" + required: true + jobs: job1: runs-on: ubuntu-latest diff --git a/__tests__/fixtures/workflow/all_fields_one_annotation.output b/__tests__/fixtures/workflow/all_fields_one_annotation.output index 9b5310e8..ff04ee20 100644 --- a/__tests__/fixtures/workflow/all_fields_one_annotation.output +++ b/__tests__/fixtures/workflow/all_fields_one_annotation.output @@ -12,6 +12,13 @@ | `inputE` |

A description E

| `string` | `false` | `false` | +### Secrets + +| name | description | required | +| --- | --- | --- | +| `notVerySecret` |

A secret

| `true` | + + ### Outputs | name | description | diff --git a/__tests__/fixtures/workflow/all_fields_workflow.output b/__tests__/fixtures/workflow/all_fields_workflow.output index 3951facb..28da9c84 100644 --- a/__tests__/fixtures/workflow/all_fields_workflow.output +++ b/__tests__/fixtures/workflow/all_fields_workflow.output @@ -11,6 +11,13 @@ | `inputE` |

A description E

| `string` | `false` | `false` | +### Secrets + +| name | description | required | +| --- | --- | --- | +| `notVerySecret` |

A secret

| `true` | + + ### Outputs | name | description | diff --git a/__tests__/fixtures/workflow/all_fields_workflow.yml b/__tests__/fixtures/workflow/all_fields_workflow.yml index 14ceeaef..6a860dc2 100644 --- a/__tests__/fixtures/workflow/all_fields_workflow.yml +++ b/__tests__/fixtures/workflow/all_fields_workflow.yml @@ -26,6 +26,11 @@ on: required: false default: false + secrets: + notVerySecret: + description: "A secret" + required: true + outputs: outputA: description: 'A description A' diff --git a/__tests__/fixtures/workflow/secrets_workflow.output b/__tests__/fixtures/workflow/secrets_workflow.output new file mode 100644 index 00000000..c943a8ae --- /dev/null +++ b/__tests__/fixtures/workflow/secrets_workflow.output @@ -0,0 +1,53 @@ +## A Workflow with secrets + +### Inputs + +| name | description | type | required | default | +| --- | --- | --- | --- | --- | +| `inputA` | | `string` | `false` | `This is a default` | +| `inputB` |

This is a multiline description

| `number` | `true` | `""` | + + +### Secrets + +| name | description | required | +| --- | --- | --- | +| `notVerySecret` |

A secret

| `true` | + + +### Outputs + +| name | description | +| --- | --- | +| `outputA` |

This is output A

| + + +### Usage + +```yaml +jobs: + job1: + uses: ***PROJECT***@***VERSION*** + with: + inputA: + # - Item 1 + # - foo, bar, baz + # - Item 2 + # - [github](https://github.com/) + # - **blah** + # - Item 3 + # + # Type: string + # Required: false + # Default: This is a default + + inputB: + # This is a + # multiline description + # + # Type: number + # Required: true + # Default: "" +``` + + diff --git a/__tests__/fixtures/workflow/secrets_workflow.yml b/__tests__/fixtures/workflow/secrets_workflow.yml new file mode 100644 index 00000000..be35c5de --- /dev/null +++ b/__tests__/fixtures/workflow/secrets_workflow.yml @@ -0,0 +1,46 @@ +name: 'A Workflow with secrets' +on: + push: + branches: + - main + schedule: + - cron: '*/15 * * * *' + workflow_call: + inputs: + inputA: + description: | + - Item 1 + - foo, bar, baz + - Item 2 + - [github](https://github.com/) + - **blah** + - Item 3 + type: string + default: 'This is a default' + required: false + inputB: + description: | + This is a + multiline description + type: number + required: true + + secrets: + notVerySecret: + description: "A secret" + required: true + + outputs: + outputA: + description: 'This is output A' + value: ${{ jobs.job1.outputs.step_output1 }} + +jobs: + job1: + runs-on: ubuntu-latest + outputs: + step_output1: ${{ steps.step1.outputs.test }} + steps: + - name: 'Step 1' + id: step1 + run: echo "test=some value" >> "$GITHUB_OUTPUT" diff --git a/__tests__/fixtures/workflow/workflow.yml b/__tests__/fixtures/workflow/workflow.yml index d2c9726b..af4db337 100644 --- a/__tests__/fixtures/workflow/workflow.yml +++ b/__tests__/fixtures/workflow/workflow.yml @@ -24,6 +24,7 @@ on: multiline description type: number required: true + outputs: outputA: description: 'This is output A' diff --git a/package.json b/package.json index cf71d419..0f9d9a03 100644 --- a/package.json +++ b/package.json @@ -76,4 +76,4 @@ "directories": { "dist": "lib" } -} +} \ No newline at end of file diff --git a/src/action-docs.ts b/src/action-docs.ts index 744ff61c..2c72d87c 100644 --- a/src/action-docs.ts +++ b/src/action-docs.ts @@ -34,6 +34,7 @@ interface WorkflowTriggerEvent { branches: string[]; cron: string[]; inputs: ActionInputsOutputs; + secrets: ActionInputsOutputs; outputs: ActionInputsOutputs; } @@ -67,6 +68,7 @@ enum InputOutputType { actionInput, workflowInput, actionOutput, + workflowSecret, } const inputOutputHeaders: Record = { @@ -79,6 +81,7 @@ const inputOutputHeaders: Record = { "default", ], [InputOutputType.actionOutput]: ["name", "description"], + [InputOutputType.workflowSecret]: ["name", "description", "required"], }; const inputOutputDefaults: Record = { @@ -260,6 +263,7 @@ function generateWorkflowDocs( options, InputOutputType.workflowInput, ), + secrets: generateSecrets(yml.on.workflow_call?.secrets, options), outputs: generateOutputs(yml.on.workflow_call?.outputs, options), runs: "", usage: generateUsage(yml.on.workflow_call?.inputs, options, false), @@ -285,6 +289,18 @@ function generateInputs( return createMarkdownSection(options, inputMdTable, "Inputs"); } +function generateSecrets( + data: ActionInputsOutputs, + options: DefaultOptions, +): string { + const secretMdTable = createMdTable( + data, + options, + InputOutputType.workflowSecret, + ); + return createMarkdownSection(options, secretMdTable, "Secrets"); +} + function generateOutputs( data: ActionInputsOutputs, options: DefaultOptions,