From 194401d7641dabe5a55a38c88243f0a23ab5f065 Mon Sep 17 00:00:00 2001 From: gautamkrishnar Date: Mon, 11 Jan 2021 14:36:10 +0530 Subject: [PATCH] Added output only mode, fixes: #51 --- README.md | 1 + action.yml | 7 +++++++ blog-post-workflow.js | 11 ++++++++++- local-run.js | 1 + test.js | 1 + 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index fa8cf04..02effe3 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,7 @@ This workflow has additional options that you can use to customize it for your u | `commit_message` | `Updated with the latest blog posts` | Allows you to customize the commit message | No | | `committer_username` | `blog-post-bot` | Allows you to customize the committer username | No | | `committer_email` | `blog-post-bot@example.com` | Allows you to customize the committer email | No | +| `output_only` | `false` | Sets the generated array as `results` [output variable](https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-syntax-for-github-actions#jobsjob_idoutputs) so that it can be consumed in other actions and parsed via utilities like jq. This will also prevent committing to readme. | No | ### Advanced usage examples #### StackOverflow example diff --git a/action.yml b/action.yml index b57c871..d65fbdd 100644 --- a/action.yml +++ b/action.yml @@ -77,6 +77,13 @@ inputs: description: "Email id used while committing to the repo" default: "blog-post-bot@example.com" required: false + output_only: + description: "Prevent updating the readme, instead sets the output to the output variable named `results`" + default: "false" + required: false +outputs: + results: + description: "JSON stringified array of posts" runs: using: node12 diff --git a/blog-post-workflow.js b/blog-post-workflow.js index 6e9bb4d..f5363f0 100644 --- a/blog-post-workflow.js +++ b/blog-post-workflow.js @@ -353,8 +353,17 @@ Promise.allSettled(promiseArray).then((results) => { if (newReadme !== readmeData) { core.info('Writing to ' + README_FILE_PATH); fs.writeFileSync(README_FILE_PATH, newReadme); + const outputOnly = core.getInput('output_only') !== 'false'; + if (!process.env.TEST_MODE) { - await commitReadme(); + if (!outputOnly) { + // Commit to readme + await commitReadme(); + } else { + // Sets output as output as `results` variable in github action + core.info('outputOnly mode: set `results` variable. Readme not updated.'); + core.setOutput('results', postsArray); + } } } else { core.info('No change detected, skipping'); diff --git a/local-run.js b/local-run.js index fa06374..ae5d00b 100644 --- a/local-run.js +++ b/local-run.js @@ -26,6 +26,7 @@ fs.writeFile(path.join(__dirname, 'test', 'Readme.md'), template, () => { process.env.INPUT_TITLE_MAX_LENGTH = ''; process.env.INPUT_DESCRIPTION_MAX_LENGTH = ''; process.env.INPUT_ITEM_EXEC = ''; + process.env.INPUT_OUTPUT_ONLY = 'false'; const testFile = process.env.DIST ? './dist/blog-post-workflow' :'./blog-post-workflow'; console.log('Testing: ', testFile); require(testFile); diff --git a/test.js b/test.js index 7069e2d..bf3520a 100644 --- a/test.js +++ b/test.js @@ -18,6 +18,7 @@ const DEFAULT_TEST_ENV = { INPUT_TITLE_MAX_LENGTH: '', INPUT_DESCRIPTION_MAX_LENGTH: '', INPUT_ITEM_EXEC: '', + INPUT_OUTPUT_ONLY: 'false', TEST_MODE: 'true' };