Files
shields/services/github/github-commit-status.service.js
dependabot-preview[bot] 478d14300c Build(deps-dev): bump eslint-plugin-import from 2.20.1 to 2.20.2 (#4859)
* Build(deps-dev): bump eslint-plugin-import from 2.20.1 to 2.20.2

Bumps [eslint-plugin-import](https://github.com/benmosher/eslint-plugin-import) from 2.20.1 to 2.20.2.
- [Release notes](https://github.com/benmosher/eslint-plugin-import/releases)
- [Changelog](https://github.com/benmosher/eslint-plugin-import/blob/master/CHANGELOG.md)
- [Commits](https://github.com/benmosher/eslint-plugin-import/compare/v2.20.1...v2.20.2)

Signed-off-by: dependabot-preview[bot] <support@dependabot.com>

* Fixes

* refactor: combine imports

* refactor: combine imports

* refactor: combine imports

* refactor: update import ordering

Co-authored-by: dependabot-preview[bot] <27856297+dependabot-preview[bot]@users.noreply.github.com>
Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
Co-authored-by: Paul Melnikow <email@paulmelnikow.com>
Co-authored-by: Caleb Cartwright <calebcartwright@users.noreply.github.com>
Co-authored-by: Caleb Cartwright <caleb.cartwright@outlook.com>
2020-04-16 18:39:13 -05:00

88 lines
2.2 KiB
JavaScript

'use strict'
const Joi = require('@hapi/joi')
const { NotFound, InvalidParameter } = require('..')
const { GithubAuthV3Service } = require('./github-auth-service')
const { documentation, errorMessagesFor } = require('./github-helpers')
const schema = Joi.object({
// https://stackoverflow.com/a/23969867/893113
status: Joi.equal('identical', 'ahead', 'behind', 'diverged'),
}).required()
module.exports = class GithubCommitStatus extends GithubAuthV3Service {
static get category() {
return 'issue-tracking'
}
static get route() {
return {
base: 'github/commit-status',
pattern: ':user/:repo/:branch/:commit',
}
}
static get examples() {
return [
{
title: 'GitHub commit merge status',
namedParams: {
user: 'badges',
repo: 'shields',
branch: 'master',
commit: '5d4ab86b1b5ddfb3c4a70a70bd19932c52603b8c',
},
staticPreview: this.render({
isInBranch: true,
branch: 'master',
}),
keywords: ['branch'],
documentation,
},
]
}
static get defaultBadgeData() {
return {
label: 'commit status',
}
}
static render({ isInBranch, branch }) {
if (isInBranch) {
return {
message: `in ${branch}`,
color: 'brightgreen',
}
} else {
// status: ahead or diverged
return {
message: `not in ${branch}`,
color: 'yellow',
}
}
}
async handle({ user, repo, branch, commit }) {
let status
try {
;({ status } = await this._requestJson({
url: `/repos/${user}/${repo}/compare/${branch}...${commit}`,
errorMessages: errorMessagesFor('commit or branch not found'),
schema,
}))
} catch (e) {
if (e instanceof NotFound) {
const { message } = this._parseJson(e.buffer)
if (message && message.startsWith('No common ancestor between')) {
throw new InvalidParameter({ prettyMessage: 'no common ancestor' })
}
}
throw e
}
const isInBranch = status === 'identical' || status === 'behind'
return this.constructor.render({ isInBranch, branch })
}
}