Skip to content

Commit

Permalink
use await instead of then (#10)
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Hall <[email protected]>
  • Loading branch information
imjasonh authored Jan 29, 2024
1 parent a6b2520 commit 6177b44
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 27 deletions.
36 changes: 18 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ if (!scope || !identity) {
process.exit(1);
}

fetch(`${actionsUrl}&audience=octo-sts.dev`, { headers: { 'Authorization': `Bearer ${actionsToken}` } })
.then(res => {
res.json()
.then(json => {
fetch(`https://octo-sts.dev/sts/exchange?scope=${scope}&identity=${identity}`, { headers: { 'Authorization': `Bearer ${json.value}` } })
.then(res => res.json()
.then(json => {
if (!json.token) { console.log(`::error::${json.message}`); process.exit(1); }
const tok = json.token;
console.log(`::add-mask::${tok}`);
const fs = require('fs');
fs.appendFile(process.env.GITHUB_OUTPUT, `token=${tok}`, function (err) { if (err) throw err; }); // Write the output.
fs.appendFile(process.env.GITHUB_STATE, `token=${tok}`, function (err) { if (err) throw err; }); // Write the state, so the post job can delete the token.
})
)
})
})
.catch(err => { console.log(`::error::${err.stack}`); process.exit(1); });
(async function main() {
// You can use await inside this function block
try {
const res = await fetch(`${actionsUrl}&audience=octo-sts.dev`, { headers: { 'Authorization': `Bearer ${actionsToken}` } });
const json = await res.json();
const res2 = await fetch(`https://octo-sts.dev/sts/exchange?scope=${scope}&identity=${identity}`, { headers: { 'Authorization': `Bearer ${json.value}` } });
const json2 = await res2.json();

if (!json2.token) { console.log(`::error::${json2.message}`); process.exit(1); }
const tok = json2.token;
console.log(`::add-mask::${tok}`);
const fs = require('fs');
fs.appendFile(process.env.GITHUB_OUTPUT, `token=${tok}`, function (err) { if (err) throw err; }); // Write the output.
fs.appendFile(process.env.GITHUB_STATE, `token=${tok}`, function (err) { if (err) throw err; }); // Write the state, so the post job can delete the token.
} catch (err) {
console.log(`::error::${err.stack}`); process.exit(1);
}
})();
23 changes: 14 additions & 9 deletions post.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
const tok = process.env.STATE_token;

if (!tok) {
console.log(`::warning::Token not found in state file.`);
console.log(`::warning::Token not found in state; nothing to revoke.`);
process.exit(0);
}

fetch('https://api.github.com/installation/token', {
method: 'DELETE',
headers: { 'Authorization': `Bearer ${tok}` },
})
.then(res => {
(async function main() {
try {
const res = await fetch('https://api.github.com/installation/token', {
method: 'DELETE',
headers: { 'Authorization': `Bearer ${tok}` },
});

if (res.status == 204) {
console.log('::warning::Token was revoked!');
console.log('Token was revoked!');
} else {
console.log(`::error::${res.status} ${res.statusText}`);
}
})
.catch(err => { console.log(`::error::${err}`); });
} catch (err) {
console.log(`::error::${err.stack}`);
}

})();

0 comments on commit 6177b44

Please sign in to comment.