Skip to content

add support for [GitHubWorkflowStatus] (Actions) using BaseSvgScraping service implementation #3898

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Nov 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions services/github/github-workflow-status.service.js
Original file line number Diff line number Diff line change
@@ -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 changes: 37 additions & 0 deletions services/github/github-workflow-status.tester.js
Original file line number Diff line number Diff line change
@@ -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,
})