Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove normalizeDate function #33513

Merged
merged 13 commits into from
Jan 13, 2025
Merged
8 changes: 4 additions & 4 deletions lib/modules/datasource/maven/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { Result } from '../../../util/result';
import type { S3UrlParts } from '../../../util/s3';
import { getS3Client, parseS3Url } from '../../../util/s3';
import { streamToString } from '../../../util/streams';
import { asTimestamp } from '../../../util/timestamp';
import { ensureTrailingSlash, parseUrl } from '../../../util/url';
import { normalizeDate } from '../metadata';
import { getGoogleAuthToken } from '../util';
import { MAVEN_REPO } from './common';
import type {
Expand Down Expand Up @@ -83,7 +83,7 @@ export async function downloadHttpProtocol(
result.isCacheable = true;
}

const lastModified = normalizeDate(res?.headers?.['last-modified']);
const lastModified = asTimestamp(res?.headers?.['last-modified']);
if (lastModified) {
result.lastModified = lastModified;
}
Expand Down Expand Up @@ -203,7 +203,7 @@ export async function downloadS3Protocol(
const data = await streamToString(Body);
const result: MavenFetchSuccess = { data };

const lastModified = normalizeDate(LastModified);
const lastModified = asTimestamp(LastModified);
if (lastModified) {
result.lastModified = lastModified;
}
Expand Down Expand Up @@ -276,7 +276,7 @@ async function checkHttpResource(
const res = await http.head(pkgUrl.toString());
const timestamp = res?.headers?.['last-modified'];
if (timestamp) {
const isoTimestamp = normalizeDate(timestamp);
const isoTimestamp = asTimestamp(timestamp);
if (isoTimestamp) {
const releaseDate = DateTime.fromISO(isoTimestamp, {
zone: 'UTC',
Expand Down
20 changes: 0 additions & 20 deletions lib/modules/datasource/metadata.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
addMetaData,
massageGithubUrl,
massageUrl,
normalizeDate,
shouldDeleteHomepage,
} from './metadata';
import { NpmDatasource } from './npm';
Expand Down Expand Up @@ -521,23 +520,4 @@ describe('modules/datasource/metadata', () => {
sourceUrl: 'https://github.com/flyingcircusio/pycountry',
});
});

describe('normalizeDate()', () => {
it('works for number input', () => {
const now = Date.now();
expect(normalizeDate(now)).toBe(new Date(now).toISOString());
});

it('works for string input', () => {
expect(normalizeDate('2021-01-01')).toBe(
new Date('2021-01-01').toISOString(),
);
});

it('works for Date instance', () => {
expect(normalizeDate(new Date('2021-01-01'))).toBe(
new Date('2021-01-01').toISOString(),
);
});
});
});
52 changes: 2 additions & 50 deletions lib/modules/datasource/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import is from '@sindresorhus/is';
import parse from 'github-url-from-git';
import { DateTime } from 'luxon';
import { detectPlatform } from '../../util/common';
import { parseGitUrl } from '../../util/git/url';
import * as hostRules from '../../util/host-rules';
import { regEx } from '../../util/regex';
import { asTimestamp } from '../../util/timestamp';
import { isHttpUrl, parseUrl, trimTrailingSlash } from '../../util/url';
import { manualChangelogUrls, manualSourceUrls } from './metadata-manual';
import type { ReleaseResult } from './types';
Expand Down Expand Up @@ -64,59 +64,11 @@ function massageGitAtUrl(url: string): string {
return massagedUrl;
}

/**
* @deprecated Use `asTimestamp` instead
*/
export function normalizeDate(input: any): string | null {
if (
typeof input === 'number' &&
!Number.isNaN(input) &&
input > 0 &&
input <= Date.now() + 24 * 60 * 60 * 1000
) {
return new Date(input).toISOString();
}

if (typeof input === 'string') {
// `Date.parse()` is more permissive, but it assumes local time zone
// for inputs like `2021-01-01`.
//
// Here we try to parse with default UTC with fallback to `Date.parse()`.
//
// It allows us not to care about machine timezones so much, though
// some misinterpretation is still possible, but only if both:
//
// 1. Renovate machine is configured for non-UTC zone
// 2. Format of `input` is very exotic
// (from `DateTime.fromISO()` perspective)
//

let luxonDate = DateTime.fromISO(input, { zone: 'UTC' });
if (luxonDate.isValid) {
return luxonDate.toISO();
}
luxonDate = DateTime.fromFormat(input, 'yyyyMMddHHmmss', {
zone: 'UTC',
});
if (luxonDate.isValid) {
return luxonDate.toISO();
}

return normalizeDate(Date.parse(input));
}

if (input instanceof Date) {
return input.toISOString();
}

return null;
}

function massageTimestamps(dep: ReleaseResult): void {
for (const release of dep.releases || []) {
let { releaseTimestamp } = release;
delete release.releaseTimestamp;
releaseTimestamp = normalizeDate(releaseTimestamp);
releaseTimestamp = asTimestamp(releaseTimestamp);
if (releaseTimestamp) {
release.releaseTimestamp = releaseTimestamp;
}
Expand Down
Loading
Loading