Commit 0c2671de authored by Alex Muntada's avatar Alex Muntada

Add invite-github script

parent 4bc46611
......@@ -170,6 +170,10 @@ Copyright: 2013, Jonas Smedegaard <>
2013, gregor herrmann <>
License: Artistic or GPL-1+
Files: scripts/invite-github
Copyright: 2016, Alex Muntada <>
License: Artistic or GPL-1+
Files: lib/*
Copyright: 2016, Alex Muntada <>
License: Artistic or GPL-1+
use warnings;
use strict;
use Getopt::Long;
use LWP::UserAgent;
use JSON::XS;
=head1 NAME
invite-github - Invite someone to a GitHub organization
invite-github [option...] {github-username}
B<dpt invite-github> can be used for inviting people to become members
of a GitHub organization. You can set the organization name through
C<DPT_GUTHUB_ORGNAME> environment variable (defaults to I<pkg-perl-tools>).
You need to have write access to the C<admin:org> scope to be able to
manage organization memberships. Please, make sure the token you use
has that scope in L<>.
=head1 OPTIONS
=item B<--role> I<name>
This option sets the role for the user. Valid roles are I<admin> or I<member>,
which is the default.
=item B<--force>
Use this option if the GitHub user is already a member of the organization
and you want to change their role anyway.
my $opt_force;
my $opt_role;
'role=s' => \$opt_role,
'force!' => \$opt_force,
) or exit 1;
my ($gh_user) = @ARGV;
die "GitHub user name is required\n" unless defined $gh_user;
die "DPT_GITHUB_OAUTH environment variable must be set\n"
unless defined $ENV{DPT_GITHUB_OAUTH}
and $ENV{DPT_GITHUB_OAUTH} =~ /\w+/;
my $orgname = $ENV{DPT_GITHUB_ORGNAME} || 'pkg-perl-tools';
my $api = "$orgname/memberships/$gh_user";
my $content_type = 'application/vnd.github.v3+json';
my %headers = (
'Authorization' => "token $ENV{DPT_GITHUB_OAUTH}",
'Content-Type' => $content_type,
'Accept' => $content_type,
sub check_membership {
my ($res) = @_;
my $json = decode_json($res->decoded_content);
if ( $res->is_success ) {
my $state = $json->{state};
my $role = $json->{role};
print "User $gh_user is already a member of $orgname with state $state and role $role\n";
else {
my $message = $json->{message};
die "Failed $gh_user membership status: $message\n";
my $ua = LWP::UserAgent->new();
my $res = $ua->get( $api, %headers );
print "User $gh_user is not a member of $orgname yet...\n"
if $res->code == 404;
$headers{Content} = encode_json({ role => $opt_role })
if $opt_role;
$res = $ua->put( $api, %headers )
if $res->code == 404 or $opt_force;
=item Copyright 2016 Alex Muntada.
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.
See for more information.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment