Skip to content

Commit

Permalink
Add Gitlab CI to Coverage (#331)
Browse files Browse the repository at this point in the history
  • Loading branch information
theogf authored May 24, 2022
1 parent e22e4fe commit 5872cf7
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Coverage"
uuid = "a2441757-f6aa-5fb2-8edb-039e3f45d037"
authors = ["Iain Dunning <iaindunning@gmail.com>", "contributors"]
version = "1.4.0"
version = "1.5.0"

[deps]
CoverageTools = "c36e975a-824b-4404-a568-ef97ca766997"
Expand Down
14 changes: 14 additions & 0 deletions src/codecovio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down
12 changes: 11 additions & 1 deletion src/coveralls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
88 changes: 88 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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