Devel-Cover-Report-Coveralls
view release on metacpan or search on metacpan
2. `get_config` ãç°å¢å¤æ°ã¨ `.coveralls.yml` ãããéä¿¡ãã JSON ã® CI é¢é£ã®é
ç®ã¨ã¨ã³ããã¤ã³ããæ±ºãã
3. `get_git_info` ã `git log -1` / `git remote -v` / `git branch` ãå¤é¨ã³ãã³ãã§å¼ãã§ git æ
å ±ãä½ã
ï¼å¤±æãã¦ã `eval` ã§ç©ºããã·ã¥ã«ãªããéä¿¡ã¯ç¶ãï¼ãgit ã®ä½æ¥ããªã¼ã®å¤ï¼`in_git_work_tree` ãå½ï¼
ã§ã¯ git ãå¼ã°ãã`GIT_ID` / `GIT_BRANCH` / `GIT_AUTHOR_NAME` ãªã©ã®ç°å¢å¤æ°ã ãã使ãã使¥ããªã¼ã®
ä¸ã§ã¯ git ã®å¤ãå
ã§ãç°å¢å¤æ°ã¯ git ãè¿ããªãã£ãé
ç®ã ããåãããåãããªãå¤ã¯ç©ºæååã«ããã
é
ç®ãã¨éããªã
4. JSON 㯠`json_encoder` ãä½ãã`@JSON_ENCODERS`ï¼`Cpanel::JSON::XS`ã`cpanfile` ã§ã¯ `recommends`ï¼ã
å
¥ã£ã¦ããã°ããã使ããç¡ããã° JSON::PP ã使ãã大ããªãã£ã¹ããªãã¥ã¼ã·ã§ã³ã§ã¯ JSON ã®çæã `report` ã®
æéã®ã»ã¨ãã©ãå ãããããJSON::PP ã¨åã JSON ã«ãªããã¨ï¼`ascii`ï¼ã `t/01_basic.t` ã®ãjson_encoderãã§
確ããã¦ãããåè£ãè¶³ãã¨ã㯠`cpanfile` ã«ã宣è¨ããCI ã§å
¥ãããã«ããï¼å
¥ã£ã¦ããªãåè£ã¯æ¯ã¹ãããªãï¼ã
`HTTP::Tiny` ã§ `<endpoint>/api/v1/jobs` ã¸ãJSON ã multipart ã® `json_file` ã«å
¥ã㦠POST ãã
çµæãæ¨æºåºåã«åºãï¼`json_file_request`ï¼ããã㯠API ã®ããã¥ã¡ã³ãã«æ¸ãããå½¢ã1.0.0 ã¾ã§ä½¿ã£ã¦
ãã form ã® `json=` ãéãã¦ã¯ããããããã¥ã¡ã³ãã«ã¯ç¡ããåãã¡ã¤ã«ã«ã¯ããã¡ã¤ã«ã®ãã¤ãåã®
MD5 ã `source_digest` ã¨ãã¦ä»ããï¼`blib/` ã®ä¸ã®ãã¡ã¤ã«ã¯ã³ãã¼å
ã®ãã®ï¼ã`source` ãæ®ã
ï¼æåã®ãªãã¸ããªã® Coveralls Enterprise ãè¦æ±ããããï¼ã
**éä¿¡ã«å¤±æããã `die` ãã**ï¼`parse_response` ãå¤å®ããï¼ã`cover` ã¯æã¾ããªãã®ã§ãçµäºã³ã¼ãã
0 以å¤ã«ãªã CI ã®ã¸ã§ããè½ã¡ãã`COVERALLS_NO_FAIL` ãæå¹ãªã `die` ããã«æ»ãã
`get_config` ã® `repo_token` ãç¡ãã¨ãã® `die` ãã`report` ãæã¾ãã¦åãããã«æ±ã
### `get_config` ã® CI å¤å®
lib/Devel/Cover/Report/Coveralls.pm view on Meta::CPAN
for my $class (@JSON_ENCODERS) {
(my $file = "$class.pm") =~ s!::!/!g;
return $class->new->ascii if eval { require $file; 1 };
# Installed but not loadable, e.g. built for another version of Perl.
warn "$class is not used: $@" if $@ !~ /^Can't locate \Q$file\E in \@INC/;
}
return JSON::PP->new->ascii;
}
# The body of a request to the jobs API: the job as a JSON file in the multipart field json_file,
# the form the API documents (https://docs.coveralls.io/api-jobs-endpoint). Returns the content
# type and the body. The boundary is not quoted in the content type, as the official
# coverage-reporter does for the nginx of coveralls.io, and is made so that it does not occur in
# the JSON (which holds the source files, this module's own among them).
sub json_file_request {
my $json = shift;
my $boundary;
do {
$boundary = 'CoverallsPerl' . join '', map { sprintf '%04x', int rand 0x10000 } 1 .. 8;
} while index($json, $boundary) >= 0;
lib/Devel/Cover/Report/Coveralls.pm view on Meta::CPAN
if (-f $CONFIG_FILE) {
$config = YAML::LoadFile($CONFIG_FILE);
}
my $json = {};
$json->{repo_token} = $config->{repo_token} if $config->{repo_token};
$json->{repo_token} = $ENV{COVERALLS_REPO_TOKEN} if $ENV{COVERALLS_REPO_TOKEN};
$json->{flag_name} = $ENV{COVERALLS_FLAG_NAME} if $ENV{COVERALLS_FLAG_NAME};
$json->{parallel} = \1 if is_enabled($ENV{COVERALLS_PARALLEL});
my $endpoint = ($ENV{COVERALLS_ENDPOINT} || $API_ENDPOINT) . $API_ENDPOINT_STEM;
if ($ENV{TRAVIS}) {
$json->{service_name} = $config->{service_name} || 'travis-ci';
$json->{service_job_id} = $ENV{TRAVIS_JOB_ID};
if($ENV{TRAVIS_PULL_REQUEST} && $ENV{TRAVIS_PULL_REQUEST} ne 'false'){
$json->{service_pull_request} = $ENV{TRAVIS_PULL_REQUEST};
}
} elsif ($ENV{CIRCLECI}) {
$json->{service_name} = 'circleci';
$json->{service_number} = $ENV{CIRCLE_WORKFLOW_ID};
# The URL of the pull request; its number is at the end. CI_PULL_REQUEST is the older name.
lib/Devel/Cover/Report/Coveralls.pm view on Meta::CPAN
);
for my $override (@overrides) {
my ($key, $name) = @$override;
my $value = env_value($name);
$json->{$key} = $value if defined $value;
}
# "false" is what Travis CI gives on a push, which a CI template may pass on as it is.
delete $json->{service_pull_request}
if defined $ENV{COVERALLS_PULL_REQUEST} && $ENV{COVERALLS_PULL_REQUEST} eq 'false';
return ($json, $endpoint);
}
sub _parse_line ($) {
my $c = shift;
return sub {
my $l = $c->location(shift);
return $l unless $l;
lib/Devel/Cover/Report/Coveralls.pm view on Meta::CPAN
for my $file (@{$options->{file}}) {
my $f = $cover->file($file);
my $c = $f->statement();
my $sf = get_source( $file, _parse_line $c ) or next;
my $branches = get_branches($f->branch, scalar @{ $sf->{coverage} });
$sf->{branches} = $branches if @$branches;
push @sfs, $sf;
}
my ($json, $endpoint) = eval { get_config() };
if (!$json) {
# No repo token, which happens on pull requests from forks: they get no secrets.
die $@ unless is_enabled($ENV{COVERALLS_NO_FAIL});
print "- Report::Coveralls --------\nerror: $@----------------------------\n";
return;
}
$json->{git} = apply_ci_git_info($json, eval { get_git_info() } || {});
$json->{source_files} = \@sfs;
my $coder = json_encoder();
my ($content_type, $body) = json_file_request($coder->encode($json));
my $response = HTTP::Tiny->new( verify_SSL => 1 )->request('POST', $endpoint, {
headers => { 'Content-Type' => $content_type },
content => $body,
});
my ($url, $error) = parse_response($response);
print("- Report::Coveralls --------\n");
if(exists($json->{service_number})){
print "My build number: ". $json->{service_number} ."\n";
}
t/01_basic.t view on Meta::CPAN
use Test::More;
use FindBin;
use Cwd ();
use Digest::MD5 ();
use JSON::PP qw(decode_json);
use File::Basename ();
use File::Temp ();
use Devel::Cover::Report::Coveralls;
my $normal_endpoint = 'https://coveralls.io/api/v1/jobs';
my $endpoint_stem = '/api/v1/jobs';
# The variables that get_config and get_git_info read to pick the CI service, or the values for it.
# Every get_config subtest clears them all with `local @ENV{@CI_VARS}`, so that the CI running the
# tests does not leak in.
my @CI_VARS = qw(
TRAVIS TRAVIS_JOB_ID TRAVIS_PULL_REQUEST
CIRCLECI CIRCLE_WORKFLOW_ID CIRCLE_PULL_REQUEST CI_PULL_REQUEST CIRCLE_BRANCH CIRCLE_SHA1
DRONE DRONE_BUILD_NUMBER DRONE_PULL_REQUEST
SEMAPHORE SEMAPHORE_BUILD_NUMBER SEMAPHORE_WORKFLOW_ID SEMAPHORE_GIT_PR_NUMBER
JENKINS_URL BUILD_NUMBER BUILD_ID BRANCH_NAME CHANGE_BRANCH CHANGE_ID
t/01_basic.t view on Meta::CPAN
GIT_ID GIT_BRANCH GIT_AUTHOR_NAME GIT_AUTHOR_EMAIL GIT_COMMITTER_NAME GIT_COMMITTER_EMAIL
GIT_MESSAGE
);
subtest 'get_config (travis)' => sub {
local @ENV{@CI_VARS};
local $ENV{COVERALLS_REPO_TOKEN} = 'abcdef';
local $ENV{TRAVIS} = 'true';
local $ENV{TRAVIS_JOB_ID} = 100000;
local $ENV{TRAVIS_PULL_REQUEST} = 'false';
my ($got, $endpoint) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_job_id}, 100000, 'config service_job_id';
is $got->{service_name}, 'travis-ci', 'config service_name';
ok !exists $got->{service_pull_request},'not a pull request';
local $ENV{TRAVIS_PULL_REQUEST} = '456';
($got, $endpoint) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_pull_request},'456','config pull request';
is $endpoint, $normal_endpoint, 'config endpoint';
};
subtest 'get_config extra env (travis)' => sub {
local @ENV{@CI_VARS};
local $ENV{COVERALLS_REPO_TOKEN} = 'abcdef';
local $ENV{TRAVIS} = 'true';
local $ENV{TRAVIS_JOB_ID} = 100000;
my $diff_endpoint = 'http://localhost';
local $ENV{COVERALLS_ENDPOINT} = $diff_endpoint;
local $ENV{COVERALLS_FLAG_NAME} = 'Unit';
my ($got, $endpoint) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_job_id}, 100000, 'config service_job_id';
is $got->{service_name}, 'travis-ci', 'config service_name';
is $got->{flag_name}, 'Unit', 'config flag_name';
is $endpoint, $diff_endpoint . $endpoint_stem, 'new endpoint';
};
subtest 'get_config github actions (push)' => sub {
local @ENV{@CI_VARS};
local $ENV{GITHUB_ACTIONS} = 'true';
local $ENV{GITHUB_TOKEN} = 'from-github';
local $ENV{GITHUB_RUN_ID} = '123456789';
local $ENV{GITHUB_RUN_ATTEMPT} = '2';
local $ENV{GITHUB_JOB} = 'test';
local $ENV{GITHUB_SHA} = 'abc123';
local $ENV{GITHUB_REF} = 'refs/heads/master';
local $ENV{GITHUB_REF_NAME} = 'master';
my ($got, $endpoint) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_name}, 'github', 'service_name';
is $got->{repo_token}, 'from-github', 'GITHUB_TOKEN without COVERALLS_REPO_TOKEN';
is $got->{service_number}, '123456789', 'service_number is the run';
is $got->{service_attempt}, '2', 'service_attempt';
is $got->{service_job_id}, 'test', 'service_job_id is the job';
is $got->{service_branch}, 'master', 'service_branch';
is $got->{commit_sha}, 'abc123', 'commit_sha';
ok !exists $got->{service_pull_request}, 'not a pull request';
is $endpoint, $normal_endpoint;
local $ENV{COVERALLS_REPO_TOKEN} = 'from-coveralls';
local $ENV{COVERALLS_FLAG_NAME} = 'perl-5.20';
($got) = Devel::Cover::Report::Coveralls::get_config();
is $got->{repo_token}, 'from-coveralls', 'COVERALLS_REPO_TOKEN first';
is $got->{service_job_id}, 'test-perl-5.20', 'the flag name tells the legs of a matrix apart';
};
subtest 'get_config github actions (pull request)' => sub {
local @ENV{@CI_VARS};
t/01_basic.t view on Meta::CPAN
is $got->{repo_token}, 'abcdef', 'the token is not GITHUB_TOKEN';
};
subtest 'get_config azure' => sub {
local @ENV{@CI_VARS};
local $ENV{SYSTEM_TEAMFOUNDATIONSERVERURI} = 1;
local $ENV{COVERALLS_REPO_TOKEN} = 'abcdef';
local $ENV{BUILD_SOURCEBRANCHNAME} = 'feature';
local $ENV{BUILD_BUILDID} = '123456789';
my ($got, $endpoint) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_name}, 'azure-pipelines', 'config service_name';
is $got->{service_number}, '123456789', 'config service_number';
is $got->{service_branch}, 'feature', 'config service_branch';
is $endpoint, $normal_endpoint;
my $git = Devel::Cover::Report::Coveralls::apply_ci_git_info($got,
Devel::Cover::Report::Coveralls::get_git_info());
is $git->{branch}, 'feature', 'the branch of Azure Pipelines over the one of git';
local $ENV{COVERALLS_GIT_BRANCH} = 'from-env';
($got) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_branch}, 'from-env', 'COVERALLS_GIT_BRANCH over BUILD_SOURCEBRANCHNAME';
};
subtest 'get_config drone' => sub {
local @ENV{@CI_VARS};
local $ENV{DRONE_PULL_REQUEST} = '666';
local $ENV{DRONE_BUILD_NUMBER} = '123';
local $ENV{DRONE} = "drone";
local $ENV{COVERALLS_REPO_TOKEN} = 'abcdef';
my ($got, $endpoint) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_name}, 'drone', 'config service_name';
is $got->{service_number}, '123', 'config service_number';
is $got->{service_pull_request}, '666', 'config service_pull_request';
is $endpoint, $normal_endpoint;
};
subtest 'get_config local' => sub {
local @ENV{@CI_VARS};
local $ENV{COVERALLS_REPO_TOKEN} = 'abcdef';
my ($got, $endpoint) = Devel::Cover::Report::Coveralls::get_config();
is $got->{service_name}, 'coveralls-perl', 'config service_name';
is $got->{service_event_type}, 'manual', 'config service_event_type';
is $endpoint, $normal_endpoint;
for my $name (qw(
COVERALLS_SERVICE_NAME COVERALLS_PERL_SERVICE_NAME COVERALLS_SERVICE_NUMBER
COVERALLS_SERVICE_JOB_ID COVERALLS_SERVICE_JOB_NUMBER
)) {
local $ENV{$name} = 'buildkite';
($got) = Devel::Cover::Report::Coveralls::get_config();
ok !exists $got->{service_event_type}, "not manual with $name";
}
{
t/01_basic.t view on Meta::CPAN
local $ENV{COVERALLS_NO_FAIL} = 'false';
$response = { success => 1, status => 200, reason => 'OK',
content => '{"url":"https://coveralls.io/jobs/1"}' };
($ok, $err, $out) = $run->();
ok $ok, 'returns on success';
like $out, qr{^register: https://coveralls.io/jobs/1$}m, 'prints the job';
my ($method, $url, $args) = @request;
is $method, 'POST', 'POST';
is $url, $normal_endpoint, 'to the jobs API';
my ($boundary) = $args->{headers}{'Content-Type'} =~ m{^multipart/form-data; boundary=(\S+)$};
ok defined $boundary, 'as multipart/form-data, with an unquoted boundary';
my ($json) = $args->{content}
=~ m{\A--\Q$boundary\E\r\nContent-Disposition: form-data; name="json_file"; filename="json_file"\r\nContent-Type: application/json\r\n\r\n(.*)\r\n--\Q$boundary\E--\r\n\z}s;
ok defined $json, 'the job is the one field json_file';
is decode_json($json)->{repo_token}, 'abcdef', 'which holds the job';
# With a source file: a statement on line 6 that ran twice.
local *Local::FakeCover::file = sub { bless {}, 'Local::FakeFile' };
local *Local::FakeFile::statement = sub { bless {}, 'Local::FakeStatements' };
( run in 1.331 second using v1.01-cache-2.11-cpan-eb134014566 )