forked from atom/github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpr-patch-container.js
165 lines (139 loc) · 4.52 KB
/
pr-patch-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
import React from 'react';
import PropTypes from 'prop-types';
import {parse as parseDiff} from 'what-the-diff';
import {toNativePathSep} from '../helpers';
import {EndpointPropType} from '../prop-types';
import {filter as filterDiff} from '../models/patch/filter';
import {buildMultiFilePatch} from '../models/patch';
export default class PullRequestPatchContainer extends React.Component {
static propTypes = {
// Pull request properties
owner: PropTypes.string.isRequired,
repo: PropTypes.string.isRequired,
number: PropTypes.number.isRequired,
// Connection properties
endpoint: EndpointPropType.isRequired,
token: PropTypes.string.isRequired,
// Fetching and parsing
refetch: PropTypes.bool,
largeDiffThreshold: PropTypes.number,
// Render prop. Called with (error or null, multiFilePatch or null)
children: PropTypes.func.isRequired,
}
state = {
multiFilePatch: null,
error: null,
last: {url: null, patch: null, etag: null},
}
componentDidMount() {
this.mounted = true;
this.fetchDiff(this.state.last);
}
componentDidUpdate(prevProps) {
const explicitRefetch = this.props.refetch && !prevProps.refetch;
const requestedURLChange = this.state.last.url !== this.getDiffURL();
if (explicitRefetch || requestedURLChange) {
const {last} = this.state;
this.setState({
multiFilePatch: null,
error: null,
last: {url: this.getDiffURL(), patch: null, etag: null},
});
this.fetchDiff(last);
}
}
componentWillUnmount() {
this.mounted = false;
}
render() {
return this.props.children(this.state.error, this.state.multiFilePatch);
}
// Generate a v3 GitHub API REST URL for the pull request resource.
// Example: https://api.github.com/repos/atom/github/pulls/1829
getDiffURL() {
return this.props.endpoint.getRestURI('repos', this.props.owner, this.props.repo, 'pulls', this.props.number);
}
buildPatch(rawDiff) {
const {filtered, removed} = filterDiff(rawDiff);
const diffs = parseDiff(filtered).map(diff => {
// diff coming from API will have the defaul git diff prefixes a/ and b/ and use *nix-style / path separators.
// e.g. a/dir/file1.js and b/dir/file2.js
// see https://git-scm.com/docs/git-diff#_generating_patches_with_p
return {
...diff,
newPath: diff.newPath ? toNativePathSep(diff.newPath.replace(/^[a|b]\//, '')) : diff.newPath,
oldPath: diff.oldPath ? toNativePathSep(diff.oldPath.replace(/^[a|b]\//, '')) : diff.oldPath,
};
});
const options = {
preserveOriginal: true,
removed,
};
if (this.props.largeDiffThreshold) {
options.largeDiffThreshold = this.props.largeDiffThreshold;
}
const mfp = buildMultiFilePatch(diffs, options);
return mfp;
}
async fetchDiff(last) {
const url = this.getDiffURL();
let response;
try {
const headers = {
Accept: 'application/vnd.github.v3.diff',
Authorization: `bearer ${this.props.token}`,
};
if (url === last.url && last.etag !== null) {
headers['If-None-Match'] = last.etag;
}
response = await fetch(url, {headers});
} catch (err) {
return this.reportDiffError(`Network error encountered fetching the patch: ${err.message}.`, err);
}
if (response.status === 304) {
// Not modified.
if (!this.mounted) {
return null;
}
return new Promise(resolve => this.setState({
multiFilePatch: last.patch,
error: null,
last,
}));
}
if (!response.ok) {
return this.reportDiffError(`Unable to fetch the diff for this pull request: ${response.statusText}.`);
}
try {
const etag = response.headers.get('ETag');
const rawDiff = await response.text();
if (!this.mounted) {
return null;
}
const multiFilePatch = this.buildPatch(rawDiff);
return new Promise(resolve => this.setState({
multiFilePatch,
error: null,
last: {url, patch: multiFilePatch, etag},
}, resolve));
} catch (err) {
return this.reportDiffError('Unable to parse the diff for this pull request.', err);
}
}
reportDiffError(message, error) {
if (!this.mounted) {
return null;
}
return new Promise(resolve => {
if (error) {
// eslint-disable-next-line no-console
console.error(error);
}
if (!this.mounted) {
resolve();
return;
}
this.setState({error: message}, resolve);
});
}
}