Skip to content

Commit

Permalink
feat(tkresults): update filters api call
Browse files Browse the repository at this point in the history
  • Loading branch information
abhinandan13jan committed Nov 5, 2024
1 parent 44f9653 commit a0f63f2
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
36 changes: 19 additions & 17 deletions src/utils/__tests__/tekton-results.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,10 +350,10 @@ describe('tekton-results', () => {

describe('createTektonResultsUrl', () => {
it('should create minimal URL', () => {
expect(createTektonResultsUrl('test-ws', 'test-ns', DataType.PipelineRun)).toEqual(
expect(createTektonResultsUrl('test-ws', 'test-ns', [DataType.PipelineRun])).toEqual(
'/plugins/tekton-results/workspaces/test-ws/apis/results.tekton.dev/v1alpha2/parents/test-ns/results/-/records?order_by=create_time+desc&page_size=30&filter=data_type+%3D%3D+%22tekton.dev%2Fv1beta1.PipelineRun%22',
);
expect(createTektonResultsUrl('test-ws', 'test-ns', DataType.TaskRun)).toEqual(
expect(createTektonResultsUrl('test-ws', 'test-ns', [DataType.TaskRun])).toEqual(
'/plugins/tekton-results/workspaces/test-ws/apis/results.tekton.dev/v1alpha2/parents/test-ns/results/-/records?order_by=create_time+desc&page_size=30&filter=data_type+%3D%3D+%22tekton.dev%2Fv1beta1.TaskRun%22',
);
});
Expand All @@ -363,7 +363,7 @@ describe('tekton-results', () => {
createTektonResultsUrl(
'test-ws',
'test-ns',
DataType.PipelineRun,
[DataType.PipelineRun],
null,
null,
'test-token',
Expand All @@ -372,34 +372,36 @@ describe('tekton-results', () => {
});

it('should create URL with filter', () => {
expect(createTektonResultsUrl('test-ws', 'test-ns', DataType.PipelineRun, 'foo=bar')).toEqual(
expect(
createTektonResultsUrl('test-ws', 'test-ns', [DataType.PipelineRun], 'foo=bar'),
).toEqual(
'/plugins/tekton-results/workspaces/test-ws/apis/results.tekton.dev/v1alpha2/parents/test-ns/results/-/records?order_by=create_time+desc&page_size=30&filter=data_type+%3D%3D+%22tekton.dev%2Fv1beta1.PipelineRun%22+%26%26+foo%3Dbar',
);
});

it('should create URL with page size', () => {
// default page size
expect(createTektonResultsUrl('test-ws', 'test-ns', DataType.PipelineRun)).toContain(
expect(createTektonResultsUrl('test-ws', 'test-ns', [DataType.PipelineRun])).toContain(
'page_size=30',
);
// min page size
expect(
createTektonResultsUrl('test-ws', 'test-ns', DataType.PipelineRun, '', {
createTektonResultsUrl('test-ws', 'test-ns', [DataType.PipelineRun], '', {
pageSize: 1,
}),
).toContain('page_size=5');

// min page size
expect(
createTektonResultsUrl('test-ws', 'test-ns', DataType.PipelineRun, '', {
createTektonResultsUrl('test-ws', 'test-ns', [DataType.PipelineRun], '', {
pageSize: 11000,
}),
).toContain('page_size=10000');
});

it('should create URL using limit to affect page size', () => {
expect(
createTektonResultsUrl('test-ws', 'test-ns', DataType.PipelineRun, '', {
createTektonResultsUrl('test-ws', 'test-ns', [DataType.PipelineRun], '', {
pageSize: 10,
limit: 5,
}),
Expand All @@ -411,7 +413,7 @@ describe('tekton-results', () => {
createTektonResultsUrl(
'test-ws',
'test-ns',
DataType.PipelineRun,
[DataType.PipelineRun],
'foo=bar',
sampleOptions,
),
Expand All @@ -425,16 +427,16 @@ describe('tekton-results', () => {
it('should return cached value', async () => {
commonFetchJSONMock.mockReturnValue(mockEmptyRecordsList);

await getFilteredRecord('test-ws', 'test-ns', DataType.PipelineRun);
await getFilteredRecord('test-ws', 'test-ns', DataType.PipelineRun);
await getFilteredRecord('test-ws', 'test-ns', [DataType.PipelineRun]);
await getFilteredRecord('test-ws', 'test-ns', [DataType.PipelineRun]);
expect(commonFetchJSONMock).toHaveBeenCalledTimes(2);

commonFetchJSONMock.mockClear();

await getFilteredRecord(
'test-ws',
'test-ns',
DataType.PipelineRun,
[DataType.PipelineRun],
null,
null,
null,
Expand All @@ -443,7 +445,7 @@ describe('tekton-results', () => {
await getFilteredRecord(
'test-ws',
'test-ns',
DataType.PipelineRun,
[DataType.PipelineRun],
null,
null,
null,
Expand All @@ -458,7 +460,7 @@ describe('tekton-results', () => {
code: 404,
};
});
const result = await getFilteredRecord('test-ws', 'test-ns', DataType.PipelineRun);
const result = await getFilteredRecord('test-ws', 'test-ns', [DataType.PipelineRun]);
expect(result).toEqual([[], { nextPageToken: null, records: [] }]);
});

Expand All @@ -469,21 +471,21 @@ describe('tekton-results', () => {
};
});
await expect(
getFilteredRecord('test-ws', 'test-ns', DataType.PipelineRun),
getFilteredRecord('test-ws', 'test-ns', [DataType.PipelineRun]),
).rejects.toBeTruthy();
});

it('should return record list and decoded value', async () => {
commonFetchJSONMock.mockReturnValue(mockRecordsList);
expect(await getFilteredRecord('test-ws', 'test-ns', DataType.PipelineRun)).toEqual(
expect(await getFilteredRecord('test-ws', 'test-ns', [DataType.PipelineRun])).toEqual(
mockResponseCheck,
);
});

it('should return record list and decoded value', async () => {
commonFetchJSONMock.mockReturnValue(mockRecordsList);
expect(
await getFilteredRecord('test-ws', 'test-ns', DataType.PipelineRun, null, { limit: 1 }),
await getFilteredRecord('test-ws', 'test-ns', [DataType.PipelineRun], null, { limit: 1 }),
).toEqual([
[mockResponseCheck[0][0]],
{
Expand Down
46 changes: 29 additions & 17 deletions src/utils/tekton-results.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,12 @@ export const NEQ = (left: string, right: string) => EXP(left, `"${right}"`, '!='
// TODO: switch to v1 once API is ready
// https://github.com/tektoncd/community/pull/1055
export enum DataType {
PipelineRun = 'tekton.dev/v1beta1.PipelineRun',
TaskRun = 'tekton.dev/v1beta1.TaskRun',
Log = 'results.tekton.dev/v1alpha2.Log',
PipelineRun = 'tekton.dev/v1.PipelineRun',
TaskRun = 'tekton.dev/v1.TaskRun',
Log = 'results.tekton.dev/v1alpha3.Log',
PipelineRun_OLD = 'tekton.dev/v1beta1.PipelineRun',
TaskRun_OLD = 'tekton.dev/v1beta1.TaskRun',
Log_OLD = 'results.tekton.dev/v1alpha2.Log',
}

export const labelsToFilter = (labels?: MatchLabels): string =>
Expand Down Expand Up @@ -188,31 +191,40 @@ const getTRUrlPrefix = (workspace: string): string => URL_PREFIX.replace(_WORKSP
export const createTektonResultsUrl = (
workspace: string,
namespace: string,
dataType: DataType,
dataTypes: DataType[],
filter?: string,
options?: TektonResultsOptions,
nextPageToken?: string,
): string =>
`${getTRUrlPrefix(workspace)}/${namespace}/results/-/records?${new URLSearchParams({
): string => {
const isMultiValued = dataTypes.length > 1;
return `${getTRUrlPrefix(workspace)}/${namespace}/results/-/records?${new URLSearchParams({
// default sort should always be by `create_time desc`
['order_by']: 'create_time desc',
['page_size']: `${Math.max(
MINIMUM_PAGE_SIZE,
Math.min(MAXIMUM_PAGE_SIZE, options?.limit >= 0 ? options.limit : options?.pageSize ?? 30),
)}`,
...(nextPageToken ? { ['page_token']: nextPageToken } : {}),
filter: AND(
EQ('data_type', dataType.toString()),
filter,
selectorToFilter(options?.selector),
options?.filter,
),
filter: isMultiValued
? AND(
OR(EQ('data_type', dataTypes[0].toString()), EQ('data_type', dataTypes[1].toString())),
filter,
selectorToFilter(options?.selector),
options?.filter,
)
: AND(
EQ('data_type', dataTypes[0].toString()),
filter,
selectorToFilter(options?.selector),
options?.filter,
),
}).toString()}`;
};

export const getFilteredRecord = async <R extends K8sResourceCommon>(
workspace: string,
namespace: string,
dataType: DataType,
dataTypes: DataType[],
filter?: string,
options?: TektonResultsOptions,
nextPageToken?: string,
Expand All @@ -221,7 +233,7 @@ export const getFilteredRecord = async <R extends K8sResourceCommon>(
const url = createTektonResultsUrl(
workspace,
namespace,
dataType,
dataTypes,
filter,
options,
nextPageToken,
Expand Down Expand Up @@ -287,7 +299,7 @@ export const getFilteredPipelineRuns = (
getFilteredRecord<PipelineRunKindV1Beta1>(
workspace,
namespace,
DataType.PipelineRun,
[DataType.PipelineRun, DataType.PipelineRun_OLD],
filter,
options,
nextPageToken,
Expand All @@ -305,7 +317,7 @@ const getFilteredTaskRuns = (
getFilteredRecord<TaskRunKindV1Beta1>(
workspace,
namespace,
DataType.TaskRun,
[DataType.TaskRun, DataType.TaskRun_OLD],
filter,
options,
nextPageToken,
Expand Down Expand Up @@ -341,7 +353,7 @@ export const getTaskRunLog = (
getFilteredRecord<any>(
workspace,
namespace,
DataType.Log,
[DataType.Log, DataType.Log_OLD],
AND(EQ(`data.spec.resource.kind`, 'TaskRun'), EQ(`data.spec.resource.name`, taskRunName)),
{ limit: 1 },
).then((x) =>
Expand Down

0 comments on commit a0f63f2

Please sign in to comment.