screwdriver-api 5.0.5 → 5.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "screwdriver-api",
3
- "version": "5.0.5",
3
+ "version": "5.0.7",
4
4
  "description": "API server for the Screwdriver.cd service",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -74,6 +74,25 @@ Example payload:
74
74
  }
75
75
  ```
76
76
 
77
+ #### Add one or more pipelines to a collection
78
+
79
+ `PUT /collections/{id}/pipelines?ids[]={pipelineId}`
80
+
81
+ Examples:
82
+
83
+ `PUT /collections/123/pipelines?ids[]=11&ids[]=23` adds pipelines with `id` equal to `11` and `23` to the collection with `id` equal to `123`
84
+
85
+
86
+ #### Remove one or more pipelines from a collection
87
+
88
+ `DELETE /collections/{id}/pipelines?ids[]={pipelineId}`
89
+
90
+ Examples:
91
+
92
+ `DELETE /collections/123/pipelines?ids[]=11&ids[]=23` removes pipelines with `id` equal to `11` and `23` from the collection with `id` equal to `123`
93
+
94
+ `DELETE /collections/123/pipelines` removes all the pipelines from the collection with `id` equal to `123`
95
+
77
96
  #### Delete a collection
78
97
 
79
98
  `DELETE /collections/{id}`
@@ -0,0 +1,82 @@
1
+ 'use strict';
2
+
3
+ const boom = require('@hapi/boom');
4
+ const schema = require('screwdriver-data-schema');
5
+ const joi = require('joi');
6
+ const idSchema = schema.models.collection.base.extract('id');
7
+ const pipelineIdSchema = schema.models.pipeline.base.extract('id');
8
+ const pipelineIdsSchema = joi
9
+ .alternatives()
10
+ .try(joi.array().items(pipelineIdSchema), pipelineIdSchema)
11
+ .required();
12
+ const IDS_KEY = 'ids[]';
13
+
14
+ module.exports = () => ({
15
+ method: 'PUT',
16
+ path: '/collections/{id}/pipelines',
17
+ options: {
18
+ description: 'Add one or more pipelines to the collection',
19
+ notes: 'Returns null if successful',
20
+ tags: ['api', 'collections'],
21
+ auth: {
22
+ strategies: ['token'],
23
+ scope: ['user', '!guest']
24
+ },
25
+
26
+ handler: async (request, h) => {
27
+ const { collectionFactory, pipelineFactory, userFactory } = request.server.app;
28
+ const { username, scmContext } = request.auth.credentials;
29
+ const { id } = request.params;
30
+
31
+ // Fetch the collection and user models
32
+ return Promise.all([collectionFactory.get(id), userFactory.get({ username, scmContext })])
33
+ .then(([collection, user]) => {
34
+ if (!collection) {
35
+ throw boom.notFound(`Collection ${id} does not exist`);
36
+ }
37
+ if (!user) {
38
+ throw boom.notFound(`User ${username} does not exist`);
39
+ }
40
+ if (collection.userId !== user.id) {
41
+ throw boom.forbidden(`User ${username} does not own collection`);
42
+ }
43
+
44
+ const pipelineIdsQueryParam = request.query[IDS_KEY];
45
+
46
+ if (!pipelineIdsQueryParam) {
47
+ throw boom.notFound(`Pipelines are not provided to add to the collection`);
48
+ }
49
+
50
+ const pipelineIdsToAdd = Array.isArray(pipelineIdsQueryParam)
51
+ ? pipelineIdsQueryParam.map(pipelineId => parseInt(pipelineId, 10))
52
+ : [parseInt(pipelineIdsQueryParam, 10)];
53
+
54
+ return pipelineFactory
55
+ .list({
56
+ params: {
57
+ scmContext,
58
+ id: pipelineIdsToAdd
59
+ }
60
+ })
61
+ .then(pipelinesToAdd => {
62
+ const newPipelineIdsToAdd = pipelinesToAdd
63
+ .filter(p => !collection.pipelineIds.includes(p.id))
64
+ .map(p => p.id);
65
+
66
+ collection.pipelineIds = [...collection.pipelineIds, ...newPipelineIdsToAdd];
67
+
68
+ return collection.update().then(() => h.response().code(204));
69
+ });
70
+ })
71
+ .catch(err => {
72
+ throw err;
73
+ });
74
+ },
75
+ validate: {
76
+ params: joi.object({
77
+ id: idSchema,
78
+ 'ids[]': pipelineIdsSchema.optional()
79
+ })
80
+ }
81
+ }
82
+ });
@@ -5,6 +5,8 @@ const getRoute = require('./get');
5
5
  const listRoute = require('./list');
6
6
  const updateRoute = require('./update');
7
7
  const removeRoute = require('./remove');
8
+ const removePipelinesRoute = require('./removePipelines');
9
+ const addPipelinesRoute = require('./addPipelines');
8
10
 
9
11
  /**
10
12
  * Collections API Plugin
@@ -16,7 +18,15 @@ const removeRoute = require('./remove');
16
18
  const collectionsPlugin = {
17
19
  name: 'collections',
18
20
  async register(server) {
19
- server.route([createRoute(), getRoute(), listRoute(), updateRoute(), removeRoute()]);
21
+ server.route([
22
+ createRoute(),
23
+ getRoute(),
24
+ listRoute(),
25
+ updateRoute(),
26
+ removeRoute(),
27
+ removePipelinesRoute(),
28
+ addPipelinesRoute()
29
+ ]);
20
30
  }
21
31
  };
22
32
 
@@ -0,0 +1,70 @@
1
+ 'use strict';
2
+
3
+ const boom = require('@hapi/boom');
4
+ const schema = require('screwdriver-data-schema');
5
+ const joi = require('joi');
6
+ const idSchema = schema.models.collection.base.extract('id');
7
+ const pipelineIdSchema = schema.models.pipeline.base.extract('id');
8
+ const pipelineIdsSchema = joi
9
+ .alternatives()
10
+ .try(joi.array().items(pipelineIdSchema), pipelineIdSchema)
11
+ .required();
12
+ const IDS_KEY = 'ids[]';
13
+
14
+ module.exports = () => ({
15
+ method: 'DELETE',
16
+ path: '/collections/{id}/pipelines',
17
+ options: {
18
+ description: 'Delete one or more pipelines from a collection',
19
+ notes: 'Returns null if successful',
20
+ tags: ['api', 'collections'],
21
+ auth: {
22
+ strategies: ['token'],
23
+ scope: ['user', '!guest']
24
+ },
25
+
26
+ handler: async (request, h) => {
27
+ const { collectionFactory, userFactory } = request.server.app;
28
+ const { username, scmContext } = request.auth.credentials;
29
+ const { id } = request.params;
30
+
31
+ // Fetch the collection and user models
32
+ return Promise.all([collectionFactory.get(request.params.id), userFactory.get({ username, scmContext })])
33
+ .then(([collection, user]) => {
34
+ if (!collection) {
35
+ throw boom.notFound(`Collection ${id} does not exist`);
36
+ }
37
+ if (!user) {
38
+ throw boom.notFound(`User ${username} does not exist`);
39
+ }
40
+ if (collection.userId !== user.id) {
41
+ throw boom.forbidden(`User ${username} does not own collection`);
42
+ }
43
+
44
+ // Only return specific pipelines
45
+ const pipelineIdsQueryParam = request.query[IDS_KEY];
46
+
47
+ if (pipelineIdsQueryParam) {
48
+ const pipelineIdsToRemove = Array.isArray(pipelineIdsQueryParam)
49
+ ? pipelineIdsQueryParam.map(pipelineId => parseInt(pipelineId, 10))
50
+ : [parseInt(pipelineIdsQueryParam, 10)];
51
+
52
+ collection.pipelineIds = collection.pipelineIds.filter(i => !pipelineIdsToRemove.includes(i));
53
+ } else {
54
+ collection.pipelineIds = [];
55
+ }
56
+
57
+ return collection.update().then(() => h.response().code(204));
58
+ })
59
+ .catch(err => {
60
+ throw err;
61
+ });
62
+ },
63
+ validate: {
64
+ params: joi.object({
65
+ id: idSchema,
66
+ 'ids[]': pipelineIdsSchema.optional()
67
+ })
68
+ }
69
+ }
70
+ });