Files
shields/services/github/github-actions-workflow-status.service.js
T
chris48sGitHubrepo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
6049ef64c8 handle workflow runs that do not have a conclusion (#8717)
Co-authored-by: repo-ranger[bot] <39074581+repo-ranger[bot]@users.noreply.github.com>
2022-12-15 22:41:33 +00:00

120 lines
2.9 KiB
JavaScript

import Joi from 'joi'
import { isBuildStatus, renderBuildStatusBadge } from '../build-status.js'
import { NotFound } from '../index.js'
import { GithubAuthV3Service } from './github-auth-service.js'
import { documentation, errorMessagesFor } from './github-helpers.js'
const schema = Joi.object({
workflow_runs: Joi.array()
.items(
Joi.object({
status: Joi.equal(
'completed',
'action_required',
'cancelled',
'failure',
'neutral',
'skipped',
'stale',
'success',
'timed_out',
'in_progress',
'queued',
'requested',
'waiting'
).required(),
conclusion: Joi.alternatives()
.try(isBuildStatus, Joi.equal('no status'), null)
.required(),
})
)
.required()
.min(0)
.max(1),
}).required()
const queryParamSchema = Joi.object({
event: Joi.string(),
branch: Joi.string().required(),
}).required()
const keywords = ['action', 'actions']
export default class GithubActionsWorkflowStatus extends GithubAuthV3Service {
static category = 'build'
static route = {
base: 'github/actions/workflow/status',
pattern: ':user/:repo/:workflow+',
queryParamSchema,
}
static examples = [
{
title: 'GitHub Workflow Status',
namedParams: {
user: 'actions',
repo: 'toolkit',
workflow: 'unit-tests.yml',
},
queryParams: {
branch: 'main',
},
staticPreview: renderBuildStatusBadge({
status: 'passing',
}),
documentation,
keywords,
},
{
title: 'GitHub Workflow Status (with event)',
namedParams: {
user: 'actions',
repo: 'toolkit',
workflow: 'unit-tests.yml',
},
queryParams: {
event: 'push',
branch: 'main',
},
staticPreview: renderBuildStatusBadge({
status: 'passing',
}),
documentation,
keywords,
},
]
static defaultBadgeData = {
label: 'build',
}
async fetch({ user, repo, workflow, branch, event }) {
return await this._requestJson({
schema,
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'),
})
}
async handle({ user, repo, workflow }, { branch, event }) {
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' })
}
const status = data.workflow_runs[0].conclusion
? data.workflow_runs[0].conclusion
: data.workflow_runs[0].status.replace('_', ' ')
return renderBuildStatusBadge({ status })
}
}