forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomment-decorations-controller.js
More file actions
252 lines (224 loc) · 7.91 KB
/
comment-decorations-controller.js
File metadata and controls
252 lines (224 loc) · 7.91 KB
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
import React, {Fragment} from 'react';
import PropTypes from 'prop-types';
import {CompositeDisposable} from 'event-kit';
import {graphql, createFragmentContainer} from 'react-relay';
import path from 'path';
import EditorCommentDecorationsController from './editor-comment-decorations-controller';
import ReviewsItem from '../items/reviews-item';
import Gutter from '../atom/gutter';
import Commands, {Command} from '../atom/commands';
import {EndpointPropType, BranchSetPropType, RemoteSetPropType, RemotePropType} from '../prop-types';
import {toNativePathSep} from '../helpers';
export class BareCommentDecorationsController extends React.Component {
static propTypes = {
// Relay response
relay: PropTypes.object.isRequired,
pullRequests: PropTypes.arrayOf(PropTypes.shape({
number: PropTypes.number.isRequired,
headRefName: PropTypes.string.isRequired,
headRefOid: PropTypes.string.isRequired,
headRepository: PropTypes.shape({
name: PropTypes.string.isRequired,
owner: PropTypes.shape({
login: PropTypes.string.isRequired,
}).isRequired,
}),
repository: PropTypes.shape({
name: PropTypes.string.isRequired,
owner: PropTypes.shape({
login: PropTypes.string.isRequired,
}).isRequired,
}).isRequired,
})),
// Connection information
endpoint: EndpointPropType.isRequired,
owner: PropTypes.string.isRequired,
repo: PropTypes.string.isRequired,
// Atom environment
workspace: PropTypes.object.isRequired,
commands: PropTypes.object.isRequired,
// Models
repoData: PropTypes.shape({
branches: BranchSetPropType.isRequired,
remotes: RemoteSetPropType.isRequired,
currentRemote: RemotePropType.isRequired,
workingDirectoryPath: PropTypes.string.isRequired,
}).isRequired,
commentThreads: PropTypes.arrayOf(PropTypes.shape({
comments: PropTypes.arrayOf(PropTypes.object).isRequired,
thread: PropTypes.shape({
id: PropTypes.string.isRequired,
}).isRequired,
})).isRequired,
commentTranslations: PropTypes.shape({
get: PropTypes.func.isRequired,
}).isRequired,
};
constructor(props, context) {
super(props, context);
this.subscriptions = new CompositeDisposable();
this.state = {openEditors: this.props.workspace.getTextEditors()};
}
componentDidMount() {
this.subscriptions.add(
this.props.workspace.observeTextEditors(this.updateOpenEditors),
this.props.workspace.onDidDestroyPaneItem(this.updateOpenEditors),
);
}
componentWillUnmount() {
this.subscriptions.dispose();
}
render() {
if (this.props.pullRequests.length === 0) {
return null;
}
const pullRequest = this.props.pullRequests[0];
// only show comment decorations if we're on a checked out pull request
// otherwise, we'd have no way of knowing which comments to show.
if (
!this.isCheckedOutPullRequest(
this.props.repoData.branches,
this.props.repoData.remotes,
pullRequest,
)
) {
return null;
}
const threadDataByPath = new Map();
const workdirPath = this.props.repoData.workingDirectoryPath;
for (const {comments, thread} of this.props.commentThreads) {
// Skip comment threads that are entirely minimized.
if (comments.every(comment => comment.isMinimized)) {
continue;
}
// There may be multiple comments in the thread, but we really only care about the root comment when rendering
// decorations.
const threadData = {
rootCommentID: comments[0].id,
threadID: thread.id,
position: comments[0].position,
nativeRelPath: toNativePathSep(comments[0].path),
fullPath: path.join(workdirPath, toNativePathSep(comments[0].path)),
};
if (threadDataByPath.get(threadData.fullPath)) {
threadDataByPath.get(threadData.fullPath).push(threadData);
} else {
threadDataByPath.set(threadData.fullPath, [threadData]);
}
}
const openEditorsWithCommentThreads = this.getOpenEditorsWithCommentThreads(threadDataByPath);
return (
<Fragment>
<Commands registry={this.props.commands} target="atom-workspace">
<Command command="github:open-reviews-tab" callback={this.openReviewsTab} />
</Commands>
{openEditorsWithCommentThreads.map(editor => {
const threadData = threadDataByPath.get(editor.getPath());
const translations = this.props.commentTranslations.get(threadData[0].nativeRelPath);
return (
<Fragment key={`github-editor-decoration-${editor.id}`}>
<Gutter
name="github-comment-icon"
priority={1}
className="comment"
editor={editor}
type="decorated"
/>
<EditorCommentDecorationsController
endpoint={this.props.endpoint}
owner={this.props.owner}
repo={this.props.repo}
number={pullRequest.number}
workdir={workdirPath}
workspace={this.props.workspace}
editor={editor}
fileName={editor.getPath()}
headSha={pullRequest.headRefOid}
threadsForPath={threadData}
commentTranslationsForPath={translations}
/>
</Fragment>
);
})}
</Fragment>);
}
getOpenEditorsWithCommentThreads(threadDataByPath) {
const haveThreads = [];
for (const editor of this.state.openEditors) {
if (threadDataByPath.has(editor.getPath())) {
haveThreads.push(editor);
}
}
return haveThreads;
}
// Determine if we already have this PR checked out.
// todo: if this is similar enough to pr-checkout-controller, extract a single
// helper function to do this check.
isCheckedOutPullRequest(branches, remotes, pullRequest) {
// determine if pullRequest.headRepository is null
// this can happen if a repository has been deleted.
if (!pullRequest.headRepository) {
return false;
}
const {repository} = pullRequest;
const headPush = branches.getHeadBranch().getPush();
const headRemote = remotes.withName(headPush.getRemoteName());
// (detect checkout from pull/### refspec)
const fromPullRefspec =
headRemote.getOwner() === repository.owner.login &&
headRemote.getRepo() === repository.name &&
headPush.getShortRemoteRef() === `pull/${pullRequest.number}/head`;
// (detect checkout from head repository)
const fromHeadRepo =
headRemote.getOwner() === pullRequest.headRepository.owner.login &&
headRemote.getRepo() === pullRequest.headRepository.name &&
headPush.getShortRemoteRef() === pullRequest.headRefName;
if (fromPullRefspec || fromHeadRepo) {
return true;
}
return false;
}
updateOpenEditors = () => {
return new Promise(resolve => {
this.setState({openEditors: this.props.workspace.getTextEditors()}, resolve);
});
}
openReviewsTab = () => {
const [pullRequest] = this.props.pullRequests;
/* istanbul ignore if */
if (!pullRequest) {
return null;
}
const uri = ReviewsItem.buildURI({
host: this.props.endpoint.getHost(),
owner: this.props.owner,
repo: this.props.repo,
number: pullRequest.number,
workdir: this.props.repoData.workingDirectoryPath,
});
return this.props.workspace.open(uri, {searchAllPanes: true});
}
}
export default createFragmentContainer(BareCommentDecorationsController, {
pullRequests: graphql`
fragment commentDecorationsController_pullRequests on PullRequest
@relay(plural: true)
{
number
headRefName
headRefOid
headRepository {
name
owner {
login
}
}
repository {
name
owner {
login
}
}
}
`,
});