Skip to content

Commit

Permalink
add a site variable
Browse files Browse the repository at this point in the history
  • Loading branch information
jhuleatt committed Jun 12, 2020
1 parent d4b8384 commit 2147ba3
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 52 deletions.
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'
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
112 changes: 61 additions & 51 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -49,18 +49,28 @@ async function run(github, context) {
}
});
const deploymentText = Buffer.concat(buf).toString('utf-8');

/**
* The deployment object looks like this:
* {
* "status": "success",
* "result": {
* "<site-id>": {
* "site": "<site-id>",
* "url": "",
* "expireTime": ""
* }
* }
* }
*/
const deployment = JSON.parse(deploymentText);
endGroup();

if (deployment.status !== 'success') {
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);
Expand Down Expand Up @@ -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',
Expand Down Expand Up @@ -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\n<sub>firebase-hosting-preview-action</sub>'
};

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' && /<sub>[\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\n<sub>firebase-hosting-preview-action</sub>'
};

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' && /<sub>[\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();
Expand Down

0 comments on commit 2147ba3

Please sign in to comment.