From 808dfeced48f30d5033b54b8be6840518cf7301e Mon Sep 17 00:00:00 2001 From: chris48s Date: Mon, 3 Oct 2022 21:32:53 +0100 Subject: [PATCH 1/4] switch from github workflows to github actions workflows --- .../github-actions-workflow-status.service.js | 85 ++++++++++++++ .../github-actions-workflow-status.tester.js | 43 +++++++ .../github/github-workflow-status.service.js | 107 ++---------------- .../github/github-workflow-status.tester.js | 33 +++--- 4 files changed, 153 insertions(+), 115 deletions(-) create mode 100644 services/github/github-actions-workflow-status.service.js create mode 100644 services/github/github-actions-workflow-status.tester.js diff --git a/services/github/github-actions-workflow-status.service.js b/services/github/github-actions-workflow-status.service.js new file mode 100644 index 0000000000000..609fc7aa4e388 --- /dev/null +++ b/services/github/github-actions-workflow-status.service.js @@ -0,0 +1,85 @@ +import Joi from 'joi' +import { isBuildStatus, renderBuildStatusBadge } from '../build-status.js' +import { BaseSvgScrapingService } from '../index.js' +import { documentation } from './github-helpers.js' + +const schema = Joi.object({ + message: Joi.alternatives() + .try(isBuildStatus, Joi.equal('no status')) + .required(), +}).required() + +const queryParamSchema = Joi.object({ + event: Joi.string(), + branch: Joi.string(), +}).required() + +const keywords = ['action', 'actions'] + +export default class GithubActionsWorkflowStatus extends BaseSvgScrapingService { + 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', + }, + staticPreview: renderBuildStatusBadge({ + status: 'passing', + }), + documentation, + keywords, + }, + { + title: 'GitHub Workflow Status (branch and 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 }) { + const { message: status } = await this._requestSvg({ + schema, + url: `https://github.com/${user}/${repo}/actions/workflows/${encodeURIComponent( + workflow + )}/badge.svg`, + options: { searchParams: { branch, event } }, + valueMatcher: />([^<>]+)<\/tspan><\/text><\/g>([^<>]+)<\/tspan><\/text><\/g> Date: Mon, 17 Oct 2022 16:14:26 +0100 Subject: [PATCH 2/4] update github actions workflow badge to use api --- .../github-actions-workflow-status.service.js | 56 ++++++++++++------- .../github-actions-workflow-status.tester.js | 31 ++++++---- 2 files changed, 56 insertions(+), 31 deletions(-) diff --git a/services/github/github-actions-workflow-status.service.js b/services/github/github-actions-workflow-status.service.js index 609fc7aa4e388..2f1896589510d 100644 --- a/services/github/github-actions-workflow-status.service.js +++ b/services/github/github-actions-workflow-status.service.js @@ -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 = { @@ -33,6 +42,9 @@ export default class GithubActionsWorkflowStatus extends BaseSvgScrapingService repo: 'toolkit', workflow: 'unit-tests.yml', }, + queryParams: { + branch: 'main', + }, staticPreview: renderBuildStatusBadge({ status: 'passing', }), @@ -40,7 +52,7 @@ export default class GithubActionsWorkflowStatus extends BaseSvgScrapingService keywords, }, { - title: 'GitHub Workflow Status (branch and event)', + title: 'GitHub Workflow Status (with event)', namedParams: { user: 'actions', repo: 'toolkit', @@ -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> Date: Mon, 17 Oct 2022 16:22:23 +0100 Subject: [PATCH 3/4] add test case for missing branch param --- services/github/github-actions-workflow-status.tester.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/services/github/github-actions-workflow-status.tester.js b/services/github/github-actions-workflow-status.tester.js index efb320b9b9092..9ebc19e4a641c 100644 --- a/services/github/github-actions-workflow-status.tester.js +++ b/services/github/github-actions-workflow-status.tester.js @@ -7,6 +7,13 @@ const isWorkflowStatus = Joi.alternatives() .try(isBuildStatus, Joi.equal('no status')) .required() +t.create('missing branch param') + .get('/actions/toolkit/unit-tests.yml.json') + .expectBadge({ + label: 'build', + message: 'invalid query parameter: branch', + }) + t.create('nonexistent repo') .get('/badges/shields-fakeness/fake.yml.json?branch=main') .expectBadge({ From e0b7dbd58cc4e0f7f7a47c51fff5e85c7162512a Mon Sep 17 00:00:00 2001 From: chris48s Date: Thu, 1 Dec 2022 19:44:58 +0000 Subject: [PATCH 4/4] custom deprecation message --- .../github/github-workflow-status.service.js | 33 ++++++++++++++----- .../github/github-workflow-status.tester.js | 10 +++--- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/services/github/github-workflow-status.service.js b/services/github/github-workflow-status.service.js index 7afee29a01b58..69e6e8f286d8d 100644 --- a/services/github/github-workflow-status.service.js +++ b/services/github/github-workflow-status.service.js @@ -1,11 +1,28 @@ -import { deprecatedService } from '../index.js' +import { BaseService } from '../index.js' -export default deprecatedService({ - category: 'build', - route: { +export default class DeprecatedGithubWorkflowStatus extends BaseService { + static category = 'build' + + static route = { base: 'github/workflow/status', pattern: ':various+', - }, - label: 'build', - dateAdded: new Date('2022-10-03'), -}) + } + + static examples = [] + + static defaultBadgeData = { label: 'build' } + + async handle() { + return { + label: 'build', + message: 'https://github.com/badges/shields/issues/8671', + /* + This is a 'special' deprecation because we are making a breaking change + We've implemented it as a custom class instead of a normal + deprecatedService so that we can include link. + */ + link: ['https://github.com/badges/shields/issues/8671'], + color: 'red', + } + } +} diff --git a/services/github/github-workflow-status.tester.js b/services/github/github-workflow-status.tester.js index 3100476f738e4..de27de37aab89 100644 --- a/services/github/github-workflow-status.tester.js +++ b/services/github/github-workflow-status.tester.js @@ -10,33 +10,33 @@ t.create('no longer available (previously nonexistent repo)') .get('/badges/shields-fakeness/fake.json') .expectBadge({ label: 'build', - message: 'no longer available', + message: 'https://github.com/badges/shields/issues/8671', }) t.create('no longer available (previously nonexistent workflow)') .get('/actions/toolkit/not-a-real-workflow.json') .expectBadge({ label: 'build', - message: 'no longer available', + message: 'https://github.com/badges/shields/issues/8671', }) t.create('no longer available (previously valid workflow)') .get('/actions/toolkit/toolkit-unit-tests.json') .expectBadge({ label: 'build', - message: 'no longer available', + message: 'https://github.com/badges/shields/issues/8671', }) t.create('no longer available (previously valid workflow - branch)') .get('/actions/toolkit/toolkit-unit-tests/master.json') .expectBadge({ label: 'build', - message: 'no longer available', + message: 'https://github.com/badges/shields/issues/8671', }) t.create('no longer available (previously valid workflow - event)') .get('/actions/toolkit/toolkit-unit-tests.json?event=push') .expectBadge({ label: 'build', - message: 'no longer available', + message: 'https://github.com/badges/shields/issues/8671', })