Skip to content

Commit

Permalink
refactor and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
karolwydmuch committed Oct 18, 2023
1 parent 586edc1 commit a63bc37
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 52 deletions.
52 changes: 0 additions & 52 deletions services/github/github-issue-7929.service.js

This file was deleted.

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: 'latest-release@10.x',
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')
.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' })

0 comments on commit a63bc37

Please sign in to comment.