Skip to content
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

[GitHub] add latest release of a branch badge #9661

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
69 changes: 69 additions & 0 deletions services/github/github-latest-branch-release.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import Joi from 'joi'
import { GithubAuthV3Service } from './github-auth-service.js'
import { documentation, httpErrorsFor } from './github-helpers.js'

const MAXIMUM_SEARCHING_PAGE_NUMBER = 5

const schema = Joi.array().items(
Joi.object({
target_commitish: Joi.string(),
tag_name: Joi.string(),
}),
)

export default class GithubLatestBranchRelease extends GithubAuthV3Service {
static category = 'version'
static route = {
base: 'github/v/latest-branch-release',
pattern: ':user/:repo/:branch',
}

static examples = [
{
title: 'GitHub latest branch release',
namedParams: { user: 'laravel', repo: 'framework', branch: '10.x' },
staticPreview: Object.assign(this.render({ releases: [] }), {
label: '[email protected]',
message: 'v10.28.0',
style: 'social',
}),
queryParams: {},
documentation,
},
]

static defaultBadgeData = { namedLogo: 'github' }

static render({ branch, latestBranchTag }) {
return {
label: `latest-release@${branch}`,
message: latestBranchTag || '',
color: 'blue',
}
}

async handle({ user, repo, branch }) {
let shouldRetry = true
let currentPage = 0
let latestBranchTag

while (shouldRetry && currentPage < MAXIMUM_SEARCHING_PAGE_NUMBER) {
currentPage++
const releases = await this._requestJson({
url: `/repos/${user}/${repo}/releases?per_page=100&page=${currentPage}`,
schema,
httpErrors: httpErrorsFor('user not found'),
})

latestBranchTag = releases.filter(
release => release.target_commitish === branch,
)[0]?.tag_name

if (latestBranchTag) {
shouldRetry = false
}
}

return this.constructor.render({ branch, latestBranchTag })
}
}
33 changes: 33 additions & 0 deletions services/github/github-latest-branch-release.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Joi from 'joi'
import { isSemver } from '../test-validators.js'
import { ServiceTester } from '../tester.js'

export const t = new ServiceTester({
id: 'GithubLatestBranchRelease',
title: 'Github Latest Branch Release',
pathPrefix: '/github',
})

t.create('Release')
.get('/v/release/expressjs/express.json')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we end up coming back to this, none of the tests in this file are actually testing the service class that has been added in this PR, but lets not get bogged down in that for the moment

.expectBadge({ label: 'release', message: isSemver, color: 'blue' })

t.create('Prerelease')
.get('/v/release/expressjs/express.json?include_prereleases')
.expectBadge({
label: 'release',
message: isSemver,
color: Joi.string().allow('blue', 'orange').required(),
})

t.create('Latest release (release)')
.get('/v/release/expressjs/express.json?display_name=release')
.expectBadge({ label: 'release', message: isSemver, color: 'blue' })

t.create('Release (No releases)')
.get('/v/release/badges/daily-tests.json')
.expectBadge({ label: 'release', message: 'no releases or repo not found' })

t.create('Release (repo not found)')
.get('/v/release/badges/helmets.json')
.expectBadge({ label: 'release', message: 'no releases or repo not found' })