Skip to content

Commit 27bd508

Browse files
ravali-rimmalapudiLakshmiRavali Rimmalapudi
andauthored
chore: Added changes to use scripts instead of community Github actions (#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>
1 parent 5367ba5 commit 27bd508

File tree

5 files changed

+154
-21
lines changed

5 files changed

+154
-21
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const core = require('@actions/core');
2+
const { Octokit } = require('@octokit/rest');
3+
4+
/**
5+
* Functionality from benc-uk/workflow-dispatch.
6+
* Link: https://github.com/benc-uk/workflow-dispatch
7+
*/
8+
const triggerWorkflow = async () => {
9+
try {
10+
const octokit = new Octokit({
11+
auth: process.env.REPO_WORKFLOW_TOKEN
12+
});
13+
const workflowRef = process.env.WORKFLOW_NAME;
14+
const ref = process.env.BRANCH_NAME;
15+
const [owner, repo] = process.env.REPO_NAME
16+
? process.env.REPO_NAME.split('/')
17+
: [null, null];
18+
19+
// Decode inputs, this MUST be a valid JSON string
20+
let inputs = {}
21+
const inputsJson = process.env.INPUTS;
22+
if(inputsJson) {
23+
inputs = JSON.parse(inputsJson)
24+
}
25+
26+
const workflow = await octokit.rest.actions.getWorkflow({
27+
owner,
28+
repo,
29+
workflow_id: workflowRef
30+
});
31+
32+
core.info(`Workflow id is: ${workflow.data.id}`)
33+
34+
const dispatchResp = await octokit.rest.actions.createWorkflowDispatch({
35+
owner,
36+
repo,
37+
workflow_id: workflow.data.id,
38+
ref,
39+
inputs
40+
});
41+
core.info(`API response status: ${dispatchResp.status} 🚀`)
42+
} catch (error) {
43+
core.setFailed(error.message)
44+
}
45+
}
46+
47+
(async () => {
48+
await triggerWorkflow();
49+
})();
50+
51+
module.exports = {
52+
triggerWorkflow,
53+
};

.github/scripts/update-release.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
const core = require('@actions/core');
2+
const { GitHub } = require('@actions/github');
3+
4+
/**
5+
* Functionality from tubone24/update_release.
6+
* Link: https://github.com/tubone24/update_release
7+
*/
8+
const updateRelease = async () => {
9+
try {
10+
const github = new GitHub(process.env.GITHUB_TOKEN);
11+
const [owner, repo] = process.env.REPO_NAME
12+
? process.env.REPO_NAME.split('/')
13+
: [null, null];
14+
const tag = process.env.TAG_NAME;
15+
const getReleaseResponse = await github.repos.getReleaseByTag({
16+
owner,
17+
repo,
18+
tag
19+
});
20+
21+
const {
22+
data: {
23+
id: oldReleaseId,
24+
html_url: oldHtmlUrl,
25+
upload_url: oldUploadUrl,
26+
body: oldBody,
27+
draft: oldDraft,
28+
name: oldName,
29+
prerelease: oldPrerelease
30+
}
31+
} = getReleaseResponse;
32+
33+
core.info(
34+
`Got release info: '${oldReleaseId}', ${oldName}, '${oldHtmlUrl}', '${oldUploadUrl},'`
35+
)
36+
core.info(`Body: ${oldBody}`)
37+
core.info(`Draft: ${oldDraft}, Prerelease: ${oldPrerelease}`)
38+
39+
const newBody = process.env.RELEASE_BODY;
40+
const newPrerelease = process.env.PRE_RELEASE;
41+
42+
let body
43+
if (newBody === '') {
44+
body = oldBody
45+
} else {
46+
body = `${oldBody}\n${newBody}`;
47+
}
48+
49+
let prerelease
50+
if (newPrerelease !== '' && !!newPrerelease) {
51+
prerelease = newPrerelease === 'true'
52+
} else {
53+
prerelease = oldPrerelease
54+
}
55+
56+
await github.repos.updateRelease({
57+
owner,
58+
release_id: oldReleaseId,
59+
repo,
60+
body,
61+
name: oldName,
62+
draft: oldDraft,
63+
prerelease
64+
})
65+
66+
core.info(`Updated release with body: ${body}`)
67+
} catch (error) {
68+
core.setFailed(error.message)
69+
}
70+
}
71+
(async () => {
72+
await updateRelease();
73+
})();
74+
75+
module.exports = {
76+
updateRelease,
77+
};

.github/workflows/release.yml

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,31 +76,27 @@ jobs:
7676
env:
7777
GITHUB_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
7878
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
79-
update-release:
80-
runs-on: ubuntu-latest
81-
needs: [update-api-specs, release]
82-
steps:
83-
- name: Checkout cli-core repo
84-
uses: actions/checkout@v2
8579
- name: Update release
86-
if: ${{needs.release.outputs.tag-name != ''}}
87-
uses: tubone24/update_release@v1.2.0
80+
run: node .github/scripts/update-release.js
8881
env:
8982
GITHUB_TOKEN: ${{ github.token }}
90-
TAG_NAME: ${{needs.release.outputs.tag-name}}
91-
with:
92-
is_append_body: true
93-
body: ${{needs.update-api-specs.outputs.change-log}}
83+
TAG_NAME: ${{ steps.semantic-release.outputs.TAG_NAME }}
84+
RELEASE_BODY: ${{needs.update-api-specs.outputs.change-log}}
85+
REPO_NAME: twilio/twilio-cli-core
9486
triggerCliWorkflow:
9587
runs-on: ubuntu-latest
96-
needs: [ update-api-specs, update-release]
88+
needs: [ update-api-specs, release]
9789
steps:
90+
- name: Checkout cli-core repo
91+
uses: actions/checkout@v2
92+
- run: |
93+
git pull
94+
npm install
9895
- name: Invoke CLI workflow with changelog and version-type
99-
if: ${{toJSON(needs.update-api-specs.outputs.change-log) != null && needs.update-api-specs.outputs.version-type != -1}}
100-
uses: benc-uk/workflow-dispatch@v1
101-
with:
102-
workflow: Cli Release
103-
token: ${{ secrets.REPO_ACCESS_TOKEN }}
104-
repo: twilio/twilio-cli
105-
ref: ${{ github.event.inputs.cli-branch }}
106-
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}}" }'
96+
run: node .github/scripts/trigger-workflow.js
97+
env:
98+
REPO_ACCESS_TOKEN: ${{ secrets.REPO_ACCESS_TOKEN }}
99+
WORKFLOW_NAME: 'release.yml'
100+
BRANCH_NAME: ${{github.event.inputs.cli-branch}}
101+
REPO_NAME: twilio/twilio-cli
102+
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}}" }'

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@
4242
"twilio": "^3.54.2"
4343
},
4444
"devDependencies": {
45+
"@actions/core": "^1.0.0",
46+
"@actions/github": "^2.2.0",
4547
"@oclif/test": "^1.2.6",
48+
"@octokit/rest": "^18.10.0",
4649
"@semantic-release/changelog": "^5.0.1",
4750
"@semantic-release/exec": "^5.0.0",
4851
"@semantic-release/git": "^9.0.0",

src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ module.exports = {
1616
secureStorage: require('./services/secure-storage'),
1717
},
1818
configureEnv: require('./services/env'),
19+
releaseScripts: {
20+
UpdateRelease: require('../.github/scripts/update-release'),
21+
TriggerWorkflow: require('../.github/scripts/trigger-workflow'),
22+
},
1923
};

0 commit comments

Comments
 (0)