Skip to content

Commit c455993

Browse files
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
1 parent 6c89276 commit c455993

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use strict'
2+
3+
const Joi = require('@hapi/joi')
4+
const { isBuildStatus } = require('../build-status')
5+
const t = (module.exports = require('../tester').createServiceTester())
6+
7+
const isWorkflowStatus = Joi.alternatives()
8+
.try(isBuildStatus, Joi.equal('no status'))
9+
.required()
10+
11+
t.create('nonexistent repo')
12+
.get('/badges/shields-fakeness/fake.json')
13+
.expectBadge({
14+
label: 'build',
15+
message: 'repo, branch, or workflow not found',
16+
})
17+
18+
t.create('nonexistent workflow')
19+
.get('/actions/toolkit/not-a-real-workflow.json')
20+
.expectBadge({
21+
label: 'build',
22+
message: 'repo, branch, or workflow not found',
23+
})
24+
25+
t.create('valid workflow')
26+
.get('/actions/toolkit/Main%20workflow.json')
27+
.expectBadge({
28+
label: 'build',
29+
message: isWorkflowStatus,
30+
})
31+
32+
t.create('valid workflow (branch)')
33+
.get('/actions/toolkit/Main%20workflow/master.json')
34+
.expectBadge({
35+
label: 'build',
36+
message: isWorkflowStatus,
37+
})

0 commit comments

Comments
 (0)