forked from Kibibit/achievibit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutilities.js
178 lines (150 loc) · 4.5 KB
/
utilities.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
var _ = require('lodash');
var utilities = {};
utilities.parseStatuses = parseStatuses;
utilities.parseUser = parseUser;
utilities.getNewFileFromPatch = getNewFileFromPatch;
utilities.getPullRequestIdFromEventData = getPullRequestIdFromEventData;
utilities.parseRepo = parseRepo;
utilities.isPullRequestAssociatedWithOrganization =
isPullRequestAssociatedWithOrganization;
utilities.initializePullRequest = initializePullRequest;
utilities.mergeBasePRData = mergeBasePRData;
utilities.parseReviewStatus = parseReviewStatus;
utilities.parseComment = parseComment;
utilities.parseReviewComment = parseReviewComment;
module.exports = utilities;
function isPullRequestAssociatedWithOrganization(eventData) {
return _.isEqual(eventData.repository.owner.type, 'Organization');
}
function initializePullRequest(eventData) {
var id = utilities.getPullRequestIdFromEventData(eventData);
var pullRequest = {
_id: id,
id: id,
url: eventData.pull_request.html_url,
number: eventData.number,
title: eventData.pull_request.title,
description: eventData.pull_request.body,
creator: utilities.parseUser(eventData.pull_request.user),
createdOn: eventData.pull_request.created_at,
//milestone: eventData.pull_request.milestone,
labels: [],
history: {},
repository: utilities.parseRepo(eventData.repository)
};
if (utilities.isPullRequestAssociatedWithOrganization(eventData)) {
pullRequest.organization =
utilities.parseUser(eventData.repository.owner, true);
}
if (eventData.pull_request.assignees) {
var assignees = eventData.pull_request.assignees;
pullRequest.assignees = [];
for (var i = 0; i < assignees.length; i++) {
pullRequest.assignees.push(utilities.parseUser(assignees[i]));
}
}
return pullRequest;
}
function parseRepo(repository) {
return {
name: repository.name,
fullname: repository.full_name,
url: repository.html_url
};
}
function parseComment(comment, isInlineComment) {
var commentObject = {
author: utilities.parseUser(comment.user),
message: comment.body,
createdOn: comment.created_at,
edited: !_.isEqual(comment.created_at, comment.updated_at),
apiUrl: comment.url
};
if (isInlineComment) {
commentObject.file = comment.path;
commentObject.commit = comment.commit_id;
commentObject.apiUrl = comment.url;
}
return commentObject;
}
function parseReviewComment(comment) {
var commentObject = {
id: comment.id,
reviewId: comment.pull_request_review_id,
author: utilities.parseUser(comment.user),
message: comment.body,
createdOn: comment.created_at,
edited: !_.isEqual(comment.created_at, comment.updated_at),
apiUrl: comment.url,
file: comment.path,
commit: comment.commit_id
};
return commentObject;
}
function parseReviewStatus(review) {
return {
id: review.id,
user: parseUser(review.user),
message: review.body || '',
state: review.state,
createdOn: review.submitted_at,
commit: review.commit_id
};
}
function parseStatuses(statuses) {
var parsed = {};
_.forEach(statuses, function(status) {
// nothing should be pending if the pull request got merged
// TODO(Thatkookooguy): check if this assumption is true
if (!_.isEqual(status.state, 'pending')) {
parsed[status.context] = {
state: status.state,
description: status.description,
target: status.target_url,
context: status.context
};
}
});
return parsed;
}
function parseUser(githubUser, isOrganization) {
var user = {
username: githubUser.login,
url: githubUser.html_url,
avatar: githubUser.avatar_url
};
if (isOrganization) {
user.organization = true;
}
return user;
}
function getNewFileFromPatch(patch) {
if (!patch) {
return;
}
return patch.split('\n').filter(function(line) {
return !line.startsWith('-') &&
!line.startsWith('@') &&
line.indexOf('No newline at end of file') === -1;
}).map(function(line) {
if (line.startsWith('+')) {
return line.replace('+', '');
}
return line;
}).join('\n');
}
function getPullRequestIdFromEventData(eventData) {
return [
eventData.repository.full_name,
'/pull/',
eventData.pull_request.number
].join('');
}
function mergeBasePRData(pullRequestsObject, eventData) {
var id = getPullRequestIdFromEventData(eventData);
if (!pullRequestsObject[id]) {
pullRequestsObject[id] = {};
}
var newData = utilities.initializePullRequest(eventData);
_.assign(pullRequestsObject[id], newData);
}