[Git][debian-gis-team/scripts][master] 2 commits: Add support for KGB webhook.

Bas Couwenberg gitlab at salsa.debian.org
Mon Feb 19 21:13:19 UTC 2018


Bas Couwenberg pushed to branch master at Debian GIS Project / scripts


Commits:
a5a1a6cd by Bas Couwenberg at 2018-02-19T21:45:06+01:00
Add support for KGB webhook.

- - - - -
b3c8e6af by Bas Couwenberg at 2018-02-19T22:01:58+01:00
Disable Irker in favor of KGB.

- - - - -


2 changed files:

- salsa-configure-repositories.pl
- salsa-create-repository.pl


Changes:

=====================================
salsa-configure-repositories.pl
=====================================
--- a/salsa-configure-repositories.pl
+++ b/salsa-configure-repositories.pl
@@ -38,6 +38,9 @@ my %cfg = (
             ci_cfg_path      => 'debian/.gitlab-ci.yml',
             email_recipients => 'pkg-grass-devel at lists.alioth.debian.org',
             irc_recipients   => '#debian-gis',
+            kgb_baseurl      => 'http://kgb.debian.net:9418/webhook/',
+            kgb_network      => 'otfc',
+            project          => '',
             debug            => 0,
             verbose          => 0,
             help             => 0,
@@ -52,6 +55,9 @@ my $result = GetOptions(
                          'c|ci-cfg-path=s'      => \$cfg{ci_cfg_path},
                          'e|email-recipients=s' => \$cfg{email_recipients},
                          'i|irc-recipients=s'   => \$cfg{irc_recipients},
+                         'k|kgb-baseurl=s'      => \$cfg{kgb_baseurl},
+                         'N|kgb-network=s'      => \$cfg{kgb_network},
+                         'p|project=s'          => \$cfg{project},
                          'd|debug'              => \$cfg{debug},
                          'v|verbose'            => \$cfg{verbose},
                          'h|help'               => \$cfg{help},
@@ -73,6 +79,10 @@ if(!$result || $cfg{help}) {
 	print "-c, --ci-cfg-path <PATH>        CI Config Path   ($cfg{ci_cfg_path})\n";
 	print "-e, --email-recipients <EMAIL>  Email recipients ($cfg{email_recipients})\n";
 	print "-i, --irc-recipients <CHANNEL>  IRC recipients   ($cfg{irc_recipients})\n";
+	print "-k, --kgb-baseurl <URL>         KGB base URL     ($cfg{kgb_baseurl})\n";
+	print "-N, --kgb-network <NAME>        KGB network name ($cfg{kgb_network})\n";
+	print "\n";
+	print "-p, --project <NAME>            Project to configure, instead of all\n";
 	print "\n";
 	print "-d, --debug                     Enable debug output\n";
 	print "-v, --verbose                   Enable verbose output\n";
@@ -106,6 +116,8 @@ print "team projects:\n".Dumper($team_projects) if($cfg{debug});
 
 # Get service settings
 foreach my $project (@{$team_projects}) {
+	next if($cfg{project} && lc($project->{name}) ne lc($cfg{project}) && lc($project->{path}) ne lc($cfg{project}));
+
 	print "\nProject: ". $project->{name} ." (". $project->{id} .")\n" if($cfg{verbose});
 
 	# Disable issues
@@ -132,6 +144,11 @@ foreach my $project (@{$team_projects}) {
 	if(!configure_irker_service($project)) {
 		exit 1;
 	}
+
+	# Configure KGB webhook
+	if(!configure_kgb_webhook($project)) {
+		exit 1;
+	}
 }
 
 exit 0;
@@ -482,14 +499,135 @@ sub configure_irker_service {
 
 		print "Irker:\n". Dumper($data) if($cfg{debug});
 
-		if(!$data->{active}) {
-			print "Activating Irker for: ". $project->{name} ." (". $project->{id} .")\n" if($cfg{verbose});
+		if($data->{active}) {
+			print "Deactivating Irker for: ". $project->{name} ." (". $project->{id} .")\n" if($cfg{verbose});
 
 			my $url = $cfg{url}.'/projects/'.uri_escape($project->{id}).'/services/irker';
 
+			my $req = HTTP::Request::Common::DELETE($url);
+			   $req->header('PRIVATE-TOKEN' => $cfg{token});
+
+			my $res = $ua->request($req);
+			if($res->is_success) {
+				my $content = $res->content;
+
+				print "Content:\n".Dumper($content) if($cfg{debug});
+
+				print "Deactivated Irker service for project: ". $project->{name} ." (". $project->{id} .")\n" if($cfg{verbose});
+
+				return 1;
+			}
+			else {
+				print "Error: Request failed! ($url)\n";
+				print "HTTP Status: ".$res->code." ".$res->message."\n";
+				print $res->content if($res->content);
+
+				return;
+			}
+		}
+		else {
+			print "Irker service already deactivated for project: ". $project->{name} ." (". $project->{id} .")\n" if($cfg{verbose});
+
+			return 2;
+		}
+	}
+	else {
+		print "Error: Request failed! ($url)\n";
+		print "HTTP Status: ".$res->code." ".$res->message."\n";
+		print $res->content if($res->content);
+
+		return;
+	}
+}
+
+sub configure_kgb_webhook {
+	my ($project) = @_;
+
+	my $url = $cfg{url}.'/projects/'.uri_escape($project->{id}).'/hooks';
+
+	my $req = new HTTP::Request(GET => $url);
+	   $req->header('PRIVATE-TOKEN' => $cfg{token});
+
+	my $res = $ua->request($req);
+	if($res->is_success) {
+		my $content = $res->content;
+
+		my $data = decode_json($content);
+
+		print "hooks:\n". Dumper($data) if($cfg{debug});
+
+		my $kgb_url = $cfg{kgb_baseurl}.'?channel='.uri_escape($cfg{irc_recipients}).'&network='.uri_escape($cfg{kgb_network});
+
+		my $hook_id  = 0;
+		my $same_url = 0;
+		foreach my $hook (@{$data}) {
+			if($hook->{url} =~ /\Q$cfg{kgb_baseurl}\E/) {
+				$hook_id = $hook->{id};
+				if($hook->{url} eq $kgb_url) {
+					$same_url = 1;
+				}
+				last;
+			}
+		}
+
+		if(!$hook_id) {
+			print "Adding KGB webhook for: ". $project->{name} ." (". $project->{id} .")\n" if($cfg{verbose});
+
+			my $url = $cfg{url}.'/projects/'.uri_escape($project->{id}).'/hooks';
+
+			my %param = (
+			              id                    => $project->{id},
+			              url                   => $kgb_url,
+			              push_events           => 'true',
+			              issues_events         => 'true',
+			              merge_requests_events => 'true',
+			              tag_push_events       => 'true',
+			              note_events           => 'true',
+			              job_events            => 'true',
+			              pipeline_events       => 'true',
+			              wiki_events           => 'true',
+			            );
+
+			print "param:\n".Dumper(\%param) if($cfg{debug});
+
+			my $req = HTTP::Request::Common::POST($url, [ %param ]);
+			   $req->header('PRIVATE-TOKEN' => $cfg{token});
+
+			my $res = $ua->request($req);
+			if($res->is_success) {
+				my $content = $res->content;
+
+				print "Content:\n".Dumper($content) if($cfg{debug});
+
+				print "Added KGB webhook for project: ". $project->{name} ." (". $project->{id} .")\n" if($cfg{verbose});
+
+				return 1;
+			}
+			else {
+				print "Error: Request failed! ($url)\n";
+				print "HTTP Status: ".$res->code." ".$res->message."\n";
+				print $res->content if($res->content);
+
+				return;
+			}
+		}
+		elsif($hook_id && !$same_url) {
+			print "Editing KGB webhook for: ". $project->{name} ." (". $project->{id} .")\n" if($cfg{verbose});
+
+			my $url = $cfg{url}.'/projects/'.uri_escape($project->{id}).'/hooks/'.uri_escape($hook_id);
+
 			my %param = (
-			              recipients        => $cfg{irc_recipients},
-			              colorize_messages => 'true',
+			              id                    => $project->{id},
+			              hook_id               => $hook_id,
+			              url                   => $kgb_url,
+			              push_events           => 'true',
+			              issues_events         => 'true',
+			              merge_requests_events => 'true',
+			              tag_push_events       => 'true',
+			              note_events           => 'true',
+			              job_events            => 'true',
+			              pipeline_events       => 'true',
+			              wiki_events           => 'true',
 			            );
 
 			print "param:\n".Dumper(\%param) if($cfg{debug});
@@ -503,7 +641,7 @@ sub configure_irker_service {
 
 				print "Content:\n".Dumper($content) if($cfg{debug});
 
-				print "Activated Irker service for project: ". $project->{name} ." (". $project->{id} .")\n" if($cfg{verbose});
+				print "Edited KGB webhook for project: ". $project->{name} ." (". $project->{id} .")\n" if($cfg{verbose});
 
 				return 1;
 			}
@@ -516,7 +654,7 @@ sub configure_irker_service {
 			}
 		}
 		else {
-			print "Irker service already active for project: ". $project->{name} ." (". $project->{id} .")\n" if($cfg{verbose});
+			print "KGB webhook already added for project: ". $project->{name} ." (". $project->{id} .")\n" if($cfg{verbose});
 
 			return 2;
 		}


=====================================
salsa-create-repository.pl
=====================================
--- a/salsa-create-repository.pl
+++ b/salsa-create-repository.pl
@@ -38,9 +38,14 @@ my %cfg = (
             ci_cfg_path      => 'debian/.gitlab-ci.yml',
             email_recipients => 'pkg-grass-devel at lists.alioth.debian.org',
             irc_recipients   => '#debian-gis',
+            kgb_baseurl      => 'http://kgb.debian.net:9418/webhook/',
+            kgb_network      => 'otfc',
             project          => '',
             description      => '',
             visibility       => 'public',
+            enable_emails    => 1,
+            enable_irker     => 0,
+            enable_kgb       => 1,
             debug            => 0,
             verbose          => 0,
             help             => 0,
@@ -55,9 +60,14 @@ my $result = GetOptions(
                          'c|ci-cfg-path=s'      => \$cfg{ci_cfg_path},
                          'e|email-recipients=s' => \$cfg{email_recipients},
                          'i|irc-recipients=s'   => \$cfg{irc_recipients},
+                         'k|kgb-baseurl=s'      => \$cfg{kgb_baseurl},
+                         'N|kgb-network=s'      => \$cfg{kgb_network},
                          'p|project=s'          => \$cfg{project},
                          'D|description=s'      => \$cfg{description},
                          'V|visibility=s'       => \$cfg{visibility},
+                         'enable-emails!'       => \$cfg{enable_emails},
+                         'enable-irker!'        => \$cfg{enable_irker},
+                         'enable-kgb!'          => \$cfg{enable_kgb},
                          'd|debug'              => \$cfg{debug},
                          'v|verbose'            => \$cfg{verbose},
                          'h|help'               => \$cfg{help},
@@ -79,6 +89,8 @@ if(!$result || $cfg{help} || !$cfg{project}) {
 	print "-c, --ci-cfg-path <PATH>        CI Config Path     ($cfg{ci_cfg_path})\n";
 	print "-e, --email-recipients <EMAIL>  Email recipients   ($cfg{email_recipients})\n";
 	print "-i, --irc-recipients <CHANNEL>  IRC recipients     ($cfg{irc_recipients})\n";
+	print "-k, --kgb-baseurl <URL>         KGB base URL       ($cfg{kgb_baseurl})\n";
+	print "-N, --kgb-network <NAME>        KGB network name   ($cfg{kgb_network})\n";
 	print "\n";
 	print "-p, --project <NAME>            Project name to create\n";
 	print "-D, --description <STRING>      Project description\n";
@@ -140,12 +152,17 @@ if(!$project) {
 }
 
 # Configure Emails on push service
-if(!configure_emails_on_push_service($project)) {
+if($cfg{enable_emails} && !configure_emails_on_push_service($project)) {
 	exit 1;
 }
 
 # Configure Irker service
-if(!configure_irker_service($project)) {
+if($cfg{enable_irker} && !configure_irker_service($project)) {
+	exit 1;
+}
+
+# Configure kgb webhook
+if($cfg{enable_kgb} && !configure_kgb_webhook($project)) {
 	exit 1;
 }
 
@@ -458,3 +475,131 @@ sub configure_irker_service {
 	}
 }
 
+sub configure_kgb_webhook {
+	my ($project) = @_;
+
+	my $url = $cfg{url}.'/projects/'.uri_escape($project->{id}).'/hooks';
+
+	my $req = new HTTP::Request(GET => $url);
+	   $req->header('PRIVATE-TOKEN' => $cfg{token});
+
+	my $res = $ua->request($req);
+	if($res->is_success) {
+		my $content = $res->content;
+
+		my $data = decode_json($content);
+
+		print "hooks:\n". Dumper($data) if($cfg{debug});
+
+		my $kgb_url = $cfg{kgb_baseurl}.'?channel='.uri_escape($cfg{irc_recipients}).'&network='.uri_escape($cfg{kgb_network});
+
+		my $hook_id  = 0;
+		my $same_url = 0;
+		foreach my $hook (@{$data}) {
+			if($hook->{url} =~ /\Q$cfg{kgb_baseurl}\E/) {
+				$hook_id = $hook->{id};
+				if($hook->{url} eq $kgb_url) {
+					$same_url = 1;
+				}
+				last;
+			}
+		}
+
+		if(!$hook_id) {
+			print "Adding KGB webhook for: ". $project->{name} ." (". $project->{id} .")\n" if($cfg{verbose});
+
+			my $url = $cfg{url}.'/projects/'.uri_escape($project->{id}).'/hooks';
+
+			my %param = (
+			              id                    => $project->{id},
+			              url                   => $kgb_url,
+			              push_events           => 'true',
+			              issues_events         => 'true',
+			              merge_requests_events => 'true',
+			              tag_push_events       => 'true',
+			              note_events           => 'true',
+			              job_events            => 'true',
+			              pipeline_events       => 'true',
+			              wiki_events           => 'true',
+			            );
+
+			print "param:\n".Dumper(\%param) if($cfg{debug});
+
+			my $req = HTTP::Request::Common::POST($url, [ %param ]);
+			   $req->header('PRIVATE-TOKEN' => $cfg{token});
+
+			my $res = $ua->request($req);
+			if($res->is_success) {
+				my $content = $res->content;
+
+				print "Content:\n".Dumper($content) if($cfg{debug});
+
+				print "Added KGB webhook for project: ". $project->{name} ." (". $project->{id} .")\n" if($cfg{verbose});
+
+				return 1;
+			}
+			else {
+				print "Error: Request failed! ($url)\n";
+				print "HTTP Status: ".$res->code." ".$res->message."\n";
+				print $res->content if($res->content);
+
+				return;
+			}
+		}
+		elsif($hook_id && !$same_url) {
+			print "Editing KGB webhook for: ". $project->{name} ." (". $project->{id} .")\n" if($cfg{verbose});
+
+			my $url = $cfg{url}.'/projects/'.uri_escape($project->{id}).'/hooks/'.uri_escape($hook_id);
+
+			my %param = (
+			              id                    => $project->{id},
+			              hook_id               => $hook_id,
+			              url                   => $kgb_url,
+			              push_events           => 'true',
+			              issues_events         => 'true',
+			              merge_requests_events => 'true',
+			              tag_push_events       => 'true',
+			              note_events           => 'true',
+			              job_events            => 'true',
+			              pipeline_events       => 'true',
+			              wiki_events           => 'true',
+			            );
+
+			print "param:\n".Dumper(\%param) if($cfg{debug});
+
+			my $req = HTTP::Request::Common::PUT($url, [ %param ]);
+			   $req->header('PRIVATE-TOKEN' => $cfg{token});
+
+			my $res = $ua->request($req);
+			if($res->is_success) {
+				my $content = $res->content;
+
+				print "Content:\n".Dumper($content) if($cfg{debug});
+
+				print "Edited KGB webhook for project: ". $project->{name} ." (". $project->{id} .")\n" if($cfg{verbose});
+
+				return 1;
+			}
+			else {
+				print "Error: Request failed! ($url)\n";
+				print "HTTP Status: ".$res->code." ".$res->message."\n";
+				print $res->content if($res->content);
+
+				return;
+			}
+		}
+		else {
+			print "KGB webhook already added for project: ". $project->{name} ." (". $project->{id} .")\n" if($cfg{verbose});
+
+			return 2;
+		}
+	}
+	else {
+		print "Error: Request failed! ($url)\n";
+		print "HTTP Status: ".$res->code." ".$res->message."\n";
+		print $res->content if($res->content);
+
+		return;
+	}
+}
+



View it on GitLab: https://salsa.debian.org/debian-gis-team/scripts/compare/3b149cfa97ac65690e60073b0f54698905e1b81c...b3c8e6af8f6fa9c7378026a92164178ce5686fb6

---
View it on GitLab: https://salsa.debian.org/debian-gis-team/scripts/compare/3b149cfa97ac65690e60073b0f54698905e1b81c...b3c8e6af8f6fa9c7378026a92164178ce5686fb6
You're receiving this email because of your account on salsa.debian.org.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.alioth.debian.org/pipermail/pkg-grass-devel/attachments/20180219/9b150613/attachment-0001.html>


More information about the Pkg-grass-devel mailing list