Skip to content

Commit

Permalink
update github actions workflow badge to use api
Browse files Browse the repository at this point in the history
  • Loading branch information
chris48s committed Oct 17, 2022
1 parent 808dfec commit 7ea7cf5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 31 deletions.
56 changes: 36 additions & 20 deletions services/github/github-actions-workflow-status.service.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import Joi from 'joi'
import { isBuildStatus, renderBuildStatusBadge } from '../build-status.js'
import { BaseSvgScrapingService } from '../index.js'
import { documentation } from './github-helpers.js'
import { NotFound } from '../index.js'
import { GithubAuthV3Service } from './github-auth-service.js'
import { documentation, errorMessagesFor } from './github-helpers.js'

const schema = Joi.object({
message: Joi.alternatives()
.try(isBuildStatus, Joi.equal('no status'))
.required(),
workflow_runs: Joi.array()
.items(
Joi.object({
conclusion: Joi.alternatives()
.try(isBuildStatus, Joi.equal('no status'))
.required(),
})
)
.required()
.min(0)
.max(1),
}).required()

const queryParamSchema = Joi.object({
event: Joi.string(),
branch: Joi.string(),
branch: Joi.string().required(),
}).required()

const keywords = ['action', 'actions']

export default class GithubActionsWorkflowStatus extends BaseSvgScrapingService {
export default class GithubActionsWorkflowStatus extends GithubAuthV3Service {
static category = 'build'

static route = {
Expand All @@ -33,14 +42,17 @@ export default class GithubActionsWorkflowStatus extends BaseSvgScrapingService
repo: 'toolkit',
workflow: 'unit-tests.yml',
},
queryParams: {
branch: 'main',
},
staticPreview: renderBuildStatusBadge({
status: 'passing',
}),
documentation,
keywords,
},
{
title: 'GitHub Workflow Status (branch and event)',
title: 'GitHub Workflow Status (with event)',
namedParams: {
user: 'actions',
repo: 'toolkit',
Expand All @@ -63,23 +75,27 @@ export default class GithubActionsWorkflowStatus extends BaseSvgScrapingService
}

async fetch({ user, repo, workflow, branch, event }) {
const { message: status } = await this._requestSvg({
return await this._requestJson({
schema,
url: `https://github.com/${user}/${repo}/actions/workflows/${encodeURIComponent(
workflow
)}/badge.svg`,
options: { searchParams: { branch, event } },
valueMatcher: />([^<>]+)<\/tspan><\/text><\/g><path/,
errorMessages: {
404: 'repo, branch, or workflow not found',
url: `/repos/${user}/${repo}/actions/workflows/${workflow}/runs`,
options: {
searchParams: {
branch,
event,
page: '1',
per_page: '1',
exclude_pull_requests: 'true',
},
},
errorMessages: errorMessagesFor('repo or workflow not found'),
})

return { status }
}

async handle({ user, repo, workflow }, { branch, event }) {
const { status } = await this.fetch({ user, repo, workflow, branch, event })
return renderBuildStatusBadge({ status })
const data = await this.fetch({ user, repo, workflow, branch, event })
if (data.workflow_runs.length === 0) {
throw new NotFound({ prettyMessage: 'branch or event not found' })
}
return renderBuildStatusBadge({ status: data.workflow_runs[0].conclusion })
}
}
31 changes: 20 additions & 11 deletions services/github/github-actions-workflow-status.tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,44 @@ const isWorkflowStatus = Joi.alternatives()
.required()

t.create('nonexistent repo')
.get('/badges/shields-fakeness/fake.yml.json')
.get('/badges/shields-fakeness/fake.yml.json?branch=main')
.expectBadge({
label: 'build',
message: 'repo, branch, or workflow not found',
message: 'repo or workflow not found',
})

t.create('nonexistent workflow')
.get('/actions/toolkit/not-a-real-workflow.yml.json')
.get('/actions/toolkit/not-a-real-workflow.yml.json?branch=main')
.expectBadge({
label: 'build',
message: 'repo, branch, or workflow not found',
message: 'repo or workflow not found',
})

t.create('valid workflow')
.get('/actions/toolkit/unit-tests.yml.json')
t.create('nonexistent branch')
.get('/actions/toolkit/unit-tests.yml.json?branch=not-a-real-branch')
.expectBadge({
label: 'build',
message: isWorkflowStatus,
message: 'branch or event not found',
})

t.create('valid workflow (branch)')
.get('/actions/toolkit/unit-tests.yml.json?branch=master.json')
t.create('nonexistent event')
.get(
'/actions/toolkit/unit-tests.yml.json?branch=main&event=not-a-real-event'
)
.expectBadge({
label: 'build',
message: 'branch or event not found',
})

t.create('valid workflow')
.get('/actions/toolkit/unit-tests.yml.json?branch=main')
.expectBadge({
label: 'build',
message: isWorkflowStatus,
})

t.create('valid workflow (event)')
.get('/actions/toolkit/unit-tests.yml.json?event=push')
t.create('valid workflow (with event)')
.get('/actions/toolkit/unit-tests.yml.json?branch=main&event=push')
.expectBadge({
label: 'build',
message: isWorkflowStatus,
Expand Down

0 comments on commit 7ea7cf5

Please sign in to comment.