Skip to content

Commit

Permalink
feat(datasource/galaxy): use schema validation and migrate to new hos…
Browse files Browse the repository at this point in the history
…tname (#25216)
  • Loading branch information
secustor committed Oct 16, 2023
1 parent 98dccff commit 4a8860f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 33 deletions.
Expand Up @@ -2,8 +2,8 @@

exports[`modules/datasource/galaxy/index getReleases processes real data 1`] = `
{
"dependencyUrl": "https://old-galaxy.ansible.com/yatesr/timezone",
"registryUrl": "https://old-galaxy.ansible.com/",
"dependencyUrl": "https://galaxy.ansible.com/yatesr/timezone",
"registryUrl": "https://galaxy.ansible.com/",
"releases": [
{
"releaseTimestamp": "2015-11-17T00:43:42.000Z",
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/datasource/galaxy/index.spec.ts
Expand Up @@ -4,7 +4,7 @@ import * as httpMock from '../../../../test/http-mock';
import { EXTERNAL_HOST_ERROR } from '../../../constants/error-messages';
import { GalaxyDatasource } from '.';

const baseUrl = 'https://old-galaxy.ansible.com/';
const baseUrl = 'https://galaxy.ansible.com/';

describe('modules/datasource/galaxy/index', () => {
describe('getReleases', () => {
Expand Down
26 changes: 8 additions & 18 deletions lib/modules/datasource/galaxy/index.ts
@@ -1,10 +1,10 @@
import is from '@sindresorhus/is';
import { logger } from '../../../logger';
import { cache } from '../../../util/cache/package/decorator';
import type { HttpResponse } from '../../../util/http/types';
import * as pep440Versioning from '../../versioning/pep440';
import { Datasource } from '../datasource';
import type { GetReleasesConfig, Release, ReleaseResult } from '../types';
import type { GalaxyResult } from './types';
import { GalaxyV1 } from './schema';

export class GalaxyDatasource extends Datasource {
static readonly id = 'galaxy';
Expand All @@ -15,7 +15,7 @@ export class GalaxyDatasource extends Datasource {

override readonly customRegistrySupport = false;

override readonly defaultRegistryUrls = ['https://old-galaxy.ansible.com/'];
override readonly defaultRegistryUrls = ['https://galaxy.ansible.com/'];

override readonly defaultVersioning = pep440Versioning.id;

Expand All @@ -32,25 +32,15 @@ export class GalaxyDatasource extends Datasource {
const userName = lookUp[0];
const projectName = lookUp[1];

// TODO: types (#22198)
const galaxyAPIUrl = `${registryUrl}api/v1/roles/?owner__username=${userName}&name=${projectName}`;
const galaxyProjectUrl = `${registryUrl}${userName}/${projectName}`;

let raw: HttpResponse<GalaxyResult> | null = null;
let body: GalaxyV1 | null = null;
try {
raw = await this.http.getJson<GalaxyResult>(galaxyAPIUrl);
const raw = await this.http.getJson(galaxyAPIUrl, GalaxyV1);
body = raw.body;
} catch (err) {
this.handleGenericErrors(err);
}

const body = raw?.body;

if (!body) {
logger.warn(
{ dependency: packageName },
`Received invalid data from ${galaxyAPIUrl}`
);
return null;
throw this.handleGenericErrors(err);
}

// istanbul ignore if
Expand Down Expand Up @@ -78,7 +68,7 @@ export class GalaxyDatasource extends Datasource {

result.dependencyUrl = galaxyProjectUrl;
const { github_user: user, github_repo: repo } = resultObject;
if (typeof user === 'string' && typeof repo === 'string') {
if (is.nonEmptyString(user) && is.nonEmptyString(repo)) {
result.sourceUrl = `https://github.com/${user}/${repo}`;
}

Expand Down
19 changes: 19 additions & 0 deletions lib/modules/datasource/galaxy/schema.ts
@@ -0,0 +1,19 @@
import { z } from 'zod';

export type GalaxyV1 = z.infer<typeof GalaxyV1>;
export const GalaxyV1 = z.object({
results: z.array(
z.object({
summary_fields: z.object({
versions: z.array(
z.object({
name: z.string(),
release_date: z.string(),
})
),
}),
github_user: z.string().optional(),
github_repo: z.string().optional(),
})
),
});
12 changes: 0 additions & 12 deletions lib/modules/datasource/galaxy/types.ts

This file was deleted.

0 comments on commit 4a8860f

Please sign in to comment.