move checking of repos whitelist to comments.js

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2019-02-07 00:05:52 +01:00
parent ec3969971c
commit 131fd4314e
No known key found for this signature in database
GPG Key ID: 4EF064D0E6D63020
5 changed files with 20 additions and 41 deletions

View File

@ -46,9 +46,10 @@ const extractArchiveBuilds = (builds) => {
}
class Comments {
constructor({client, owner, builds}) {
constructor({client, owner, repos, builds}) {
this.gh = client
this.db = builds
this.repos = repos /* whitelist of repos to which we post */
this.owner = owner /* name of user who makes the comments */
/* add helper for formatting dates */
Handlebars.registerHelper('date', dateHelper)
@ -101,6 +102,10 @@ class Comments {
}
async update ({repo, pr}) {
/* check if repo is in a whitelist */
if (!this.repos.includes(repo)) {
throw Error(`Repo not whitelisted: ${repo}`)
}
/* check if comment was already posted */
let comment_id = await this.db.getCommentID({repo, pr})
if (comment_id) {

View File

@ -1,17 +1,13 @@
const Joi = require('joi')
/* whitelisted repos are controlled by env variables in server.js */
const genSchema = (REPOS_WHITELIST) => (
Joi.object().keys({
id: Joi.alternatives().try(Joi.number().positive(), Joi.string()).required(),
commit: Joi.string().regex(/^[a-zA-Z0-9]{6,40}$/).required(),
repo: Joi.string().max(30).required().valid(REPOS_WHITELIST),
success: Joi.boolean().required(),
platform: Joi.string().max(20).required(),
duration: Joi.string().max(20).required(),
url: Joi.string().uri().required(),
pkg_url: Joi.string().uri().allow(null),
})
)
const schema = Joi.object().keys({
id: Joi.alternatives().try(Joi.number().positive(), Joi.string()).required(),
commit: Joi.string().regex(/^[a-zA-Z0-9]{6,40}$/).required(),
success: Joi.boolean().required(),
platform: Joi.string().max(20).required(),
duration: Joi.string().max(20).required(),
url: Joi.string().uri().required(),
pkg_url: Joi.string().uri().allow(null),
})
module.exports = genSchema
module.exports = schema

View File

@ -5,7 +5,7 @@ const Octokit = require('@octokit/rest')
const App = require('./app')
const Builds = require('./builds')
const Comments = require('./comments')
const Schema = require('./schema')
const schema = require('./schema')
/* DEFAULTS */
const LOG_LEVEL = process.env.LOG_LEVEL || 'INFO'
@ -25,12 +25,10 @@ const builds = new Builds(DB_PATH, DB_SAVE_INTERVAL)
/* necessary to post and update comments */
const gh = new Octokit({auth: `token ${GH_TOKEN}`})
/* set valid repo names */
const schema = Schema(GH_REPO_NAMES)
const ghc = new Comments({
client: gh,
owner: GH_REPO_OWNER,
repos: GH_REPO_NAMES,
builds: builds,
})
const app = App({ghc, schema})

View File

@ -2,7 +2,6 @@
const BUILD = {
id: 'ID-1',
commit: 'abcd1234',
repo: 'REPO-1',
success: true,
platform: 'PLATFORM-1',
duration: 'DURATION-1',
@ -13,7 +12,6 @@ const BUILD = {
const getBuild = (idx) => ({
id: `ID-${idx}`,
commit: `COMMIT-${Math.floor(idx/4)}`,
repo: `REPO-${Math.floor(idx/8)}`,
success: (idx%3) ? true : false,
platform: `PLATFORM-${idx}`,
duration: `DURATION-${idx} 12 sec`,

View File

@ -3,15 +3,14 @@ const sinon = require('sinon')
const Joi = require('joi')
const sample = require('./sample')
const Schema = require('../src/schema')
const schema = require('../src/schema')
let build, schema
let build
describe('Schema', () => {
beforeEach(() => {
/* refresh for every test */
build = Object.assign({}, sample.BUILD)
schema = Schema(['REPO-1'])
})
describe('id', () => {
@ -49,23 +48,6 @@ describe('Schema', () => {
})
})
describe('repo', () => {
it('has to be a repo', async () => {
let rval = await Joi.validate(build, schema)
expect(rval).to.eql(build)
})
it('can\'t be a null', () => {
build.repo = null
expect(Joi.validate(build, schema)).rejectedWith('"repo" must be a string')
})
it('has to be on whitelist', () => {
build.repo = 'REPO-WRONG'
expect(Joi.validate(build, schema)).rejectedWith('"repo" must be one of [REPO-1]')
})
})
describe('pkg_url', () => {
it('has to be a URL', async () => {
let rval = await Joi.validate(build, schema)