| 1 | package App::SD::Replica::github; | ||
| 2 | use Any::Moose; | ||
| 3 | extends qw/App::SD::ForeignReplica/; | ||
| 4 | |||
| 5 | use Params::Validate qw(:all); | ||
| 6 | use Memoize; | ||
| 7 | |||
| 8 | use URI; | ||
| 9 | use Memoize; | ||
| 10 | use Config::GitLike::Git; | ||
| 11 | use Prophet::ChangeSet; | ||
| 12 | |||
| 13 | use constant scheme => 'github'; | ||
| 14 | use constant pull_encoder => 'App::SD::Replica::github::PullEncoder'; | ||
| 15 | use constant push_encoder => 'App::SD::Replica::github::PushEncoder'; | ||
| 16 | |||
| 17 | has github => ( isa => 'Net::GitHub::V2', is => 'rw' ); | ||
| 18 | has remote_url => ( isa => 'Str', is => 'rw' ); | ||
| 19 | has owner => ( isa => 'Str', is => 'rw' ); | ||
| 20 | has repo => ( isa => 'Str', is => 'rw' ); | ||
| 21 | has query => ( isa => 'Str', is => 'rw' ); | ||
| 22 | has foreign_username => ( isa => 'Str', is => 'rw' ); | ||
| 23 | |||
| 24 | our %PROP_MAP = ( | ||
| 25 | state => 'status', | ||
| 26 | title => 'summary', | ||
| 27 | created_at => 'created', | ||
| 28 | ); | ||
| 29 | |||
| 30 | =head2 GitHub sync details | ||
| 31 | |||
| 32 | GitHub has a somewhat limited ticketing functionality (GitHub calls tickets | ||
| 33 | "issues"). | ||
| 34 | |||
| 35 | Tickets have two states: open or closed. | ||
| 36 | |||
| 37 | Once a ticket is created, the following modifications can be made to it: | ||
| 38 | - edit ticket body/title | ||
| 39 | - add a new comment | ||
| 40 | - edit a comment's body | ||
| 41 | - close a ticket (or reopen) | ||
| 42 | |||
| 43 | Thus, there is no "history" API call---we just get the current state, and we | ||
| 44 | can formulate our own history based on the list of comments, updated_at | ||
| 45 | timestamps, and comparing our state with the current state. | ||
| 46 | |||
| 47 | GitHub issues can also have arbitrary "labels" applied to them, but we're | ||
| 48 | currently ignoring this functionality. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | sub BUILD { | ||
| 53 | my $self = shift; | ||
| 54 | |||
| 55 | eval { | ||
| 56 | require Net::GitHub; | ||
| 57 | }; | ||
| 58 | |||
| 59 | if ($@) { | ||
| 60 | die "SD requires Net::GitHub to sync with a GitHub server. " | ||
| 61 | . "'cpan Net::GitHub' may sort this out for you\n"; | ||
| 62 | } | ||
| 63 | |||
| 64 | my ( $server, $owner, $repo ) | ||
| 65 | = $self->{url} | ||
| 66 | =~ m{^github:((?:(?:http|git)://.*?github.com/|git\@github.com:))?(.*?)/([^/\.]+)(?:(/|\.git))?} | ||
| 67 | or die | ||
| 68 | "Can't parse Github server spec. Expected github:owner/repository or github:http://github.com/owner/repository.\n"; | ||
| 69 | |||
| 70 | my ( $uri, $username, $api_token ); | ||
| 71 | |||
| 72 | if ($server && $server =~ m!http!) { | ||
| 73 | $uri = URI->new($server); | ||
| 74 | if ( my $auth = $uri->userinfo ) { | ||
| 75 | ( $username, $api_token ) = split /:/, $auth, 2; | ||
| 76 | $uri->userinfo(undef); | ||
| 77 | } | ||
| 78 | } | ||
| 79 | else { | ||
| 80 | $uri = 'http://github.com/'; | ||
| 81 | } | ||
| 82 | |||
| 83 | # try loading github username & token from git configuration | ||
| 84 | # see http://github.com/blog/180-local-github-config | ||
| 85 | unless ( $api_token ) { | ||
| 86 | my $config = Config::GitLike::Git->new; | ||
| 87 | $config->load; | ||
| 88 | $username = $config->get(key => 'github.user'); | ||
| 89 | $api_token = $config->get(key => 'github.token'); | ||
| 90 | } | ||
| 91 | |||
| 92 | ($username, $api_token) = $self->login_loop( | ||
| 93 | uri => $uri, | ||
| 94 | username => $username, | ||
| 95 | password => $api_token, | ||
| 96 | secret_prompt => sub { | ||
| 97 | my ($uri, $username) = @_; | ||
| 98 | return "GitHub API token for $username (from ${uri}account): "; | ||
| 99 | }, | ||
| 100 | login_callback => sub { | ||
| 101 | my ($self, $username, $api_token) = @_; | ||
| 102 | |||
| 103 | $self->github( | ||
| 104 | Net::GitHub->new( | ||
| 105 | login => $username, | ||
| 106 | token => $api_token, | ||
| 107 | repo => $repo, | ||
| 108 | owner => $owner, | ||
| 109 | ) ); | ||
| 110 | }, | ||
| 111 | ); | ||
| 112 | |||
| 113 | $self->remote_url("$uri"); | ||
| 114 | $self->owner( $owner ); | ||
| 115 | $self->repo( $repo ); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub record_pushed_transactions {} | ||
| 119 | |||
| 120 | sub _uuid_url { | ||
| 121 | my $self = shift; | ||
| 122 | Carp::cluck "- can't make a uuid for this" unless ($self->remote_url && $self->owner && $self->repo ); | ||
| 123 | return join( '/', $self->remote_url, $self->owner , $self->repo ) ; | ||
| 124 | } | ||
| 125 | |||
| 126 | sub remote_uri_path_for_comment { | ||
| 127 | my $self = shift; | ||
| 128 | my $id = shift; | ||
| 129 | return "/comment/".$id; | ||
| 130 | } | ||
| 131 | |||
| 132 | sub remote_uri_path_for_id { | ||
| 133 | my $self = shift; | ||
| 134 | my $id = shift; | ||
| 135 | return "/ticket/".$id; | ||
| 136 | } | ||
| 137 | |||
| 138 | sub database_settings { | ||
| 139 | my $self = shift; | ||
| 140 | return { | ||
| 141 | # TODO limit statuses too? the problem is github's statuses are so poor, | ||
| 142 | # it only has 2 statuses: 'open' and 'closed'. | ||
| 143 | project_name => $self->owner . '/' . $self->repo, | ||
| 144 | }; | ||
| 145 | |||
| 146 | } | ||
| 147 | |||
| 148 | __PACKAGE__->meta->make_immutable; | ||
| 149 | no Any::Moose; | ||
| 150 | 1; |