1 contributor
<%
# API
my $api = gitprep_api;
# Parameters
my $base_user_id = param('user');
my $base_project_id = param('project');
my $row_id = param('row_id');
# Pull requests
my $pull_request = app->dbi->model('pull_request')->select(
[
{__MY__ => '*'},
{'__open_user' => ['id']}
],
where => {'pull_request.row_id' => $row_id}
)->one;
# Base information
my $base_project_row_id = $pull_request->{base_project};
my $base_project = app->dbi->model('project')->select(
[
{__MY__ => '*'},
{__user => ['id']}
],
where => {'project.row_id' => $base_project_row_id}
)->one;
my $base_branch = $pull_request->{base_branch};
Carp::croak "pull_request invalid user id"
if $base_user_id ne $base_project->{'__user.id'};
Carp::croak "pull_request invalid project id"
if $base_project_id ne $base_project->{id};
# Target information
my $target_project_row_id = $pull_request->{target_project};
my $target_project = app->dbi->model('project')->select(
[
{__MY__ => '*'},
{__user => ['id']}
],
where => {'project.row_id' => $target_project_row_id}
)->one;
my $target_branch = $pull_request->{target_branch};
my $target_user_id = $target_project->{'__user.id'};
my $taregt_project_id = $target_project->{id};
# Session
my $session_user_row_id = $api->session_user_row_id;
my $session_user_id = $api->session_user_id;
# Base repository information
my $base_rep_info = app->rep_info($base_user_id, $base_project_id);
# Target repository information
my $target_rep_info = app->rep_info($target_user_id, $base_project_id);
# Working repository information
my $work_rep_info = app->work_rep_info($base_user_id, $base_project_id);
# Git
my $git = $self->app->git;
if (lc $self->req->method eq 'post') {
# Access controll
unless ($api->can_write_access($session_user_id, $base_user_id, $base_project_id)) {
$self->reply->exception('Forbbiden');
return;
}
# Close pull request
my $op = param('op');
if ($op eq 'close') {
app->dbi->model('pull_request')->update(
{
open => 0,
open_user => $session_user_row_id
},
where => {row_id => $row_id}
);
$self->redirect_to('current');
return;
}
elsif ($op eq 'reopen') {
my $open_time = time;
app->dbi->model('pull_request')->update(
{
open => 1,
open_time => $open_time,
open_user => $session_user_row_id
},
where => {row_id => $row_id}
);
$self->redirect_to('current');
return;
}
elsif ($op eq 'merge') {
# Lock working repository
my $lock_fh = $self->app->manager->lock_rep($work_rep_info);
# Prepare merge
$self->app->manager->prepare_merge(
$work_rep_info,
$base_rep_info,
$base_branch,
$target_rep_info,
$target_branch
);
# Merge
my $merge_success = $self->app->manager->merge(
$work_rep_info,
$target_rep_info,
$target_branch
);
if ($merge_success) {
# Push
app->manager->push($work_rep_info, $base_branch);
app->dbi->model('pull_request')->update(
{
open => 0,
open_user => $session_user_row_id
},
where => {row_id => $row_id}
);
$self->redirect_to("/$base_user_id/$base_project_id/tree/$base_branch");
}
}
}
# Commits
my $commits = $git->forward_commits($base_rep_info, $base_branch, $target_branch);
my $commits_count = @$commits;
my $commits_date = {};
my $authors = {};
for my $commit (@$commits) {
my $date = $commit->{age_string_date_local};
$commits_date->{$date} ||= [];
$authors->{$commit->{author}} = 1;
push @{$commits_date->{$date}}, $commit;
}
my $authors_count = keys %$authors;
# Start commit
my $start_commit = $git->separated_commit($base_rep_info, $base_branch, $target_branch);
# End commit
my $end_commit = $git->get_commit($target_rep_info, $target_branch);
# Pull request first message
my $pull_request_message_first = app->dbi->model('pull_request_message')->select(
[
{__MY__ => '*'},
{__user => ['id']}
],
where => {pull_request => $row_id, number => 1}
)->one;
# Check merge automatically
my $merge_success;
if ($api->can_write_access($session_user_id, $base_user_id, $base_project_id) && $pull_request->{open}) {
my $lock_fh = $self->app->manager->lock_rep($work_rep_info);
# Prepare merge
$self->app->manager->prepare_merge(
$work_rep_info,
$base_rep_info,
$base_branch,
$target_rep_info,
$target_branch
);
# Check merge automatical
$merge_success = $self->app->manager->merge(
$work_rep_info,
$target_rep_info,
$target_branch
);
}
# Commit body arguments
my %commit_body_args = (
id => $end_commit->{id},
from_id => $start_commit->{id},
rev => $end_commit->{id},
from_rev => $start_commit->{id}
);
%>
% layout 'common', title => "Pull Requests Tags \x{30fb} $base_user_id/$base_project_id";
%= include '/include/header';
<div class="container">
<div style="font-size:23px;margin-top:20px;margin-bottom:9px;">
<%
my $pull_title = $pull_request->{target_branch};
$pull_title =~ s/_/ /g;
$pull_title = ucfirst $pull_title;
%>
<%= $pull_title %> <span style="color:#767676;">#<%= $pull_request->{row_id} %></span>
</div>
<div>
<div style="display:inline-block;color:white;margin-right:4px;">
% if ($pull_request->{open}) {
<div style="background:#6cc644;padding:4px 8px;border-radius:3px;">
Open
</div>
% } else {
<div style="background:#bd2c00;padding:4px 8px;border-radius:3px;">
Closed
</div>
% }
</div>
% my $open_user_id = $pull_request->{'__open_user.id'};
<a style="color:#333333;font-weight:bold" href="<%= url_for("/$open_user_id") %>"><%= $open_user_id %></a>
<span style="color:#767676">
wants to merge <%= $commits_count %> commits
into
<span style="display:inline-block;padding:2px 4px;background:#e8f0f8;color:#336479;border-radius:3px;">
<%= $pull_request->{base_branch} %>
</span>
from
<span style="display:inline-block;padding:2px 4px;background:#e8f0f8;color:#336479;border-radius:3px;">
% if ($base_user_id ne $target_user_id) {
<%= $target_user_id %>
/
% }
<%= $pull_request->{target_branch} %>
</span>
</span>
</div>
% if (!$commits_count) {
<div class="compare-nothing" style="margin-top:10px">
<div>
<b><big>There isn't anything to compare.</big></b>
</div>
</div>
% } else {
<div>
<ul class="compare-header">
<li>
<b><%= @$commits %></b> <span>commit</span>
</li>
<li>
<b><%= $authors_count %></b> <span>contributor</span>
</li>
<li>
</li>
<li>
</li>
</ul>
<div class="pull-comment">
<div class="pull-comment-header">
<b><%= $pull_request_message_first->{'__open_user.id'} %></b>
<span style="color:#767676">
commented
<%= $api->age_string($pull_request_message_first->{update_time}) %></b>
</span>
</div>
<div class="pull-comment-body">
<%= $pull_request_message_first->{message} %>
</div>
</div>
<div class="commits">
% for my $date (reverse sort keys %$commits_date) {
% my $commits = $commits_date->{$date};
<div class="commit-date">
<i class="icon-off"></i><span>Commits on <%= $date %></span>
</div>
<ul class="compare-commits-date-container">
% for my $commit (sort {$b->{author_epoch} <=> $a->{author_epoch}} @$commits) {
<%
my $commit_author_email = $commit->{author_email};
my $commit_author_id = app->dbi->model('user')->select(
'id',
where => {email => $commit_author_email}
)->value;
%>
<li>
<div class="compare-commits-author">
<span title="<%= $commit->{author_email} %>">
% if (defined $commit_author_id) {
<a href="<%= url_for("/$commit_author_id") %>"><%= $commit_author_id %></a>
% } else {
<%= $commit->{author_name} %>
% }
</span>
</div>
<div class="compare-commits-commit-title">
<a style="color:#333" href="<%= url_for("/$base_user_id/$base_project_id/commit/$commit->{id}") %>">
<%= $commit->{title_short} %>
</a>
</div>
<div class="compare-commits-commit-id">
<a href="<%= url_for("/$base_user_id/$base_project_id/commit/$commit->{id}") %>">
<%= substr($commit->{id}, 0, 7) %>
</a>
</div>
</li>
% }
</ul>
% }
</div>
%= include '/include/commit_body', %commit_body_args;
% }
% if ($api->can_write_access($session_user_id, $base_user_id, $base_project_id)) {
% if ($commits_count && $merge_success && $pull_request->{open}) {
<form action="<%= url_for %>" method="post">
<%= hidden_field op => 'merge' %>
<div class="pull-request-form">
<div style="overflow:hidden">
<div style="float:left;padding:10px;padding-right:0">
<div style="width:30px;height:30px;text-align:center;border-radius:15px;background:#95c97e;color:white;padding-top:5px;"><%= "\x{2714}" %></div>
</div>
<div style="float:left">
<div class="pull-request-form-title">
<div>
<b>This branch has no conflicts with the base branch</b>
</div>
<div>
<span style="color:#767676">Merging can be performed automatically.</span>
</div>
</div>
</div>
</div>
<div class="pull-request-form-button">
<%= submit_button 'Merge pull request', class => "btn btn-success" %>
</div>
</div>
</form>
% }
% if ($pull_request->{open}) {
<div style="text-align:right;margin-top:10px;">
<form action="<%= url_for %>" method="post">
<%= hidden_field op => 'close' %>
<%= submit_button 'Close pull request', class => 'btn' %>
</form>
</div>
% } else {
<div style="text-align:right;margin-top:10px;">
<form action="<%= url_for %>" method="post">
<%= hidden_field op => 'reopen' %>
<%= submit_button 'Reopen pull request', class => 'btn' %>
</form>
</div>
% }
% }
</div>
</div>
%= include '/include/footer';