Skip to content

Commit

Permalink
Add GH commit check badge
Browse files Browse the repository at this point in the history
  • Loading branch information
itsvs committed Dec 22, 2020
1 parent 91b108d commit 7e4ce81
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
73 changes: 73 additions & 0 deletions services/github/github-commit-checks.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
'use strict'

const Joi = require('joi')
const { NotFound, InvalidParameter } = require('..')
const { GithubAuthV3Service } = require('./github-auth-service')
const { documentation, errorMessagesFor } = require('./github-helpers')

const schema = Joi.object({
state: Joi.equal('failure', 'pending', 'success'),
}).required()

module.exports = class GithubCommitStatus extends GithubAuthV3Service {
static category = 'build'
static route = {
base: 'github/commit-checks',
pattern: ':user/:repo/:ref',
}

static examples = [
{
title: 'GitHub commit checks state',
namedParams: {
user: 'badges',
repo: 'shields',
ref: 'master',
},
staticPreview: this.render({
state: 'success',
}),
keywords: ['branch'],
documentation,
},
]

static defaultBadgeData = { label: 'checks' }

static render({ state }) {
if (state === "success") {
return {
message: `passing`,
color: 'brightgreen',
}
} else if (state === "pending") {
return {
message: `pending`,
color: 'yellow',
}
} else {
return {
message: `failing`,
color: 'red',
}
}
}

async handle({ user, repo, ref }) {
let state
try {
;({ state } = await this._requestJson({
url: `/repos/${user}/${repo}/commits/${ref}/status`,
errorMessages: errorMessagesFor('ref not found'),
schema,
}))
} catch (e) {
if (e instanceof NotFound) {
throw new InvalidParameter({ prettyMessage: 'invalid ref' })
}
throw e
}

return this.constructor.render({ state })
}
}
28 changes: 28 additions & 0 deletions services/github/github-commit-checks.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
'use strict'

const { invalidJSONString } = require('../response-fixtures')
const t = (module.exports = require('../tester').createServiceTester())

t.create('commit checks - passing')
.get('/badges/shields/5b31a2c692cf999bd499420086b537479efc93e7.json')
.expectBadge({
label: 'checks',
message: 'passing',
color: 'brightgreen',
})

t.create('commit checks - failing')
.get('/badges/shields/91b108d4b7359b2f8794a4614c11cb1157dc9fff.json')
.expectBadge({
label: 'checks',
message: 'failing',
color: 'red',
})

t.create('commit checks - nonexistent ref')
.get('/badges/shields/this-ref-does-not-exist.json')
.expectBadge({
label: 'checks',
message: 'ref not found',
color: 'red',
})

0 comments on commit 7e4ce81

Please sign in to comment.