This repository has been archived by the owner on Dec 15, 2022. It is now read-only.
/
pr-statuses-container.js
228 lines (207 loc) · 6.66 KB
/
pr-statuses-container.js
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
import React from 'react';
import {createRefetchContainer, graphql} from 'react-relay';
import PropTypes from 'prop-types';
import {autobind} from 'core-decorators';
import {toSentence} from '../helpers';
import PrStatusContextContainer from './pr-status-context-container';
import Octicon from '../atom/octicon';
import DonutChart from '../views/donut-chart';
import PeriodicRefresher from '../periodic-refresher';
import {RelayConnectionPropType} from '../prop-types';
export const stateToIconAndStyle = {
EXPECTED: {category: 'PENDING', icon: 'primitive-dot', style: 'status-warning'},
PENDING: {category: 'PENDING', icon: 'primitive-dot', style: 'status-warning'},
SUCCESS: {category: 'SUCCESS', icon: 'check', style: 'status-success'},
ERROR: {category: 'FAILURE', icon: 'alert', style: 'status-error'},
FAILURE: {category: 'FAILURE', icon: 'x', style: 'status-error'},
};
function category(state) {
const info = stateToIconAndStyle[state];
if (!info) {
throw new Error(`Unknown state ${state}`);
}
return info.category;
}
class StatusDonutChart extends React.Component {
static propTypes = {
pending: PropTypes.number,
failed: PropTypes.number,
succeeded: PropTypes.number,
}
render() {
const {pending, failed, succeeded, ...others} = this.props; // eslint-disable-line no-unused-vars
const slices = ['pending', 'failed', 'succeeded'].reduce((acc, type) => {
const count = this.props[type];
if (count > 0) {
acc.push({type, className: type, count});
}
return acc;
}, []);
return <DonutChart {...others} slices={slices} />;
}
}
export class PrStatuses extends React.Component {
static propTypes = {
relay: PropTypes.shape({
refetch: PropTypes.func.isRequired,
}).isRequired,
displayType: PropTypes.oneOf([
'check', 'full',
]),
pullRequest: PropTypes.shape({
id: PropTypes.string.isRequired,
commits: RelayConnectionPropType(
PropTypes.shape({
commit: PropTypes.shape({
status: PropTypes.shape({
state: PropTypes.string.isRequired,
contexts: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.string.isRequired,
}).isRequired,
).isRequired,
}),
}).isRequired,
}).isRequired,
).isRequired,
}).isRequired,
}
static defaultProps = {
displayType: 'full',
}
static lastRefreshPerPr = new Map()
static SUCCESS_REFRESH_TIMEOUT = 3 * 60 * 1000
static PENDING_REFRESH_TIMEOUT = 30 * 1000
static MINIMUM_REFRESH_INTERVAL = 15 * 1000
componentDidMount() {
this.refresher = new PeriodicRefresher(PrStatuses, {
interval: () => {
if (this.isPendingResults()) {
return PrStatuses.PENDING_REFRESH_TIMEOUT;
} else {
return PrStatuses.SUCCESS_REFRESH_TIMEOUT;
}
},
getCurrentId: () => this.props.pullRequest.id,
refresh: this.refresh,
minimumIntervalPerId: PrStatuses.MINIMUM_REFRESH_INTERVAL,
});
this.refresher.start();
}
componentWillUnmount() {
this.refresher.destroy();
}
@autobind
refresh() {
this.props.relay.refetch({
id: this.props.pullRequest.id,
}, null, null, {force: true});
}
render() {
const headCommit = this.props.pullRequest.commits.edges[0].node.commit;
if (!headCommit.status) { return null; }
const {state, contexts} = headCommit.status;
if (this.props.displayType === 'check') {
const {icon, style} = stateToIconAndStyle[state];
return <Octicon icon={icon} className={style} />;
} else if (this.props.displayType === 'full') {
return (
<div className="github-PrStatuses">
<div className="github-PrStatuses-header">
<div className="github-PrStatuses-donut-chart">
{this.renderDonutChart(headCommit.status)}
</div>
<div className="github-PrStatuses-summary">
{this.summarySentence(headCommit.status)}
</div>
</div>
<ul className="github-PrStatuses-list">
{contexts.map(context => <PrStatusContextContainer key={context.id} context={context} />)}
</ul>
</div>
);
} else {
throw new Error('Invalid `displayType` prop value');
}
}
isPendingResults() {
const headCommit = this.props.pullRequest.commits.edges[0].node.commit;
if (!headCommit.status) { return false; }
const {contexts} = headCommit.status;
return contexts.some(c => category(c.state) === 'PENDING');
}
renderDonutChart(status) {
const {contexts} = status;
const pendingLen = contexts.filter(c => category(c.state) === 'PENDING').length;
const failedLen = contexts.filter(c => category(c.state) === 'FAILURE').length;
const succeededLen = contexts.filter(c => category(c.state) === 'SUCCESS').length;
return (
<StatusDonutChart
pending={pendingLen}
failed={failedLen}
succeeded={succeededLen}
/>
);
}
summarySentence(status) {
if (this.isAllSucceeded(status)) {
return 'All checks succeeded';
} else if (this.isAllFailed(status)) {
return 'All checks failed';
} else {
const {contexts} = status;
const noun = contexts.length === 1 ? 'check' : 'checks';
const parts = [];
const pending = contexts.filter(c => category(c.state) === 'PENDING');
const failing = contexts.filter(c => category(c.state) === 'FAILURE');
const succeeded = contexts.filter(c => category(c.state) === 'SUCCESS');
if (pending.length) {
parts.push(`${pending.length} pending`);
}
if (failing.length) {
parts.push(`${failing.length} failing`);
}
if (succeeded.length) {
parts.push(`${succeeded.length} successful`);
}
return toSentence(parts) + ` ${noun}`;
}
}
isAllSucceeded(status) {
return category(status.state) === 'SUCCESS';
}
isAllFailed(status) {
return status.contexts.every(c => category(c.state) === 'FAILURE');
}
}
export default createRefetchContainer(PrStatuses, {
pullRequest: graphql`
fragment prStatusesContainer_pullRequest on PullRequest {
id
commits(last:1) {
edges {
node {
commit {
status {
state
contexts {
id
state
...prStatusContextContainer_context
}
}
}
}
}
}
}
`,
}, graphql`
query prStatusesContainerRefetchQuery($id: ID!) {
node(id: $id) {
... on PullRequest {
...prStatusesContainer_pullRequest
}
}
}
`);