Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Gitlab CI to Coverage #331

Merged
merged 12 commits into from
May 24, 2022
Merged
10 changes: 7 additions & 3 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,13 +176,16 @@ function add_ci_to_kwargs(kwargs::Dict)
if ENV["BUILDKITE_PULL_REQUEST"] != "false"
kwargs = set_defaults(kwargs, pr = ENV["BUILDKITE_PULL_REQUEST"])
end
elseif haskey(ENV, "GITLAB_CI")
elseif haskey(ENV, "GITLAB_CI")
theogf marked this conversation as resolved.
Show resolved Hide resolved
# Gitlab API: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
kwargs = set_defaults(kwargs,
service = "gitlab",
branch = ENV["CI_COMMIT_REF_NAME"],
branch = ENV["CI_COMMIT_BRANCH"],
commit = ENV["CI_COMMIT_SHA"],
job = ENV["CI_JOB_ID"],
build_url = ENV["CI_REPOSITORY_URL"]
build_url = ENV["CI_PIPELINE_URL"],
build = ENV["CI_PIPELINE_IID"],
pull_request = ENV["CI_MERGE_REQUEST_TITLE"],
)
else
error("No compatible CI platform detected")
Expand Down
5 changes: 4 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 @@ -106,9 +106,12 @@ function prepare_request(fcs::Vector{FileCoverage}, local_env::Bool, git_info=qu
github_pr::Union{AbstractString, Integer}
((github_pr isa Integer) || (!isempty(github_pr))) && (data["service_pull_request"] = strip(string(github_pr)))
elseif haskey(ENV, "GITLAB_CI")
# Gitlab API: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
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"] = ENV["CI_COMMIT_BRANCH"]
else
data["git"] = parse_git_info(git_info)
end
Expand Down
118 changes: 118 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,81 @@ withenv(
end
end

# test Gitlab ci submission process

# set up Gitlab ci env
# test circle ci submission process
theogf marked this conversation as resolved.
Show resolved Hide resolved

# set up circle ci env
withenv(
"GITLAB_CI" => "true",
"CI_MERGE_REQUEST_TITLE" => "t_pr",
"CI_JOB_ID" => "t_proj",
"CI_COMMIT_REF_NAME" => "t_branch",
"CI_COMMIT_SHA" => "t_commit",
"CI_PROJECT_NAME" => "t_repo",
"CI_REPOSITORY_URL" => "t_url",
"CI_MERGE_REQUEST_ID" => "t_num",
) 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("pull_request=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("pull_request=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("pull_request=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("pull_request=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("pull_request=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",
Expand Down Expand Up @@ -637,6 +712,49 @@ withenv(
end
end

# test Travis
theogf marked this conversation as resolved.
Show resolved Hide resolved
withenv("TRAVIS" => "true",
"TRAVIS_BUILD_NUMBER" => "my_job_num",
"TRAVIS_JOB_ID" => "my_job_id",
"TRAVIS_PULL_REQUEST" => "t_pr",
"COVERALLS_PARALLEL" => "true") 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"] == "travis-ci"
@test request["service_pull_request"] == "t_pr"
@test request["parallel"] == "true"
end

# test Travis
theogf marked this conversation as resolved.
Show resolved Hide resolved
withenv("TRAVIS" => "true",
"TRAVIS_BUILD_NUMBER" => "my_job_num",
"TRAVIS_JOB_ID" => "my_job_id",
"TRAVIS_PULL_REQUEST" => "t_pr",
"COVERALLS_PARALLEL" => "true") 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"] == "travis-ci"
@test request["service_pull_request"] == "t_pr"
@test request["parallel"] == "true"
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",) 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"
@test request["parallel"] == "true"
end

# test git_info (only works with Jenkins & local at the moment)
withenv("JENKINS" => "true",
"BUILD_ID" => "my_job_id",
Expand Down