From 8eba3f618474cab12b680acd7acb6c6ed798a68b Mon Sep 17 00:00:00 2001 From: LakshmiRavali Rimmalapudi Date: Tue, 21 Sep 2021 18:56:43 +0530 Subject: [PATCH 1/3] Added changes to use scripts instead of community Github actions --- .github/scripts/trigger-workflow.js | 49 ++++++++++++++++++ .github/scripts/update-release.js | 77 +++++++++++++++++++++++++++++ .github/workflows/release.yml | 39 +++++++-------- package.json | 3 ++ 4 files changed, 147 insertions(+), 21 deletions(-) create mode 100644 .github/scripts/trigger-workflow.js create mode 100644 .github/scripts/update-release.js diff --git a/.github/scripts/trigger-workflow.js b/.github/scripts/trigger-workflow.js new file mode 100644 index 00000000..ddf53eee --- /dev/null +++ b/.github/scripts/trigger-workflow.js @@ -0,0 +1,49 @@ +const core = require('@actions/core'); +const { Octokit } = require('@octokit/rest'); + +/** + * Functionality from benc-uk/workflow-dispatch. + * Link: https://github.com/benc-uk/workflow-dispatch + */ +const run = async () => { + try { + const octokit = new Octokit({ + auth: process.env.REPO_WORKFLOW_TOKEN + }); + const workflowRef = process.env.WORKFLOW_NAME; + const ref = process.env.BRANCH_NAME; + const [owner, repo] = process.env.REPO_NAME + ? process.env.REPO_NAME.split('/') + : [null, null]; + + // Decode inputs, this MUST be a valid JSON string + let inputs = {} + const inputsJson = process.env.INPUTS; + if(inputsJson) { + inputs = JSON.parse(inputsJson) + } + + const workflow = await octokit.rest.actions.getWorkflow({ + owner, + repo, + workflow_id: workflowRef + }); + + core.info(`Workflow id is: ${workflow.data.id}`) + + const dispatchResp = await octokit.rest.actions.createWorkflowDispatch({ + owner, + repo, + workflow_id: workflow.data.id, + ref, + inputs + }); + core.info(`API response status: ${dispatchResp.status} 🚀`) + } catch (error) { + core.setFailed(error.message) + } +} + +(async () => { + await run(); +})(); diff --git a/.github/scripts/update-release.js b/.github/scripts/update-release.js new file mode 100644 index 00000000..4c09b960 --- /dev/null +++ b/.github/scripts/update-release.js @@ -0,0 +1,77 @@ +const core = require('@actions/core'); +const { GitHub } = require('@actions/github'); + +/** + * Functionality from tubone24/update_release. + * Link: https://github.com/tubone24/update_release + */ +const updateRelease = async () => { + try { + const github = new GitHub(process.env.GITHUB_TOKEN); + const [owner, repo] = process.env.REPO_NAME + ? process.env.REPO_NAME.split('/') + : [null, null]; + const tag = process.env.TAG_NAME; + const getReleaseResponse = await github.repos.getReleaseByTag({ + owner, + repo, + tag + }); + + const { + data: { + id: oldReleaseId, + html_url: oldHtmlUrl, + upload_url: oldUploadUrl, + body: oldBody, + draft: oldDraft, + name: oldName, + prerelease: oldPrerelease + } + } = getReleaseResponse; + + core.info( + `Got release info: '${oldReleaseId}', ${oldName}, '${oldHtmlUrl}', '${oldUploadUrl},'` + ) + core.info(`Body: ${oldBody}`) + core.info(`Draft: ${oldDraft}, Prerelease: ${oldPrerelease}`) + + const newBody = process.env.RELEASE_BODY; + const newPrerelease = process.env.PRE_RELEASE; + + let body + if (newBody === '') { + body = oldBody + } else { + body = `${oldBody}\n${newBody}`; + } + + let prerelease + if (newPrerelease !== '' && !!newPrerelease) { + prerelease = newPrerelease === 'true' + } else { + prerelease = oldPrerelease + } + + await github.repos.updateRelease({ + owner, + release_id: oldReleaseId, + repo, + body, + name: oldName, + draft: oldDraft, + prerelease + }) + + core.info(`Updated release with body: ${body}`) + } catch (error) { + core.setFailed(error.message) + } +} +(async () => { + await updateRelease(); +})(); + +module.exports = { + updateRelease, +}; diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index b07b2b5f..fc200065 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -72,31 +72,28 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} NPM_TOKEN: ${{ secrets.NPM_TOKEN }} - update-release: - runs-on: ubuntu-latest - needs: [update-api-specs, release] - steps: - - name: Checkout cli-core repo - uses: actions/checkout@v2 - name: Update release - if: ${{needs.release.outputs.tag-name != ''}} - uses: tubone24/update_release@v1.2.0 + run: node .github/scripts/update-release.js env: GITHUB_TOKEN: ${{ github.token }} - TAG_NAME: ${{needs.release.outputs.tag-name}} - with: - is_append_body: true - body: ${{needs.update-api-specs.outputs.change-log}} + TAG_NAME: ${{ steps.semantic-release.outputs.TAG_NAME }} + RELEASE_BODY: ${{needs.update-api-specs.outputs.change-log}} + REPO_NAME: twilio/twilio-cli-core triggerCliWorkflow: runs-on: ubuntu-latest - needs: [ update-api-specs, update-release] + needs: [ update-api-specs, release] steps: + - name: Checkout cli-core repo + uses: actions/checkout@v2 + - run: | + git pull + npm install - name: Invoke CLI workflow with changelog and version-type - if: ${{toJSON(needs.update-api-specs.outputs.change-log) != null && needs.update-api-specs.outputs.version-type != -1}} - uses: benc-uk/workflow-dispatch@v1 - with: - workflow: Cli Release - token: ${{ secrets.REPO_ACCESS_TOKEN }} - repo: twilio/twilio-cli - ref: ${{ github.event.inputs.cli-branch }} - inputs: '{ "change-log": ${{ toJSON(needs.update-api-specs.outputs.change-log) }}, "version-type": "${{needs.update-api-specs.outputs.version-type}}", "homebrew-branch": "${{github.event.inputs.homebrew-branch}}", "homebrew-prerelease": "${{github.event.inputs.homebrew-prerelease}}" }' + if: ${{toJSON(needs.update-api-specs.outputs.change-log) != null && needs.update-api-specs.outputs.version-type != '-1'}} + run: node .github/scripts/trigger-workflow.js + env: + REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }} + WORKFLOW_NAME: 'release.yml' + BRANCH_NAME: ${{github.event.inputs.cli-branch}} + REPO_NAME: LakshmiRavali/twilio-cli + INPUTS: '{ "change-log": ${{ toJSON(needs.update-api-specs.outputs.change-log) }}, "version-type": "${{needs.update-api-specs.outputs.version-type}}", "homebrew-branch": "${{github.event.inputs.homebrew-branch}}", "homebrew-prerelease": "${{github.event.inputs.homebrew-prerelease}}" }' diff --git a/package.json b/package.json index 5ecdc585..1b7dd7d9 100644 --- a/package.json +++ b/package.json @@ -42,7 +42,10 @@ "twilio": "^3.54.2" }, "devDependencies": { + "@actions/core": "^1.5.0", + "@actions/github": "^5.0.0", "@oclif/test": "^1.2.6", + "@octokit/rest": "^18.10.0", "@semantic-release/changelog": "^5.0.1", "@semantic-release/exec": "^5.0.0", "@semantic-release/git": "^9.0.0", From a5126f6da0acc7390d4fadc9af8d6192dc451ff1 Mon Sep 17 00:00:00 2001 From: RavaliRimmalapudi Date: Fri, 24 Sep 2021 11:18:25 +0530 Subject: [PATCH 2/3] Exporing the scripts to use in cli workflows --- .github/scripts/trigger-workflow.js | 8 ++++++-- .github/workflows/release.yml | 1 - package.json | 4 ++-- src/index.js | 4 ++++ 4 files changed, 12 insertions(+), 5 deletions(-) diff --git a/.github/scripts/trigger-workflow.js b/.github/scripts/trigger-workflow.js index ddf53eee..5fa52e4b 100644 --- a/.github/scripts/trigger-workflow.js +++ b/.github/scripts/trigger-workflow.js @@ -5,7 +5,7 @@ const { Octokit } = require('@octokit/rest'); * Functionality from benc-uk/workflow-dispatch. * Link: https://github.com/benc-uk/workflow-dispatch */ -const run = async () => { +const triggerWorkflow = async () => { try { const octokit = new Octokit({ auth: process.env.REPO_WORKFLOW_TOKEN @@ -45,5 +45,9 @@ const run = async () => { } (async () => { - await run(); + await triggerWorkflow(); })(); + +module.exports = { + triggerWorkflow, +}; diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index fc200065..077d731d 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -89,7 +89,6 @@ jobs: git pull npm install - name: Invoke CLI workflow with changelog and version-type - if: ${{toJSON(needs.update-api-specs.outputs.change-log) != null && needs.update-api-specs.outputs.version-type != '-1'}} run: node .github/scripts/trigger-workflow.js env: REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }} diff --git a/package.json b/package.json index 1b7dd7d9..eeb39e8d 100644 --- a/package.json +++ b/package.json @@ -42,8 +42,8 @@ "twilio": "^3.54.2" }, "devDependencies": { - "@actions/core": "^1.5.0", - "@actions/github": "^5.0.0", + "@actions/core": "^1.0.0", + "@actions/github": "^2.2.0", "@oclif/test": "^1.2.6", "@octokit/rest": "^18.10.0", "@semantic-release/changelog": "^5.0.1", diff --git a/src/index.js b/src/index.js index b5057670..21804397 100644 --- a/src/index.js +++ b/src/index.js @@ -16,4 +16,8 @@ module.exports = { secureStorage: require('./services/secure-storage'), }, configureEnv: require('./services/env'), + releaseScripts: { + UpdateRelease: require('../.github/scripts/update-release'), + TriggerWorkflow: require('../.github/scripts/trigger-workflow'), + }, }; From 9e724d192e39f1cecdf7dfe6911be73a924bae51 Mon Sep 17 00:00:00 2001 From: ravali-rimmalapudi <83863595+ravali-rimmalapudi@users.noreply.github.com> Date: Tue, 28 Sep 2021 22:59:03 +0530 Subject: [PATCH 3/3] Update release.yml --- .github/workflows/release.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 077d731d..b30f1063 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -94,5 +94,5 @@ jobs: REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }} WORKFLOW_NAME: 'release.yml' BRANCH_NAME: ${{github.event.inputs.cli-branch}} - REPO_NAME: LakshmiRavali/twilio-cli + REPO_NAME: twilio/twilio-cli INPUTS: '{ "change-log": ${{ toJSON(needs.update-api-specs.outputs.change-log) }}, "version-type": "${{needs.update-api-specs.outputs.version-type}}", "homebrew-branch": "${{github.event.inputs.homebrew-branch}}", "homebrew-prerelease": "${{github.event.inputs.homebrew-prerelease}}" }'