From 4a37d8a8ff2a20b352e1af46bb20fb1878aa3021 Mon Sep 17 00:00:00 2001 From: Alexey Melnichuk Date: Wed, 24 Sep 2014 14:59:53 +0500 Subject: [PATCH] Change. Allows use environment variables for information about repo. --- rockspecs/luacov-coveralls-scm-0.rockspec | 1 + src/luacov/coveralls/CiInfo.lua | 72 +++++++++++++------- src/luacov/coveralls/CiRepo.lua | 46 +++++++++++++ src/luacov/reporter/coveralls.lua | 82 ++++++++++++++--------- 4 files changed, 147 insertions(+), 54 deletions(-) create mode 100644 src/luacov/coveralls/CiRepo.lua diff --git a/rockspecs/luacov-coveralls-scm-0.rockspec b/rockspecs/luacov-coveralls-scm-0.rockspec index a9475ab..89d52f6 100644 --- a/rockspecs/luacov-coveralls-scm-0.rockspec +++ b/rockspecs/luacov-coveralls-scm-0.rockspec @@ -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 = { diff --git a/src/luacov/coveralls/CiInfo.lua b/src/luacov/coveralls/CiInfo.lua index c07bb4f..148b3a8 100644 --- a/src/luacov/coveralls/CiInfo.lua +++ b/src/luacov/coveralls/CiInfo.lua @@ -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; }; } @@ -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; } diff --git a/src/luacov/coveralls/CiRepo.lua b/src/luacov/coveralls/CiRepo.lua new file mode 100644 index 0000000..938fec3 --- /dev/null +++ b/src/luacov/coveralls/CiRepo.lua @@ -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 diff --git a/src/luacov/reporter/coveralls.lua b/src/luacov/reporter/coveralls.lua index e0649db..4f29164 100644 --- a/src/luacov/reporter/coveralls.lua +++ b/src/luacov/reporter/coveralls.lua @@ -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 @@ -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 "", "\n") + debug_print(o, " branch : ", ci.branch () or "", "\n") + debug_print(o, " job_id : ", ci.job_id () or "", "\n") + debug_print(o, " commit_id : ", ci.commit_id () or "", "\n") + debug_print(o, " author_name : ", ci.author_name () or "", "\n") + debug_print(o, " author_email : ", ci.author_email () or "", "\n") + debug_print(o, " committer_name : ", ci.committer_name () or "", "\n") + debug_print(o, " committer_email : ", ci.committer_email () or "", "\n") + debug_print(o, " message : ", ci.message () or "", "\n") + debug_print(o, " token : ", ci.token() and "" or "", "\n") + + debug_print(o, "Repository: \n") + debug_print(o, " type : ", repo:type (), "\n") + debug_print(o, " path : ", repo:path () or "", "\n") + debug_print(o, " version : ", repo:version () or "", "\n") + debug_print(o, " id : ", repo:id () or "", "\n") + debug_print(o, " author_name : ", repo:last_author_name () or "", "\n") + debug_print(o, " author_email : ", repo:last_author_email () or "", "\n") + debug_print(o, " committer_name : ", repo:last_committer_name () or "", "\n") + debug_print(o, " committer_email : ", repo:last_committer_email () or "", "\n") + debug_print(o, " message : ", repo:last_message () or "", "\n") + debug_print(o, " current_branch : ", repo:current_branch () or "", "\n") if cc.pathcorrect then local pat = {} @@ -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 @@ -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