screwdriver-api 8.0.93 → 8.0.95

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": "8.0.93",
3
+ "version": "8.0.95",
4
4
  "description": "API server for the Screwdriver.cd service",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -0,0 +1,131 @@
1
+ 'use strict';
2
+
3
+ const boom = require('@hapi/boom');
4
+ const joi = require('joi');
5
+ const schema = require('screwdriver-data-schema');
6
+ const userListSchema = joi.array().items(schema.models.user.get).label('List of users');
7
+ const pipelineIdSchema = schema.models.pipeline.base.extract('id');
8
+
9
+ /**
10
+ * @typedef {import('screwdriver-models/lib/pipeline')} Pipeline
11
+ * @typedef {import('screwdriver-models/lib/user')} User
12
+ * @typedef {import('screwdriver-models/lib/userFactory')} UserFactory
13
+ */
14
+
15
+ /**
16
+ * Retrieves the full user objects for all enabled administrators associated with a pipeline
17
+ * based on the pipeline SCM context.
18
+ *
19
+ * This function first extracts a list of usernames that are explicitly set as 'true'
20
+ * in the pipeline's 'admins' object. It then uses the provided user factory's 'list' method
21
+ * to fetch the detailed user objects for these usernames from the pipeline SCM context.
22
+ *
23
+ * @param {Pipeline} pipeline The pipeline model object containing 'admins' and 'scmContext'.
24
+ * @param {UserFactory} userFactory User Factory.
25
+ * @returns {Promise<User[]>} A promise that resolves to an array of User objects for the
26
+ * enabled pipeline administrators.
27
+ */
28
+ function getAdminUsersFromPipelineSCMContext(pipeline, userFactory) {
29
+ const { admins, scmContext } = pipeline;
30
+ const adminUserNames = [];
31
+
32
+ for (const username of Object.keys(admins)) {
33
+ if (admins[username]) {
34
+ adminUserNames.push(username);
35
+ }
36
+ }
37
+
38
+ const listConfig = {
39
+ params: {
40
+ username: adminUserNames,
41
+ scmContext
42
+ }
43
+ };
44
+
45
+ return userFactory.list(listConfig);
46
+ }
47
+
48
+ /**
49
+ * Retrieves the full user objects for administrators associated with a pipeline
50
+ * who belong to a different SCM Context than the pipeline itself.
51
+ *
52
+ * This function uses a list of admin user IDs and the pipeline's SCM context
53
+ * to fetch detailed user objects. It then filters the results, returning only
54
+ * those users whose own registered SCM context does NOT match the pipeline's SCM context.
55
+ *
56
+ * @param {Pipeline} pipeline - The pipeline model object containing 'adminUserIds' (array of user IDs)
57
+ * and 'scmContext' (the SCM context of the pipeline).
58
+ * @param {UserFactory} userFactory - User Factory.
59
+ * @returns {Promise<User[]>} A promise that resolves to an array of User objects
60
+ * whose registered SCM context differs from the pipeline's context.
61
+ */
62
+ function getAdminUsersFromOtherSCMContext(pipeline, userFactory) {
63
+ const { adminUserIds, scmContext } = pipeline;
64
+
65
+ const listConfig = {
66
+ params: {
67
+ id: adminUserIds
68
+ }
69
+ };
70
+
71
+ return userFactory.list(listConfig).then(adminUsers => {
72
+ return adminUsers.filter(user => user.scmContext !== scmContext);
73
+ });
74
+ }
75
+
76
+ module.exports = () => ({
77
+ method: 'GET',
78
+ path: '/pipelines/{id}/admins',
79
+ options: {
80
+ description: 'Get all admin users for a given pipeline',
81
+ notes: 'Returns all admin users for a given pipeline',
82
+ tags: ['api', 'pipelines', 'admins'],
83
+ auth: {
84
+ strategies: ['token'],
85
+ scope: ['user', 'admin']
86
+ },
87
+
88
+ handler: async (request, h) => {
89
+ const { pipelineFactory, userFactory } = request.server.app;
90
+
91
+ return pipelineFactory
92
+ .get(request.params.id)
93
+ .then(pipeline => {
94
+ if (!pipeline) {
95
+ throw boom.notFound('Pipeline does not exist');
96
+ }
97
+
98
+ return Promise.all([
99
+ getAdminUsersFromPipelineSCMContext(pipeline, userFactory),
100
+ getAdminUsersFromOtherSCMContext(pipeline, userFactory)
101
+ ]);
102
+ })
103
+ .then(([adminUsersFromPipelineSCMContext, adminUsersFromOtherSCMContext]) => {
104
+ return [...adminUsersFromPipelineSCMContext, ...adminUsersFromOtherSCMContext];
105
+ })
106
+ .then(adminUsers =>
107
+ h.response(
108
+ adminUsers.map(user => {
109
+ const output = user.toJson();
110
+
111
+ delete output.token;
112
+ delete output.settings;
113
+
114
+ return output;
115
+ })
116
+ )
117
+ )
118
+ .catch(err => {
119
+ throw err;
120
+ });
121
+ },
122
+ response: {
123
+ schema: userListSchema
124
+ },
125
+ validate: {
126
+ params: joi.object({
127
+ id: pipelineIdSchema
128
+ })
129
+ }
130
+ }
131
+ });
@@ -28,6 +28,7 @@ const metricsRoute = require('./metrics');
28
28
  const latestBuild = require('./latestBuild');
29
29
  const latestCommitEvent = require('./latestCommitEvent');
30
30
  const getAdmin = require('./admins/get');
31
+ const listAdminsRoute = require('./admins/list');
31
32
  const deleteCache = require('./caches/delete');
32
33
  const openPrRoute = require('./openPr');
33
34
  const createTemplateRoute = require('./templates/create');
@@ -265,6 +266,7 @@ const pipelinesPlugin = {
265
266
  latestBuild(),
266
267
  latestCommitEvent(),
267
268
  getAdmin(),
269
+ listAdminsRoute(),
268
270
  deleteCache(),
269
271
  openPrRoute(),
270
272
  createTemplateRoute(),