Skip to content

Commit

Permalink
fix(buildkite): inspect all lines for plugin definitions (#15548)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpierce committed May 16, 2022
1 parent 123d426 commit 06a2262
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 72 deletions.
2 changes: 1 addition & 1 deletion lib/modules/manager/buildkite/__fixtures__/pipeline2.yml
Expand Up @@ -2,7 +2,7 @@ steps:
# Prebuild the app image, upload it to a registry for later steps
- name: "Docker Build"
plugins:
docker-compose#v1.3.2:
docker-compose#v1.3.2: # Comment at the end....
build: app
image-repository: index.docker.io/org/repo

Expand Down
8 changes: 8 additions & 0 deletions lib/modules/manager/buildkite/__fixtures__/pipeline7.yml
@@ -0,0 +1,8 @@
.docker-options: &some-options
propagate-environment: true
copy-checkout: true

.python3-container: &python3-container
ssh://git@github.some-domain.com/some-org/some-plugin#v3.2.7:
some-config: some-value
<<: *some-options
11 changes: 11 additions & 0 deletions lib/modules/manager/buildkite/extract.spec.ts
Expand Up @@ -49,5 +49,16 @@ describe('modules/manager/buildkite/extract', () => {
expect(res).toHaveLength(1);
expect(res).toEqual([expectedPackageDependency]);
});

it('extracts plugins outside plugins sections', () => {
const res = extractPackageFile(Fixtures.get('pipeline7.yml'))?.deps;
const expectedPackageDependency: PackageDependency = {
currentValue: 'v3.2.7',
datasource: 'github-tags',
depName: 'some-org/some-plugin',
registryUrls: ['https://github.some-domain.com'],
};
expect(res).toEqual([expectedPackageDependency]);
});
});
});
123 changes: 52 additions & 71 deletions lib/modules/manager/buildkite/extract.ts
Expand Up @@ -9,85 +9,66 @@ export function extractPackageFile(content: string): PackageFile | null {
const deps: PackageDependency[] = [];
try {
const lines = content.split(newlineRegex);
let isPluginsSection = false;
let pluginsIndent = '';
for (let lineNumber = 1; lineNumber <= lines.length; lineNumber += 1) {
const lineIdx = lineNumber - 1;
const line = lines[lineIdx];
const pluginsSection = regEx(
/^(?<pluginsIndent>\s*)(-?\s*)plugins:/

for (const line of lines) {
// Search each line for plugin names
const depLineMatch = regEx(
/^[\s-]*(?<depName>[^#\s]+)#(?<currentValue>[^:]+)/
).exec(line);
if (pluginsSection?.groups) {
logger.trace(`Matched plugins on line ${lineNumber}`);
isPluginsSection = true;
pluginsIndent = pluginsSection.groups.pluginsIndent;
} else if (isPluginsSection) {
logger.debug(`serviceImageLine: "${line}"`);
const { currentIndent } = regEx(/^(?<currentIndent>\s*)/).exec(line)
?.groups ?? /* istanbul ignore next: should never happen */ {
currentIndent: '',
};
const depLineMatch = regEx(
/^\s+(?:-\s+)?(?<depName>[^#]+)#(?<currentValue>[^:]+)/
).exec(line);
if (currentIndent.length <= pluginsIndent.length) {
isPluginsSection = false;
pluginsIndent = '';
} else if (depLineMatch?.groups) {
const { depName, currentValue } = depLineMatch.groups;
logger.trace('depLineMatch');
let skipReason: SkipReason | undefined;
let repo: string | undefined;
const gitPluginMatch = regEx(
/(ssh:\/\/git@|https:\/\/)(?<registry>[^/]+)\/(?<gitPluginName>.*)/
).exec(depName);
if (gitPluginMatch?.groups) {
logger.debug('Examining git plugin');
const { registry, gitPluginName } = gitPluginMatch.groups;
const gitDepName = gitPluginName.replace(regEx('\\.git$'), '');
const dep: PackageDependency = {
depName: gitDepName,
currentValue: currentValue,
registryUrls: ['https://' + registry],
datasource: GithubTagsDatasource.id,
};
deps.push(dep);
continue;
} else if (isVersion(currentValue)) {
const splitName = depName.split('/');
if (splitName.length === 1) {
repo = `buildkite-plugins/${depName}-buildkite-plugin`;
} else if (splitName.length === 2) {
repo = `${depName}-buildkite-plugin`;
} else {
logger.warn(
{ dependency: depName },
'Something is wrong with buildkite plugin name'
);
skipReason = 'invalid-dependency-specification';
}
} else {
logger.debug(
{ currentValue },
'Skipping non-pinned current version'
);
skipReason = 'invalid-version';
}

if (depLineMatch?.groups) {
const { depName, currentValue } = depLineMatch.groups;
logger.trace('depLineMatch');
let skipReason: SkipReason | undefined;
let repo: string | undefined;
logger.debug({ depName }, 'Found BuildKite plugin');
// Plugins may simply be git repos. If so, we need to parse out the registry.
const gitPluginMatch = regEx(
/(ssh:\/\/git@|https:\/\/)(?<registry>[^/]+)\/(?<gitPluginName>.*)/
).exec(depName);
if (gitPluginMatch?.groups) {
logger.debug('Examining git plugin');
const { registry, gitPluginName } = gitPluginMatch.groups;
const gitDepName = gitPluginName.replace(regEx('\\.git$'), '');
const dep: PackageDependency = {
depName,
currentValue,
skipReason,
depName: gitDepName,
currentValue: currentValue,
registryUrls: ['https://' + registry],
datasource: GithubTagsDatasource.id,
};
if (repo) {
dep.datasource = GithubTagsDatasource.id;
dep.packageName = repo;
}
deps.push(dep);
continue;
} else if (isVersion(currentValue)) {
const splitName = depName.split('/');
if (splitName.length === 1) {
repo = `buildkite-plugins/${depName}-buildkite-plugin`;
} else if (splitName.length === 2) {
repo = `${depName}-buildkite-plugin`;
} else {
logger.warn(
{ dependency: depName },
'Something is wrong with BuildKite plugin name'
);
skipReason = 'invalid-dependency-specification';
}
} else {
logger.debug({ currentValue }, 'Skipping non-pinned current version');
skipReason = 'invalid-version';
}
const dep: PackageDependency = {
depName,
currentValue,
skipReason,
};
if (repo) {
dep.datasource = GithubTagsDatasource.id;
dep.packageName = repo;
}
deps.push(dep);
}
}
} catch (err) /* istanbul ignore next */ {
logger.warn({ err }, 'Error extracting buildkite plugins');
logger.warn({ err }, 'Error extracting BuildKite plugins');
}

if (!deps.length) {
Expand Down

0 comments on commit 06a2262

Please sign in to comment.