|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const Joi = require('@hapi/joi') |
| 4 | +const { renderBuildStatusBadge } = require('../build-status') |
| 5 | +const { nonNegativeInteger } = require('../validators') |
| 6 | +const { GithubAuthV3Service } = require('./github-auth-service') |
| 7 | +const { documentation, errorMessagesFor } = require('./github-helpers') |
| 8 | +const { NotFound } = require('..') |
| 9 | + |
| 10 | +const schema = Joi.object({ |
| 11 | + total_count: nonNegativeInteger, |
| 12 | + check_suites: Joi.array() |
| 13 | + .items( |
| 14 | + Joi.object({ |
| 15 | + status: Joi.string().required(), |
| 16 | + conclusion: Joi.string() |
| 17 | + .required() |
| 18 | + .allow(null), |
| 19 | + app: Joi.object({ |
| 20 | + id: nonNegativeInteger, |
| 21 | + name: Joi.string().required(), |
| 22 | + }).required(), |
| 23 | + }) |
| 24 | + ) |
| 25 | + .min(0) |
| 26 | + .required(), |
| 27 | +}).required() |
| 28 | + |
| 29 | +module.exports = class GitHubActions extends GithubAuthV3Service { |
| 30 | + static get category() { |
| 31 | + return 'build' |
| 32 | + } |
| 33 | + |
| 34 | + static get route() { |
| 35 | + return { |
| 36 | + base: 'github/actions', |
| 37 | + pattern: ':user/:repo/:ref', |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + static get examples() { |
| 42 | + return [ |
| 43 | + { |
| 44 | + title: 'GitHub Actions', |
| 45 | + namedParams: { |
| 46 | + user: 'actions', |
| 47 | + repo: 'setup-node', |
| 48 | + ref: 'master', |
| 49 | + }, |
| 50 | + staticPreview: renderBuildStatusBadge({ status: 'success' }), |
| 51 | + documentation: ` |
| 52 | + You can use a branch name, tag, or commit SHA for the ref parameter. |
| 53 | + ${documentation} |
| 54 | + `, |
| 55 | + }, |
| 56 | + ] |
| 57 | + } |
| 58 | + |
| 59 | + static get defaultBadgeData() { |
| 60 | + return { |
| 61 | + label: 'workflow', |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + static render({ status, conclusion }) { |
| 66 | + if (status !== 'completed') { |
| 67 | + return { |
| 68 | + message: 'in progress', |
| 69 | + } |
| 70 | + } |
| 71 | + return renderBuildStatusBadge({ status: conclusion }) |
| 72 | + } |
| 73 | + |
| 74 | + async fetch({ user, repo, ref }) { |
| 75 | + return this._requestJson({ |
| 76 | + url: `/repos/${user}/${repo}/commits/${ref}/check-suites`, |
| 77 | + options: { |
| 78 | + headers: { |
| 79 | + Accept: 'application/vnd.github.antiope-preview+json', |
| 80 | + }, |
| 81 | + }, |
| 82 | + schema, |
| 83 | + errorMessages: errorMessagesFor('repo or ref not found'), |
| 84 | + }) |
| 85 | + } |
| 86 | + |
| 87 | + transform(json) { |
| 88 | + if (json.total_count === 0) { |
| 89 | + throw new NotFound({ prettyMessage: 'no GitHub Actions found' }) |
| 90 | + } |
| 91 | + |
| 92 | + const actionsSuite = json.check_suites.find( |
| 93 | + suite => suite.app.name.toLowerCase() === 'github actions' |
| 94 | + ) |
| 95 | + if (!actionsSuite) { |
| 96 | + throw new NotFound({ prettyMessage: 'no GitHub Actions found' }) |
| 97 | + } |
| 98 | + |
| 99 | + return { status: actionsSuite.status, conclusion: actionsSuite.conclusion } |
| 100 | + } |
| 101 | + |
| 102 | + async handle({ user, repo, ref }) { |
| 103 | + const json = await this.fetch({ user, repo, ref }) |
| 104 | + const { status, conclusion } = this.transform(json) |
| 105 | + return this.constructor.render({ status, conclusion }) |
| 106 | + } |
| 107 | +} |
0 commit comments