Skip to content

Commit

Permalink
chore: Added changes to use scripts instead of community Github actio…
Browse files Browse the repository at this point in the history
…ns (twilio#155)

* Added changes to use scripts instead of community Github actions

* Exporing the scripts to use in cli workflows

* Update release.yml

Co-authored-by: LakshmiRavali Rimmalapudi <lrimmalapudi@LakshmiRavalis-MacBook-Pro.local>
  • Loading branch information
ravali-rimmalapudi and LakshmiRavali Rimmalapudi committed Sep 28, 2021
1 parent 5367ba5 commit 27bd508
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 21 deletions.
53 changes: 53 additions & 0 deletions .github/scripts/trigger-workflow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
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 triggerWorkflow = 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 triggerWorkflow();
})();

module.exports = {
triggerWorkflow,
};
77 changes: 77 additions & 0 deletions .github/scripts/update-release.js
Original file line number Diff line number Diff line change
@@ -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,
};
38 changes: 17 additions & 21 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -76,31 +76,27 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_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}}" }'
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: 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}}" }'
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@
"twilio": "^3.54.2"
},
"devDependencies": {
"@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",
"@semantic-release/exec": "^5.0.0",
"@semantic-release/git": "^9.0.0",
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
},
};

0 comments on commit 27bd508

Please sign in to comment.