package bugzilla; use strict; use base 'Exporter'; use Exporter; use lockapi; use testapi; use utils; use POSIX qw(strftime); use JSON; use REST::Client; our @EXPORT = qw(convert_to_bz_timestamp get_newest_bug check_bug_status_field close_notabug bugzilla_setup); sub bugzilla_setup { # This routines makes necessary settings to enable Bugzilla (staging/production) # reporting from Anaconda easily, without the need to type the Bugzilla API. # Switch to shell send_key("alt-f3"); assert_screen("anaconda_text_install_shell"); # Navigate to the libreport configuration directory type_string("cd /etc/libreport/events/\n"); # Add some sleeps to make the output video more viewable sleep(3); # Change values in the report_Bugzilla.conf file my $apikey = get_var("SECRET_BUGZILLA_APIKEY") // undef; # If we want to report to Bugzilla Staging, then we will # change the configuration file. Normally, it is set # to report to the production Bugzilla. my $bugzilla = get_var("BUGZILLA_INSTANCE") // "staging"; if ($bugzilla eq "staging") { enter_cmd("sed -i 's\/bugzilla.redhat.com\/bugzilla.stage.redhat.com\/g' report_Bugzilla.conf"); sleep(3); } # If API key is defined, update the configuration file with the API key if ($apikey) { enter_cmd("sed -i 's\/Bugzilla_APIKey =\/Bugzilla_APIKey = $apikey\/g' report_Bugzilla.conf"); sleep(3); } #Switch back to the installation console send_key("alt-f1"); assert_screen("anaconda_text_install_shell"); } sub start_bugzilla_client { # Start a Bugzilla REST client for setting up communication. # This is a local subroutine, not intended for export. my $host = shift; my $bugzilla = REST::Client->new(); $bugzilla->setHost($host); return $bugzilla; } sub convert_to_bz_timestamp { # This subroutine takes the epoch time and converts it to # the Bugzilla timestamp format (YYYY-MM-DDTHH:MM:SS) # in the GMT time zone. my $epochtime = shift; my $bz_stamp = strftime("%FT%T", gmtime($epochtime)); return $bz_stamp; } sub get_newest_bug { # This subroutine makes an API call to Bugzilla and # fetches the newest bug that have been created. # This will be the bug created by Anaconda in this # test run. my ($timestamp, $login) = @_; $timestamp = convert_to_bz_timestamp($timestamp); my $bugzilla = start_bugzilla_client(); my $api_call = $bugzilla->GET("/rest/bug?creator=$login&status=NEW&created_after=$timestamp"); my $rest_json = decode_json($api_call->responseContent()); my $last_id; eval { $last_id = $rest_json->{bugs}[-1]->{id}; 1; } or do { record_soft_failure "Bugzilla returned an empty list of bugs which is unexpected!"; $last_id = 0; }; return $last_id; } sub check_bug_status_field { # This will check that the status field matches the one # tested status. Arguments are bug_id and status. my ($bug_id, $status) = @_; my $bugzilla = start_bugzilla_client(); my $api_call = $bugzilla->GET("/rest/bug/$bug_id"); my $rest_json = decode_json($api_call->responseContent()); if ($rest_json->{bugs}[0]->{status} eq $status) { return 1; } else { return 0; } } sub close_notabug { # This will call Bugzilla and close the bug with the requested # bug id as a NOTABUG. my ($bug_id, $key) = @_; my $bugzilla = start_bugzilla_client(); my $api_call = $bugzilla->PUT("/rest/bug/$bug_id?api_key=$key&status=CLOSED&resolution=NOTABUG"); my $rest_json = decode_json($api_call->responseContent()); if ($rest_json->{bugs}[0]->{changes}->{status}->{added} ne "CLOSED") { return 0; } else { return 1; } }