Skip to content

Commit 18f1e79

Browse files
authoredNov 22, 2024··
fix: Add ability to filter pipeline events for commit message, author, or creator (#3244)
1 parent 2bac861 commit 18f1e79

File tree

3 files changed

+101
-17
lines changed

3 files changed

+101
-17
lines changed
 

‎plugins/pipelines/README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,19 @@ Query Params:
110110
* `sortBy` - *Optional* Field to sort by
111111
* `type` - *Optional* Get pipeline or pr events (default `pipeline`)
112112
* `prNum` - *Optional* Return only PR events of specified PR number
113-
* `sha` - *Optional* Search `sha` and `configPipelineSha` for events
114113
* `groupEventId` - *Optional* Return only events with a specified groupEventId
115114
* `id` - *Optional* Fetch specific event ID; alternatively can use greater than(`gt:`) or less than(`lt:`) prefix
115+
* `sha` - *Optional* Search `sha` and `configPipelineSha` for events
116+
* `author` - *Optional* Search commit author `username` and `name` for events
117+
* `creator` - *Optional* Search creator `username` and `name` for events
118+
* `message` - *Optional* Search commit `message` for events
119+
120+
_Caveats_: Only one of the search fields can be used at one time (sha, author, creator, or message).
116121

117122
`GET /pipelines/{id}/events?page={pageNumber}&count={countNumber}&sort={sort}&type={type}&prNum={prNumber}&sha={sha}`
118123

124+
`GET /pipelines/{id}/events?message={message}`
125+
119126
`GET /pipelines/{id}/events?id=gt:{eventId}&count={countNumber}` (greater than eventId)
120127

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

‎plugins/pipelines/listEvents.js

+30-16
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ module.exports = () => ({
3434

3535
handler: async (request, h) => {
3636
const factory = request.server.app.pipelineFactory;
37-
const { page, count, sha, prNum, id, sort, sortBy, groupEventId } = request.query;
37+
const { page, count, sha, prNum, id, sort, sortBy, groupEventId, message, author, creator } = request.query;
3838

3939
return factory
4040
.get(request.params.id)
@@ -62,13 +62,18 @@ module.exports = () => ({
6262
config.params.prNum = prNum;
6363
}
6464

65+
// Do a search
66+
// See https://www.w3schools.com/sql/sql_like.asp for syntax
6567
if (sha) {
66-
config.search = {
67-
field: ['sha', 'configPipelineSha'],
68-
// Do a search for sha
69-
// See https://www.w3schools.com/sql/sql_like.asp for syntax
70-
keyword: `${sha}%`
71-
};
68+
config.search = { field: ['sha', 'configPipelineSha'], keyword: `${sha}%` };
69+
} else if (message) {
70+
config.search = { field: ['commit'], keyword: `%"message":"${message}%` };
71+
} else if (author) {
72+
// searches name and username
73+
config.search = { field: ['commit'], keyword: `%name":"${author}%` };
74+
} else if (creator) {
75+
// searches name and username
76+
config.search = { field: ['creator'], keyword: `%name":"${creator}%` };
7277
}
7378

7479
if (groupEventId) {
@@ -95,15 +100,24 @@ module.exports = () => ({
95100
id: pipelineIdSchema
96101
}),
97102
query: schema.api.pagination.concat(
98-
joi.object({
99-
type: typeSchema,
100-
prNum: prNumSchema,
101-
sha: shaSchema,
102-
id: queryIdSchema,
103-
groupEventId: pipelineIdSchema,
104-
search: joi.forbidden(), // we don't support search for Pipeline list events
105-
getCount: joi.forbidden() // we don't support getCount for Pipeline list events
106-
})
103+
joi
104+
.object({
105+
type: typeSchema,
106+
prNum: prNumSchema,
107+
sha: shaSchema,
108+
message: joi.string().label('Commit message').example('fix: Typo'),
109+
author: joi.string().label('Author Name').example('Dao Lam'),
110+
creator: joi.string().label('Creator Name').example('Dao Lam'),
111+
id: queryIdSchema,
112+
groupEventId: pipelineIdSchema,
113+
search: joi.forbidden(), // we don't support search for Pipeline list events
114+
getCount: joi.forbidden() // we don't support getCount for Pipeline list events
115+
})
116+
// https://joi.dev/api/?v=17.13.3#objectoxorpeers-options
117+
.oxor('sha', 'message', 'author', 'creator')
118+
.messages({
119+
'object.oxor': 'You can only specify one search parameter: sha, message, author, or creator.'
120+
})
107121
)
108122
}
109123
}

‎test/plugins/pipelines.test.js

+63
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,60 @@ describe('pipeline plugin test', () => {
15681568
});
15691569
});
15701570

1571+
it('returns 200 for getting events with commit author name', () => {
1572+
options.url = `/pipelines/${id}/events?author=Dao`;
1573+
1574+
return server.inject(options).then(reply => {
1575+
assert.calledOnce(pipelineMock.getEvents);
1576+
assert.calledWith(pipelineMock.getEvents, {
1577+
params: { type: 'pipeline' },
1578+
search: {
1579+
field: ['commit'],
1580+
keyword: '%name":"Dao%'
1581+
},
1582+
sort: 'descending'
1583+
});
1584+
assert.deepEqual(reply.result, testEvents);
1585+
assert.equal(reply.statusCode, 200);
1586+
});
1587+
});
1588+
1589+
it('returns 200 for getting events with commit creator name', () => {
1590+
options.url = `/pipelines/${id}/events?creator=Dao`;
1591+
1592+
return server.inject(options).then(reply => {
1593+
assert.calledOnce(pipelineMock.getEvents);
1594+
assert.calledWith(pipelineMock.getEvents, {
1595+
params: { type: 'pipeline' },
1596+
search: {
1597+
field: ['creator'],
1598+
keyword: '%name":"Dao%'
1599+
},
1600+
sort: 'descending'
1601+
});
1602+
assert.deepEqual(reply.result, testEvents);
1603+
assert.equal(reply.statusCode, 200);
1604+
});
1605+
});
1606+
1607+
it('returns 200 for getting events with commit message', () => {
1608+
options.url = `/pipelines/${id}/events?message=Update screwdriver.yaml`;
1609+
1610+
return server.inject(options).then(reply => {
1611+
assert.calledOnce(pipelineMock.getEvents);
1612+
assert.calledWith(pipelineMock.getEvents, {
1613+
params: { type: 'pipeline' },
1614+
search: {
1615+
field: ['commit'],
1616+
keyword: '%"message":"Update screwdriver.yaml%'
1617+
},
1618+
sort: 'descending'
1619+
});
1620+
assert.deepEqual(reply.result, testEvents);
1621+
assert.equal(reply.statusCode, 200);
1622+
});
1623+
});
1624+
15711625
it('returns 200 for getting events with groupEventId', () => {
15721626
options.url = `/pipelines/${id}/events?groupEventId=4`;
15731627

@@ -1632,6 +1686,15 @@ describe('pipeline plugin test', () => {
16321686
assert.equal(reply.statusCode, 500);
16331687
});
16341688
});
1689+
1690+
it('returns 400 when trying to search multiple fields at once', () => {
1691+
options.url = `/pipelines/${id}/events?creator=Dao&sha=33`;
1692+
1693+
return server.inject(options).then(reply => {
1694+
assert.equal(reply.statusCode, 400);
1695+
assert.equal(reply.result.message, 'Invalid request query input');
1696+
});
1697+
});
16351698
});
16361699

16371700
describe('GET /pipelines/{id}/builds', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.