Skip to content

Commit

Permalink
Merge branch 'badges:master' into More-GitHub-Icons
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelHinrichs authored Jun 24, 2024
2 parents c083c80 + 5c81299 commit c7c07c8
Show file tree
Hide file tree
Showing 22 changed files with 349 additions and 174 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
run: echo "SHORT_SHA=${GITHUB_SHA::7}" >> $GITHUB_ENV

- name: Build
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
with:
context: .
push: false
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/create-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push snapshot release to DockerHub
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
with:
context: .
push: true
Expand All @@ -62,7 +62,7 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push snapshot release to GHCR
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
with:
context: .
push: true
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish-docker-next.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:

- name: Build and push to DockerHub
id: docker_build_push
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
with:
context: .
push: true
Expand All @@ -49,7 +49,7 @@ jobs:
password: ${{ secrets.GITHUB_TOKEN }}

- name: Build and push to GHCR
uses: docker/build-push-action@v5
uses: docker/build-push-action@v6
with:
context: .
push: true
Expand Down
8 changes: 7 additions & 1 deletion badge-maker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# Changelog

## 4.0.0 [WIP]
## 4.0.0

### Breaking Changes

- Drop compatibility with Node < 16

### Features

- Add `links` and `logoBase64` params

## 3.3.1

- Improve font measuring in for-the-badge and social styles
Expand Down
2 changes: 2 additions & 0 deletions badge-maker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ The format is the following:
message: 'passed', // (Required) Badge message
labelColor: '#555', // (Optional) Label color
color: '#4c1', // (Optional) Message color
logoBase64: 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSI2NCIgaGVpZ2h0PSI2NCI+PHJlY3Qgd2lkdGg9IjY0IiBoZWlnaHQ9IjY0IiByeD0iOCIgZmlsbD0iI2IxY2U1NiIvPjxwYXRoIGQ9Ik04IDBoMjR2NjRIOGMtNC40MzIgMC04LTMuNTY4LTgtOFY4YzAtNC40MzIgMy41NjgtOCA4LTh6IiBmaWxsPSIjNWQ1ZDVkIi8+PC9zdmc+' // (Optional) Any custom logo can be passed in a URL parameter by base64 encoding
links: ['https://example.com', 'https://example.com'], // (Optional) Links array of maximum two links

// (Optional) One of: 'plastic', 'flat', 'flat-square', 'for-the-badge' or 'social'
// Each offers a different visual design.
Expand Down
2 changes: 2 additions & 0 deletions badge-maker/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ interface Format {
labelColor?: string
color?: string
style?: 'plastic' | 'flat' | 'flat-square' | 'for-the-badge' | 'social'
logoBase64?: string
links?: Array<string>
}

export declare class ValidationError extends Error {}
Expand Down
37 changes: 33 additions & 4 deletions badge-maker/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,30 @@ function _validate(format) {
throw new ValidationError('Field `message` is required')
}

const stringFields = ['labelColor', 'color', 'message', 'label']
const stringFields = ['labelColor', 'color', 'message', 'label', 'logoBase64']
stringFields.forEach(function (field) {
if (field in format && typeof format[field] !== 'string') {
throw new ValidationError(`Field \`${field}\` must be of type string`)
}
})

if ('links' in format) {
if (!Array.isArray(format.links)) {
throw new ValidationError('Field `links` must be an array of strings')
} else {
if (format.links.length > 2) {
throw new ValidationError(
'Field `links` must not have more than 2 elements',
)
}
format.links.forEach(function (field) {
if (typeof field !== 'string') {
throw new ValidationError('Field `links` must be an array of strings')
}
})
}
}

const styleValues = [
'plastic',
'flat',
Expand All @@ -38,11 +55,21 @@ function _validate(format) {
}

function _clean(format) {
const expectedKeys = ['label', 'message', 'labelColor', 'color', 'style']
const expectedKeys = [
'label',
'message',
'labelColor',
'color',
'style',
'logoBase64',
'links',
]

const cleaned = {}
Object.keys(format).forEach(key => {
if (format[key] != null && expectedKeys.includes(key)) {
if (format[key] != null && key === 'logoBase64') {
cleaned.logo = format[key]
} else if (format[key] != null && expectedKeys.includes(key)) {
cleaned[key] = format[key]
} else {
throw new ValidationError(
Expand All @@ -65,7 +92,9 @@ function _clean(format) {
* @param {string} format.message (Required) Badge message (e.g: 'passing')
* @param {string} format.labelColor (Optional) Label color
* @param {string} format.color (Optional) Message color
* @param {string} format.style (Optional) Visual style e.g: 'flat'
* @param {string} format.style (Optional) Visual style (e.g: 'flat')
* @param {string} format.logoBase64 (Optional) Logo data URL
* @param {Array} format.links (Optional) Links array (e.g: ['https://example.com', 'https://example.com'])
* @returns {string} Badge in SVG format
* @see https://github.com/badges/shields/tree/master/badge-maker/README.md
*/
Expand Down
30 changes: 30 additions & 0 deletions badge-maker/lib/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ describe('makeBadge function', function () {
style: 'flat',
}),
).to.satisfy(isSvg)
expect(
makeBadge({
label: 'build',
message: 'passed',
color: 'green',
style: 'flat',
labelColor: 'blue',
logoBase64: 'data:image/svg+xml;base64,PHN2ZyB4bWxu',
links: ['https://example.com', 'https://example.com'],
}),
)
.to.satisfy(isSvg)
// explicitly make an assertion about logoBase64
// this param is not a straight passthrough
.and.to.include('data:image/svg+xml;base64,PHN2ZyB4bWxu')
})

it('should throw a ValidationError with invalid inputs', function () {
Expand All @@ -46,6 +61,21 @@ describe('makeBadge function', function () {
expect(() =>
makeBadge({ label: 'build', message: 'passed', labelColor: 7 }),
).to.throw(ValidationError, 'Field `labelColor` must be of type string')
expect(() =>
makeBadge({ label: 'build', message: 'passed', logoBase64: 7 }),
).to.throw(ValidationError, 'Field `logoBase64` must be of type string')
expect(() =>
makeBadge({ label: 'build', message: 'passed', links: 'test' }),
).to.throw(ValidationError, 'Field `links` must be an array of strings')
expect(() =>
makeBadge({ label: 'build', message: 'passed', links: [1] }),
).to.throw(ValidationError, 'Field `links` must be an array of strings')
expect(() =>
makeBadge({ label: 'build', message: 'passed', links: ['1', '2', '3'] }),
).to.throw(
ValidationError,
'Field `links` must not have more than 2 elements',
)
expect(() =>
makeBadge({ label: 'build', message: 'passed', format: 'png' }),
).to.throw(ValidationError, "Unexpected field 'format'")
Expand Down
2 changes: 0 additions & 2 deletions badge-maker/lib/make-badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ module.exports = function makeBadge({
color,
labelColor,
logo,
logoPosition,
logoSize,
logoWidth,
links = ['', ''],
Expand Down Expand Up @@ -55,7 +54,6 @@ module.exports = function makeBadge({
message,
links,
logo,
logoPosition,
logoWidth,
logoSize,
logoPadding: logo && label.length ? 3 : 0,
Expand Down
2 changes: 1 addition & 1 deletion badge-maker/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "badge-maker",
"version": "3.3.1",
"version": "4.0.0",
"description": "Shields.io badge library",
"keywords": [
"GitHub",
Expand Down
1 change: 0 additions & 1 deletion core/base-service/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ const serviceDataSchema = Joi.object({
logoSvg: Joi.string(),
logoColor: optionalStringWhenNamedLogoPresent,
logoWidth: optionalNumberWhenAnyLogoPresent,
logoPosition: optionalNumberWhenAnyLogoPresent,
cacheSeconds: Joi.number().integer().min(0),
style: Joi.string(),
})
Expand Down
1 change: 0 additions & 1 deletion core/base-service/base.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,6 @@ describe('BaseService', function () {
logo: undefined,
logoWidth: undefined,
logoSize: undefined,
logoPosition: undefined,
links: [],
labelColor: undefined,
cacheLengthSeconds: undefined,
Expand Down
18 changes: 3 additions & 15 deletions core/base-service/coalesce-badge.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ import toArray from './to-array.js'
// base64-encoded logos). Otherwise the default color is used. If the color
// is specified for a multicolor Shield logo, the named logo will be used and
// colored. The appearance of the logo can be customized using `logoWidth`,
// and in the case of the popout badge, `logoPosition`. When `?logo=` is
// specified, any logo-related parameters specified dynamically by the
// service, or by default in the service, are ignored.
// When `?logo=` is specified, any logo-related parameters specified
// dynamically by the service, or by default in the service, are ignored.
// 2. The second precedence is the dynamic logo returned by a service. This is
// used only by the Endpoint badge. The `logoColor` can be overridden by the
// query string.
Expand Down Expand Up @@ -56,7 +55,6 @@ export default function coalesceBadge(
} = overrides
let {
logoWidth: overrideLogoWidth,
logoPosition: overrideLogoPosition,
logoSize: overrideLogoSize,
color: overrideColor,
labelColor: overrideLabelColor,
Expand All @@ -78,7 +76,6 @@ export default function coalesceBadge(
overrideLabelColor = `${overrideLabelColor}`
}
overrideLogoWidth = +overrideLogoWidth || undefined
overrideLogoPosition = +overrideLogoPosition || undefined

const {
isError,
Expand All @@ -91,7 +88,6 @@ export default function coalesceBadge(
logoColor: serviceLogoColor,
logoSize: serviceLogoSize,
logoWidth: serviceLogoWidth,
logoPosition: serviceLogoPosition,
link: serviceLink,
cacheSeconds: serviceCacheSeconds,
style: serviceStyle,
Expand Down Expand Up @@ -122,12 +118,7 @@ export default function coalesceBadge(
style = 'flat'
}

let namedLogo,
namedLogoColor,
logoSize,
logoWidth,
logoPosition,
logoSvgBase64
let namedLogo, namedLogoColor, logoSize, logoWidth, logoSvgBase64
if (overrideLogo) {
// `?logo=` could be a named logo or encoded svg.
const overrideLogoSvgBase64 = decodeDataUrlFromQueryParam(overrideLogo)
Expand All @@ -143,7 +134,6 @@ export default function coalesceBadge(
// original width or position.
logoSize = overrideLogoSize
logoWidth = overrideLogoWidth
logoPosition = overrideLogoPosition
} else {
if (serviceLogoSvg) {
logoSvgBase64 = svg2base64(serviceLogoSvg)
Expand All @@ -156,7 +146,6 @@ export default function coalesceBadge(
}
logoSize = coalesce(overrideLogoSize, serviceLogoSize)
logoWidth = coalesce(overrideLogoWidth, serviceLogoWidth)
logoPosition = coalesce(overrideLogoPosition, serviceLogoPosition)
}
if (namedLogo) {
const iconSize = getIconSize(String(namedLogo).toLowerCase())
Expand Down Expand Up @@ -195,7 +184,6 @@ export default function coalesceBadge(
namedLogo,
logo: logoSvgBase64,
logoWidth,
logoPosition,
logoSize,
links: toArray(overrideLink || serviceLink),
cacheLengthSeconds: coalesce(serviceCacheSeconds, defaultCacheSeconds),
Expand Down
17 changes: 1 addition & 16 deletions core/base-service/coalesce-badge.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,14 +195,13 @@ describe('coalesceBadge', function () {
).to.equal(getSimpleIcon({ name: 'npm', color: 'blue' })).and.not.be.empty
})

it("when the logo is overridden, it ignores the service's logo color, position, and width", function () {
it("when the logo is overridden, it ignores the service's logo color and width", function () {
expect(
coalesceBadge(
{ logo: 'npm' },
{
namedLogo: 'appveyor',
logoColor: 'red',
logoPosition: -3,
logoWidth: 100,
},
{},
Expand Down Expand Up @@ -288,20 +287,6 @@ describe('coalesceBadge', function () {
})
})

describe('Logo position', function () {
it('overrides the logoPosition', function () {
expect(coalesceBadge({ logoPosition: -10 }, {}, {})).to.include({
logoPosition: -10,
})
})

it('applies the logo position', function () {
expect(
coalesceBadge({}, { namedLogo: 'npm', logoPosition: -10 }, {}),
).to.include({ logoPosition: -10 })
})
})

describe('Links', function () {
it('overrides the links', function () {
expect(
Expand Down
1 change: 0 additions & 1 deletion core/base-service/legacy-request-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const globalQueryParams = new Set([
'link',
'logo',
'logoColor',
'logoPosition',
'logoSize',
'logoWidth',
'link',
Expand Down
Loading

0 comments on commit c7c07c8

Please sign in to comment.