Skip to content

Commit

Permalink
refactor(github): Simplify datasource cacheability detection (#18852)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael Kriese <michael.kriese@visualon.de>
  • Loading branch information
zharinov and viceice committed Nov 10, 2022
1 parent 5162394 commit 9b76b2e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
36 changes: 35 additions & 1 deletion lib/util/github/graphql/datasource-helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ import { parse as graphqlParse } from 'graphql';
import * as httpMock from '../../../../test/http-mock';
import { GithubGraphqlResponse, GithubHttp } from '../../http/github';
import { range } from '../../range';
import { GithubGraphqlDatasourceHelper as Datasource } from './datasource-helper';
import {
GithubGraphqlDatasourceHelper as Datasource,
GithubGraphqlDatasourceHelper,
} from './datasource-helper';
import type {
GithubDatasourceItem,
GithubGraphqlDatasourceAdapter,
Expand Down Expand Up @@ -382,5 +385,36 @@ describe('util/github/graphql/datasource-helper', () => {
]);
});
});

describe('Cacheable flag', () => {
const data = [
{ version: v1, releaseTimestamp: t1, foo: '1' },
{ version: v2, releaseTimestamp: t2, foo: '2' },
{ version: v3, releaseTimestamp: t3, foo: '3' },
];

test.each`
isPrivate | isCacheable
${true} | ${false}
${false} | ${true}
`(
'private=$isPrivate => isCacheable=$isCacheable',
async ({ isPrivate, isCacheable }) => {
httpMock
.scope('https://api.github.com/')
.post('/graphql')
.reply(200, resp(data, undefined, isPrivate));

const instance = new GithubGraphqlDatasourceHelper(
{ packageName: 'foo/bar' },
http,
adapter
);
await instance.getItems();

expect(instance).toHaveProperty('isCacheable', isCacheable);
}
);
});
});
});
16 changes: 10 additions & 6 deletions lib/util/github/graphql/datasource-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export class GithubGraphqlDatasourceHelper<

private cursor: string | null = null;

private isCacheable = false;

constructor(
packageConfig: GithubPackageConfig,
private http: GithubHttp,
Expand Down Expand Up @@ -166,8 +168,11 @@ export class GithubGraphqlDatasourceHelper<

this.queryCount += 1;

const isRepoPrivate = data.repository.isRepoPrivate;
const res = { ...data.repository.payload, isRepoPrivate };
if (!this.isCacheable && data.repository.isRepoPrivate === false) {
this.isCacheable = true;
}

const res = data.repository.payload;
return [res, null];
}

Expand Down Expand Up @@ -243,9 +248,8 @@ export class GithubGraphqlDatasourceHelper<

/**
* This method intentionally was made not async, though it returns `Promise`.
*
* It helps us to avoid potential race conditions during concurrent fetching
* of the same package releases.
* This method doesn't make pages to be fetched concurrently.
* Instead, it ensures that same package release is not fetched twice.
*/
private doConcurrentQuery(): Promise<ResultItem[]> {
const packageFingerprint = this.getFingerprint();
Expand All @@ -256,7 +260,7 @@ export class GithubGraphqlDatasourceHelper<
return resultPromise;
}

private async getItems(): Promise<ResultItem[]> {
async getItems(): Promise<ResultItem[]> {
const res = await this.doConcurrentQuery();
return res;
}
Expand Down
12 changes: 2 additions & 10 deletions lib/util/github/graphql/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface GithubGraphqlDatasourceAdapter<

export type RawQueryResponse<Payload> = [Payload, null] | [null, Error];

export interface GithubGraphqlRepoResponsePayload<T> {
export interface GithubGraphqlPayload<T> {
nodes: T[];
pageInfo?: {
hasNextPage?: boolean;
Expand All @@ -42,18 +42,10 @@ export interface GithubGraphqlRepoResponsePayload<T> {
export interface GithubGraphqlRepoResponse<T> {
repository: {
isRepoPrivate?: boolean;
payload: GithubGraphqlRepoResponsePayload<T>;
payload: GithubGraphqlPayload<T>;
};
}

/**
* Payload data unified with `isRepoPrivate` flag moved from upper level
*/
export type GithubGraphqlPayload<T> =
GithubGraphqlRepoResponse<T>['repository']['payload'] & {
isRepoPrivate: GithubGraphqlRepoResponse<T>['repository']['isRepoPrivate'];
};

export interface GithubPackageConfig {
/**
* Example: renovatebot/renovate
Expand Down

0 comments on commit 9b76b2e

Please sign in to comment.