Skip to content

Commit 1f0a99f

Browse files
authoredApr 5, 2024··
fix: Add filter to pipelines/id/jobs for pr jobs only (#3078)
1 parent b9ed3f4 commit 1f0a99f

File tree

3 files changed

+55
-3
lines changed

3 files changed

+55
-3
lines changed
 

‎plugins/pipelines/README.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,13 @@ When `latest=true` and `groupEventId` is set, only latest builds in a pipeline b
115115
#### Get all jobs (including pull requests jobs)
116116
`archived` is optional and has a default value of `false`, which makes the endpoint not return archived jobs (e.g. closed pull requests)
117117

118-
`GET /pipelines/{id}/jobs?archived={boolean}`
118+
Arguments:
119+
120+
* `archived` - Optional and has a default value of `false`, which makes the endpoint not return archived jobs (e.g. closed pull requests)
121+
* `type` - Optional and can be set to `pr` or `pipeline` to only return PR jobs or non-PR jobs
122+
* `jobName` - Optional and can be set to only return only a single job
123+
124+
`GET /pipelines/{id}/jobs?archived={boolean}&type={type}&jobName={jobName}`
119125

120126
#### Get Pipeline Admin
121127
`GET /pipelines/{id}/admin`

‎plugins/pipelines/listJobs.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const schema = require('screwdriver-data-schema');
66
const jobListSchema = joi.array().items(schema.models.job.get).label('List of jobs');
77
const jobNameSchema = schema.models.job.base.extract('name');
88
const pipelineIdSchema = schema.models.pipeline.base.extract('id');
9+
const JOB_PR_PATTERN = `PR-%:%`;
910

1011
module.exports = () => ({
1112
method: 'GET',
@@ -21,7 +22,7 @@ module.exports = () => ({
2122

2223
handler: async (request, h) => {
2324
const { pipelineFactory } = request.server.app;
24-
const { page, count, jobName } = request.query;
25+
const { page, count, jobName, type } = request.query;
2526

2627
return pipelineFactory
2728
.get(request.params.id)
@@ -36,6 +37,20 @@ module.exports = () => ({
3637
}
3738
};
3839

40+
if (type) {
41+
config.search = {
42+
field: 'name',
43+
// Do a search for PR-%:% in job name
44+
// See https://www.w3schools.com/sql/sql_like.asp for syntax
45+
keyword: JOB_PR_PATTERN
46+
};
47+
48+
if (type === 'pipeline') {
49+
// Do a search for job name without PR-%:% pattern
50+
// See https://www.w3schools.com/sql/sql_not.asp for syntax
51+
config.search.inverse = true;
52+
}
53+
}
3954
if (jobName) {
4055
config.params.name = jobName;
4156
}
@@ -59,6 +74,7 @@ module.exports = () => ({
5974
}),
6075
query: schema.api.pagination.concat(
6176
joi.object({
77+
type: joi.string().valid('', 'pr', 'pipeline').label('Job type filter (pr or pipeline)').optional(),
6278
archived: joi.boolean().truthy('true').falsy('false').default(false),
6379
jobName: jobNameSchema,
6480
search: joi.forbidden(), // we don't support search for Pipeline list jobs

‎test/plugins/pipelines.test.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ describe('pipeline plugin test', () => {
931931
}));
932932

933933
it('returns 200 for getting jobs with jobNames', () => {
934-
options.url = `/pipelines/${id}/jobs?jobName=deploy`;
934+
options.url = `/pipelines/${id}/jobs?jobName=deploy&type=`;
935935

936936
return server.inject(options).then(reply => {
937937
assert.equal(reply.statusCode, 200);
@@ -945,6 +945,36 @@ describe('pipeline plugin test', () => {
945945
});
946946
});
947947

948+
it('returns 200 for getting jobs with pr type', () => {
949+
options.url = `/pipelines/${id}/jobs?type=pr`;
950+
951+
return server.inject(options).then(reply => {
952+
assert.equal(reply.statusCode, 200);
953+
assert.calledWith(pipelineMock.getJobs, {
954+
params: {
955+
archived: false
956+
},
957+
search: { field: 'name', keyword: 'PR-%:%' }
958+
});
959+
assert.deepEqual(reply.result, testJobs);
960+
});
961+
});
962+
963+
it('returns 200 for getting jobs with pipeline type', () => {
964+
options.url = `/pipelines/${id}/jobs?type=pipeline`;
965+
966+
return server.inject(options).then(reply => {
967+
assert.equal(reply.statusCode, 200);
968+
assert.calledWith(pipelineMock.getJobs, {
969+
params: {
970+
archived: false
971+
},
972+
search: { field: 'name', keyword: 'PR-%:%', inverse: true }
973+
});
974+
assert.deepEqual(reply.result, testJobs);
975+
});
976+
});
977+
948978
it('returns 400 for wrong query format', () => {
949979
pipelineFactoryMock.get.resolves(null);
950980
options.url = `/pipelines/${id}/jobs?archived=blah`;

0 commit comments

Comments
 (0)
Please sign in to comment.