add support for [GitHubWorkflowStatus] (Actions) using BaseSvgScraping service implementation (#3898)
* feat: add BaseSvgScraping service impl for GH Actions * chore: fix GH Actions service test name * feat: add branch support to GH Actions/Workflows * chore: fix schema for GH actions * refactor: update route path per PR discussion
This commit is contained in:
88
services/github/github-workflow-status.service.js
Normal file
88
services/github/github-workflow-status.service.js
Normal file
@@ -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><path/,
|
||||
errorMessages: {
|
||||
404: 'repo, branch, or workflow not found',
|
||||
},
|
||||
})
|
||||
|
||||
return { status }
|
||||
}
|
||||
|
||||
async handle({ user, repo, workflow, branch }) {
|
||||
const { status } = await this.fetch({ user, repo, workflow, branch })
|
||||
return renderBuildStatusBadge({ status })
|
||||
}
|
||||
}
|
||||
37
services/github/github-workflow-status.tester.js
Normal file
37
services/github/github-workflow-status.tester.js
Normal file
@@ -0,0 +1,37 @@
|
||||
'use strict'
|
||||
|
||||
const Joi = require('@hapi/joi')
|
||||
const { isBuildStatus } = require('../build-status')
|
||||
const t = (module.exports = require('../tester').createServiceTester())
|
||||
|
||||
const isWorkflowStatus = Joi.alternatives()
|
||||
.try(isBuildStatus, Joi.equal('no status'))
|
||||
.required()
|
||||
|
||||
t.create('nonexistent repo')
|
||||
.get('/badges/shields-fakeness/fake.json')
|
||||
.expectBadge({
|
||||
label: 'build',
|
||||
message: 'repo, branch, or workflow not found',
|
||||
})
|
||||
|
||||
t.create('nonexistent workflow')
|
||||
.get('/actions/toolkit/not-a-real-workflow.json')
|
||||
.expectBadge({
|
||||
label: 'build',
|
||||
message: 'repo, branch, or workflow not found',
|
||||
})
|
||||
|
||||
t.create('valid workflow')
|
||||
.get('/actions/toolkit/Main%20workflow.json')
|
||||
.expectBadge({
|
||||
label: 'build',
|
||||
message: isWorkflowStatus,
|
||||
})
|
||||
|
||||
t.create('valid workflow (branch)')
|
||||
.get('/actions/toolkit/Main%20workflow/master.json')
|
||||
.expectBadge({
|
||||
label: 'build',
|
||||
message: isWorkflowStatus,
|
||||
})
|
||||
Reference in New Issue
Block a user