Skip to content

Commit

Permalink
Change. Allows use environment variables for information about repo.
Browse files Browse the repository at this point in the history
  • Loading branch information
moteus committed Sep 24, 2014
1 parent a7d766e commit 4a37d8a
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 54 deletions.
1 change: 1 addition & 0 deletions rockspecs/luacov-coveralls-scm-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ build = {
['luacov.reporter.coveralls'] = "src/luacov/reporter/coveralls.lua",
['luacov.coveralls.GitRepo' ] = "src/luacov/coveralls/GitRepo.lua",
['luacov.coveralls.CiInfo' ] = "src/luacov/coveralls/CiInfo.lua",
['luacov.coveralls.CiRepo' ] = "src/luacov/coveralls/GitRepo.lua",
['luacov.coveralls.utils' ] = "src/luacov/coveralls/utils.lua",
},
install = {
Expand Down
72 changes: 48 additions & 24 deletions src/luacov/coveralls/CiInfo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,39 @@ local ENV = setmetatable({}, {__index = env})

local CI_CONFIG = {
["travis-ci"] = {
branch = "TRAVIS_BRANCH";
job_id = "TRAVIS_JOB_ID";
token = "COVERALLS_REPO_TOKEN";
branch = "TRAVIS_BRANCH";
job_id = "TRAVIS_JOB_ID";
token = "COVERALLS_REPO_TOKEN";
commit_id = "TRAVIS_COMMIT";
author_name = NULL;
author_email = NULL;
committer_name = NULL;
committer_email = NULL;
message = NULL;
};

codeship = {
branch = "CI_BRANCH";
job_id = "CI_BUILD_NUMBER";
token = "COVERALLS_REPO_TOKEN";
branch = "CI_BRANCH";
job_id = "CI_BUILD_NUMBER";
token = "COVERALLS_REPO_TOKEN";
commit_id = "CI_COMMIT_ID";
author_name = NULL;
author_email = NULL;
committer_name = "CI_COMMITTER_NAME";
committer_email = "CI_COMMITTER_EMAIL";
message = "CI_MESSAGE";
};

circleci = {
branch = "CIRCLE_BRANCH";
job_id = "CIRCLE_BUILD_NUM";
token = "COVERALLS_REPO_TOKEN";
branch = "CIRCLE_BRANCH";
job_id = "CIRCLE_BUILD_NUM";
token = "COVERALLS_REPO_TOKEN";
commit_id = NULL;
author_name = NULL;
author_email = NULL;
committer_name = NULL;
committer_email = NULL;
message = NULL;
};
}

Expand All @@ -45,21 +65,25 @@ local function cfg()
return CI_CONFIG[name] or NULL
end

local function ci_branch()
return ENV[cfg().branch]
end

local function ci_job_id()
return ENV[cfg().job_id]
end

local function ci_token()
return ENV[cfg().token]
end
local function ci_branch () return ENV[cfg().branch ] end
local function ci_job_id () return ENV[cfg().job_id ] end
local function ci_token () return ENV[cfg().token ] end
local function ci_commit_id () return ENV[cfg().commit_id ] end
local function ci_author_name () return ENV[cfg().author_name ] end
local function ci_author_email () return ENV[cfg().author_email ] end
local function ci_committer_name () return ENV[cfg().committer_name ] end
local function ci_committer_email() return ENV[cfg().committer_email] end
local function ci_message () return ENV[cfg().message ] end

return {
name = ci_name;
branch = ci_branch;
job_id = ci_job_id;
token = ci_token;
name = ci_name;
branch = ci_branch;
job_id = ci_job_id;
token = ci_token;
commit_id = ci_commit_id;
author_name = ci_author_name;
author_email = ci_author_email;
committer_name = ci_committer_name;
committer_email = ci_committer_email;
message = ci_message;
}
46 changes: 46 additions & 0 deletions src/luacov/coveralls/CiRepo.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
local GitRepo = require"luacov.coveralls.GitRepo"
local ci = require"luacov.coveralls.CiInfo"

local function try_any_repo(repo_path)
-- currenly support only git
local repo, err = GitRepo:new(repo_path)
if repo then return repo, "git" end

-- @todo warning message about failure

local function dummy() end
return setmetatable({}, {__index = function() return dummy end}), "unknown"
end

-----------------------------------------------------------
local CiRepoInfo = {} do
CiRepoInfo.__index = CiRepoInfo

function CiRepoInfo:new(repo_path)
local repo, type = try_any_repo(repo_path)
local o = setmetatable({
_repo = assert(repo);
_repo_type = assert(type);
}, self)

return o
end

CiRepoInfo.type = function(self) return self._repo_type end

CiRepoInfo.path = function(self) return self._repo:path() end
CiRepoInfo.version = function(self) return self._repo:version() end

CiRepoInfo.id = function(self) return self._repo:id() or ci.commit_id() end
CiRepoInfo.last_author_name = function(self) return self._repo:last_author_name() or ci.author_name() end
CiRepoInfo.last_author_email = function(self) return self._repo:last_author_email() or ci.author_email() end
CiRepoInfo.last_committer_name = function(self) return self._repo:last_committer_name() or ci.committer_name() end
CiRepoInfo.last_committer_email = function(self) return self._repo:last_committer_email() or ci.committer_email() end
CiRepoInfo.last_message = function(self) return self._repo:last_message() or ci.message() end
CiRepoInfo.current_branch = function(self) return self._repo:current_branch() or ci.branch() end
CiRepoInfo.remotes = function(self) return self._repo:remotes() end

end
-----------------------------------------------------------

return CiRepoInfo
82 changes: 52 additions & 30 deletions src/luacov/reporter/coveralls.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ local coveralls = {}
local luacov_reporter = require"luacov.reporter"
local utils = require"luacov.coveralls.utils"
local ci = require"luacov.coveralls.CiInfo"
local GitRepo = require"luacov.coveralls.GitRepo"
local CiRepo = require"luacov.coveralls.CiRepo"

local json = utils.json
local unix_path = require"path".new("/")

local ReporterBase = luacov_reporter.ReporterBase

Expand Down Expand Up @@ -56,9 +58,32 @@ function CoverallsReporter:new(conf)
local cc = conf.coveralls or {}
self._debug = not not cc.debug

local ci_name = ci.name()
if ci_name then debug_print(o, "CI detected: ", ci_name, "\n")
else debug_print(o, "CI not detected\n") end
local repo, err = CiRepo:new(cc.root or '.')
assert(repo, "LuaCov-covealls internal error :" .. tostring(err))

debug_print(o, "CI: \n")
debug_print(o, " name : ", ci.name () or "<UNKNOWN>", "\n")
debug_print(o, " branch : ", ci.branch () or "<UNKNOWN>", "\n")
debug_print(o, " job_id : ", ci.job_id () or "<UNKNOWN>", "\n")
debug_print(o, " commit_id : ", ci.commit_id () or "<UNKNOWN>", "\n")
debug_print(o, " author_name : ", ci.author_name () or "<UNKNOWN>", "\n")
debug_print(o, " author_email : ", ci.author_email () or "<UNKNOWN>", "\n")
debug_print(o, " committer_name : ", ci.committer_name () or "<UNKNOWN>", "\n")
debug_print(o, " committer_email : ", ci.committer_email () or "<UNKNOWN>", "\n")
debug_print(o, " message : ", ci.message () or "<UNKNOWN>", "\n")
debug_print(o, " token : ", ci.token() and "<DETECTED>" or "<NOT DETECTED>", "\n")

debug_print(o, "Repository: \n")
debug_print(o, " type : ", repo:type (), "\n")
debug_print(o, " path : ", repo:path () or "<UNKNOWN>", "\n")
debug_print(o, " version : ", repo:version () or "<UNKNOWN>", "\n")
debug_print(o, " id : ", repo:id () or "<UNKNOWN>", "\n")
debug_print(o, " author_name : ", repo:last_author_name () or "<UNKNOWN>", "\n")
debug_print(o, " author_email : ", repo:last_author_email () or "<UNKNOWN>", "\n")
debug_print(o, " committer_name : ", repo:last_committer_name () or "<UNKNOWN>", "\n")
debug_print(o, " committer_email : ", repo:last_committer_email () or "<UNKNOWN>", "\n")
debug_print(o, " message : ", repo:last_message () or "<UNKNOWN>", "\n")
debug_print(o, " current_branch : ", repo:current_branch () or "<UNKNOWN>", "\n")

if cc.pathcorrect then
local pat = {}
Expand Down Expand Up @@ -92,36 +117,28 @@ function CoverallsReporter:new(conf)

o._json = base_file or {}

o._json.service_name = o._json.service_name or ci_name
o._json.service_name = o._json.service_name or ci.name()
o._json.repo_token = o._json.repo_token or cc.repo_token or ci.token()
o._json.service_job_id = o._json.service_job_id or ci.job_id()
o._json.source_files = o._json.source_files or json.init_array{}

if not GitRepo then
debug_print(o, "Warning! can not load GitRepo: " .. (GitRepo or ''))
else
local repo, err = GitRepo:new(cc.root or '.')
if not repo then debug_print(o, "Warning! can not get git info: " .. (err or ''))
else
debug_print(o, "git path:", repo:path(), "\n")

o._json.git = o._json.git or {}
o._json.git.head = o._json.git.head or {}

o._json.git.head.id = o._json.git.head.id or repo:id()
o._json.git.head.author_name = o._json.git.head.author_name or repo:last_author_name()
o._json.git.head.author_email = o._json.git.head.author_email or repo:last_author_email()
o._json.git.head.committer_name = o._json.git.head.committer_name or repo:last_committer_name()
o._json.git.head.committer_email = o._json.git.head.committer_email or repo:last_committer_email()
o._json.git.head.message = o._json.git.head.message or repo:last_message()
o._json.git.branch = o._json.git.branch or ci.branch() or repo:current_branch()
if not o._json.git.remotes then
o._json.git.remotes = json.init_array{}
local t = repo:remotes()
if t then for name, url in pairs(t) do
table.insert(o._json.git.remotes,{name=name,url=url})
end end
end
if repo:type() == 'git' then
o._json.git = o._json.git or {}
o._json.git.head = o._json.git.head or {}

o._json.git.head.id = o._json.git.head.id or repo:id()
o._json.git.head.author_name = o._json.git.head.author_name or repo:last_author_name()
o._json.git.head.author_email = o._json.git.head.author_email or repo:last_author_email()
o._json.git.head.committer_name = o._json.git.head.committer_name or repo:last_committer_name()
o._json.git.head.committer_email = o._json.git.head.committer_email or repo:last_committer_email()
o._json.git.head.message = o._json.git.head.message or repo:last_message()
o._json.git.branch = o._json.git.branch or ci.branch() or repo:current_branch()
if not o._json.git.remotes then
o._json.git.remotes = json.init_array{}
local t = repo:remotes()
if t then for name, url in pairs(t) do
table.insert(o._json.git.remotes,{name=name,url=url})
end end
end
end

Expand All @@ -137,6 +154,11 @@ function CoverallsReporter:correct_path(path)
end
end

-- @todo check if we have path not relevant to repo
-- if is abs path then this is error path.

path = unix_path:normolize(path)

debug_print(self, path, "\n")
return path
end
Expand Down

0 comments on commit 4a37d8a

Please sign in to comment.