2013-03-20 06:53:59 +04:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Adobe Systems Incorporated. All rights reserved.
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
* copy of this software and associated documentation files (the "Software"),
|
|
|
|
* to deal in the Software without restriction, including without limitation
|
|
|
|
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
|
|
|
* and/or sell copies of the Software, and to permit persons to whom the
|
|
|
|
* Software is furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
*/
|
2014-02-05 04:51:55 +04:00
|
|
|
/*jslint regexp:true*/
|
2013-03-20 09:44:15 +04:00
|
|
|
/*global module, require, process*/
|
2013-03-20 06:53:59 +04:00
|
|
|
|
|
|
|
module.exports = function (grunt) {
|
|
|
|
"use strict";
|
|
|
|
|
2014-02-05 04:51:55 +04:00
|
|
|
var child_process = require("child_process"),
|
|
|
|
http = require("http"),
|
|
|
|
https = require("https"),
|
|
|
|
build = {},
|
2013-11-22 07:29:48 +04:00
|
|
|
path = require("path"),
|
2013-03-20 06:53:59 +04:00
|
|
|
q = require("q"),
|
2014-02-05 04:51:55 +04:00
|
|
|
querystring = require("querystring"),
|
2013-03-20 06:53:59 +04:00
|
|
|
qexec = q.denodeify(child_process.exec);
|
2013-11-22 07:29:48 +04:00
|
|
|
|
|
|
|
function getGitInfo(cwd) {
|
|
|
|
var opts = { cwd: cwd, maxBuffer: 1024 * 1024 },
|
|
|
|
json = {};
|
|
|
|
|
2013-11-23 01:34:33 +04:00
|
|
|
// count the number of commits for our version number
|
|
|
|
// <major>.<sprint>.<patch>-<number of commits>
|
2013-11-22 07:29:48 +04:00
|
|
|
return qexec("git log --format=%h", opts).then(function (stdout) {
|
|
|
|
json.commits = stdout.toString().match(/[0-9a-f]\n/g).length;
|
2013-11-23 01:34:33 +04:00
|
|
|
|
|
|
|
// get the hash for the current commit (HEAD)
|
2013-11-22 07:29:48 +04:00
|
|
|
return qexec("git rev-parse HEAD", opts);
|
|
|
|
}).then(function (stdout) {
|
|
|
|
json.sha = /([a-f0-9]+)/.exec(stdout.toString())[1];
|
2013-11-23 01:34:33 +04:00
|
|
|
|
|
|
|
// compare HEAD to the HEADs on the remote
|
|
|
|
return qexec("git ls-remote --heads origin", opts);
|
|
|
|
}).then(function (stdout) {
|
|
|
|
var log = stdout.toString(),
|
2013-11-23 02:07:45 +04:00
|
|
|
re = new RegExp(json.sha + "\\srefs/heads/(\\S+)\\s"),
|
|
|
|
match = re.exec(log),
|
|
|
|
reflog;
|
2013-11-23 01:34:33 +04:00
|
|
|
|
|
|
|
// if HEAD matches to a remote branch HEAD, grab the branch name
|
|
|
|
if (match) {
|
|
|
|
json.branch = match[1];
|
|
|
|
return json;
|
|
|
|
}
|
|
|
|
|
2013-11-23 02:07:45 +04:00
|
|
|
// else, try match HEAD using reflog
|
|
|
|
reflog = qexec("git reflog show --no-abbrev-commit --all", opts);
|
|
|
|
|
|
|
|
return reflog.then(function (stdout) {
|
|
|
|
var log = stdout.toString(),
|
|
|
|
re = new RegExp(json.sha + "\\srefs/(remotes/origin|heads)/(\\S+)@"),
|
|
|
|
match = re.exec(log);
|
2013-11-22 07:29:48 +04:00
|
|
|
|
2013-11-23 02:07:45 +04:00
|
|
|
json.branch = (match && match[2]) || "(no branch)";
|
|
|
|
|
|
|
|
return json;
|
|
|
|
});
|
2013-11-22 07:29:48 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function toProperties(prefix, json) {
|
|
|
|
var out = "";
|
|
|
|
|
|
|
|
Object.keys(json).forEach(function (key) {
|
|
|
|
out += prefix + key + "=" + json[key] + "\n";
|
|
|
|
});
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
2013-03-20 06:53:59 +04:00
|
|
|
|
|
|
|
// task: build-num
|
|
|
|
grunt.registerTask("build-prop", "Write build.prop properties file for Jenkins", function () {
|
2013-11-22 07:29:48 +04:00
|
|
|
var done = this.async(),
|
|
|
|
json = {},
|
|
|
|
out = "",
|
|
|
|
opts = { cwd: process.cwd(), maxBuffer: 1024 * 1024 },
|
|
|
|
version = grunt.config("pkg").version,
|
|
|
|
www_repo = process.cwd(),
|
|
|
|
shell_repo = path.resolve(www_repo, grunt.config("shell.repo")),
|
|
|
|
www_git,
|
|
|
|
shell_git;
|
|
|
|
|
|
|
|
getGitInfo(www_repo).then(function (json) {
|
|
|
|
www_git = json;
|
|
|
|
return getGitInfo(shell_repo);
|
|
|
|
}).then(function (json) {
|
|
|
|
shell_git = json;
|
2013-11-27 01:47:51 +04:00
|
|
|
}, function (err) {
|
|
|
|
// shell git info is optional
|
|
|
|
grunt.log.writeln(err);
|
|
|
|
}).finally(function () {
|
2013-11-22 07:29:48 +04:00
|
|
|
out += "brackets_build_version=" + version.substr(0, version.lastIndexOf("-") + 1) + www_git.commits + "\n";
|
|
|
|
out += toProperties("brackets_www_", www_git);
|
2013-11-27 01:47:51 +04:00
|
|
|
|
|
|
|
if (shell_git) {
|
|
|
|
out += toProperties("brackets_shell_", shell_git);
|
|
|
|
}
|
2013-03-20 06:53:59 +04:00
|
|
|
|
|
|
|
grunt.log.write(out);
|
|
|
|
grunt.file.write("build.prop", out);
|
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
2013-12-03 02:31:09 +04:00
|
|
|
|
2014-02-05 04:51:55 +04:00
|
|
|
// task: cla-check-pull
|
|
|
|
grunt.registerTask("cla-check-pull", "Check if a given GitHub user has signed the CLA", function () {
|
|
|
|
var done = this.async(),
|
|
|
|
body = "",
|
|
|
|
options = {},
|
|
|
|
travis = process.env.TRAVIS === "true",
|
|
|
|
pull = travis ? process.env.TRAVIS_PULL_REQUEST : (grunt.option("pull") || false),
|
|
|
|
request;
|
|
|
|
|
|
|
|
pull = parseInt(pull, 10);
|
|
|
|
|
|
|
|
if (isNaN(pull)) {
|
|
|
|
grunt.log.writeln(JSON.stringify(process.env));
|
|
|
|
|
|
|
|
if (travis) {
|
|
|
|
// Kicked off a travis build without a pull request, skip CLA check
|
|
|
|
grunt.log.writeln("Travis build without pull request");
|
|
|
|
done();
|
|
|
|
} else {
|
|
|
|
// Grunt command-line option missing, fail CLA check
|
|
|
|
grunt.log.writeln("Missing pull request number. Use 'grunt cla-check-pull --pull=<NUMBER>'.");
|
|
|
|
done(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
options.host = "api.github.com";
|
|
|
|
options.path = "/repos/adobe/brackets/issues/" + pull;
|
|
|
|
options.method = "GET";
|
|
|
|
options.headers = {
|
|
|
|
"User-Agent" : "Node.js"
|
|
|
|
};
|
|
|
|
|
|
|
|
request = https.request(options, function (res) {
|
|
|
|
res.on("data", function (chunk) {
|
|
|
|
body += chunk;
|
|
|
|
});
|
|
|
|
|
|
|
|
res.on("end", function () {
|
|
|
|
var json = JSON.parse(body),
|
|
|
|
login = json.user && json.user.login;
|
|
|
|
|
|
|
|
if (login) {
|
|
|
|
grunt.option("user", login);
|
2014-02-05 05:11:53 +04:00
|
|
|
grunt.task.run("cla-check");
|
2014-02-05 04:51:55 +04:00
|
|
|
|
|
|
|
done();
|
|
|
|
} else {
|
|
|
|
grunt.log.writeln("Unexpected response from api.github.com");
|
|
|
|
grunt.log.writeln("statusCode: " + res.statusCode);
|
|
|
|
grunt.log.writeln("headers: " + JSON.stringify(res.headers));
|
|
|
|
grunt.log.writeln("data: " + body);
|
|
|
|
|
|
|
|
done(false);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
res.on("error", function (err) {
|
|
|
|
grunt.log.writeln(err);
|
|
|
|
done(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
// task: cla-check
|
|
|
|
grunt.registerTask("cla-check", "Check if a given GitHub user has signed the CLA", function () {
|
|
|
|
var done = this.async(),
|
|
|
|
user = grunt.option("user") || "",
|
|
|
|
body = "",
|
|
|
|
options = {},
|
|
|
|
postdata = querystring.stringify({contributor: user}),
|
|
|
|
request;
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
grunt.log.writeln("Missing user name. Use 'grunt cla-check --user=<GITHUB USER NAME>'.");
|
|
|
|
done(false);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check CLA exceptions first
|
|
|
|
var exceptions = grunt.file.readJSON("tasks/cla-exceptions.json");
|
|
|
|
|
|
|
|
if (exceptions[user]) {
|
|
|
|
grunt.log.writeln(user + " exempt from the standard contributor license agreement");
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query dev.brackets.io for CLA status
|
|
|
|
options.host = "dev.brackets.io";
|
|
|
|
options.path = "/cla/brackets/check.cfm";
|
|
|
|
options.method = "POST";
|
|
|
|
options.headers = {
|
|
|
|
"Content-Type" : "application/x-www-form-urlencoded",
|
|
|
|
"Content-Length" : postdata.length
|
|
|
|
};
|
|
|
|
|
|
|
|
request = http.request(options, function (res) {
|
|
|
|
res.on("data", function (chunk) {
|
|
|
|
body += chunk;
|
|
|
|
});
|
|
|
|
|
|
|
|
res.on("end", function () {
|
|
|
|
if (body.match(/.*REJECTED.*/)) {
|
2014-03-24 20:29:52 +04:00
|
|
|
grunt.log.error(user + " has NOT submitted the contributor license agreement. See http://dev.brackets.io/brackets-contributor-license-agreement.html.");
|
2014-02-05 04:51:55 +04:00
|
|
|
done(false);
|
|
|
|
} else {
|
|
|
|
grunt.log.writeln(user + " has submitted the contributor license agreement");
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
res.on("error", function (err) {
|
|
|
|
grunt.log.writeln(err);
|
|
|
|
done(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
request.write(postdata);
|
|
|
|
request.end();
|
|
|
|
});
|
|
|
|
|
2013-12-03 02:31:09 +04:00
|
|
|
build.getGitInfo = getGitInfo;
|
|
|
|
|
|
|
|
return build;
|
2014-03-24 20:29:52 +04:00
|
|
|
};
|