summaryrefslogtreecommitdiff
path: root/web/src/api.js
blob: fc63317a96905c0874e59cd2b6f184ff7e948487 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright 2018 Red Hat, Inc
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.

import Axios from 'axios'

let authToken = undefined

export function setAuthToken(token) {
  authToken = token
}

function getHomepageUrl(url) {
  //
  // Discover serving location from href.
  //
  // This is only needed for sub-directory serving.
  // Serving the application from '/' may simply default to '/'
  //
  // Note that this is not enough for sub-directory serving,
  // The static files location also needs to be adapted with the 'homepage'
  // settings of the package.json file.
  //
  // This homepage url is used for the Router and Link resolution logic
  //
  let baseUrl
  if (url) {
    baseUrl = url
  } else {
    baseUrl = window.location.href
  }
  // Get dirname of the current url
  baseUrl = baseUrl.replace(/\\/g, '/').replace(/\/[^/]*$/, '/')

  // Remove any query strings
  if (baseUrl.includes('?')) {
    baseUrl = baseUrl.slice(0, baseUrl.lastIndexOf('?'))
  }
  // Remove any hash anchor
  if (baseUrl.includes('/#')) {
    baseUrl = baseUrl.slice(0, baseUrl.lastIndexOf('/#') + 1)
  }

  // Remove known sub-path
  const subDir = [
    '/autohold/',
    '/build/',
    '/buildset/',
    '/job/',
    '/project/',
    '/stream/',
    '/status/',
  ]
  subDir.forEach(path => {
    if (baseUrl.includes(path)) {
      baseUrl = baseUrl.slice(0, baseUrl.lastIndexOf(path) + 1)
    }
  })

  // Remove tenant scope
  if (baseUrl.includes('/t/')) {
    baseUrl = baseUrl.slice(0, baseUrl.lastIndexOf('/t/') + 1)
  }
  if (!baseUrl.endsWith('/')) {
    baseUrl = baseUrl + '/'
  }
  // console.log('Homepage url is ', baseUrl)
  return baseUrl
}

function getZuulUrl() {
  // Return the zuul root api absolute url
  const ZUUL_API = process.env.REACT_APP_ZUUL_API
  let apiUrl

  if (ZUUL_API) {
    // Api url set at build time, use it
    apiUrl = ZUUL_API
  } else {
    // Api url is relative to homepage path
    apiUrl = getHomepageUrl() + 'api/'
  }
  if (!apiUrl.endsWith('/')) {
    apiUrl = apiUrl + '/'
  }
  if (!apiUrl.endsWith('/api/')) {
    apiUrl = apiUrl + 'api/'
  }
  // console.log('Api url is ', apiUrl)
  return apiUrl
}
const apiUrl = getZuulUrl()


function getStreamUrl(apiPrefix) {
  const streamUrl = (apiUrl + apiPrefix)
    .replace(/(http)(s)?:\/\//, 'ws$2://') + 'console-stream'
  // console.log('Stream url is ', streamUrl)
  return streamUrl
}

function makeRequest(url, method, data) {
  if (method === undefined) {
    method = 'get'
  }

  // This performs a simple GET and tries to detect if CORS errors are
  // due to proxy authentication errors.
  const instance = Axios.create({
    baseURL: apiUrl
  })

  if (authToken) {
    instance.defaults.headers.common['Authorization'] = 'Bearer ' + authToken
  }

  const config = {method, url, data}

  // First try the request as normal
  let res = instance.request(config).catch(err => {
    if (err.response === undefined) {
      // This is either a Network, DNS, or CORS error, but we can't tell which.
      // If we're behind an authz proxy, it's possible our creds have timed out
      // and the CORS error is because we're getting a redirect.
      // Apache mod_auth_mellon (and possibly other authz proxies) will avoid
      // issuing a redirect if X-Requested-With is set to 'XMLHttpRequest' and
      // will instead issue a 403.  We can use this to detect that case.
      instance.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest'
      let res2 = instance.request(config).catch(err2 => {
        if (err2.response && err2.response.status === 403) {
          // We might be getting a redirect or something else,
          // so reload the page.
          console.log('Received 403 after unknown error; reloading')
          window.location.reload()
        }
        // If we're still getting an error, we don't know the cause,
        // it could be a transient network error, so we won't reload, we'll just
        // wait for it to clear.
        throw (err2)
      })
      return res2
    }
    throw (err)
  })
  return res
}

// Direct APIs
function fetchInfo() {
  return makeRequest('info')
}

function fetchComponents() {
  return makeRequest('components')
}

function fetchTenantInfo(apiPrefix) {
  return makeRequest(apiPrefix + 'info')
}

function fetchOpenApi() {
  return Axios.get(getHomepageUrl() + 'openapi.yaml')
}

function fetchTenants() {
  return makeRequest(apiUrl + 'tenants')
}

function fetchConfigErrors(apiPrefix) {
  return makeRequest(apiPrefix + 'config-errors')
}

function fetchStatus(apiPrefix) {
  return makeRequest(apiPrefix + 'status')
}

function fetchChangeStatus(apiPrefix, changeId) {
  return makeRequest(apiPrefix + 'status/change/' + changeId)
}

function fetchFreezeJob(apiPrefix, pipelineName, projectName, branchName, jobName) {
  return makeRequest(apiPrefix +
                   'pipeline/' + pipelineName +
                   '/project/' + projectName +
                   '/branch/' + branchName +
                   '/freeze-job/' + jobName)
}

function fetchBuild(apiPrefix, buildId) {
  return makeRequest(apiPrefix + 'build/' + buildId)
}

function fetchBuilds(apiPrefix, queryString) {
  let path = 'builds'
  if (queryString) {
    path += '?' + queryString.slice(1)
  }
  return makeRequest(apiPrefix + path)
}

function fetchBuildset(apiPrefix, buildsetId) {
  return makeRequest(apiPrefix + 'buildset/' + buildsetId)
}

function fetchBuildsets(apiPrefix, queryString) {
  let path = 'buildsets'
  if (queryString) {
    path += '?' + queryString.slice(1)
  }
  return makeRequest(apiPrefix + path)
}

function fetchPipelines(apiPrefix) {
  return makeRequest(apiPrefix + 'pipelines')
}

function fetchProject(apiPrefix, projectName) {
  return makeRequest(apiPrefix + 'project/' + projectName)
}

function fetchProjects(apiPrefix) {
  return makeRequest(apiPrefix + 'projects')
}

function fetchJob(apiPrefix, jobName) {
  return makeRequest(apiPrefix + 'job/' + jobName)
}

function fetchJobGraph(apiPrefix, projectName, pipelineName, branchName) {
  return makeRequest(apiPrefix +
                   'pipeline/' + pipelineName +
                   '/project/' + projectName +
                   '/branch/' + branchName +
                   '/freeze-jobs')
}

function fetchJobs(apiPrefix) {
  return makeRequest(apiPrefix + 'jobs')
}

function fetchLabels(apiPrefix) {
  return makeRequest(apiPrefix + 'labels')
}

function fetchNodes(apiPrefix) {
  return makeRequest(apiPrefix + 'nodes')
}

function fetchSemaphores(apiPrefix) {
  return makeRequest(apiPrefix + 'semaphores')
}

function fetchAutoholds(apiPrefix) {
  return makeRequest(apiPrefix + 'autohold')
}

function fetchAutohold(apiPrefix, requestId) {
  return makeRequest(apiPrefix + 'autohold/' + requestId)
}

function fetchUserAuthorizations(apiPrefix) {
  return makeRequest(apiPrefix + 'authorizations')
}

function dequeue(apiPrefix, projectName, pipeline, change) {
  return makeRequest(
    apiPrefix + 'project/' + projectName + '/dequeue',
    'post',
    {
      pipeline: pipeline,
      change: change,
    }
  )
}

function dequeue_ref(apiPrefix, projectName, pipeline, ref) {
  return makeRequest(
    apiPrefix + 'project/' + projectName + '/dequeue',
    'post',
    {
      pipeline: pipeline,
      ref: ref,
    }
  )
}

function enqueue(apiPrefix, projectName, pipeline, change) {
  return makeRequest(
    apiPrefix + 'project/' + projectName + '/enqueue',
    'post',
    {
      pipeline: pipeline,
      change: change,
    }
  )
}

function enqueue_ref(apiPrefix, projectName, pipeline, ref, oldrev, newrev) {
  return makeRequest(
    apiPrefix + 'project/' + projectName + '/enqueue',
    'post',
    {
      pipeline: pipeline,
      ref: ref,
      oldrev: oldrev,
      newrev: newrev,
    }
  )
}

function autohold(apiPrefix, projectName, job, change, ref,
                  reason, count, node_hold_expiration) {
  return makeRequest(
    apiPrefix + 'project/' + projectName + '/autohold',
    'post',
    {
      change: change,
      job: job,
      ref: ref,
      reason: reason,
      count: count,
      node_hold_expiration: node_hold_expiration,
    }
  )
}

function autohold_delete(apiPrefix, requestId) {
  return makeRequest(
    apiPrefix + '/autohold/' + requestId,
    'delete'
  )
}

function promote(apiPrefix, pipeline, changes) {
  return makeRequest(
    apiPrefix + '/promote',
    'post',
    {
      pipeline: pipeline,
      changes: changes,
    }
  )
}


export {
  apiUrl,
  getHomepageUrl,
  getStreamUrl,
  fetchChangeStatus,
  fetchConfigErrors,
  fetchStatus,
  fetchBuild,
  fetchBuilds,
  fetchBuildset,
  fetchBuildsets,
  fetchFreezeJob,
  fetchPipelines,
  fetchProject,
  fetchProjects,
  fetchJob,
  fetchJobGraph,
  fetchJobs,
  fetchLabels,
  fetchNodes,
  fetchOpenApi,
  fetchSemaphores,
  fetchTenants,
  fetchInfo,
  fetchComponents,
  fetchTenantInfo,
  fetchUserAuthorizations,
  fetchAutoholds,
  fetchAutohold,
  autohold,
  autohold_delete,
  dequeue,
  dequeue_ref,
  enqueue,
  enqueue_ref,
  promote,
}