Skip to content

Commit

Permalink
feat: validate config options
Browse files Browse the repository at this point in the history
  • Loading branch information
dessant committed May 4, 2018
1 parent 195176e commit 2833846
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 26 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,18 @@ The file can be empty, or it can override any of these default settings:

# Label used to mark issues as support requests
supportLabel: support

# Comment to post on issues marked as support requests. Add a link
# to a support page, or set to `false` to disable
supportComment: >
👋 We use the issue tracker exclusively for bug reports and feature requests.
However, this issue appears to be a support request. Please use our
support channels to get help with the project.
# Whether to close issues marked as support requests
# Close issues marked as support requests
close: true
# Whether to lock issues marked as support requests

# Lock issues marked as support requests
lock: false
```
Expand Down
7 changes: 5 additions & 2 deletions assets/app-description.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ After installing the app, create `.github/support.yml` in the default branch to

# Label used to mark issues as support requests
supportLabel: support

# Comment to post on issues marked as support requests. Add a link
# to a support page, or set to `false` to disable
supportComment: >
We use the issue tracker exclusively for bug reports and feature requests.
However, this issue appears to be a support request. Please use our
support channels to get help with the project.
# Whether to close issues marked as support requests
# Close issues marked as support requests
close: true
# Whether to lock issues marked as support requests

# Lock issues marked as support requests
lock: false
```
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"release": "standard-version && yarn run push"
},
"dependencies": {
"joi": "^13.2.0",
"probot": "^3.0.3"
},
"devDependencies": {
Expand Down
10 changes: 0 additions & 10 deletions src/defaults.js

This file was deleted.

39 changes: 32 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,52 @@
const Support = require('./support');
const schema = require('./schema');

module.exports = robot => {
robot.on('issues.labeled', async context => {
const app = await getSupport(context);
await app.labeled();
if (app) {
await app.labeled();
}
});

robot.on('issues.unlabeled', async context => {
const app = await getSupport(context);
await app.unlabeled();
if (app) {
await app.unlabeled();
}
});

robot.on('issues.reopened', async context => {
const app = await getSupport(context);
await app.reopened();
if (app) {
await app.reopened();
}
});

async function getSupport(context) {
let config = await context.config('support.yml');
if (!config) {
config = {perform: false};
const config = await getConfig(context);
if (config) {
return new Support(context, config, robot.log);
}
}

async function getConfig(context) {
const {owner, repo} = context.issue();
let config;
try {
let repoConfig = await context.config('support.yml');
if (!repoConfig) {
repoConfig = {perform: false};
}
const {error, value} = schema.validate(repoConfig);
if (error) {
throw error;
}
config = value;
} catch (err) {
robot.log.warn({err: new Error(err), owner, repo}, 'Invalid config');
}

return new Support(context, config, robot.log);
return config;
}
};
32 changes: 32 additions & 0 deletions src/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const Joi = require('joi');

const schema = Joi.object().keys({
supportLabel: Joi.string()
.default('support')
.description('Label used to mark issues as support requests'),

supportComment: Joi.alternatives()
.try(Joi.string(), Joi.boolean().only(false))
.error(() => '"supportComment" must be a string or false')
.default(
'👋 We use the issue tracker exclusively for bug reports ' +
'and feature requests. However, this issue appears to be a support ' +
'request. Please use our support channels to get help with the project.'
)
.description(
'Comment to post on issues marked as support requests. ' +
'Add a link to a support page, or set to `false` to disable'
),

close: Joi.boolean()
.default(true)
.description('Close issues marked as support requests'),

lock: Joi.boolean()
.default(false)
.description('Lock issues marked as support requests'),

perform: Joi.boolean().default(!process.env.DRY_RUN)
});

module.exports = schema;
8 changes: 3 additions & 5 deletions src/support.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
const defaults = require('./defaults');

module.exports = class Support {
constructor(context, config, logger) {
constructor(context, config, logger = console) {
this.context = context;
this.config = Object.assign({}, defaults, config || {});
this.logger = logger || console;
this.config = config;
this.logger = logger;
}

hasLabel() {
Expand Down
28 changes: 28 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,10 @@ hoek@4.x.x:
version "4.2.0"
resolved "https://registry.yarnpkg.com/hoek/-/hoek-4.2.0.tgz#72d9d0754f7fe25ca2d01ad8f8f9a9449a89526d"

hoek@5.x.x:
version "5.0.3"
resolved "https://registry.yarnpkg.com/hoek/-/hoek-5.0.3.tgz#b71d40d943d0a95da01956b547f83c4a5b4a34ac"

home-or-tmp@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/home-or-tmp/-/home-or-tmp-2.0.0.tgz#e36c3f2d2cae7d746a857e38d18d5f32a7882db8"
Expand Down Expand Up @@ -2208,6 +2212,12 @@ isarray@1.0.0, isarray@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/isarray/-/isarray-1.0.0.tgz#bb935d48582cba168c06834957a54a3e07124f11"

isemail@3.x.x:
version "3.1.2"
resolved "https://registry.yarnpkg.com/isemail/-/isemail-3.1.2.tgz#937cf919002077999a73ea8b1951d590e84e01dd"
dependencies:
punycode "2.x.x"

isexe@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/isexe/-/isexe-2.0.0.tgz#e8fbf374dc556ff8947a10dcb0572d633f2cfa10"
Expand Down Expand Up @@ -2518,6 +2528,14 @@ jju@^1.1.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/jju/-/jju-1.3.0.tgz#dadd9ef01924bc728b03f2f7979bdbd62f7a2aaa"

joi@^13.2.0:
version "13.2.0"
resolved "https://registry.yarnpkg.com/joi/-/joi-13.2.0.tgz#72307f1765bb40b068361f9368a4ba1092b8478e"
dependencies:
hoek "5.x.x"
isemail "3.x.x"
topo "3.x.x"

js-tokens@^3.0.0, js-tokens@^3.0.2:
version "3.0.2"
resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-3.0.2.tgz#9866df395102130e38f7f996bceb65443209c25b"
Expand Down Expand Up @@ -3722,6 +3740,10 @@ punycode@1.3.2:
version "1.3.2"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.3.2.tgz#9653a036fb7c1ee42342f2325cceefea3926c48d"

punycode@2.x.x:
version "2.1.0"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.1.0.tgz#5f863edc89b96db09074bad7947bf09056ca4e7d"

punycode@^1.4.1:
version "1.4.1"
resolved "https://registry.yarnpkg.com/punycode/-/punycode-1.4.1.tgz#c0d5a63b2718800ad8e1eb0fa5269c84dd41845e"
Expand Down Expand Up @@ -4707,6 +4729,12 @@ toml@^2.3.2:
version "2.3.3"
resolved "https://registry.yarnpkg.com/toml/-/toml-2.3.3.tgz#8d683d729577cb286231dfc7a8affe58d31728fb"

topo@3.x.x:
version "3.0.0"
resolved "https://registry.yarnpkg.com/topo/-/topo-3.0.0.tgz#37e48c330efeac784538e0acd3e62ca5e231fe7a"
dependencies:
hoek "5.x.x"

tough-cookie@^2.3.2, tough-cookie@~2.3.0, tough-cookie@~2.3.3:
version "2.3.3"
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.3.3.tgz#0b618a5565b6dea90bf3425d04d55edc475a7561"
Expand Down

0 comments on commit 2833846

Please sign in to comment.