public
Last active

Add Github projects to Ohloh

  • Download Gist
github2ohloh.pl
Perl
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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
#!/usr/bin/perl
use v5.14;
 
use Mojo::UserAgent;
 
my $ua = Mojo::UserAgent->new->max_redirects(5);
 
my $user = $ENV{GITHUB_USER} // '...';
my @committer_names = ( '...', '...', );
$ENV{OHLOH_USER} //= '...';
$ENV{OHLOH_PASS} //= '...';
 
my @repos;
while( 1 ) {
state $page = 1;
my $repos = $ua->get(
"https://api.github.com/users/$user/repos?page=$page&per_page=100",
)
->res
->json
;
last unless eval { @$repos };
push @repos, @$repos;
$page++;
}
 
say "Found " . @repos . " repos on Github for $user";
 
 
{
say "Logging in to Ohloh";
my $new_tx = $ua->get( 'https://www.ohloh.net/sessions/new' );
my $login_tx = $ua->post_form(
'https://www.ohloh.net/sessions' => {
'login[login]' => $ENV{OHLOH_USER},
'login[password]' => $ENV{OHLOH_PASS},
'login[remember_me]' => '0',
'login[remember_me]' => '1',
},
);
}
 
 
my %errors;
 
foreach my $repo ( @repos ) {
say "Adding $repo->{full_name}";
 
my $tx = $ua->post_form(
'https://www.ohloh.net/p/check_forge' => {
'codelocation' => $repo->{html_url},
},
);
if( $tx->res->body =~ m/Duplicate Project Found/i ) {
say "\tDuplicate project";
}
else {
my $inputs = get_project_form_inputs( \ $tx->res->body );
my $tx = $ua->post_form( 'https://www.ohloh.net/p' => $inputs );
if( $tx->res->body =~ /There was a problem/i ) {
my @errors = get_tx_errors( $tx );
say "\tThere was a problem! Project not added";
say "\t", join "\n\t", @errors;
$errors{ $repo->{html_url} }++;
next;
}
say "\tProject added";
}
}
 
foreach my $repo ( @repos ) {
say "Adding committters for $repo->{full_name}";
 
my $committers_added = 0;
foreach my $committer_name ( @committer_names ) {
if( add_position( $repo->{html_url}, $committer_name ) ) {
say "\tPosition added for [$committer_name]";
$committers_added++;
}
}
 
unless( $committers_added > 0 ) {
say "\tNo committers found!";
$errors{ $repo->{html_url} }++;
}
}
 
sub get_tx_errors {
my( $tx ) = @_;
my @errors = $tx->res->dom
->find( 'p.error' )
->map( sub { $_->text } )
->grep( sub { length $_ } )
;
}
 
sub get_project_form_inputs {
my( $html_ref ) = @_;
require HTML::Form;
my %hash =
map { ( $_->name, $_->value ) }
grep { defined $_->name }
map { $_->inputs }
grep { defined $_->value( 'project[baseline_name]' ) }
HTML::Form->parse( $$html_ref, 'https://www.ohloh.com/' );
return \%hash;
}
 
sub add_position {
# Or, just https://www.ohloh.net/accounts/me/positions/one_click_create?committer_name=brian+d+foy&project_name=Tie-Toggle
# this appears to work, but not really
my( $url, $committer_name ) = @_;
my( $name ) = $url =~ m|.*/(.*)|;
state $fields = [ map { ( "position[$_]", '' ) } qw(
organization
title
description
start_month
start_year
stop_month
stop_year
language_0
usedproject_0
) ];
my $tx = $ua->post_form(
"https://www.ohloh.net/accounts/$ENV{OHLOH_USER}/positions" => {
'position[project_oss]' => $name,
'position[committer_name]' => $committer_name,
"position[stop_date_type]" => "automatic",
"position[start_date_type]" => "automatic",
"commit" => "Submit",
@$fields,
},
);
 
my @errors = get_tx_errors( $tx );
 
if( grep { /already claimed/i } @errors ) {
say "\tAlready claimed!";
return 1;
}
elsif( grep { /No recognized committer/i } @errors ) {
say "\tNo recognized committer with [$committer_name]";
return 0;
}
elsif( grep length, @errors ) {
foreach my $error ( @errors ) {
say "\tError: [$error]";
}
 
#say $tx->req->to_string;
return 0;
}
return 1;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.