Skip to content

Commit

Permalink
add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jumoog committed Sep 29, 2024
1 parent aee7689 commit f7cf5e2
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 44 deletions.
102 changes: 102 additions & 0 deletions __tests__/validate-and-update-manifest.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import {
cleanUpOldReleases,
updateDocsVersion
} from '../src/validate-and-update-manifest'

// Declare the global variable type
declare global {
let currentVersion: string | undefined
}

describe('cleanUpOldReleases', () => {
it('keeps only versions with the two highest targetAbi', () => {
const input = [
{
guid: 'c83d86bb-a1e0-4c35-a113-e2101cf4ee6b',
name: 'Intro Skipper',
overview: 'Automatically detect and skip intros in television episodes',
description:
'Analyzes the audio of television episodes and detects introduction sequences.',
owner: 'AbandonedCart, rlauuzo, jumoog (forked from ConfusedPolarBear)',
category: 'General',
imageUrl:
'https://raw.githubusercontent.com/intro-skipper/intro-skipper/master/images/logo.png',
versions: [
{
version: '1.0.0',
targetAbi: '10.8.0',
changelog: '',
sourceUrl: '',
checksum: '',
timestamp: ''
},
{
version: '1.1.0',
targetAbi: '10.9.0',
changelog: '',
sourceUrl: '',
checksum: '',
timestamp: ''
},
{
version: '1.2.0',
targetAbi: '10.9.0',
changelog: '',
sourceUrl: '',
checksum: '',
timestamp: ''
},
{
version: '0.9.0',
targetAbi: '10.7.0',
changelog: '',
sourceUrl: '',
checksum: '',
timestamp: ''
}
]
}
]

const result = cleanUpOldReleases(input)

// Check if only 3 versions are kept (2 with 10.9.0 and 1 with 10.8.0)
expect(result[0].versions.length).toBe(3)
// Check if the kept versions have the correct targetAbi values
expect(result[0].versions.map(v => v.targetAbi).sort()).toEqual([
'10.8.0',
'10.9.0',
'10.9.0'
])
})
})

describe('updateDocsVersion', () => {
const currentVersion = '10.9.0'

it('updates the Jellyfin version in the documentation', () => {
const input = 'Requires Jellyfin 10.8.0 (or newer)'
const { updatedContent, wasUpdated } = updateDocsVersion(
input,
currentVersion
)

// Check if the version is updated correctly
expect(updatedContent).toBe('Requires Jellyfin 10.9.0 (or newer)')
// Check if the wasUpdated flag is true
expect(wasUpdated).toBe(true)
})

it('makes no changes if the version is already up to date', () => {
const input = 'Requires Jellyfin 10.9.0 (or newer)'
const { updatedContent, wasUpdated } = updateDocsVersion(
input,
currentVersion
)

// Check if the content remains unchanged
expect(updatedContent).toBe(input)
// Check if the wasUpdated flag is false
expect(wasUpdated).toBe(false)
})
})
60 changes: 41 additions & 19 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

85 changes: 61 additions & 24 deletions src/validate-and-update-manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@ import { URL } from 'url'
const repository = process.env.GITHUB_REPOSITORY
const version = process.env.NEW_FILE_VERSION
const gitHubRepoVisibilty = process.env.GITHUB_REPO_VISIBILITY
let currentVersion: Version | undefined
let currentVersion: Version
let targetAbi = ''

type Manifest = {
guid: string
name: string
overview: string
description: string
owner: string
category: string
imageUrl: string
versions: Version[]
}

type Version = {
version: string | undefined
version: string
changelog: string
targetAbi: string
sourceUrl: string
Expand All @@ -38,14 +49,15 @@ if (!fs.existsSync(bugReportFormPath)) {
core.setFailed(`${bugReportFormPath} file not found`)
}

const jsonData = JSON.parse(fs.readFileSync(manifestPath, 'utf8'))

export async function updateManifest(): Promise<void> {
let jsonData: Manifest[] = JSON.parse(fs.readFileSync(manifestPath, 'utf8'))

try {
currentVersion = await getNugetPackageVersion('Jellyfin.Model', '10.*-*')
targetAbi = `${currentVersion}.0`
const newVersion = {
version,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
version: version!,
changelog: `- See the full changelog at [GitHub](https://github.com/${repository}/releases/tag/10.9/v${version})\n`,
targetAbi,
sourceUrl: `https://github.com/${repository}/releases/download/10.9/v${version}/intro-skipper-v${version}.zip`,
Expand All @@ -62,10 +74,29 @@ export async function updateManifest(): Promise<void> {
jsonData[0].versions.unshift(newVersion)

core.info('Manifest updated successfully.')
updateDocsVersion(readmePath)
updateDocsVersion(bugReportFormPath)
const readmeContent = fs.readFileSync(readmePath, 'utf8')
const { updatedContent: updatedReadme, wasUpdated: readmeWasUpdated } =
updateDocsVersion(readmeContent, currentVersion.version)
if (readmeWasUpdated) {
fs.writeFileSync(readmePath, updatedReadme)
core.info(`Updated ${readmePath} with new Jellyfin version.`)
} else {
core.info(`${readmePath} has already newest Jellyfin version.`)
}

cleanUpOldReleases()
const bugReportFormContent = fs.readFileSync(bugReportFormPath, 'utf8')
const {
updatedContent: updatedBugReport,
wasUpdated: bugReportWasUpdated
} = updateDocsVersion(bugReportFormContent, currentVersion?.version)
if (bugReportWasUpdated) {
fs.writeFileSync(bugReportFormPath, updatedBugReport)
core.info(`Updated ${bugReportFormPath} with new Jellyfin version.`)
} else {
core.info(`${bugReportFormPath} has already newest Jellyfin version.`)
}

jsonData = cleanUpOldReleases(jsonData)

// Write the modified JSON data back to the file
fs.writeFileSync(manifestPath, JSON.stringify(jsonData, null, 4), 'utf8')
Expand Down Expand Up @@ -155,26 +186,23 @@ function getMD5FromFile(): string {
return crypto.createHash('md5').update(fileBuffer).digest('hex')
}

function updateDocsVersion(docsPath: string): void {
const readMeContent = fs.readFileSync(docsPath, 'utf8')

const updatedContent = readMeContent.replace(
export function updateDocsVersion(
content: string,
currentVersion: string
): { updatedContent: string; wasUpdated: boolean } {
const updatedContent = content.replace(
/Jellyfin.*\(or newer\)/,
`Jellyfin ${currentVersion} (or newer)`
)
if (readMeContent !== updatedContent) {
fs.writeFileSync(docsPath, updatedContent)
core.info(`Updated ${docsPath} with new Jellyfin version.`)
} else {
core.info(`${docsPath} has already newest Jellyfin version.`)
}
const wasUpdated = content !== updatedContent
return { updatedContent, wasUpdated }
}

function cleanUpOldReleases(): void {
export function cleanUpOldReleases(jsonData: Manifest[]): Manifest[] {
// Extract all unique targetAbi values
const abiSet = new Set()
for (const entry of jsonData) {
for (const v of entry.versions as Version[]) {
for (const v of entry.versions) {
abiSet.add(v.targetAbi)
}
}
Expand All @@ -196,17 +224,18 @@ function cleanUpOldReleases(): void {
const secondHighestAbi = abiArray[1]

// Filter the versions array to keep only those with the highest or second highest targetAbi
for (const entry of jsonData) {
entry.versions = (entry.versions as Version[]).filter(
return jsonData.map(entry => ({
...entry,
versions: entry.versions.filter(
v => v.targetAbi === highestAbi || v.targetAbi === secondHighestAbi
)
}
}))
}

async function getNugetPackageVersion(
packageName: string,
versionPattern: string
): Promise<Version | undefined> {
): Promise<Version> {
// Convert package name to lowercase for the NuGet API
const url = `https://api.nuget.org/v3-flatcontainer/${packageName.toLowerCase()}/index.json`

Expand Down Expand Up @@ -252,4 +281,12 @@ async function getNugetPackageVersion(
}`
)
}
return {
version: '',
changelog: '',
targetAbi: '',
sourceUrl: '',
checksum: '',
timestamp: ''
}
}

0 comments on commit f7cf5e2

Please sign in to comment.