mirror of
https://github.com/renovatebot/renovate.git
synced 2026-10-25 15:01:49 +00:00
197 lines
5.3 KiB
TypeScript
197 lines
5.3 KiB
TypeScript
import semver from 'semver';
|
|
import { regEx } from '../../../util/regex.ts';
|
|
import { GoDatasource } from '../../datasource/go/index.ts';
|
|
import { GolangVersionDatasource } from '../../datasource/golang-version/index.ts';
|
|
import { parseVersionCommit } from '../../versioning/gomod/index.ts';
|
|
import { isVersion } from '../../versioning/semver/index.ts';
|
|
import type { PackageDependency } from '../types.ts';
|
|
import type { GoModManagerData } from './types.ts';
|
|
|
|
function trimQuotes(str: string): string {
|
|
return str.replace(regEx(/^"(?<value>.*)"$/), '$<value>');
|
|
}
|
|
|
|
const requireRegex = regEx(
|
|
/^(?<keyword>require)?\s+(?<module>[^\s]+\/?[^\s]+)\s+(?<version>[^\s]+)(?:\s*\/\/\s*(?<comment>[^\s]+)\s*)?$/,
|
|
);
|
|
|
|
const replaceRegex = regEx(
|
|
/^(?<keyword>replace)?\s+(?<module>[^\s]+\/?[^\s]+)\s*=>\s*(?<replacement>[^\s]+)(?:\s+(?<version>[^\s]+))?(?:\s*\/\/\s*(?<comment>[^\s]+)\s*)?$/,
|
|
);
|
|
|
|
export const excludeBlockStartRegex = regEx(/^(?<keyword>exclude)\s+\(\s*$/);
|
|
|
|
export const endBlockRegex = regEx(/^\s*\)\s*$/);
|
|
|
|
const toolRegex = regEx(/^(?<keyword>tool)?\s+(?<module>[^\s]+\/?[^\s]+)\s*$/);
|
|
|
|
const goVersionRegex = regEx(/^\s*go\s+(?<version>[^\s]+)\s*$/);
|
|
|
|
const toolchainVersionRegex = regEx(/^\s*toolchain\s+go(?<version>[^\s]+)\s*$/);
|
|
|
|
// Go writes this version for a module whose `replace` directive points to a
|
|
// local path, with the major version of the module path, such as
|
|
// `v2.0.0-00010101000000-000000000000` for a `/v2` module
|
|
const placeholderPseudoVersionRegex = regEx(
|
|
/^v\d+\.\d+\.\d+-00010101000000-000000000000$/,
|
|
);
|
|
|
|
function isPlaceholderPseudoVersion(version: string): boolean {
|
|
return placeholderPseudoVersionRegex.test(version);
|
|
}
|
|
|
|
export function parseLine(
|
|
input: string,
|
|
): PackageDependency<GoModManagerData> | null {
|
|
const goVersionMatches = goVersionRegex.exec(input)?.groups;
|
|
if (goVersionMatches) {
|
|
const { version: currentValue } = goVersionMatches;
|
|
|
|
const dep: PackageDependency<GoModManagerData> = {
|
|
datasource: GolangVersionDatasource.id,
|
|
versioning: 'go-mod-directive',
|
|
depType: 'golang',
|
|
depName: 'go',
|
|
currentValue,
|
|
commitMessageTopic: 'go module directive',
|
|
};
|
|
|
|
if (!semver.validRange(currentValue)) {
|
|
dep.skipReason = 'invalid-version';
|
|
}
|
|
|
|
return dep;
|
|
}
|
|
|
|
const toolchainMatches = toolchainVersionRegex.exec(input)?.groups;
|
|
if (toolchainMatches) {
|
|
const { version: currentValue } = toolchainMatches;
|
|
|
|
const dep: PackageDependency<GoModManagerData> = {
|
|
datasource: GolangVersionDatasource.id,
|
|
depType: 'toolchain',
|
|
depName: 'go',
|
|
currentValue,
|
|
commitMessageTopic: 'go toolchain directive',
|
|
};
|
|
|
|
if (!semver.valid(currentValue)) {
|
|
dep.skipReason = 'invalid-version';
|
|
}
|
|
|
|
return dep;
|
|
}
|
|
|
|
const requireMatches = requireRegex.exec(input)?.groups;
|
|
if (requireMatches) {
|
|
const { keyword, module, version: currentValue, comment } = requireMatches;
|
|
|
|
const depName = trimQuotes(module);
|
|
|
|
const dep: PackageDependency<GoModManagerData> = {
|
|
datasource: GoDatasource.id,
|
|
depType: 'require',
|
|
depName,
|
|
currentValue,
|
|
};
|
|
|
|
if (isVersion(currentValue)) {
|
|
const digest = parseVersionCommit(currentValue)?.revision;
|
|
if (digest) {
|
|
dep.currentDigest = digest;
|
|
dep.digestOneAndOnly = true;
|
|
dep.versioning = 'loose';
|
|
if (isPlaceholderPseudoVersion(currentValue)) {
|
|
dep.skipReason = 'invalid-version';
|
|
}
|
|
}
|
|
} else {
|
|
dep.skipReason = 'invalid-version';
|
|
}
|
|
|
|
if (comment === 'indirect') {
|
|
dep.depType = 'indirect';
|
|
dep.enabled = false;
|
|
}
|
|
|
|
if (!keyword) {
|
|
dep.managerData = { multiLine: true };
|
|
}
|
|
|
|
return dep;
|
|
}
|
|
|
|
const replaceMatches = replaceRegex.exec(input)?.groups;
|
|
if (replaceMatches) {
|
|
const {
|
|
keyword,
|
|
replacement,
|
|
version: currentValue,
|
|
comment,
|
|
} = replaceMatches;
|
|
|
|
const depName = trimQuotes(replacement);
|
|
|
|
const dep: PackageDependency<GoModManagerData> = {
|
|
datasource: GoDatasource.id,
|
|
depType: 'replace',
|
|
depName,
|
|
currentValue,
|
|
};
|
|
|
|
if (isVersion(currentValue)) {
|
|
const digest = parseVersionCommit(currentValue)?.revision;
|
|
if (digest) {
|
|
dep.currentDigest = digest;
|
|
dep.digestOneAndOnly = true;
|
|
dep.versioning = 'loose';
|
|
if (isPlaceholderPseudoVersion(currentValue)) {
|
|
dep.skipReason = 'invalid-version';
|
|
}
|
|
}
|
|
} else if (currentValue) {
|
|
dep.skipReason = 'invalid-version';
|
|
} else {
|
|
dep.skipReason = 'unspecified-version';
|
|
delete dep.currentValue;
|
|
}
|
|
|
|
if (comment === 'indirect') {
|
|
dep.depType = 'indirect';
|
|
dep.enabled = false;
|
|
}
|
|
|
|
if (!keyword) {
|
|
dep.managerData = { multiLine: true };
|
|
}
|
|
|
|
if (depName.startsWith('/') || depName.startsWith('.')) {
|
|
dep.skipReason = 'local-dependency';
|
|
}
|
|
|
|
return dep;
|
|
}
|
|
|
|
const toolMatches = toolRegex.exec(input)?.groups;
|
|
if (toolMatches) {
|
|
const { keyword, module } = toolMatches;
|
|
|
|
const depName = trimQuotes(module);
|
|
|
|
const dep: PackageDependency<GoModManagerData> = {
|
|
datasource: GoDatasource.id,
|
|
depType: 'tool',
|
|
depName,
|
|
skipReason: 'unversioned-reference',
|
|
};
|
|
|
|
if (!keyword) {
|
|
dep.managerData = { multiLine: true };
|
|
}
|
|
|
|
return dep;
|
|
}
|
|
|
|
return null;
|
|
}
|