summaryrefslogtreecommitdiff
path: root/web/src/containers/status/Change.jsx
blob: b2ab50c5bbc202f279fa7e158acb8d61c3e95fe7 (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
// 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 * as React from 'react'
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
import { Link } from 'react-router-dom'

import {
  Button,
  Dropdown,
  DropdownItem,
  KebabToggle,
  Modal,
  ModalVariant
} from '@patternfly/react-core'
import {
  AngleDoubleUpIcon,
  BanIcon,
} from '@patternfly/react-icons'
import { dequeue, dequeue_ref, promote } from '../../api'
import { addDequeueError, addPromoteError } from '../../actions/adminActions'

import { addNotification } from '../../actions/notifications'
import { fetchStatusIfNeeded } from '../../actions/status'

import LineAngleImage from '../../images/line-angle.png'
import LineTImage from '../../images/line-t.png'
import ChangePanel from './ChangePanel'


class Change extends React.Component {
  static propTypes = {
    change: PropTypes.object.isRequired,
    queue: PropTypes.object.isRequired,
    expanded: PropTypes.bool.isRequired,
    pipeline: PropTypes.object,
    tenant: PropTypes.object,
    user: PropTypes.object,
    dispatch: PropTypes.func,
    preferences: PropTypes.object
  }

  state = {
    showDequeueModal: false,
    showPromoteModal: false,
    showAdminActions: false,
  }

  dequeueConfirm = () => {
    const { tenant, change, pipeline } = this.props
    let projectName = change.project
    let changeId = change.id || 'N/A'
    let changeRef = change.ref
    this.setState(() => ({ showDequeueModal: false }))
    // post-merge
    if (/^[0-9a-f]{40}$/.test(changeId)) {
      dequeue_ref(tenant.apiPrefix, projectName, pipeline.name, changeRef)
        .then(() => {
          this.props.dispatch(fetchStatusIfNeeded(tenant))
        })
        .catch(error => {
          this.props.dispatch(addDequeueError(error))
        })
      // pre-merge, ie we have a change id
    } else if (changeId !== 'N/A') {
      dequeue(tenant.apiPrefix, projectName, pipeline.name, changeId)
        .then(() => {
          this.props.dispatch(fetchStatusIfNeeded(tenant))
        })
        .catch(error => {
          this.props.dispatch(addDequeueError(error))
        })
    } else {
      this.props.dispatch(addNotification({
        url: null,
        status: 'Invalid change ' + changeRef + ' on project ' + projectName,
        text: '',
        type: 'error',
      }))
    }
  }

  dequeueCancel = () => {
    this.setState(() => ({ showDequeueModal: false }))
  }

  renderDequeueModal() {
    const { showDequeueModal } = this.state
    const { change } = this.props
    let projectName = change.project
    let changeId = change.id || change.ref
    const title = 'You are about to dequeue a change'
    return (
      <Modal
        variant={ModalVariant.small}
        // titleIconVariant={BullhornIcon}
        isOpen={showDequeueModal}
        title={title}
        onClose={this.dequeueCancel}
        actions={[
          <Button key="deq_confirm" variant="primary" onClick={this.dequeueConfirm}>Confirm</Button>,
          <Button key="deq_cancel" variant="link" onClick={this.dequeueCancel}>Cancel</Button>,
        ]}>
        <p>Please confirm that you want to cancel <strong>all ongoing builds</strong> on change <strong>{changeId}</strong> for project <strong>{projectName}</strong>.</p>
      </Modal>
    )
  }

  promoteConfirm = () => {
    const { tenant, change, pipeline } = this.props
    let changeId = change.id || 'NA'
    this.setState(() => ({ showPromoteModal: false }))
    if (changeId !== 'N/A') {
      promote(tenant.apiPrefix, pipeline.name, [changeId,])
        .then(() => {
          this.props.dispatch(fetchStatusIfNeeded(tenant))
        })
        .catch(error => {
          this.props.dispatch(addPromoteError(error))
        })
    } else {
      this.props.dispatch(addNotification({
        url: null,
        status: 'Invalid change ' + changeId + ' for promotion',
        text: '',
        type: 'error'
      }))
    }
  }

  promoteCancel = () => {
    this.setState(() => ({ showPromoteModal: false }))
  }

  renderPromoteModal() {
    const { showPromoteModal } = this.state
    const { change } = this.props
    let changeId = change.id || 'N/A'
    const title = 'You are about to promote a change'
    return (
      <Modal
        variant={ModalVariant.small}
        // titleIconVariant={BullhornIcon}
        isOpen={showPromoteModal}
        title={title}
        onClose={this.promoteCancel}
        actions={[
          <Button key="prom_confirm" variant="primary" onClick={this.promoteConfirm}>Confirm</Button>,
          <Button key="prom_cancel" variant="link" onClick={this.promoteCancel}>Cancel</Button>,
        ]}>
        <p>Please confirm that you want to promote change <strong>{changeId}</strong>.</p>
      </Modal>
    )
  }

  renderAdminCommands(idx) {
    const { showAdminActions } = this.state
    const { queue } = this.props
    const dropdownCommands = [
      <DropdownItem
        key="dequeue"
        icon={<BanIcon style={{
          color: 'var(--pf-global--danger-color--100)',
        }} />}
        description="Stop all jobs for this change"
        onClick={(event) => {
          event.preventDefault()
          this.setState(() => ({ showDequeueModal: true }))
        }}
      >Dequeue</DropdownItem>,
      <DropdownItem
        key="promote"
        icon={<AngleDoubleUpIcon style={{
          color: 'var(--pf-global--default-color--200)',
        }} />}
        description="Promote this change to the top of the queue"
        onClick={(event) => {
          event.preventDefault()
          this.setState(() => ({ showPromoteModal: true }))
        }}
      >Promote</DropdownItem>
    ]
    return (
      <Dropdown
        title='Actions'
        isOpen={showAdminActions}
        onSelect={() => {
          this.setState({ showAdminActions: !showAdminActions })
          const element = document.getElementById('toggle-id-' + idx + '-' + queue.uuid)
          element.focus()
        }}
        dropdownItems={dropdownCommands}
        isPlain
        toggle={
          <KebabToggle
            onToggle={(showAdminActions) => {
              this.setState({ showAdminActions })
            }}
            id={'toggle-id-' + idx + '-' + queue.uuid} />
        }
      />
    )

  }


  renderStatusIcon(change) {
    let iconGlyph = 'pficon pficon-ok'
    let iconTitle = 'Succeeding'
    if (change.active !== true) {
      iconGlyph = 'pficon pficon-pending'
      iconTitle = 'Waiting until closer to head of queue to' +
        ' start jobs'
    } else if (change.live !== true) {
      iconGlyph = 'pficon pficon-info'
      iconTitle = 'Dependent change required for testing'
    } else if (change.failing_reasons &&
      change.failing_reasons.length > 0) {
      let reason = change.failing_reasons.join(', ')
      iconTitle = 'Failing because ' + reason
      if (reason.match(/merge conflict/)) {
        iconGlyph = 'pficon pficon-error-circle-o zuul-build-merge-conflict'
      } else {
        iconGlyph = 'pficon pficon-error-circle-o'
      }
    }
    const icon = (
      <span
        className={'zuul-build-status ' + iconGlyph}
        title={iconTitle} />
    )
    if (change.live) {
      return (
        <Link to={this.props.tenant.linkPrefix + '/status/change/' + change.id}>
          {icon}
        </Link>
      )
    } else {
      return icon
    }
  }

  renderLineImg(change, i) {
    let image = LineTImage
    if (change._tree_branches.indexOf(i) === change._tree_branches.length - 1) {
      // Angle line
      image = LineAngleImage
    }
    return <img alt="Line" src={image} style={{ verticalAlign: 'baseline' }} />
  }

  render() {
    const { change, queue, expanded, pipeline, user, tenant } = this.props
    let row = []
    let adminMenuWidth = 15
    let i
    for (i = 0; i < queue._tree_columns; i++) {
      let className = ''
      if (i < change._tree.length && change._tree[i] !== null) {
        if (this.props.preferences.darkMode) {
          className = ' zuul-change-row-line-dark'
        } else {
          className = ' zuul-change-row-line'
        }
      }
      row.push(
        <td key={i} className={'zuul-change-row' + className}>
          {i === change._tree_index ? this.renderStatusIcon(change) : ''}
          {change._tree_branches.indexOf(i) !== -1 ? (
            this.renderLineImg(change, i)) : ''}
        </td>)
    }
    let changeWidth = (user.isAdmin && user.scope.indexOf(tenant.name) !== -1)
      ? 360 - adminMenuWidth - 16 * queue._tree_columns
      : 360 - 16 * queue._tree_columns
    row.push(
      <td key={i + 1}
        className="zuul-change-cell"
        style={{ width: changeWidth + 'px' }}>
        <ChangePanel change={change} globalExpanded={expanded} pipeline={pipeline} />
      </td>
    )
    if (user.isAdmin && user.scope.indexOf(tenant.name) !== -1) {
      row.push(
        <td key={i + 2}
          style={{ verticalAlign: 'top', width: adminMenuWidth + 'px' }}>
          {this.renderAdminCommands(i + 2)}
        </td>
      )
    }

    return (
      <>
        <table className="zuul-change-box" style={{ boxSizing: 'content-box' }}>
          <tbody>
            <tr>{row}</tr>
          </tbody>
        </table>
        {this.renderDequeueModal()}
        {this.renderPromoteModal()}
      </>
    )
  }
}

export default connect(state => ({
  tenant: state.tenant,
  user: state.user,
  preferences: state.preferences,
}))(Change)