diff --git a/action.yml b/action.yml index fa781199..2b5bde96 100644 --- a/action.yml +++ b/action.yml @@ -4,6 +4,8 @@ author: 'Jason Miller (https://github.com/developit)' runs: using: 'node12' main: 'action.min.js' +env: + FIREBASE_CLI_PREVIEWS: hostingchannels inputs: repo-token: description: 'The GITHUB_TOKEN secret' @@ -17,4 +19,7 @@ inputs: description: 'Use .web.app suffix instead of .firebaseapp.com?' channel-ttl: description: 'How long should a preview live?' - default: '7d' \ No newline at end of file + default: '7d' + site-id: + description: 'The id of your hosting site (if you do not have multiple sites configured, this will be your Firebase project id)' + required: true \ No newline at end of file diff --git a/index.js b/index.js index cf0822cd..f28e51d8 100644 --- a/index.js +++ b/index.js @@ -24,9 +24,9 @@ async function run(github, context) { '--no-package-lock', 'https://storage.googleapis.com/firebase-preview-drop/node/firebase-tools/firebase-tools-7.13.0-hostingpreviews.1.tgz' ]); - await exec(firebase, ['--open-sesame', 'hostingchannels']); endGroup(); + const siteId = getInput('site-id'); const previewId = context.payload.pull_request.head.sha.substring(0, 7); const channelId = `pr${context.payload.pull_request.number}-${previewId}`; const channelTTL = getInput('channel-ttl'); @@ -49,6 +49,20 @@ async function run(github, context) { } }); const deploymentText = Buffer.concat(buf).toString('utf-8'); + + /** + * The deployment object looks like this: + * { + * "status": "success", + * "result": { + * "": { + * "site": "", + * "url": "", + * "expireTime": "" + * } + * } + * } + */ const deployment = JSON.parse(deploymentText); endGroup(); @@ -56,11 +70,7 @@ async function run(github, context) { throw Error(deploymentText); } - let url = deployment.result.channels.url; - - if (getInput('use-web-tld')) { - url = url.replace(/\.firebaseapp\.com$/, '.web.app'); - } + let url = deployment.result[siteId].url; setOutput('details_url', url); setOutput('target_url', url); @@ -103,8 +113,8 @@ async function run(github, context) { summary: `[${result.url}](${result.url})` } }); - } catch (e) { - setFailed(e.message); + } catch (e) { + setFailed(e.message); await finish({ conclusion: 'failure', @@ -141,49 +151,49 @@ async function createCheck(github, context) { // create a PR comment, or update one if it already exists async function postOrUpdateComment(github, context, commentMarkdown) { const commentInfo = { - ...context.repo, - issue_number: context.issue.number - }; - - const comment = { - ...commentInfo, - body: commentMarkdown + '\n\nfirebase-hosting-preview-action' - }; - - startGroup(`Updating PR comment`); - let commentId; - try { - const comments = (await github.issues.listComments(commentInfo)).data; - for (let i=comments.length; i--; ) { - const c = comments[i]; - if (c.user.type === 'Bot' && /[\s\n]*firebase-hosting-preview-action/.test(c.body)) { - commentId = c.id; - break; - } - } - } - catch (e) { - console.log('Error checking for previous comments: ' + e.message); - } - - if (commentId) { - try { - await github.issues.updateComment({ - ...context.repo, - comment_id: commentId, - body: comment.body - }); - } - catch (e) { - commentId = null; - } - } - - if (!commentId) { - try { - await github.issues.createComment(comment); - } catch (e) { - console.log(`Error creating comment: ${e.message}`); + ...context.repo, + issue_number: context.issue.number + }; + + const comment = { + ...commentInfo, + body: commentMarkdown + '\n\nfirebase-hosting-preview-action' + }; + + startGroup(`Updating PR comment`); + let commentId; + try { + const comments = (await github.issues.listComments(commentInfo)).data; + for (let i = comments.length; i--;) { + const c = comments[i]; + if (c.user.type === 'Bot' && /[\s\n]*firebase-hosting-preview-action/.test(c.body)) { + commentId = c.id; + break; + } + } + } + catch (e) { + console.log('Error checking for previous comments: ' + e.message); + } + + if (commentId) { + try { + await github.issues.updateComment({ + ...context.repo, + comment_id: commentId, + body: comment.body + }); + } + catch (e) { + commentId = null; + } + } + + if (!commentId) { + try { + await github.issues.createComment(comment); + } catch (e) { + console.log(`Error creating comment: ${e.message}`); } } endGroup();