diff --git a/services/github/github-workflow-status.service.js b/services/github/github-workflow-status.service.js new file mode 100644 index 0000000000000..24ba4800fe8d3 --- /dev/null +++ b/services/github/github-workflow-status.service.js @@ -0,0 +1,88 @@ +'use strict' + +const Joi = require('@hapi/joi') +const { isBuildStatus, renderBuildStatusBadge } = require('../build-status') +const { documentation } = require('./github-helpers') +const { BaseSvgScrapingService } = require('..') + +const schema = Joi.object({ + message: Joi.alternatives() + .try(isBuildStatus, Joi.equal('no status')) + .required(), +}).required() + +const keywords = ['action', 'actions'] + +module.exports = class GithubWorkflowStatus extends BaseSvgScrapingService { + static get category() { + return 'build' + } + + static get route() { + return { + base: 'github/workflow/status', + pattern: ':user/:repo/:workflow/:branch*', + } + } + + static get examples() { + return [ + { + title: 'GitHub Workflow Status', + pattern: ':user/:repo/:workflow', + namedParams: { + user: 'actions', + repo: 'toolkit', + workflow: 'Main workflow', + }, + staticPreview: renderBuildStatusBadge({ + status: 'passing', + }), + documentation, + keywords, + }, + { + title: 'GitHub Workflow Status (branch)', + pattern: ':user/:repo/:workflow/:branch', + namedParams: { + user: 'actions', + repo: 'toolkit', + workflow: 'Main workflow', + branch: 'master', + }, + staticPreview: renderBuildStatusBadge({ + status: 'passing', + }), + documentation, + keywords, + }, + ] + } + + static get defaultBadgeData() { + return { + label: 'build', + } + } + + async fetch({ user, repo, workflow, branch }) { + const { message: status } = await this._requestSvg({ + schema, + url: `https://github.com/${user}/${repo}/workflows/${encodeURIComponent( + workflow + )}/badge.svg`, + options: { qs: { branch } }, + valueMatcher: />([^<>]+)<\/tspan><\/text><\/g>