diff --git a/Project.toml b/Project.toml index 8223321..a11430c 100644 --- a/Project.toml +++ b/Project.toml @@ -1,7 +1,7 @@ name = "Coverage" uuid = "a2441757-f6aa-5fb2-8edb-039e3f45d037" authors = ["Iain Dunning ", "contributors"] -version = "1.4.0" +version = "1.5.0" [deps] CoverageTools = "c36e975a-824b-4404-a568-ef97ca766997" diff --git a/src/codecovio.jl b/src/codecovio.jl index c6f93cb..0cf2129 100644 --- a/src/codecovio.jl +++ b/src/codecovio.jl @@ -71,6 +71,7 @@ end add_ci_to_kwargs(; kwargs...) = add_ci_to_kwargs(Dict{Symbol,Any}(kwargs)) function add_ci_to_kwargs(kwargs::Dict) + # https://docs.codecov.com/reference/upload if lowercase(get(ENV, "APPVEYOR", "false")) == "true" appveyor_pr = get(ENV, "APPVEYOR_PULL_REQUEST_NUMBER", "") appveyor_job = join( @@ -175,6 +176,19 @@ function add_ci_to_kwargs(kwargs::Dict) if ENV["BUILDKITE_PULL_REQUEST"] != "false" kwargs = set_defaults(kwargs, pr = ENV["BUILDKITE_PULL_REQUEST"]) end + elseif lowercase(get(ENV, "GITLAB_CI", "false")) == "true" + # Gitlab API: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html + branch = ENV["CI_COMMIT_REF_NAME"] + num_mr = branch == ENV["CI_DEFAULT_BRANCH"] ? "false" : ENV["CI_MERGE_REQUEST_IID"] + kwargs = set_defaults(kwargs, + service = "gitlab", + branch = branch, + commit = ENV["CI_COMMIT_SHA"], + job = ENV["CI_JOB_ID"], + build_url = ENV["CI_PIPELINE_URL"], + build = ENV["CI_PIPELINE_IID"], + pr = num_mr, + ) else error("No compatible CI platform detected") end diff --git a/src/coveralls.jl b/src/coveralls.jl index 77eb00b..19cd14f 100644 --- a/src/coveralls.jl +++ b/src/coveralls.jl @@ -69,7 +69,7 @@ end function prepare_request(fcs::Vector{FileCoverage}, local_env::Bool, git_info=query_git_info) data = Dict{String,Any}("source_files" => map(to_json, fcs)) - + # Coveralls API : https://docs.coveralls.io/api-reference if local_env # Attempt to parse git info via git_info, unless the user explicitly disables it by setting git_info to nothing data["service_name"] = "local" @@ -105,6 +105,16 @@ function prepare_request(fcs::Vector{FileCoverage}, local_env::Bool, git_info=qu github_pr = get(github_pr_info, "number", "") github_pr::Union{AbstractString, Integer} ((github_pr isa Integer) || (!isempty(github_pr))) && (data["service_pull_request"] = strip(string(github_pr))) + elseif lowercase(get(ENV, "GITLAB_CI", "false")) == "true" + # Gitlab API: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html + branch = ENV["CI_COMMIT_REF_NAME"] + num_mr = branch == ENV["CI_DEFAULT_BRANCH"] ? "false" : ENV["CI_MERGE_REQUEST_IID"] + data["service_pull_request"] = num_mr + data["service_number"] = ENV["CI_PIPELINE_IID"] + data["service_job_id"] = ENV["CI_JOB_ID"] + data["service_name"] = "gitlab" + data["git"] = parse_git_info(git_info) + data["git"]["branch"] = branch else data["git"] = parse_git_info(git_info) end diff --git a/test/runtests.jl b/test/runtests.jl index b81a76f..530f1bf 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -482,6 +482,79 @@ withenv( end end + # test Gitlab ci submission process + + # set up Gitlab ci env + withenv( + "GITLAB_CI" => "true", + "CI_MERGE_REQUEST_IID" => "t_pr", + "CI_JOB_ID" => "t_proj", + "CI_COMMIT_REF_NAME" => "t_branch", + "CI_COMMIT_SHA" => "t_commit", + "CI_PROJECT_NAME" => "t_repo", + "CI_PIPELINE_URL" => "t_url", + "CI_PIPELINE_IID" => "t_num", + "CI_DEFAULT_BRANCH" => "master", + ) do + + # default values + codecov_url = construct_uri_string_ci() + @test occursin("codecov.io", codecov_url) + @test occursin("service=gitlab", codecov_url) + @test occursin("branch=t_branch", codecov_url) + @test occursin("commit=t_commit", codecov_url) + @test occursin("pr=t_pr", codecov_url) + @test occursin("build_url=t_url", codecov_url) + @test occursin("build=t_num", codecov_url) + + # env var url override + withenv( "CODECOV_URL" => "https://enterprise-codecov-1.com" ) do + + codecov_url = construct_uri_string_ci() + @test occursin("enterprise-codecov-1.com", codecov_url) + @test occursin("service=gitlab", codecov_url) + @test occursin("branch=t_branch", codecov_url) + @test occursin("commit=t_commit", codecov_url) + @test occursin("pr=t_pr", codecov_url) + @test occursin("build_url=t_url", codecov_url) + @test occursin("build=t_num", codecov_url) + + # function argument url override + codecov_url = construct_uri_string_ci(codecov_url="https://enterprise-codecov-2.com") + @test occursin("enterprise-codecov-2.com", codecov_url) + @test occursin("service=gitlab", codecov_url) + @test occursin("branch=t_branch", codecov_url) + @test occursin("commit=t_commit", codecov_url) + @test occursin("pr=t_pr", codecov_url) + @test occursin("build_url=t_url", codecov_url) + @test occursin("build=t_num", codecov_url) + + # env var token + withenv( "CODECOV_TOKEN" => "token_name_1" ) do + + codecov_url = construct_uri_string_ci() + @test occursin("enterprise-codecov-1.com", codecov_url) + @test occursin("token=token_name_1", codecov_url) + @test occursin("service=gitlab", codecov_url) + @test occursin("branch=t_branch", codecov_url) + @test occursin("commit=t_commit", codecov_url) + @test occursin("pr=t_pr", codecov_url) + @test occursin("build_url=t_url", codecov_url) + @test occursin("build=t_num", codecov_url) + + # function argument token url override + codecov_url = construct_uri_string_ci(token="token_name_2") + @test occursin("enterprise-codecov-1.com", codecov_url) + @test occursin("service=gitlab", codecov_url) + @test occursin("branch=t_branch", codecov_url) + @test occursin("commit=t_commit", codecov_url) + @test occursin("pr=t_pr", codecov_url) + @test occursin("build_url=t_url", codecov_url) + @test occursin("build=t_num", codecov_url) + end + end + end + # test codecov token masking withenv( "APPVEYOR" => "true", @@ -637,6 +710,21 @@ withenv( end end + # test Gitlab see https://docs.coveralls.io/api-reference + withenv("GITLAB_CI" => "true", + "CI_PIPELINE_IID" => "my_job_num", + "CI_JOB_ID" => "my_job_id", + "CI_COMMIT_REF_NAME" => "test", + "CI_DEFAULT_BRANCH" => "master", + "CI_MERGE_REQUEST_IID" => "t_pr") do + request = Coverage.Coveralls.prepare_request(fcs, false) + @test request["repo_token"] == "token_name_1" + @test request["service_number"] == "my_job_num" + @test request["service_job_id"] == "my_job_id" + @test request["service_name"] == "gitlab" + @test request["service_pull_request"] == "t_pr" + end + # test git_info (only works with Jenkins & local at the moment) withenv("JENKINS" => "true", "BUILD_ID" => "my_job_id",