summaryrefslogtreecommitdiff
path: root/web/src/actions/build.js
blob: e29ecd624c61605563a0b0f76da108c91100dafb (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
// 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'

import * as API from '../api'

export const BUILD_FETCH_REQUEST = 'BUILD_FETCH_REQUEST'
export const BUILD_FETCH_SUCCESS = 'BUILD_FETCH_SUCCESS'
export const BUILD_FETCH_FAIL = 'BUILD_FETCH_FAIL'

export const BUILD_OUTPUT_REQUEST = 'BUILD_OUTPUT_FETCH_REQUEST'
export const BUILD_OUTPUT_SUCCESS = 'BUILD_OUTPUT_FETCH_SUCCESS'
export const BUILD_OUTPUT_FAIL = 'BUILD_OUTPUT_FETCH_FAIL'

export const BUILD_MANIFEST_REQUEST = 'BUILD_MANIFEST_FETCH_REQUEST'
export const BUILD_MANIFEST_SUCCESS = 'BUILD_MANIFEST_FETCH_SUCCESS'
export const BUILD_MANIFEST_FAIL = 'BUILD_MANIFEST_FETCH_FAIL'

export const requestBuild = () => ({
  type: BUILD_FETCH_REQUEST
})

export const receiveBuild = (buildId, build) => ({
  type: BUILD_FETCH_SUCCESS,
  buildId: buildId,
  build: build,
  receivedAt: Date.now()
})

const failedBuild = (error, url) => {
  error.url = url
  return {
    type: BUILD_FETCH_FAIL,
    error
  }
}

export const requestBuildOutput = () => ({
  type: BUILD_OUTPUT_REQUEST
})

const receiveBuildOutput = (buildId, output) => {
  const hosts = {}
  // Compute stats
  output.forEach(phase => {
    Object.entries(phase.stats).forEach(([host, stats]) => {
      if (!hosts[host]) {
        hosts[host] = stats
        hosts[host].failed = []
      } else {
        hosts[host].changed += stats.changed
        hosts[host].failures += stats.failures
        hosts[host].ok += stats.ok
      }
      if (stats.failures > 0) {
        // Look for failed tasks
        phase.plays.forEach(play => {
          play.tasks.forEach(task => {
            if (task.hosts[host]) {
              if (task.hosts[host].results &&
                  task.hosts[host].results.length > 0) {
                task.hosts[host].results.forEach(result => {
                  if (result.failed) {
                    result.name = task.task.name
                    hosts[host].failed.push(result)
                  }
                })
              } else if (task.hosts[host].rc || task.hosts[host].failed) {
                let result = task.hosts[host]
                result.name = task.task.name
                hosts[host].failed.push(result)
              }
            }
          })
        })
      }
    })
  })
  return {
    type: BUILD_OUTPUT_SUCCESS,
    buildId: buildId,
    hosts: hosts,
    output: output,
    receivedAt: Date.now()
  }
}

const failedBuildOutput = (error, url) => {
  error.url = url
  return {
    type: BUILD_OUTPUT_FAIL,
    error
  }
}

export const requestBuildManifest = () => ({
  type: BUILD_MANIFEST_REQUEST
})

const receiveBuildManifest = (buildId, manifest) => {
  const index = {}

  const renderNode = (root, object) => {
    const path = root + '/' + object.name

    if ('children' in object && object.children) {
      object.children.map(n => renderNode(path, n))
    } else {
      index[path] = object
    }
  }

  manifest.tree.map(n => renderNode('', n))
  return {
    type: BUILD_MANIFEST_SUCCESS,
    buildId: buildId,
    manifest: {tree: manifest.tree, index: index},
    receivedAt: Date.now()
  }
}

const failedBuildManifest = (error, url) => {
  error.url = url
  return {
    type: BUILD_MANIFEST_FAIL,
    error
  }
}

export const fetchBuild = (tenant, buildId, state, force) => dispatch => {
  const build = state.build.builds[buildId]
  if (!force && build) {
    return Promise.resolve()
  }
  dispatch(requestBuild())
  return API.fetchBuild(tenant.apiPrefix, buildId)
    .then(response => {
      dispatch(receiveBuild(buildId, response.data))
    })
    .catch(error => dispatch(failedBuild(error, tenant.apiPrefix)))
}

const fetchBuildOutput = (buildId, state, force) => dispatch => {
  const build = state.build.builds[buildId]
  const url = build.log_url.substr(0, build.log_url.lastIndexOf('/') + 1)
  if (!force && build.output) {
    return Promise.resolve()
  }
  dispatch(requestBuildOutput())
  return Axios.get(url + 'job-output.json.gz')
    .then(response => dispatch(receiveBuildOutput(buildId, response.data)))
    .catch(error => {
      if (!error.request) {
        throw error
      }
      // Try without compression
      Axios.get(url + 'job-output.json')
        .then(response => dispatch(receiveBuildOutput(
          buildId, response.data)))
    })
    .catch(error => dispatch(failedBuildOutput(error, url)))
}

export const fetchBuildManifest = (buildId, state, force) => dispatch => {
  const build = state.build.builds[buildId]
  if (!force && build.manifest) {
    return Promise.resolve()
  }

  dispatch(requestBuildManifest())
  for (let artifact of build.artifacts) {
    if ('metadata' in artifact &&
        'type' in artifact.metadata &&
        artifact.metadata.type === 'zuul_manifest') {
      return Axios.get(artifact.url)
        .then(manifest => {
          dispatch(receiveBuildManifest(buildId, manifest.data))
        })
        .catch(error => dispatch(failedBuildManifest(error, artifact.url)))
    }
  }
  dispatch(failedBuildManifest('no manifest found'))
}

export const fetchBuildIfNeeded = (tenant, buildId, force) => (dispatch, getState) => {
  dispatch(fetchBuild(tenant, buildId, getState(), force))
    .then(() => {
      dispatch(fetchBuildOutput(buildId, getState(), force))
      dispatch(fetchBuildManifest(buildId, getState(), force))
    })
}