From 615fbaca87e42dab6f4d84fe77c2b99f82527498 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 8 Jun 2024 15:47:41 +0200 Subject: [PATCH] fix(vote): improve UX when posting comment fails (#814) When the token doesn't have the permission to post the comment directly, we can fallback to showing the user the error message and what command they should type. --- lib/voting_session.js | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/voting_session.js b/lib/voting_session.js index fb91996f..9a3f7977 100644 --- a/lib/voting_session.js +++ b/lib/voting_session.js @@ -114,7 +114,7 @@ export default class VotingSession extends Session { const body = 'I would like to close this vote, and for this effect, I\'m revealing my ' + `key part:\n\n${'```'}\n${keyPart}\n${'```'}\n`; if (this.postComment) { - const { html_url } = await this.req.json(`https://api.github.com/repos/${this.owner}/${this.repo}/issues/${this.prid}/comments`, { + const { message, html_url } = await this.req.json(`https://api.github.com/repos/${this.owner}/${this.repo}/issues/${this.prid}/comments`, { agent: this.req.proxyAgent, method: 'POST', headers: { @@ -124,13 +124,23 @@ export default class VotingSession extends Session { }, body: JSON.stringify({ body }) }); - this.cli.log('Comment posted at:', html_url); - } else if (isGhAvailable()) { + if (html_url) { + this.cli.log(`Comment posted at: ${html_url}`); + return; + } else { + this.cli.warn(message); + this.cli.error('Failed to post comment'); + } + } + if (isGhAvailable()) { this.cli.log('\nRun the following command to post the comment:\n'); this.cli.log( `gh pr comment ${this.prid} --repo ${this.owner}/${this.repo} ` + `--body-file - <<'EOF'\n${body}\nEOF` ); + } else { + this.cli.log('\nPost the following comment on the PR thread:\n'); + this.cli.log(body); } } }