Skip to content

Commit b8802f8

Browse files
steveohverythorough
authored andcommitted
Interactive Gulp task for incident creation (#34)
* Add inquirer to get some prompts * Create some questions Refs #16 * Add tomljs to read config for choices * Kebab-case the input file name * Fix listing errors * Lint all relevant js files * Add new incident npm script * Set as default so out of the box this just works * refactor out args that are used everywhere * Add inquirer validation * Handle inquirer answers * Create toml from answers * Fix linting * Treat title and file name separately This allows for Hugo new to work and enhances the gulp task but breaks TranslationBaseName e.g.: `post` for `post.es.md` (if `Multilingual` is enabled.) which I’m not sure is enabled by default. * add comment to replace * update new issue creation * fix spelling
1 parent fef087b commit b8802f8

File tree

5 files changed

+133
-12
lines changed

5 files changed

+133
-12
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,10 @@ Incidents are plain markdown files inside the `site/content/incidents` directory
4141
### Creating new incidents
4242

4343
Adding incidents to your status page is as simple as adding a new document to the incidents collection.
44-
Create a new incident using Hugo with a command like this as of Hugo v0.24:
44+
Create a new incident using npm:
4545

4646
```
47-
cd site
48-
hugo new incidents/oh-no-something-went-wrong.md
47+
npm run new-incident
4948
```
5049

5150
Hugo will create a new Markdown file for you with title and date based on the file name and a few predefined settings in the header. To learn more about the different severities and report, you can see more detailed examples in `site/archetypes/incidents.md`.

gulpfile.babel.js

Lines changed: 120 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,17 @@ import cssnext from "postcss-cssnext";
77
import BrowserSync from "browser-sync";
88
import webpack from "webpack";
99
import webpackConfig from "./webpack.conf";
10+
import inquirer from "inquirer";
11+
import toml from "tomljs";
12+
import fs from "fs";
13+
import path from "path";
14+
import kebabCase from "lodash.kebabcase";
15+
import tomlify from "tomlify-j0.4";
1016

1117
const browserSync = BrowserSync.create();
1218
const hugoBin = `./bin/hugo_0.26_${process.platform}_amd64${process.platform === "windows" ? ".exe" : ""}`;
13-
const defaultArgs = ["-d", "../dist", "-s", "site", "-v"];
19+
const defaultArgs = ["-s", "site", "-v"];
20+
const buildArgs = ["-d", "../dist"];
1421

1522
gulp.task("hugo", (cb) => buildSite(cb));
1623
gulp.task("hugo-preview", (cb) => buildSite(cb, ["--buildDrafts", "--buildFuture"]));
@@ -50,9 +57,120 @@ gulp.task("server", ["hugo", "css", "js"], () => {
5057
gulp.watch("./site/**/*", ["hugo"]);
5158
});
5259

60+
gulp.task("new-incident", (cb) => {
61+
const file = fs.readFileSync("site/config.toml").toString();
62+
const config = toml(file);
63+
64+
const questions = [{
65+
type: "input",
66+
name: "name",
67+
message: "What is the cause of the incident?",
68+
validate: (value) => {
69+
if (value.length > 0) {
70+
return true;
71+
}
72+
73+
return "You must have a cause title!";
74+
}
75+
}, {
76+
type: "list",
77+
name: "severity",
78+
message: "What is the severity of the incident?",
79+
choices: ["under-maintenance", "degraded-performance", "partial-outage", "major-outage"]
80+
}, {
81+
type: "checkbox",
82+
name: "affected",
83+
message: "What are the affected systems?",
84+
choices: config.params.systems,
85+
validate: (value) => {
86+
if (value.length > 0) {
87+
return true;
88+
}
89+
90+
return "You must have an affected system?!";
91+
}
92+
}, {
93+
type: "input",
94+
name: "description",
95+
message: "Add a terse description of the incident"
96+
}, {
97+
type: "confirm",
98+
name: "open",
99+
message: "Open the incident for editing?",
100+
default: false
101+
}];
102+
103+
inquirer.prompt(questions).then((answers) => {
104+
let args = ["new", `incidents${path.sep}${kebabCase(answers.name)}.md`];
105+
args = args.concat(defaultArgs);
106+
107+
const hugo = cp.spawn(hugoBin, args, {stdio: "pipe"});
108+
hugo.stdout.on("data", (data) => {
109+
const message = data.toString();
110+
111+
if (message.indexOf(" created") === -1) {
112+
return;
113+
}
114+
115+
const path = message.split(" ")[0];
116+
117+
const incident = fs.readFileSync(path).toString();
118+
const frontMatter = toml(incident);
119+
120+
frontMatter.severity = answers.severity;
121+
frontMatter.affectedsystems = answers.affected;
122+
frontMatter.title = answers.name.replace(/-/g, " ");
123+
124+
const content = generateFrontMatter(frontMatter, answers);
125+
126+
fs.writeFileSync(path, content);
127+
128+
if (!answers.open) {
129+
return;
130+
}
131+
132+
let cmd = "xdg-open";
133+
switch (process.platform) {
134+
case "darwin": {
135+
cmd = "open";
136+
break;
137+
}
138+
case "win32":
139+
case "win64": {
140+
cmd = "start";
141+
break;
142+
}
143+
default: {
144+
cmd = "xdg-open";
145+
break;
146+
}
147+
}
148+
149+
cp.exec(`${cmd} ${path}`);
150+
});
151+
152+
hugo.on("close", (code) => {
153+
if (code === 0) {
154+
cb();
155+
} else {
156+
cb("new incident creation failed");
157+
}
158+
});
159+
});
160+
});
161+
162+
function generateFrontMatter(frontMatter, answers) {
163+
return `+++
164+
${tomlify(frontMatter, null, 2)}
165+
+++
166+
${answers.description}`;
167+
}
168+
53169
function buildSite(cb, options) {
54-
const args = options ? defaultArgs.concat(options) : defaultArgs;
170+
let args = options ? defaultArgs.concat(options) : defaultArgs;
171+
args = args.concat(buildArgs);
55172

173+
// cp needs to be in site directory
56174
return cp.spawn(hugoBin, args, {stdio: "inherit"}).on("close", (code) => {
57175
if (code === 0) {
58176
browserSync.reload();

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
"build": "gulp build",
1010
"build-preview": "gulp build-preview",
1111
"start": "gulp server",
12-
"lint": "eslint src"
12+
"lint": "eslint src gulpfile.babel.js webpack.conf.js",
13+
"new-incident": "gulp new-incident"
1314
},
1415
"author": "",
1516
"license": "MIT",
@@ -34,9 +35,13 @@
3435
"gulp-postcss": "^6.1.1",
3536
"gulp-util": "^3.0.7",
3637
"imports-loader": "^0.6.5",
38+
"inquirer": "^3.2.3",
39+
"lodash.kebabcase": "^4.1.1",
3740
"postcss-cssnext": "^2.7.0",
3841
"postcss-import": "^8.1.2",
3942
"postcss-loader": "^0.9.1",
43+
"tomlify-j0.4": "^2.2.0",
44+
"tomljs": "^0.1.3",
4045
"url-loader": "^0.5.7",
4146
"webpack": "^1.13.1",
4247
"whatwg-fetch": "^1.0.0"

site/config.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@ title = "StatusKit"
1212
# List of systems monitored in this status page.
1313
# You'll be able to change their status every time
1414
# you open or update an incident.
15-
# Example:
16-
# systems = ["API", "CDN", "DNS", "Site delivery"]
17-
systems = []
15+
# Replace these examples with your own system names.
16+
systems = ["API", "CDN", "DNS", "Site delivery"]

src/js/app.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
const daysSince = document.getElementById("days-since-latest");
33

44
if (daysSince) {
5-
const aDay = 1000*60*60*24;
5+
const aDay = 1000 * 60 * 60 * 24;
66

77
const dateSince = new Date(daysSince.getAttribute("data-latest-incident-date"));
88
const now = new Date();
99

1010
const timeSince = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()) -
11-
new Date(dateSince.getUTCFullYear(), dateSince.getUTCMonth(), dateSince.getUTCDate());
11+
new Date(dateSince.getUTCFullYear(), dateSince.getUTCMonth(), dateSince.getUTCDate());
1212
const endDays = Math.floor(timeSince / aDay);
1313

1414
const count = endDays === 1 ? `${endDays} day` : `${endDays} days`;
15-
daysSince.innerHTML = `${count} since last incident`
15+
daysSince.innerHTML = `${count} since last incident`;
1616
}

0 commit comments

Comments
 (0)