Skip to content

Commit 6c2ea52

Browse files
authored
Merge pull request #6 from pinanks/DOT-945
DOT-945
2 parents f57baee + bc90add commit 6c2ea52

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

commands/utils/constants.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ var constants = {}
33
constants.stage = {
44
AUTH_URL: "https://stage-api.lambdatestinternal.com/storybook/auth",
55
RENDER_API_URL: "https://stage-api.lambdatestinternal.com/storybook/render",
6-
BUILD_STATUS_URL: "https://stage-api.lambdatestinternal.com/storybook/status"
6+
BUILD_STATUS_URL: "https://stage-api.lambdatestinternal.com/storybook/status",
7+
BASE_URL: "https://stage-api.lambdatestinternal.com",
8+
SB_BUILD_VALIDATE_PATH: "/storybook/validate"
79
};
810
constants.prod = {
911
AUTH_URL: "https://api.lambdatest.com/storybook/auth",

commands/utils/dom.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ async function sendDoM(storybookUrl, stories, storybookConfig, options) {
4343
await browser.close()
4444

4545
// Create form
46-
// let commit = await getLastCommit();
46+
let commit = await getLastCommit();
4747
const form = new formData();
4848
for (const [storyId, storyInfo] of Object.entries(stories)) {
4949
const file = fs.readFileSync('doms/' + storyId + '.html');
@@ -52,11 +52,10 @@ async function sendDoM(storybookUrl, stories, storybookConfig, options) {
5252
form.append('resolution', storybookConfig.resolutions);
5353
form.append('browser', storybookConfig.browsers);
5454
form.append('projectToken', process.env.PROJECT_TOKEN);
55-
// form.append('buildName', options.buildname);
56-
// form.append('branch', commit.branch);
57-
// form.append('commitId', commit.shortHash);
58-
// form.append('commitAuthor', commit.author.name);
59-
// form.append('commitMessage', commit.subject);
55+
form.append('branch', commit.branch);
56+
form.append('commitId', commit.shortHash);
57+
form.append('commitAuthor', commit.author.name);
58+
form.append('commitMessage', commit.subject);
6059

6160
// Send DOM to render API
6261
await axios.post(constants[options.env].RENDER_API_URL, form, {

commands/utils/validate.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const axios = require('axios');
22
var { constants } = require('./constants');
33
const fs = require('fs');
4+
const { getLastCommit } = require('./git');
45

56
function validateProjectToken(options) {
67
if (process.env.PROJECT_TOKEN) {
@@ -60,4 +61,28 @@ async function validateStorybookDir(dir) {
6061
}
6162
};
6263

63-
module.exports = { validateProjectToken, validateStorybookUrl, validateStorybookDir };
64+
async function validateLatestBuild(options) {
65+
let commit = await getLastCommit();
66+
return axios.get(new URL(constants[options.env].SB_BUILD_VALIDATE_PATH, constants[options.env].BASE_URL).href, {
67+
headers: {
68+
projectToken: process.env.PROJECT_TOKEN
69+
},
70+
params: {
71+
branch: commit.branch,
72+
commitId: commit.shortHash
73+
}})
74+
.then(function (response) {
75+
if (response.data.status === 'Failure') {
76+
console.log(`[smartui] Build with commit '${commit.shortHash}' on branch '${commit.branch}' already exists.`);
77+
console.log('[smartui] Use option --force-rebuild to forcefully push a new build.');
78+
process.exit(0);
79+
}
80+
})
81+
.catch(function (error) {
82+
// TODO: Add retries
83+
console.log('[smartui] Error: ', error.message);
84+
process.exit(1);
85+
});
86+
}
87+
88+
module.exports = { validateProjectToken, validateStorybookUrl, validateStorybookDir, validateLatestBuild };

index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
const { Command, Option } = require('commander');
44
const program = new Command();
55
const { storybook } = require('./commands/storybook');
6-
const { validateProjectToken } = require('./commands/utils/validate');
6+
const { validateProjectToken, validateLatestBuild } = require('./commands/utils/validate');
77
const { createConfig } = require('./commands/config');
88

99
program
@@ -26,10 +26,12 @@ program.command('storybook')
2626
.description('Snapshot Storybook stories')
2727
.argument('<url|directory>', 'Storybook url or static build directory')
2828
.option('-c --config <file>', 'Config file path')
29+
.option('--force-rebuild', 'Force a rebuild of an already existing build.', false)
2930
// .option('--env <prod|stage>', 'Runtime environment option', 'prod')
3031
.action(async function(serve, options) {
3132
options.env = program.opts().env || 'prod';
3233
await validateProjectToken(options);
34+
if (!options.forceRebuild) await validateLatestBuild(options);
3335
storybook(serve, options);
3436
});
3537

0 commit comments

Comments
 (0)