-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
586edc1
commit a63bc37
Showing
3 changed files
with
102 additions
and
52 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' }) |