|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const Joi = require('@hapi/joi') |
| 4 | +const { isBuildStatus, renderBuildStatusBadge } = require('../build-status') |
| 5 | +const { documentation } = require('./github-helpers') |
| 6 | +const { BaseSvgScrapingService } = require('..') |
| 7 | + |
| 8 | +const schema = Joi.object({ |
| 9 | + message: Joi.alternatives() |
| 10 | + .try(isBuildStatus, Joi.equal('no status')) |
| 11 | + .required(), |
| 12 | +}).required() |
| 13 | + |
| 14 | +const keywords = ['action', 'actions'] |
| 15 | + |
| 16 | +module.exports = class GithubWorkflowStatus extends BaseSvgScrapingService { |
| 17 | + static get category() { |
| 18 | + return 'build' |
| 19 | + } |
| 20 | + |
| 21 | + static get route() { |
| 22 | + return { |
| 23 | + base: 'github/workflow/status', |
| 24 | + pattern: ':user/:repo/:workflow/:branch*', |
| 25 | + } |
| 26 | + } |
| 27 | + |
| 28 | + static get examples() { |
| 29 | + return [ |
| 30 | + { |
| 31 | + title: 'GitHub Workflow Status', |
| 32 | + pattern: ':user/:repo/:workflow', |
| 33 | + namedParams: { |
| 34 | + user: 'actions', |
| 35 | + repo: 'toolkit', |
| 36 | + workflow: 'Main workflow', |
| 37 | + }, |
| 38 | + staticPreview: renderBuildStatusBadge({ |
| 39 | + status: 'passing', |
| 40 | + }), |
| 41 | + documentation, |
| 42 | + keywords, |
| 43 | + }, |
| 44 | + { |
| 45 | + title: 'GitHub Workflow Status (branch)', |
| 46 | + pattern: ':user/:repo/:workflow/:branch', |
| 47 | + namedParams: { |
| 48 | + user: 'actions', |
| 49 | + repo: 'toolkit', |
| 50 | + workflow: 'Main workflow', |
| 51 | + branch: 'master', |
| 52 | + }, |
| 53 | + staticPreview: renderBuildStatusBadge({ |
| 54 | + status: 'passing', |
| 55 | + }), |
| 56 | + documentation, |
| 57 | + keywords, |
| 58 | + }, |
| 59 | + ] |
| 60 | + } |
| 61 | + |
| 62 | + static get defaultBadgeData() { |
| 63 | + return { |
| 64 | + label: 'build', |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + async fetch({ user, repo, workflow, branch }) { |
| 69 | + const { message: status } = await this._requestSvg({ |
| 70 | + schema, |
| 71 | + url: `https://github.com/${user}/${repo}/workflows/${encodeURIComponent( |
| 72 | + workflow |
| 73 | + )}/badge.svg`, |
| 74 | + options: { qs: { branch } }, |
| 75 | + valueMatcher: />([^<>]+)<\/tspan><\/text><\/g><path/, |
| 76 | + errorMessages: { |
| 77 | + 404: 'repo, branch, or workflow not found', |
| 78 | + }, |
| 79 | + }) |
| 80 | + |
| 81 | + return { status } |
| 82 | + } |
| 83 | + |
| 84 | + async handle({ user, repo, workflow, branch }) { |
| 85 | + const { status } = await this.fetch({ user, repo, workflow, branch }) |
| 86 | + return renderBuildStatusBadge({ status }) |
| 87 | + } |
| 88 | +} |
0 commit comments