/
utilities.js
234 lines (205 loc) · 6.82 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
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
var expect = require('chai').expect;
var utilities = require('../utilities');
describe('achievibit Utilities', function() {
describe('parseStatuses', function() {
it('should ignore pending statuses', function() {
var statuses = [
new Status('commit1', 'pending')
];
var parsedStatuses = utilities.parseStatuses(statuses);
expect(parsedStatuses).to.be.an('object');
expect(parsedStatuses).to.not.include.keys('commit1');
});
it('should parse all other statuses besides pending', function() {
var statuses = [
new Status('commit1', 'pending'),
new Status('commit2', 'success'),
new Status('commit3', 'error'),
new Status('commit4', 'failed')
];
var parsedStatuses = utilities.parseStatuses(statuses);
expect(parsedStatuses).to.be.an('object');
expect(parsedStatuses).to.include.keys(
'commit2',
'commit3',
'commit4'
);
});
});
describe('mergeBasePRData - recollect basic pull request data', function() {
it('should create a PR entry if not exists', function() {
var eventData = require('./stubs/eventData.mock');
var pullRequestsObject = {};
utilities.mergeBasePRData(pullRequestsObject, eventData);
var expectedId = 'Kibibit/achievibit/pull/1';
expect(pullRequestsObject[expectedId]).to.be.an('object');
expect(pullRequestsObject[expectedId]).to.include.keys(
'id',
'_id', // ??
'url',
'number',
'title',
'description',
'creator',
'createdOn',
'labels',
'history',
'repository'
);
});
it('should merge new data if old data exist', function() {
var eventData = require('./stubs/eventData.mock');
var pullRequestsObject = {
'Kibibit/achievibit/pull/1': {
'id': 'Kibibit/achievibit/pull/1',
'url': 'nice',
'number': 1,
'title': 'old_title',
'description': 'old_description',
}
};
utilities.mergeBasePRData(pullRequestsObject, eventData);
var expectedId = 'Kibibit/achievibit/pull/1';
expect(pullRequestsObject[expectedId]).to.be.an('object');
expect(pullRequestsObject[expectedId]).to.include.keys(
'id',
'_id', // ??
'url',
'number',
'title',
'description',
'creator',
'createdOn',
'labels',
'history',
'repository'
);
});
});
describe('initializePullRequest - get all basic data from event', function() {
it('should return the correct data', function() {
var eventData = require('./stubs/eventData.mock');
var pullRequest = utilities.initializePullRequest(eventData);
expect(pullRequest).to.be.an('object');
expect(pullRequest).to.include.keys(
'id',
'_id', // ??
'url',
'number',
'title',
'description',
'creator',
'createdOn',
'labels',
'history',
'repository'
);
expect(pullRequest).to.not.include.keys('organization', 'assignees');
});
it('should have organization associated if found', function() {
var eventData = require('./stubs/eventData.mock');
eventData.repository.owner.type = 'Organization';
var pullRequest = utilities.initializePullRequest(eventData);
expect(pullRequest).to.be.an('object');
expect(pullRequest).to.include.keys('organization');
});
it('should have assignees if found', function() {
var eventData = require('./stubs/eventData.mock');
eventData.pull_request.assignees = [];
eventData.pull_request.assignees.push({
'login': 'Thatkookooguy',
'avatar_url': 'https://avatars.githubusercontent.com/u/10427304?v=3',
'html_url': 'https://github.com/Thatkookooguy',
});
var pullRequest = utilities.initializePullRequest(eventData);
expect(pullRequest).to.be.an('object');
expect(pullRequest).to.include.keys('assignees');
});
});
describe('parseUser - GitHub User to achievibit User', function() {
it('should return the correct data', function() {
var achievibitUser = utilities.parseUser(new GithubUser(), false);
expect(achievibitUser).to.be.an('object');
expect(achievibitUser).to.include.keys('username', 'url', 'avatar');
});
it('should flag as organization if set to true', function() {
var achievibitUser = utilities.parseUser(new GithubUser(), true);
expect(achievibitUser).to.be.an('object');
expect(achievibitUser)
.to.include.keys('username', 'url', 'avatar', 'organization');
});
});
describe('parseRepo - GitHub Repo to achievibit Repo', function() {
it('should return the correct data', function() {
var achievibitUser = utilities.parseRepo(new GithubRepo());
expect(achievibitUser).to.be.an('object');
expect(achievibitUser).to.include.keys('name', 'fullname', 'url');
});
});
describe('isPullRequestAssociatedWithOrganization', function() {
it('should return true if associated with organization', function() {
var pullRequest = new PullRequest();
pullRequest.repository.owner.type = 'Organization';
var isOrganizationPresent =
utilities.isPullRequestAssociatedWithOrganization(pullRequest);
expect(isOrganizationPresent).to.be.a('boolean');
expect(isOrganizationPresent).to.be.true;
});
it('should return false if NOT associated with organization', function() {
var pullRequest = new PullRequest();
var isOrganizationPresent =
utilities.isPullRequestAssociatedWithOrganization(pullRequest);
expect(isOrganizationPresent).to.be.a('boolean');
expect(isOrganizationPresent).to.be.false;
});
});
describe('getPullRequestIdFromEventData', function() {
it('should create an id string out of GitHub\'s eventData', function() {
var eventData = {
'pull_request': {
number: 1
},
repository: {
'full_name': 'repo'
}
};
var id = utilities.getPullRequestIdFromEventData(eventData);
expect(id).to.be.a('string');
expect(id).to.equal('repo/pull/1');
});
});
});
function GithubRepo() {
return {
'name': 'repo-name',
'full_name': 'repo-fullname',
'html_url': 'repo-url',
'owner': new GithubUser()
};
}
function GithubUser() {
return {
'login': 'login',
'html_url': 'html_url',
'avatar_url': 'avatar_url'
};
}
function PullRequest() {
return {
'id': 'test',
'url': 'url',
'number': 3,
'creator': {
'username': 'creator'
},
'repository': new GithubRepo()
};
}
function Status(context, state) {
return {
'state': state || 'pending',
'description': 'description yo',
'target': 'url',
'context': context || 'commit'
};
}