Skip to content

Modify index.js to support include_paths #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions circle.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,23 @@ machine:
- docker
environment:
CLOUDSDK_CORE_DISABLE_PROMPTS: 1
image_name: codeclimate-fixme

dependencies:
pre:
- echo $gcloud_json_key_base64 | sed 's/ //g' | base64 -d > /tmp/gcloud_key.json
- curl https://sdk.cloud.google.com | bash
- gcloud auth activate-service-account $gcloud_account_email --key-file /tmp/gcloud_key.json
- gcloud docker -a
PRIVATE_REGISTRY: us.gcr.io/code_climate

test:
override:
- docker build -t=$registry_root/$image_name:b$CIRCLE_BUILD_NUM .
- docker build -t=$PRIVATE_REGISTRY/$CIRCLE_PROJECT_REPONAME:b$CIRCLE_BUILD_NUM .

deployment:
registry:
branch: master
owner: codeclimate
commands:
- docker push $registry_root/$image_name:b$CIRCLE_BUILD_NUM
- echo $GCLOUD_JSON_KEY_BASE64 | sed 's/ //g' | base64 -d > /tmp/gcloud_key.json
- curl https://sdk.cloud.google.com | bash
- gcloud auth activate-service-account --key-file /tmp/gcloud_key.json
- gcloud docker -a
- docker push $PRIVATE_REGISTRY/$CIRCLE_PROJECT_REPONAME:b$CIRCLE_BUILD_NUM

notify:
webhooks:
- url: https://cc-slack-proxy.herokuapp.com/circle
74 changes: 50 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var printIssue = function(fileName, lineNum, matchedString){
"path": fileName,
"lines": {
"begin": lineNum,
"end": lineNum
"end": lineNum
}
}
};
Expand All @@ -44,7 +44,7 @@ var findFixmes = function(file){
if (results !== ""){
// Parses grep output
var lines = results.split("\n");

lines.forEach(function(line, index, array){
// grep spits out an extra line that we can ignore
if(index < (array.length-1)){
Expand All @@ -65,39 +65,65 @@ var findFixmes = function(file){
})
}

var eligibleFile = function(fp, excludePaths){
return (excludePaths.indexOf(fp.split("/code/")[1]) < 0) &&
!fs.lstatSync(fp).isDirectory() &&
(excludeExtensions.indexOf(path.extname(fp)) < 0)
// Returns an array of unique array values not included in the other provided array
var diff = function(a1, a2) {
var result = [];

for (var i = 0; i < a1.length; i++) {
if (a2.indexOf(a1[i]) === -1) {
result.push(a1[i]);
}

}
return result;
}

// Uses glob to traverse code directory and find files to analyze,
// excluding files passed in with by CLI config
var fileWalk = function(excludePaths){
var analysisFiles = [];
var allFiles = glob.sync("/code/**/**", {});
// Returns all the file paths in the main directory that match the given pattern
var buildFiles = function(paths) {
var files = [];

allFiles.forEach(function(file, i, a){
if(eligibleFile(file, excludePaths)){
analysisFiles.push(file);
}
paths.forEach(function(path, i, a) {
var pattern = "/code/" + path + "**"
files.push.apply(files, glob.sync(pattern, {}));
});

return files;
}

// Filters the directory paths out
var filterFiles = function(files) {
return files.filter(function(file) {
return !fs.lstatSync(file).isDirectory();
});

return analysisFiles;
}

// Returns file paths based on the exclude_paths values in config file
var buildFilesWithExclusions = function(exclusions) {
var allFiles = glob.sync("/code/**/**", {});
var excludedFiles = buildFiles(exclusions);

return diff(allFiles, excludedFiles);
}

// Returns file paths based on the include_paths values in config file
var buildFilesWithInclusions = function(inclusions) {
return buildFiles(inclusions);
}

FixMe.prototype.runEngine = function(){
// Check for existence of config.json, parse exclude paths if it exists
var analysisFiles = []

if (fs.existsSync("/config.json")) {
var engineConfig = JSON.parse(fs.readFileSync("/config.json"));
var excludePaths = engineConfig.exclude_paths;
} else {
var excludePaths = [];
}

// Walk /code/ path and find files to analyze
var analysisFiles = fileWalk(excludePaths);
if (engineConfig.hasOwnProperty("include_paths")) {
analysisFiles = buildFilesWithInclusions(engineConfig.include_paths);
} else if (engineConfig.hasOwnProperty("exclude_paths")) {
analysisFiles = buildFilesWithExclusions(engineConfig.exclude_paths);
}
}

analysisFiles = filterFiles(analysisFiles);
// Execute main loop and find fixmes in valid files
analysisFiles.forEach(function(f, i, a){
findFixmes(f);
Expand Down