Skip to content

Commit 9957f4b

Browse files
committed
Add OpenQA::VcsProvider::{GitHUb,Gitea}
1 parent 2022c29 commit 9957f4b

File tree

9 files changed

+135
-47
lines changed

9 files changed

+135
-47
lines changed

lib/OpenQA/Schema/Result/ScheduledProducts.pm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -935,7 +935,8 @@ sub _format_check_description ($verb, $count, $total) {
935935
sub report_status_to_git ($self, $callback = undef) {
936936
my $id = $self->id;
937937
my $settings = $self->{_settings} // $self->settings;
938-
my $vcs = OpenQA::VcsProvider->new(app => OpenQA::App->singleton);
938+
return undef unless $self->webhook_id;
939+
my $vcs = OpenQA::VcsProvider->new(type => $self->webhook_id, app => OpenQA::App->singleton);
939940
$vcs->read_settings($settings) or return undef;
940941
my ($state, $verb, $count, $total) = $self->state_for_ci_status;
941942
return undef unless $state;

lib/OpenQA/Setup.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ sub read_config ($app) {
249249
'assets/storage_duration' => {
250250
# intentionally left blank for overview
251251
},
252-
secrets => {github_token => ''},
252+
secrets => {github_token => '', gitea_token => ''},
253253
# allow dynamic config keys based on job results
254254
hooks => {},
255255
influxdb => {

lib/OpenQA/VcsProvider.pm

Lines changed: 8 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,15 @@
33

44
package OpenQA::VcsProvider;
55

6-
use Mojo::Base -base, -signatures;
7-
use Mojo::JSON qw(encode_json);
8-
use Mojo::URL;
6+
use Mojo::Base -signatures;
7+
use OpenQA::VcsProvider::GitHub;
8+
use OpenQA::VcsProvider::Gitea;
99

10-
has 'app';
11-
has 'base_url';
12-
has 'statuses_url';
13-
14-
sub read_settings ($self, $settings) {
15-
$self->statuses_url($settings->{GITHUB_STATUSES_URL});
16-
$self->base_url($settings->{CI_TARGET_URL});
17-
return undef unless $self->statuses_url;
18-
return 1;
19-
}
20-
21-
sub report_status_to_git ($self, $params, $scheduled_product_id, $callback = undef) {
22-
$params->{context} //= 'openqa';
23-
$params->{description} //= 'openQA test run';
24-
my $base_url = $self->base_url;
25-
$params->{target_url} //= "$base_url/admin/productlog?id=$scheduled_product_id"
26-
if $scheduled_product_id && $base_url;
27-
28-
my $url = Mojo::URL->new($self->statuses_url);
29-
my $app = $self->app;
30-
my $ua = $app->ua;
31-
my $tx = $ua->build_tx(POST => $url);
32-
my $req = $tx->req;
33-
my $headers = $req->headers;
34-
my $github_token = $app->config->{secrets}->{github_token};
35-
my $json = encode_json($params);
36-
$req->body($json);
37-
$headers->content_type('application/json');
38-
$headers->content_length(length $json);
39-
$headers->header(Accept => 'application/vnd.github+json');
40-
$headers->header(Authorization => "Bearer $github_token");
41-
$headers->header('X-GitHub-Api-Version' => '2022-11-28');
42-
$ua->start($tx, $callback);
43-
return $tx;
10+
sub new ($class, %args) {
11+
my $type = delete $args{type};
12+
my ($provider) = split m/:/, $type;
13+
$class = {gh => 'GitHub', gitea => 'Gitea'}->{$provider} or return undef;
14+
return "OpenQA::VcsProvider::$class"->new(%args);
4415
}
4516

4617
1;

lib/OpenQA/VcsProvider/Base.pm

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Copyright SUSE LLC
2+
# SPDX-License-Identifier: GPL-2.0-or-later
3+
4+
package OpenQA::VcsProvider::Base;
5+
6+
use Mojo::Base -base, -signatures;
7+
use Mojo::JSON qw(encode_json);
8+
use Mojo::URL;
9+
10+
has 'app';
11+
has 'base_url';
12+
has 'statuses_url';
13+
14+
sub add_params ($self, $params, $scheduled_product_id) {
15+
$params->{context} //= 'openqa';
16+
$params->{description} //= 'openQA test run';
17+
my $base_url = $self->base_url;
18+
$params->{target_url} //= "$base_url/admin/productlog?id=$scheduled_product_id"
19+
if $scheduled_product_id && $base_url;
20+
}
21+
22+
sub create_request ($self, $params) {
23+
my $app = $self->app;
24+
my $ua = $app->ua;
25+
# TODO Note that anyone who can create an openQA job can set settings
26+
# with a webhook id and a statuses URL. Maybe we should configure the
27+
# base url for each git provider and double check the url, because
28+
# we are making an API request with a token to an otherwise unchecked URL
29+
my $url = Mojo::URL->new($self->statuses_url);
30+
my $tx = $ua->build_tx(POST => $url);
31+
my $req = $tx->req;
32+
my $json = encode_json($params);
33+
$req->body($json);
34+
my $headers = $req->headers;
35+
$headers->content_type('application/json');
36+
$headers->content_length(length $json);
37+
38+
return $tx;
39+
}
40+
41+
sub report_status_to_git ($self, $params, $scheduled_product_id, $callback = undef) {
42+
$self->add_params($params, $scheduled_product_id);
43+
44+
my $tx = $self->create_request($params);
45+
$self->app->ua->start($tx, $callback);
46+
return $tx;
47+
}
48+
49+
1;

lib/OpenQA/VcsProvider/GitHub.pm

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright SUSE LLC
2+
# SPDX-License-Identifier: GPL-2.0-or-later
3+
4+
package OpenQA::VcsProvider::GitHub;
5+
6+
use Mojo::Base 'OpenQA::VcsProvider::Base', -signatures;
7+
8+
sub read_settings ($self, $settings) {
9+
$self->statuses_url($settings->{GITHUB_STATUSES_URL});
10+
$self->base_url($settings->{CI_TARGET_URL});
11+
return undef unless $self->statuses_url;
12+
return 1;
13+
}
14+
15+
sub create_request ($self, $params) {
16+
my $tx = $self->SUPER::create_request($params);
17+
18+
my $headers = $tx->req->headers;
19+
my $github_token = $self->app->config->{secrets}->{github_token};
20+
$headers->header(Accept => 'application/vnd.github+json');
21+
$headers->header(Authorization => "Bearer $github_token");
22+
$headers->header('X-GitHub-Api-Version' => '2022-11-28');
23+
24+
return $tx;
25+
}
26+
27+
1;

lib/OpenQA/VcsProvider/Gitea.pm

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright SUSE LLC
2+
# SPDX-License-Identifier: GPL-2.0-or-later
3+
4+
package OpenQA::VcsProvider::Gitea;
5+
6+
use Mojo::Base 'OpenQA::VcsProvider::Base', -signatures;
7+
8+
sub read_settings ($self, $settings) {
9+
$self->statuses_url($settings->{GITEA_STATUSES_URL});
10+
$self->base_url($settings->{CI_TARGET_URL});
11+
return undef unless $self->statuses_url;
12+
return 1;
13+
}
14+
15+
sub create_request ($self, $params) {
16+
my $tx = $self->SUPER::create_request($params);
17+
18+
my $headers = $tx->req->headers;
19+
# TODO there might be more than one gitea server -> add sections?
20+
my $token = $self->app->config->{secrets}->{gitea_token};
21+
$headers->header(Accept => 'application/json');
22+
$headers->header(Authorization => "Bearer $token");
23+
24+
return $tx;
25+
}
26+
27+
1;

lib/OpenQA/WebAPI/Controller/API/V1/Webhook.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ sub product ($self) {
144144

145145
# create scheduled product and enqueue minion job with parameter
146146
my $scheduled_product = $scheduled_products->create_with_event(\%params, $self->current_user, $webhook_id);
147-
my $vcs = OpenQA::VcsProvider->new(app => $app);
147+
my $vcs = OpenQA::VcsProvider->new(type => $webhook_id, app => $app);
148148
my $cb = sub ($ua, $tx, @) {
149149
if (my $err = $tx->error) {
150150
$scheduled_product->delete;

t/16-utils-vcs-provider.t

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,25 @@
44

55
use Test::Most;
66

7+
use Mojo::Base -signatures;
78
use FindBin;
89
use lib "$FindBin::Bin/lib", "$FindBin::Bin/../external/os-autoinst-common/lib";
910
require OpenQA::Test::Database;
1011
use OpenQA::Test::TimeLimit '5';
11-
use OpenQA::VcsProvider;
12+
use OpenQA::VcsProvider::GitHub;
13+
use OpenQA::VcsProvider::Gitea;
1214
use Test::Mojo;
1315
use Test::Warnings ':report_warnings';
1416

1517
my $schema = OpenQA::Test::Database->new->create(fixtures_glob => '03-users.pl');
1618
my $t = Test::Mojo->new('OpenQA::WebAPI');
1719

18-
subtest 'reporting status to GitHub' => sub {
20+
sub test_report_status_to_git ($app, $statuses_url_key, $webhook_id) {
1921
# avoid making an actual query to GitHub; this test just checks whether an expected request would have been done
20-
my $app = $t->app;
21-
$app->config->{secrets}->{github_token} = 'some-token';
2222

23-
my $git = OpenQA::VcsProvider->new(app => $app);
23+
my $git = OpenQA::VcsProvider->new(app => $app, type => $webhook_id);
2424
my $url = 'http://127.0.0.1/repos/some/repo/statuses/some-sha';
25-
$git->read_settings({GITHUB_STATUSES_URL => $url, CI_TARGET_URL => 'https://openqa.opensuse.org'});
25+
$git->read_settings({$statuses_url_key => $url, CI_TARGET_URL => 'https://openqa.opensuse.org'});
2626
my $tx = $git->report_status_to_git({state => 'pending'}, '42');
2727
my $req = $tx->req;
2828
is $req->method, 'POST', 'method';
@@ -38,4 +38,17 @@ subtest 'reporting status to GitHub' => sub {
3838
ok $tx->is_finished, 'transaction has finished (and thus was started in first place)';
3939
};
4040

41+
42+
subtest 'reporting status to GitHub' => sub {
43+
my $app = $t->app;
44+
$app->config->{secrets}->{github_token} = 'some-token';
45+
test_report_status_to_git($app, 'GITHUB_STATUSES_URL', 'gh:pr:123');
46+
};
47+
48+
subtest 'reporting status to Gitea' => sub {
49+
my $app = $t->app;
50+
$app->config->{secrets}->{gitea_token} = 'some-token';
51+
test_report_status_to_git($app, 'GITEA_STATUSES_URL', 'gitea:pr:123');
52+
};
53+
4154
done_testing();

t/api/16-webhooks.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ subtest 'failure when reporting status to GitHub' => sub {
9090
};
9191

9292
# mock reporting back to GitHub
93-
my $vcs_mock = Test::MockModule->new('OpenQA::VcsProvider');
93+
my $vcs_mock = Test::MockModule->new('OpenQA::VcsProvider::GitHub');
9494
my $minion_job_id;
9595
my $status_reports = 0;
9696
my $expected_sha = '04a3f669ea13a4aa7cbd4569f578a66f7403c43d';

0 commit comments

Comments
 (0)