forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomment-decorations-container.js
264 lines (237 loc) · 8.03 KB
/
comment-decorations-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
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
import React from 'react';
import PropTypes from 'prop-types';
import yubikiri from 'yubikiri';
import {QueryRenderer, graphql} from 'react-relay';
import CommentDecorationsController from '../controllers/comment-decorations-controller';
import ObserveModel from '../views/observe-model';
import RelayEnvironment from '../views/relay-environment';
import {GithubLoginModelPropType} from '../prop-types';
import {UNAUTHENTICATED, INSUFFICIENT} from '../shared/keytar-strategy';
import RelayNetworkLayerManager from '../relay-network-layer-manager';
import {PAGE_SIZE} from '../helpers';
import AggregatedReviewsContainer from './aggregated-reviews-container';
import CommentPositioningContainer from './comment-positioning-container';
import PullRequestPatchContainer from './pr-patch-container';
export default class CommentDecorationsContainer extends React.Component {
static propTypes = {
workspace: PropTypes.object.isRequired,
commands: PropTypes.object.isRequired,
localRepository: PropTypes.object.isRequired,
loginModel: GithubLoginModelPropType.isRequired,
reportRelayError: PropTypes.func.isRequired,
};
render() {
return (
<ObserveModel model={this.props.localRepository} fetchData={this.fetchRepositoryData}>
{this.renderWithLocalRepositoryData}
</ObserveModel>
);
}
renderWithLocalRepositoryData = repoData => {
if (!repoData) {
return null;
}
return (
<ObserveModel
model={this.props.loginModel}
fetchParams={[repoData]}
fetchData={this.fetchToken}>
{token => this.renderWithToken(token, {repoData})}
</ObserveModel>
);
}
renderWithToken(token, {repoData}) {
if (!token || token === UNAUTHENTICATED || token === INSUFFICIENT || token instanceof Error) {
// we're not going to prompt users to log in to render decorations for comments
// just let it go and move on with our lives.
return null;
}
const head = repoData.branches.getHeadBranch();
if (!head.isPresent()) {
return null;
}
const push = head.getPush();
if (!push.isPresent() || !push.isRemoteTracking()) {
return null;
}
const pushRemote = repoData.remotes.withName(push.getRemoteName());
if (!pushRemote.isPresent() || !pushRemote.isGithubRepo()) {
return null;
}
const endpoint = repoData.currentRemote.getEndpoint();
const environment = RelayNetworkLayerManager.getEnvironmentForHost(endpoint, token);
const query = graphql`
query commentDecorationsContainerQuery(
$headOwner: String!
$headName: String!
$headRef: String!
$reviewCount: Int!
$reviewCursor: String
$threadCount: Int!
$threadCursor: String
$commentCount: Int!
$commentCursor: String
$first: Int!
) {
repository(owner: $headOwner, name: $headName) {
ref(qualifiedName: $headRef) {
associatedPullRequests(first: $first, states: [OPEN]) {
totalCount
nodes {
number
headRefOid
...commentDecorationsController_pullRequests
...aggregatedReviewsContainer_pullRequest @arguments(
reviewCount: $reviewCount
reviewCursor: $reviewCursor
threadCount: $threadCount
threadCursor: $threadCursor
commentCount: $commentCount
commentCursor: $commentCursor
)
}
}
}
}
}
`;
const variables = {
headOwner: pushRemote.getOwner(),
headName: pushRemote.getRepo(),
headRef: push.getRemoteRef(),
first: 1,
reviewCount: PAGE_SIZE,
reviewCursor: null,
threadCount: PAGE_SIZE,
threadCursor: null,
commentCount: PAGE_SIZE,
commentCursor: null,
};
return (
<RelayEnvironment.Provider value={environment}>
<QueryRenderer
environment={environment}
query={query}
variables={variables}
render={queryResult => this.renderWithPullRequest({
endpoint,
owner: variables.headOwner,
repo: variables.headName,
...queryResult,
}, {repoData, token})}
/>
</RelayEnvironment.Provider>
);
}
renderWithPullRequest({error, props, endpoint, owner, repo}, {repoData, token}) {
if (error) {
// eslint-disable-next-line no-console
console.warn(`error fetching CommentDecorationsContainer data: ${error}`);
return null;
}
if (
!props || !props.repository || !props.repository.ref ||
!props.repository.ref.associatedPullRequests ||
props.repository.ref.associatedPullRequests.totalCount === 0
) {
// no loading spinner for you
// just fetch silently behind the scenes like a good little container
return null;
}
const currentPullRequest = props.repository.ref.associatedPullRequests.nodes[0];
return (
<AggregatedReviewsContainer
pullRequest={currentPullRequest}
reportRelayError={this.props.reportRelayError}>
{({errors, summaries, commentThreads}) => {
return this.renderWithReviews(
{errors, summaries, commentThreads},
{currentPullRequest, repoResult: props, endpoint, owner, repo, repoData, token},
);
}}
</AggregatedReviewsContainer>
);
}
renderWithReviews(
{errors, summaries, commentThreads},
{currentPullRequest, repoResult, endpoint, owner, repo, repoData, token},
) {
if (errors && errors.length > 0) {
// eslint-disable-next-line no-console
console.warn('Errors aggregating reviews and comments for current pull request', ...errors);
return null;
}
if (commentThreads.length === 0) {
return null;
}
return (
<PullRequestPatchContainer
owner={owner}
repo={repo}
number={currentPullRequest.number}
endpoint={endpoint}
token={token}
largeDiffThreshold={Infinity}>
{(patchError, patch) => this.renderWithPatch(
{error: patchError, patch},
{summaries, commentThreads, currentPullRequest, repoResult, endpoint, owner, repo, repoData, token},
)}
</PullRequestPatchContainer>
);
}
renderWithPatch(
{error, patch},
{summaries, commentThreads, currentPullRequest, repoResult, endpoint, owner, repo, repoData, token},
) {
if (error) {
// eslint-disable-next-line no-console
console.warn('Error fetching patch for current pull request', error);
return null;
}
if (!patch) {
return null;
}
return (
<CommentPositioningContainer
multiFilePatch={patch}
commentThreads={commentThreads}
prCommitSha={currentPullRequest.headRefOid}
localRepository={this.props.localRepository}
workdir={repoData.workingDirectoryPath}>
{commentTranslations => {
if (!commentTranslations) {
return null;
}
return (
<CommentDecorationsController
endpoint={endpoint}
owner={owner}
repo={repo}
workspace={this.props.workspace}
commands={this.props.commands}
repoData={repoData}
commentThreads={commentThreads}
commentTranslations={commentTranslations}
pullRequests={repoResult.repository.ref.associatedPullRequests.nodes}
/>
);
}}
</CommentPositioningContainer>
);
}
fetchRepositoryData = repository => {
return yubikiri({
branches: repository.getBranches(),
remotes: repository.getRemotes(),
currentRemote: repository.getCurrentGitHubRemote(),
workingDirectoryPath: repository.getWorkingDirectoryPath(),
});
}
fetchToken = (loginModel, repoData) => {
const endpoint = repoData.currentRemote.getEndpoint();
if (!endpoint) {
return null;
}
return loginModel.getToken(endpoint.getLoginAccount());
}
}