Skip to content

Instantly share code, notes, and snippets.

@briandfoy
Created July 27, 2012 19:43
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briandfoy/3190109 to your computer and use it in GitHub Desktop.
Save briandfoy/3190109 to your computer and use it in GitHub Desktop.
Add Github projects to Ohloh
#!/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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment