Skip to content

Commit

Permalink
chore: add support for skipping broken versions
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikThePendric committed Oct 2, 2023
1 parent a12d0de commit 04d5071
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 35 deletions.
4 changes: 4 additions & 0 deletions .github/actions/supported-versions/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ inputs:
default: ''
config-dir:
description: 'Relative directory for the d2.config file'
broken-versions:
description: 'Comma delimeted lists of known broken versions. If a supported stable version is in this list it will be excluded. Each version string must match a tag on DockerHub, i.e. "2.38.4.3-rc"'
required: false
default: ''
outputs:
versions:
description: 'JSON stringified array of version strings, including patch and hotfix numbers.'
Expand Down
48 changes: 39 additions & 9 deletions .github/actions/supported-versions/dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2725,8 +2725,9 @@ function isValidVersionString(versionString, isRC) {
}
// RCs must end with `-rc`, others must not
if (
(isRC && !versionString.endsWith('-rc')) ||
(!isRC && versionString.endsWith('-rc'))
typeof isRC === 'boolean' &&
((isRC && !versionString.endsWith('-rc')) ||
(!isRC && versionString.endsWith('-rc')))
) {
return false
}
Expand Down Expand Up @@ -2778,6 +2779,26 @@ function getReleaseCandidates() {
return rcVersions
}

function getBrokenVersions() {
const inputValue = core.getInput('broken-versions')

if (!inputValue) {
return []
}

const brokenVersions = inputValue.split(',').map((str) => str.trim())

for (const brokenVersion of brokenVersions) {
if (!isValidVersionString(brokenVersion)) {
throw new Error(
`The string "${brokenVersion}" provided to \`broken-versions\` is not a valid version`
)
}
}

return brokenVersions
}

function getStableVersions() {
const httpClient = new HttpClient()
return httpClient
Expand All @@ -2799,11 +2820,12 @@ function computeFullVersionName({
: patchVersionName
}

function computeSupportedVersions(
function computeSupportedVersions({
stableVersions,
minDHIS2Version = '',
releaseCandidates = []
) {
releaseCandidates = [],
brokenVersions = [],
} = {}) {
const minDHIS2VersionNumber = minDHIS2Version.split('.')[1] ?? 0
const latestVersion = stableVersions.find(({ latest }) => latest)?.version
const stableVersionsLookup = stableVersions.reduce((acc, versionObj) => {
Expand Down Expand Up @@ -2834,9 +2856,14 @@ function computeSupportedVersions(
const versionObj =
versionReleaseCandidate ?? stableVersionsLookup.get(currentVersion)
const { fullName, supported } = versionObj
const isBroken = brokenVersions.some(
(brokenVersion) => brokenVersion === fullName
)

if (supported && currentVersion >= minDHIS2VersionNumber) {
supportedVersions.push(fullName)
if (!isBroken) {
supportedVersions.push(fullName)
}
} else {
isSupported = false
}
Expand All @@ -2851,12 +2878,14 @@ async function main() {
try {
const minDHIS2Version = getMinDHIS2Version()
const releaseCandidates = getReleaseCandidates()
const brokenVersions = getBrokenVersions()
const stableVersions = await getStableVersions()
const supportedVersions = computeSupportedVersions(
const supportedVersions = computeSupportedVersions({
stableVersions,
minDHIS2Version,
releaseCandidates
)
releaseCandidates,
brokenVersions,
})

core.notice(
'The following supported versions were detected: ' +
Expand All @@ -2875,6 +2904,7 @@ module.exports = {
getMinDHIS2Version,
getReleaseCandidates,
getStableVersions,
getBrokenVersions,
computeSupportedVersions,
main,
STABLE_VERSIONS_URL,
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/dhis2-verify-app.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ jobs:
- name: Retrieve supported versions
id: supported-versions
uses: ./.github/actions/supported-versions
with:
broken-versions: '2.39.2.1'
outputs:
versions: ${{ steps.supported-versions.outputs.versions }}

Expand Down
64 changes: 45 additions & 19 deletions src/gh-js-actions/supported-versions/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
isValidVersionString,
getMinDHIS2Version,
getReleaseCandidates,
getBrokenVersions,
getStableVersions,
computeSupportedVersions,
main,
Expand Down Expand Up @@ -78,6 +79,28 @@ describe('Custom GitHub JS Action - Supported Versions', () => {
expect(getReleaseCandidates()).toEqual([])
})
})
describe('getBrokenVersions', () => {
it('returns an array of broken versions if one is supplied', () => {
const brokenVersion1 = '2.39.2.1'
jest.spyOn(core, 'getInput').mockReturnValueOnce(brokenVersion1)
expect(getBrokenVersions()).toEqual([brokenVersion1])
})
it('returns an array of broken versions if multiple are supplied', () => {
const brokenVersion1 = '2.39.2.1'
const brokenVersion2 = '2.37.9.1'
jest.spyOn(core, 'getInput').mockReturnValueOnce(
`${brokenVersion1},${brokenVersion2}`
)
expect(getBrokenVersions()).toEqual([
brokenVersion1,
brokenVersion2,
])
})
it('returns an empty array when input is empty', () => {
jest.spyOn(core, 'getInput').mockReturnValueOnce('')
expect(getBrokenVersions()).toEqual([])
})
})

describe('getStableVersions', () => {
it('calls the right URL to get the stable versions and returns the versions array', async () => {
Expand All @@ -93,32 +116,35 @@ describe('Custom GitHub JS Action - Supported Versions', () => {

describe('computeSupportedVersions', () => {
it('should produce the correct output given a known input', () => {
expect(computeSupportedVersions(versionsFixture.versions)).toEqual([
'2.40.0.1',
'2.39.2.1',
'2.38.4.3',
'2.37.9.1',
])
expect(
computeSupportedVersions({
stableVersions: versionsFixture.versions,
})
).toEqual(['2.40.0.1', '2.39.2.1', '2.38.4.3', '2.37.9.1'])
})
it('should Produces the right output when a minDHIS2Version is provided', () => {
it('should omit lower versions when a minDHIS2Version is provided', () => {
expect(
computeSupportedVersions(versionsFixture.versions, '2.38')
computeSupportedVersions({
stableVersions: versionsFixture.versions,
minDHIS2Version: '2.38',
})
).toEqual(['2.40.0.1', '2.39.2.1', '2.38.4.3'])
})
it('should produce the right output when a release candidate is provided', () => {
it('should replace a stables version with RCs when releaseCandidates are provided', () => {
expect(
computeSupportedVersions(versionsFixture.versions, undefined, [
'2.40.0.2-rc',
])
).toEqual(['2.40.0.2-rc', '2.39.2.1', '2.38.4.3', '2.37.9.1'])
computeSupportedVersions({
stableVersions: versionsFixture.versions,
releaseCandidates: ['2.40.0.2-rc', '2.39.2.2-rc'],
})
).toEqual(['2.40.0.2-rc', '2.39.2.2-rc', '2.38.4.3', '2.37.9.1'])
})
it('should produce the right output when multiple release candidates are provided', () => {
it('should omit broken versions when brokenVersions are provided', () => {
expect(
computeSupportedVersions(versionsFixture.versions, undefined, [
'2.40.0.2-rc',
'2.39.2.2-rc',
])
).toEqual(['2.40.0.2-rc', '2.39.2.2-rc', '2.38.4.3', '2.37.9.1'])
computeSupportedVersions({
stableVersions: versionsFixture.versions,
brokenVersions: ['2.39.2.1', '2.37.9.1'],
})
).toEqual(['2.40.0.1', '2.38.4.3'])
})
})

Expand Down
43 changes: 36 additions & 7 deletions src/gh-js-actions/supported-versions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,26 @@ function getReleaseCandidates() {
return rcVersions
}

function getBrokenVersions() {
const inputValue = core.getInput('broken-versions')

if (!inputValue) {
return []
}

const brokenVersions = inputValue.split(',').map((str) => str.trim())

for (const brokenVersion of brokenVersions) {
if (!isValidVersionString(brokenVersion)) {
throw new Error(
`The string "${brokenVersion}" provided to \`broken-versions\` is not a valid version`
)
}
}

return brokenVersions
}

function getStableVersions() {
const httpClient = new HttpClient()
return httpClient
Expand All @@ -92,11 +112,12 @@ function computeFullVersionName({
: patchVersionName
}

function computeSupportedVersions(
function computeSupportedVersions({
stableVersions,
minDHIS2Version = '',
releaseCandidates = []
) {
releaseCandidates = [],
brokenVersions = [],
} = {}) {
const minDHIS2VersionNumber = minDHIS2Version.split('.')[1] ?? 0
const latestVersion = stableVersions.find(({ latest }) => latest)?.version
const stableVersionsLookup = stableVersions.reduce((acc, versionObj) => {
Expand Down Expand Up @@ -127,9 +148,14 @@ function computeSupportedVersions(
const versionObj =
versionReleaseCandidate ?? stableVersionsLookup.get(currentVersion)
const { fullName, supported } = versionObj
const isBroken = brokenVersions.some(
(brokenVersion) => brokenVersion === fullName
)

if (supported && currentVersion >= minDHIS2VersionNumber) {
supportedVersions.push(fullName)
if (!isBroken) {
supportedVersions.push(fullName)
}
} else {
isSupported = false
}
Expand All @@ -144,12 +170,14 @@ async function main() {
try {
const minDHIS2Version = getMinDHIS2Version()
const releaseCandidates = getReleaseCandidates()
const brokenVersions = getBrokenVersions()
const stableVersions = await getStableVersions()
const supportedVersions = computeSupportedVersions(
const supportedVersions = computeSupportedVersions({
stableVersions,
minDHIS2Version,
releaseCandidates
)
releaseCandidates,
brokenVersions,
})

core.notice(
'The following supported versions were detected: ' +
Expand All @@ -168,6 +196,7 @@ module.exports = {
getMinDHIS2Version,
getReleaseCandidates,
getStableVersions,
getBrokenVersions,
computeSupportedVersions,
main,
STABLE_VERSIONS_URL,
Expand Down

0 comments on commit 04d5071

Please sign in to comment.