-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitFilesToObjectsConverter.js
181 lines (143 loc) · 4.7 KB
/
GitFilesToObjectsConverter.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
"use strict";
var type = require("typed");
var Promise = require("es6-promise").Promise;
var TestHandler = require("./TestHandler.js");
var MarkerHandler = require("./MarkerHandler.js");
/**
* Converts commit objects to files object, that includes
* storing relevant markers for each file inside the file object
* instead of seperate files.
*
* Since markers are sometimes sent after the files, it
* ends up in the next commit, IF this happens, we retrieve it.
*
*/
var GitFilesToObjectsConverter = function(){
var filesMissingMarkers = 0;
var filesMissingTests = 0;
var markersRetrievedFromNext = 0;
var testsRetrievedFromNext = 0;
var statesContainingTestsFiles = 0;
var testHandler = new TestHandler();
var convert = function(commits){
type.ofInput({"Array<Commit>": commits});
// Extends the input object
return new Promise(function(resolve, reject){
try{
var states = [];
// Sort commits first
commits = commits.sort(function(a, b){return b.time - a.time;});
commits.map(function(commit, index){
// Skip commits with no file changes
if(commit.files.length === 0){
console.log("Commit contains no files.." + commit.sha);
return;
}
var state = type.create("RepoState");
state.files = [];
state.time = commit.time;
state.commitSha = commit.sha;
state.commitMsg = commit.msg;
states.push(state);
var markersFiles = getFilesByNameRegex(commit.files, /\.markers\.json/);
//No markers were found, check next commit
if(markersFiles.length===0){
var nextCommit = commits[index+1];
if(nextCommit !== undefined){
var timeDiff = nextCommit.time - commit.time;
if(timeDiff <= 1900){
markersRetrievedFromNext++;
markersFiles = getFilesByNameRegex(nextCommit.files, /\.markers\.json/);
}
}
}
if(markersFiles.length === 0){
filesMissingMarkers++;
}
_storeTestFiles(commit);
var markers = new MarkerHandler(markersFiles);
var relevantFiles = getRelevantFiles(commit.files);
relevantFiles.map(function(_file){
var file = type.create("StateFile");
file.name = _file.name;
file.fileContents = _file.fileContents;
// Find markers and tests for file
file.markers = markers.getMarkersForFile(_file);
file.foundMarkers = true;
file.foundTests = false;
// If markers file was not changed in commit,
// it must be the same as last time
if(markersFiles.length === 0){
file.foundMarkers = false;
}
state.files.push(file);
});
});
// Ensure that markers are added to file state where
// the markers file have not explicitly been edited
var fileMarkers = {};
states.map(function(state){
state.files.map(function(file, index){
if(!file.foundMarkers){
if(fileMarkers[file.name] === undefined){
file.markers = [];
}else{
file.markers = fileMarkers[file.name];
}
}
fileMarkers[file.name] = file.markers;
});
});
//Must reverse the array, as pushing makes last commit end up in front
console.log("Got markers from next: ", markersRetrievedFromNext);
console.log("States missong markers: ", filesMissingMarkers);
console.log("Got tests from next: ", testsRetrievedFromNext);
console.log("States missong tests: ", filesMissingTests);
console.log("States containing tests: ", statesContainingTestsFiles);
console.log("Applying tests");
resolve({
states: states,
tests: testHandler.getTests()
});
} catch(e){
console.trace();
console.error("Caught it ");
reject(Error(e));
}
});
};
var _storeTestFiles = function(commit){
var testsFiles = getFilesByNameRegex(commit.files, /\.tests\.json/);
if(testsFiles.length > 0){
statesContainingTestsFiles++;
testsFiles.forEach(function(file){
testHandler.parseFile(file, commit.time);
});
} else {
filesMissingTests++;
}
};
var getRelevantFiles = function(files){
type.ofInput({"Array<File>":files})
return files.filter(function(file){if(file.name.match(/\.markers\.json/) == null && file.name.match(/\.tests\.json/) == null){return true;}});
};
var getFilesByNameRegex = function(fileList,fileNameRegex){
type.ofInput({"Array<File>": fileList, "RegExp": fileNameRegex});
var files = fileList.filter(function(file){
var match = file.name.match(fileNameRegex);
if( match !== null){
return match[0];
}
});
return files || [];
};
var getFileByName = function(files,fileName){
type.ofInput({"Array<File>": files, "String": fileName});
var file = files.filter(function(file){if(file.name === fileName){return file;}})[0];
return file || {};
};
return{
convert: convert
};
};
module.exports = GitFilesToObjectsConverter;