Repository navigation
Expand file tree
/
Copy pathpr.js
More file actions
163 lines (140 loc) · 3.15 KB
/
Copy pathpr.js
File metadata and controls
163 lines (140 loc) · 3.15 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
class CommentBuilder {
constructor() {
this._id = 0;
this._path = 'first.txt';
this._position = 0;
this._authorLogin = 'someone';
this._authorAvatarUrl = 'https://avatars3.githubusercontent.com/u/17565?s=32&v=4';
this._url = 'https://github.com/atom/github/pull/1829/files#r242224689';
this._createdAt = 0;
this._body = 'Lorem ipsum dolor sit amet, te urbanitas appellantur est.';
this._replyTo = null;
}
id(i) {
this._id = i;
return this;
}
path(p) {
this._path = p;
return this;
}
position(pos) {
this._position = pos;
return this;
}
authorLogin(login) {
this._authorLogin = login;
return this;
}
authorAvatarUrl(url) {
this._authorAvatarUrl = url;
return this;
}
url(u) {
this._url = u;
return this;
}
createdAt(ts) {
this._createdAt = ts;
return this;
}
body(text) {
this._body = text;
return this;
}
replyTo(replyToId) {
this._replyTo = {id: replyToId};
return this;
}
build() {
return {
id: this._id,
author: {
login: this._authorLogin,
avatarUrl: this._authorAvatarUrl,
},
body: this._body,
path: this._path,
position: this._position,
createdAt: this._createdAt,
url: this._url,
replyTo: this._replyTo,
};
}
}
class ReviewBuilder {
constructor() {
this.nextCommentID = 0;
this._id = 0;
this._comments = [];
this._submittedAt = '2018-12-28T20:40:55Z';
}
id(i) {
this._id = i;
return this;
}
submittedAt(timestamp) {
this._submittedAt = timestamp;
return this;
}
addComment(block = () => {}) {
const builder = new CommentBuilder();
builder.id(this.nextCommentID);
this.nextCommentID++;
block(builder);
this._comments.push(builder.build());
return this;
}
build() {
const comments = this._comments.map(comment => {
return {node: comment};
});
return {
id: this._id,
submittedAt: this._submittedAt,
comments: {edges: comments},
};
}
}
class PullRequestBuilder {
constructor() {
this.nextCommentID = 0;
this.nextReviewID = 0;
this._reviews = [];
}
addReview(block = () => {}) {
const builder = new ReviewBuilder();
builder.id(this.nextReviewID);
this.nextReviewID++;
block(builder);
this._reviews.push(builder.build());
return this;
}
build() {
const commentThreads = {};
this._reviews.forEach(review => {
review.comments.edges.forEach(({node: comment}) => {
if (comment.replyTo && comment.replyTo.id && commentThreads[comment.replyTo.id]) {
commentThreads[comment.replyTo.id].push(comment);
} else {
commentThreads[comment.id] = [comment];
}
});
});
return {
reviews: {nodes: this._reviews},
commentThreads: Object.keys(commentThreads).map(rootCommentId => {
return {
rootCommentId,
comments: commentThreads[rootCommentId],
};
}),
};
}
}
export function reviewBuilder() {
return new ReviewBuilder();
}
export function pullRequestBuilder() {
return new PullRequestBuilder();
}