gitprep / templates / import-branch.html.ep /
9358bf9 10 years ago
1 contributor
176 lines | 5.413kb
<%
  
  my $api = gitprep_api;
  
  my $user = param('user');
  my $project = param('project');
  my $remote_user = param('remote_user');
  my $remote_project = param('remote_project');
  my $remote_branch = param('remote-branch');
  
  # Authentication
  unless ($api->logined($user)) {
    $self->redirect_to('/');
    return;
  }
  
  # Branches
  my $git = app->git;
  my $remote_branches = $git->branches($remote_user, $remote_project);
  my $remote_branch_names = [map { $_->{name} } @$remote_branches];
  
  my $op = param('op') || '';
  my $errors;
  if ($op eq 'import' && lc $self->req->method eq 'post') {
  
    # Validation
    my $api = gitprep_api;
    my $params = $api->params;
    my $rule = [
      user => [
        ['user_name' => 'User name is invalid.']
      ],
      project => [
        ['project_name' => 'Repository name is invalid.']
      ],
      branch => [
        ['not_blank' => 'Branch name is empty.']
      ],
      remote_user => [
        ['user_name' => 'Remote User name is invalid.']
      ],
      remote_project => [
        ['project_name' => 'Remote repository is invalid.']
      ],
      'remote-branch' => [
        ['not_blank' => 'Remote branch name is empty.']
      ],
      force => {require => 0} => [
        'any'
      ]
    ];
    my $vresult = app->vc->validate($params, $rule);
    
    if ($vresult->is_ok) {
      my $safe_params = $vresult->data;
      
      # Valid paramters
      my $user = $safe_params->{user};
      my $project = $safe_params->{project};
      my $branch = $safe_params->{branch};
      my $remote_user = $safe_params->{remote_user};
      my $remote_project = $safe_params->{remote_project};
      my $remote_branch = $safe_params->{'remote-branch'};
      my $force = $safe_params->{force};
      
      # Check branch name
      my $branches = $git->branches($user, $project);
      
      if (!$force && grep { $branch eq $_->{name} } @$branches) {
        $errors = ["Branch \"$branch\" is already exists. If you want to import this branch, check force option."];
      }
      else {
        eval {
          $git->import_branch(
            $user,
            $project,
            $branch,
            $remote_user,
            $remote_project,
            $remote_branch,
            {force => $force}
          );
        };
        
        if ($@) {
          $errors = ['Internal Error'];
        }
        else {
          flash(message => "Success: " . ($force ? 'force ' : '') . "import \"$remote_user / $remote_project / $remote_branch\" into \"$user / $project / $branch\"");
          $self->redirect_to('current');
          return;
        }
      }
    }
    else {
      $errors = $vresult->messages;
    }
  }
%>

% layout 'common', title => "Import branch";
  %= include 'include/header';

  %= javascript begin
    $('document').ready(function () {
      
      // Select remote branch
      $('[name=copy-branch-name]').on('click', function () {
        $('[name=branch]').val($('[name=remote_branch]').val());
        return false;
      });
    });
  % end
  
  <div class="container">
    %= include '/include/project_header';
    % if (my $message = flash('message')) {
      <div class="alert alert-success">
        <button type="button" class="close" data-dismiss="alert">&times;</button>
        <%= $message %>
      </div>
    % }
    % if ($errors) {
      <div class="alert alert-error">
        <button type="button" class="close" data-dismiss="alert">&times;</button>
        % for my $error (@$errors) {
          <p><%= $error %></p>
        % }
      </div>
    % }
    <h3>Import branch</h3>
    <form action="<%= url_for("/$user/$project/import-branch/$remote_user/$remote_project")->query(op => 'import') %>" method="post">
      <div class="row" style="font-size:22px">
        <div class="span6">
          <div class="well" style="text-align:center">
            <div style="color:blue;margin-bottom:15px">
              %= "$user / $project";
            </div>
            <div>
              %= text_field 'branch', placeholder => "Branch name", style => "margin-top:12px;width:250px";
              <button name="copy-branch-name", class="btn" style="font-size:12px; padding-left:3px;padding-right:3px;color:#666">Copy Branch Name</button>
            </div>
            <table style="width:100%">
              <tr>
                <td style="width:80px;text-align:right">
                  %= submit_button 'Import', class => "btn";
                </td>
                <td style="width:20px;">
                  <%= check_box force => 1 %> 
                </td>
                <td style="font-size:13px;padding-top:7px;text-align:left">
                  Force
                </td>
              </tr>
            </table>
          </div>
        </div>
        <div class="span1">
          <div style="padding: 19px;text-align:center;font-size:26px">
            &lArr;
          </div>
        </div>
        <div class="span5">
          <div class="well" style="text-align:center">
            <div style="color:green;margin-bottom:15px">
              %= "$remote_user / $remote_project";
            </div>
            % param(remote_branch => $remote_branch);
            %= select_field 'remote-branch' => $remote_branch_names, style => "width:250px";
          </div>
        </div>
      </div>
    </form>
  </div>
  
  %= include '/include/footer';