Skip to content

Commit

Permalink
fix(3163): Add sort and sortby query params for pipeline list events (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
tkyi authored Aug 13, 2024
1 parent c3888a7 commit f446885
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
4 changes: 3 additions & 1 deletion plugins/pipelines/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ Query Params:
* `page` - *Optional* Specific page of the set to return
* `count` - *Optional* Number of items per page
* `sort` - *Optional* Sort rangekey by `ascending` or `descending` (default `descending`)
* `sortBy` - *Optional* Field to sort by
* `type` - *Optional* Get pipeline or pr events (default `pipeline`)
* `prNum` - *Optional* Return only PR events of specified PR number
* `sha` - *Optional* Search `sha` and `configPipelineSha` for events
Expand All @@ -115,7 +116,8 @@ Query Params:
`GET /pipelines/{id}/events?page={pageNumber}&count={countNumber}&sort={sort}&type={type}&prNum={prNumber}&sha={sha}`

`GET /pipelines/{id}/events?id=gt:{eventId}&count={countNumber}` (greater than eventId)
`GET /pipelines/{id}/events?id=lt:{eventId}&count={countNumber}` (less than eventId)

`GET /pipelines/{id}/events?id=lt:{eventId}&count={countNumber}&sort=ascending` (less than eventId)

#### Get all pipeline builds
`page`, `count`, `sort`, `latest`, `sortBy`, `fetchSteps`, `readOnly`, and `groupEventId` are optional
Expand Down
10 changes: 7 additions & 3 deletions plugins/pipelines/listEvents.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const INEQUALITY_SIGNS = /^(gt|lt):([\d]+)$/;
const queryIdSchema = joi
.alternatives()
.try(pipelineIdSchema, joi.string().regex(INEQUALITY_SIGNS))
.label('Query ID schema')
.description('Event ID; alternatively can use greater than or less than prefix (gt:/lt:)')
.example('gt:12345');

module.exports = () => ({
Expand All @@ -34,7 +34,7 @@ module.exports = () => ({

handler: async (request, h) => {
const factory = request.server.app.pipelineFactory;
const { page, count, sha, prNum, id } = request.query;
const { page, count, sha, prNum, id, sort, sortBy } = request.query;

return factory
.get(request.params.id)
Expand All @@ -44,7 +44,7 @@ module.exports = () => ({
}

const eventType = request.query.type || 'pipeline';
const config = { params: { type: eventType } };
const config = { params: { type: eventType }, sort };

if (page || count) {
config.paginate = {
Expand All @@ -53,6 +53,10 @@ module.exports = () => ({
};
}

if (sortBy) {
config.sortBy = sortBy;
}

if (prNum) {
config.params.type = 'pr';
config.params.prNum = prNum;
Expand Down
16 changes: 10 additions & 6 deletions test/plugins/pipelines.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1536,7 +1536,7 @@ describe('pipeline plugin test', () => {

return server.inject(options).then(reply => {
assert.calledOnce(pipelineMock.getEvents);
assert.calledWith(pipelineMock.getEvents, { params: { type: 'pr' } });
assert.calledWith(pipelineMock.getEvents, { params: { type: 'pr' }, sort: 'descending' });
assert.deepEqual(reply.result, testEvents);
assert.equal(reply.statusCode, 200);
});
Expand All @@ -1549,7 +1549,8 @@ describe('pipeline plugin test', () => {
assert.calledOnce(pipelineMock.getEvents);
assert.calledWith(pipelineMock.getEvents, {
params: { type: 'pr' },
paginate: { page: undefined, count: 30 }
paginate: { page: undefined, count: 30 },
sort: 'descending'
});
assert.deepEqual(reply.result, testEvents);
assert.equal(reply.statusCode, 200);
Expand All @@ -1561,20 +1562,22 @@ describe('pipeline plugin test', () => {

return server.inject(options).then(reply => {
assert.calledOnce(pipelineMock.getEvents);
assert.calledWith(pipelineMock.getEvents, { params: { prNum: 4, type: 'pr' } });
assert.calledWith(pipelineMock.getEvents, { params: { prNum: 4, type: 'pr' }, sort: 'descending' });
assert.deepEqual(reply.result, testEvents);
assert.equal(reply.statusCode, 200);
});
});

it('returns 200 for getting events with id less than 888', () => {
options.url = `/pipelines/${id}/events?id=lt:888&count=5`;
options.url = `/pipelines/${id}/events?id=lt:888&count=5&sort=ascending&sortBy=createTime`;

return server.inject(options).then(reply => {
assert.calledOnce(pipelineMock.getEvents);
assert.calledWith(pipelineMock.getEvents, {
params: { id: 'lt:888', type: 'pipeline' },
paginate: { page: undefined, count: 5 }
paginate: { page: undefined, count: 5 },
sort: 'ascending',
sortBy: 'createTime'
});
assert.deepEqual(reply.result, testEvents);
assert.equal(reply.statusCode, 200);
Expand All @@ -1591,7 +1594,8 @@ describe('pipeline plugin test', () => {
search: {
field: ['sha', 'configPipelineSha'],
keyword: 'ccc49349d3cffbd12ea9e3d41521480b4aa5de5f%'
}
},
sort: 'descending'
});
assert.deepEqual(reply.result, testEvents);
assert.equal(reply.statusCode, 200);
Expand Down

0 comments on commit f446885

Please sign in to comment.