Files
shields/services/github/github-pull-request-check-state.service.js
dependabot[bot] b9d96755ec chore(deps-dev): bump prettier from 2.8.8 to 3.0.0 (#9357)
* chore(deps-dev): bump prettier from 2.8.8 to 3.0.0

Bumps [prettier](https://github.com/prettier/prettier) from 2.8.8 to 3.0.0.
- [Release notes](https://github.com/prettier/prettier/releases)
- [Changelog](https://github.com/prettier/prettier/blob/main/CHANGELOG.md)
- [Commits](https://github.com/prettier/prettier/compare/2.8.8...3.0.0)

---
updated-dependencies:
- dependency-name: prettier
  dependency-type: direct:development
  update-type: version-update:semver-major
...

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

* reformat all the things (prettier 3)

* update tests to await calls to prettier.format()

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: chris48s <git@chris-shaw.dev>
2023-07-10 09:27:51 +00:00

102 lines
2.6 KiB
JavaScript

import Joi from 'joi'
import countBy from 'lodash.countby'
import { GithubAuthV3Service } from './github-auth-service.js'
import { fetchIssue } from './github-common-fetch.js'
import { documentation, httpErrorsFor } from './github-helpers.js'
const schema = Joi.object({
state: Joi.equal('failure', 'pending', 'success').required(),
statuses: Joi.array()
.items(
Joi.object({
state: Joi.equal('error', 'failure', 'pending', 'success').required(),
}),
)
.default([]),
}).required()
const keywords = ['pullrequest', 'detail']
export default class GithubPullRequestCheckState extends GithubAuthV3Service {
static category = 'build'
static route = {
base: 'github/status',
pattern: ':variant(s|contexts)/pulls/:user/:repo/:number(\\d+)',
}
static examples = [
{
title: 'GitHub pull request check state',
pattern: 's/pulls/:user/:repo/:number',
namedParams: {
user: 'badges',
repo: 'shields',
number: '1110',
},
staticPreview: this.render({ variant: 's', state: 'pending' }),
keywords,
documentation,
},
{
title: 'GitHub pull request check contexts',
pattern: 'contexts/pulls/:user/:repo/:number',
namedParams: {
user: 'badges',
repo: 'shields',
number: '1110',
},
staticPreview: this.render({
variant: 'contexts',
state: 'pending',
stateCounts: { passed: 5, pending: 1 },
}),
keywords,
documentation,
},
]
static defaultBadgeData = { label: 'checks', namedLogo: 'github' }
static render({ variant, state, stateCounts }) {
let message
if (variant === 'contexts') {
message = Object.entries(stateCounts)
.map(([state, count]) => `${count} ${state}`)
.join(', ')
} else {
message = state
}
const color = {
pending: 'dbab09',
success: '2cbe4e',
failure: 'cb2431',
}[state]
return { message, color }
}
static transform({ state, statuses }) {
return {
state,
stateCounts: countBy(statuses, 'state'),
}
}
async handle({ variant, user, repo, number }) {
const {
head: { sha: ref },
} = await fetchIssue(this, { user, repo, number })
// https://developer.github.com/v3/repos/statuses/#get-the-combined-status-for-a-specific-ref
const json = await this._requestJson({
schema,
url: `/repos/${user}/${repo}/commits/${ref}/status`,
httpErrors: httpErrorsFor('commit not found'),
})
const { state, stateCounts } = this.constructor.transform(json)
return this.constructor.render({ variant, state, stateCounts })
}
}