Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jenkins-plugins): support yaml file format (#9069)
Co-authored-by: HonkingGoose <34918129+HonkingGoose@users.noreply.github.com> Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
- Loading branch information
1 parent
af1e4ee
commit 62e7fe3
Showing
9 changed files
with
239 additions
and
33 deletions.
There are no files selected for viewing
Empty file.
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,4 @@ | ||
## This is an invalid YAML document | ||
|
||
text and plugins are not supported and | ||
invalid-plugin:0.0.0 |
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,26 @@ | ||
plugins: | ||
- artifactId: git | ||
source: | ||
version: latest | ||
- artifactId: job-import-plugin | ||
source: | ||
version: '2.10' | ||
- artifactId: invalid-version-plugin | ||
source: | ||
version: 2.10 # Float will cause the version to be 2.1 and will be ignored with a warning | ||
- artifactId: ignore-plugin | ||
source: | ||
version: '2.10' | ||
renovate: | ||
ignore: true | ||
- artifactId: docker | ||
- artifactId: cloudbees-bitbucket-branch-source | ||
source: | ||
version: experimental | ||
- artifactId: script-security | ||
source: | ||
url: http://ftp-chi.osuosl.org/pub/jenkins/plugins/script-security/1.56/script-security.hpi | ||
- artifactId: workflow-step-api | ||
groupId: org.jenkins-ci.plugins.workflow | ||
source: | ||
version: 2.19-rc289.d09828a05a74 |
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
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
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 |
---|---|---|
@@ -1,33 +1,112 @@ | ||
import yaml from 'js-yaml'; | ||
import * as datasourceJenkins from '../../datasource/jenkins-plugins'; | ||
import { logger } from '../../logger'; | ||
import { SkipReason } from '../../types'; | ||
import { isSkipComment } from '../../util/ignore'; | ||
import * as dockerVersioning from '../../versioning/docker'; | ||
import type { PackageDependency, PackageFile } from '../types'; | ||
import type { JenkinsPlugin, JenkinsPlugins } from './types'; | ||
|
||
export function extractPackageFile(content: string): PackageFile | null { | ||
logger.trace('jenkins.extractPackageFile()'); | ||
const YamlExtension = /\.ya?ml$/; | ||
|
||
function getDependency(plugin: JenkinsPlugin): PackageDependency { | ||
const dep: PackageDependency = { | ||
datasource: datasourceJenkins.id, | ||
versioning: dockerVersioning.id, | ||
depName: plugin.artifactId, | ||
}; | ||
|
||
if (plugin.source?.version) { | ||
dep.currentValue = plugin.source.version.toString(); | ||
if (typeof plugin.source.version !== 'string') { | ||
dep.skipReason = SkipReason.InvalidVersion; | ||
logger.warn( | ||
{ dep }, | ||
'Jenkins plugin dependency version is not a string and will be ignored' | ||
); | ||
} | ||
} else { | ||
dep.skipReason = SkipReason.NoVersion; | ||
} | ||
|
||
if ( | ||
plugin.source?.version === 'latest' || | ||
plugin.source?.version === 'experimental' || | ||
plugin.groupId | ||
) { | ||
dep.skipReason = SkipReason.UnsupportedVersion; | ||
} | ||
|
||
if (plugin.source?.url) { | ||
dep.skipReason = SkipReason.InternalPackage; | ||
} | ||
|
||
if (!dep.skipReason && plugin.renovate?.ignore) { | ||
dep.skipReason = SkipReason.Ignored; | ||
} | ||
|
||
logger.debug({ dep }, 'Jenkins plugin dependency'); | ||
return dep; | ||
} | ||
|
||
function extractYaml(content: string): PackageDependency[] { | ||
const deps: PackageDependency[] = []; | ||
|
||
try { | ||
const doc = yaml.safeLoad(content, { json: true }) as JenkinsPlugins; | ||
if (doc?.plugins) { | ||
for (const plugin of doc.plugins) { | ||
if (plugin.artifactId) { | ||
const dep = getDependency(plugin); | ||
deps.push(dep); | ||
} | ||
} | ||
} | ||
} catch (err) { | ||
logger.warn({ err }, 'Error parsing Jenkins plugins'); | ||
} | ||
return deps; | ||
} | ||
|
||
function extractText(content: string): PackageDependency[] { | ||
const deps: PackageDependency[] = []; | ||
const regex = /^\s*(?<depName>[\d\w-]+):(?<currentValue>[^#\s]+)[#\s]*(?<comment>.*)$/; | ||
|
||
for (const line of content.split('\n')) { | ||
const match = regex.exec(line); | ||
|
||
if (match) { | ||
const { depName, currentValue, comment } = match.groups; | ||
const dep: PackageDependency = { | ||
datasource: datasourceJenkins.id, | ||
versioning: dockerVersioning.id, | ||
depName, | ||
currentValue, | ||
const plugin: JenkinsPlugin = { | ||
artifactId: depName, | ||
source: { | ||
version: currentValue, | ||
}, | ||
renovate: { | ||
ignore: isSkipComment(comment), | ||
}, | ||
}; | ||
|
||
if (isSkipComment(comment)) { | ||
dep.skipReason = SkipReason.Ignored; | ||
} | ||
const dep = getDependency(plugin); | ||
deps.push(dep); | ||
} | ||
} | ||
return deps; | ||
} | ||
|
||
export function extractPackageFile( | ||
content: string, | ||
fileName: string | ||
): PackageFile | null { | ||
logger.trace('jenkins.extractPackageFile()'); | ||
const deps: PackageDependency[] = []; | ||
|
||
if (YamlExtension.test(fileName)) { | ||
deps.push(...extractYaml(content)); | ||
} else { | ||
deps.push(...extractText(content)); | ||
} | ||
|
||
if (deps.length === 0) { | ||
return null; | ||
} | ||
return { deps }; | ||
} |
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
export { extractPackageFile } from './extract'; | ||
|
||
export const defaultConfig = { | ||
fileMatch: ['(^|/)plugins\\.txt'], | ||
fileMatch: ['(^|/)plugins\\.(txt|ya?ml)$'], | ||
}; |
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 |
---|---|---|
@@ -1,13 +1,4 @@ | ||
The Jenkins manager supports the following format of the plugin list: | ||
The Jenkins manager supports a custom text or YAML format of the plugin list as described [here](https://github.com/jenkinsci/plugin-installation-manager-tool#plugin-input-format). | ||
Only versions from the main [Jenkins update center](https://updates.jenkins.io/) are supported. | ||
|
||
```text | ||
plugin1:1.2.3 | ||
plugin2:4.5 # this is a comment | ||
# this line is ignored | ||
# Renovate will not upgrade the following dependency: | ||
plugin3:7.8.9 # renovate:ignore | ||
``` | ||
|
||
There's no strict specification on the name of the files, but usually it's `plugins.txt` | ||
There are no strict filename rules, the convention is to name the file `plugins.txt` or `plugins.yaml`. |
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,19 @@ | ||
export interface JenkinsPluginRenovate { | ||
ignore?: boolean; | ||
} | ||
|
||
export interface JenkinsPluginSource { | ||
version?: string; | ||
url?: string; | ||
} | ||
|
||
export interface JenkinsPlugin { | ||
artifactId?: string; | ||
groupId?: string; | ||
source?: JenkinsPluginSource; | ||
renovate?: JenkinsPluginRenovate; | ||
} | ||
|
||
export interface JenkinsPlugins { | ||
plugins?: JenkinsPlugin[]; | ||
} |