File: invite-github

package info (click to toggle)
pkg-perl-tools 0.82
  • links: PTS, VCS
  • area: main
  • in suites: forky, trixie
  • size: 796 kB
  • sloc: sh: 3,255; perl: 3,239; makefile: 142; python: 18
file content (118 lines) | stat: -rwxr-xr-x 2,921 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/perl

use warnings;
use strict;

use Getopt::Long;
use LWP::UserAgent;
use JSON::XS;

=head1 NAME

dpt-invite-github - Invite someone to a GitHub organization

=head1 SYNOPSIS

    dpt invite-github [option...] {github-username}

=head1 DESCRIPTION

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_GITHUB_ORGNAME> environment variable (defaults to I<pkg-perl-tools>).

This script needs the C<DPT_GITHUB_OAUTH> environment variable,
which can be set in F<~/.dpt.conf> or F<~/.config/dpt.conf>.
Cf. L<dpt-github-oauth(1)> and L<dpt-config(5)>.

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<https://github.com/settings/tokens>.

=head1 OPTIONS

=over

=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.

=back

=cut

my $opt_force;
my $opt_role;

GetOptions(
    '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 = "https://api.github.com/orgs/$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;

check_membership($res);


=head1 LICENSE AND COPYRIGHT

=over

=item Copyright 2016 Alex Muntada.

=back

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 http://dev.perl.org/licenses/ for more information.

=cut