2022-07-16 07:48:26 -06:00
|
|
|
import { logger } from '../../../logger';
|
2022-03-03 02:35:26 -07:00
|
|
|
import { cache } from '../../../util/cache/package/decorator';
|
|
|
|
import { regEx } from '../../../util/regex';
|
2021-03-02 13:44:55 -07:00
|
|
|
import type { DigestConfig, GetReleasesConfig, ReleaseResult } from '../types';
|
2021-10-25 22:37:26 -06:00
|
|
|
import { GitDatasource } from './base';
|
2021-05-11 04:51:21 -06:00
|
|
|
import type { RawRefs } from './types';
|
2020-03-27 10:41:29 -06:00
|
|
|
|
|
|
|
// git will prompt for known hosts or passwords, unless we activate BatchMode
|
|
|
|
process.env.GIT_SSH_COMMAND = 'ssh -o BatchMode=yes';
|
|
|
|
|
2022-05-18 03:56:59 -06:00
|
|
|
export class GitRefsDatasource extends GitDatasource {
|
|
|
|
static override readonly id = 'git-refs';
|
2020-06-25 06:07:03 -06:00
|
|
|
|
2021-10-25 22:37:26 -06:00
|
|
|
constructor() {
|
|
|
|
super(GitRefsDatasource.id);
|
2020-06-25 06:07:03 -06:00
|
|
|
}
|
|
|
|
|
2021-10-25 22:37:26 -06:00
|
|
|
override readonly customRegistrySupport = false;
|
2020-06-25 06:07:03 -06:00
|
|
|
|
2024-05-21 01:04:12 -06:00
|
|
|
override readonly sourceUrlSupport = 'package';
|
|
|
|
override readonly sourceUrlNote =
|
|
|
|
'The source URL is determined by using the `packageName` and `registryUrl`.';
|
|
|
|
|
2021-10-25 22:37:26 -06:00
|
|
|
@cache({
|
|
|
|
namespace: `datasource-${GitRefsDatasource.id}`,
|
2022-03-03 08:08:43 -07:00
|
|
|
key: ({ packageName }: GetReleasesConfig) => packageName,
|
2021-10-25 22:37:26 -06:00
|
|
|
})
|
|
|
|
override async getReleases({
|
2022-03-03 08:08:43 -07:00
|
|
|
packageName,
|
2021-10-25 22:37:26 -06:00
|
|
|
}: GetReleasesConfig): Promise<ReleaseResult | null> {
|
2022-07-16 07:48:26 -06:00
|
|
|
let rawRefs: RawRefs[] | null = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
rawRefs = await this.getRawRefs({ packageName });
|
|
|
|
} catch (err) /* istanbul ignore next */ {
|
|
|
|
logger.debug({ err }, 'Error getting git-refs');
|
|
|
|
}
|
2020-06-25 06:07:03 -06:00
|
|
|
|
2022-04-16 00:32:17 -06:00
|
|
|
if (!rawRefs) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-10-25 22:37:26 -06:00
|
|
|
const refs = rawRefs
|
|
|
|
.filter((ref) => ref.type === 'tags' || ref.type === 'heads')
|
2021-12-10 02:12:21 -07:00
|
|
|
.map((ref) => ref.value);
|
2020-06-25 06:07:03 -06:00
|
|
|
|
2021-10-25 22:37:26 -06:00
|
|
|
const uniqueRefs = [...new Set(refs)];
|
2020-06-25 06:07:03 -06:00
|
|
|
|
2022-03-03 08:08:43 -07:00
|
|
|
const sourceUrl = packageName
|
2021-10-25 22:37:26 -06:00
|
|
|
.replace(regEx(/\.git$/), '')
|
|
|
|
.replace(regEx(/\/$/), '');
|
2020-06-25 06:07:03 -06:00
|
|
|
|
2021-10-25 22:37:26 -06:00
|
|
|
const result: ReleaseResult = {
|
|
|
|
sourceUrl,
|
|
|
|
releases: uniqueRefs.map((ref) => ({
|
|
|
|
version: ref,
|
|
|
|
gitRef: ref,
|
2024-04-22 09:23:05 -06:00
|
|
|
newDigest: rawRefs.find((rawRef) => rawRef.value === ref)?.hash,
|
2021-10-25 22:37:26 -06:00
|
|
|
})),
|
|
|
|
};
|
2020-06-25 06:07:03 -06:00
|
|
|
|
2021-10-25 22:37:26 -06:00
|
|
|
return result;
|
|
|
|
}
|
2020-05-02 00:19:15 -06:00
|
|
|
|
2021-10-25 22:37:26 -06:00
|
|
|
override async getDigest(
|
2022-03-03 08:08:43 -07:00
|
|
|
{ packageName }: DigestConfig,
|
2023-11-07 08:50:29 -07:00
|
|
|
newValue?: string,
|
2021-10-25 22:37:26 -06:00
|
|
|
): Promise<string | null> {
|
2022-05-18 03:56:59 -06:00
|
|
|
const rawRefs: RawRefs[] | null = await this.getRawRefs({ packageName });
|
2022-04-16 00:32:17 -06:00
|
|
|
|
|
|
|
// istanbul ignore if
|
|
|
|
if (!rawRefs) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let ref: RawRefs | undefined;
|
2021-11-18 06:36:41 -07:00
|
|
|
if (newValue) {
|
|
|
|
ref = rawRefs.find(
|
|
|
|
(rawRef) =>
|
2023-11-07 08:50:29 -07:00
|
|
|
['heads', 'tags'].includes(rawRef.type) && rawRef.value === newValue,
|
2021-11-18 06:36:41 -07:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
ref = rawRefs.find(
|
2023-11-07 08:50:29 -07:00
|
|
|
(rawRef) => rawRef.type === '' && rawRef.value === 'HEAD',
|
2021-11-18 06:36:41 -07:00
|
|
|
);
|
|
|
|
}
|
2021-10-25 22:37:26 -06:00
|
|
|
if (ref) {
|
|
|
|
return ref.hash;
|
|
|
|
}
|
|
|
|
return null;
|
2020-05-02 00:19:15 -06:00
|
|
|
}
|
|
|
|
}
|