Merge pull request #228 from abf/rosa-build:226-remove-API-calls-from-JS

#226: Remove API calls from JS
This commit is contained in:
avm 2013-07-25 13:19:44 +04:00
commit 72f933217b
18 changed files with 194 additions and 83 deletions

View File

@ -1,47 +1,41 @@
RosaABF.controller('ProjectRefsController', ['$scope', '$http', 'ApiProject', function($scope, $http, ApiProject) {
RosaABF.controller('ProjectBranchesController', ['$scope', '$http', 'ApiProject', function($scope, $http, ApiProject) {
$scope.singleton = ApiProject.singleton;
$scope.branches = [];
$scope.tags = [];
$scope.singleton = ApiProject.singleton;
$scope.branches = [];
$scope.project_id = null;
$scope.current_ref = null;
$scope.project_resource = null;
$scope.init = function(project_id, ref, locale) {
$scope.project_id = project_id;
$scope.init = function(owner_uname, project_name, ref) {
$scope.current_ref = ref;
$scope.project_resource = ApiProject.resource.get({id: $scope.project_id}, function(results) {
$scope.project = new Project(results.project);
$scope.getRefs();
});
$scope.project_resource = ApiProject.resource.get(
{owner: owner_uname, project: project_name},
function(results) {
$scope.project = new Project(results.project);
$scope.getBranches();
}
);
}
$scope.getRefs = function() {
$scope.getBranches = function() {
$scope.project_resource.$refs({id: $scope.project_id}, function(results) {
$scope.tags = [];
$scope.branches = [];
_.each(results.refs_list, function(ref){
var result = new ProjectRef(ref);
if (result.isTag) {
if (result.ref == $scope.current_ref) {
$scope.tags.unshift(result);
} else {
$scope.tags.push(result);
}
} else {
$scope.project_resource.$branches(
{owner: $scope.project.owner.uname, project: $scope.project.name},
function(results) {
$scope.branches = [];
_.each(results.refs_list, function(ref){
var result = new ProjectRef(ref);
if (result.ref == $scope.current_ref) {
$scope.branches.unshift(result);
} else {
$scope.branches.push(result);
}
}
});
$scope.updateBranchesCount();
});
});
$scope.updateBranchesCount();
}
);
}
@ -58,9 +52,9 @@ RosaABF.controller('ProjectRefsController', ['$scope', '$http', 'ApiProject', fu
from_ref: branch.ref,
new_ref: branch.new_ref
}, function() { // on success
$scope.getRefs();
$scope.getBranches();
}, function () { // on error
$scope.getRefs();
$scope.getBranches();
}
);
}
@ -78,7 +72,7 @@ RosaABF.controller('ProjectRefsController', ['$scope', '$http', 'ApiProject', fu
return this.value.match('.*\/branches\/' + branch.ref + '$');
}).remove();
}, function () { // on error
$scope.getRefs();
$scope.getBranches();
}
);
}

View File

@ -0,0 +1,31 @@
RosaABF.controller('ProjectTagsController', ['$scope', '$http', 'ApiProject', function($scope, $http, ApiProject) {
$scope.tags = [];
$scope.project_resource = null;
$scope.init = function(owner_uname, project_name) {
$scope.project_resource = ApiProject.resource.get(
{owner: owner_uname, project: project_name},
function(results) {
$scope.project = new Project(results.project);
$scope.getTags();
}
);
}
$scope.getTags = function() {
$scope.project_resource.$tags(
{owner: $scope.project.owner.uname, project: $scope.project.name},
function(results) {
$scope.tags = [];
_.each(results.refs_list, function(ref){
$scope.tags.push(new ProjectRef(ref));
});
}
);
}
}]);

View File

@ -1,9 +1,8 @@
RosaABF.controller('PullRequestController',['$scope', '$http', 'ApiPullRequest', 'ApiProject', 'DateTimeFormatter', function($scope, $http, ApiPullRequest, ApiProject, DateTimeFormatter) {
$scope.project_id = null;
$scope.project_resource = null;
$scope.serial_id = null;
$scope.pull_params = null;
$scope.pull = null;
$scope.pull_resource = null;
@ -13,37 +12,36 @@ RosaABF.controller('PullRequestController',['$scope', '$http', 'ApiPullRequest',
$scope.can_delete_branch = false;
$scope.init = function(project_id, serial_id) {
$scope.project_id = project_id;
$scope.serial_id = serial_id;
$scope.init = function(owner_uname, project_name, serial_id) {
$scope.pull_params = {
owner: owner_uname,
project: project_name,
serial_id: serial_id
};
$scope.getPullRequest();
}
$scope.getPullRequest = function() {
$scope.pull_resource = ApiPullRequest.resource.get(
{project_id: $scope.project_id, serial_id: $scope.serial_id},
function(results) {
$scope.pull = results.pull_request;
if ($scope.pull.merged_at) { $scope.merged_at = DateTimeFormatter.utc($scope.pull.merged_at); }
if ($scope.pull.closed_at) { $scope.closed_at = DateTimeFormatter.utc($scope.pull.closed_at); }
}
);
$scope.pull_resource = ApiPullRequest.resource.get($scope.pull_params, function(results) {
$scope.pull = results.pull_request;
if ($scope.pull.merged_at) { $scope.merged_at = DateTimeFormatter.utc($scope.pull.merged_at); }
if ($scope.pull.closed_at) { $scope.closed_at = DateTimeFormatter.utc($scope.pull.closed_at); }
});
}
// @param [from_ref] - sets only at first time
$scope.getBranch = function(from_ref) {
if (!$scope.project_resource) {
$scope.project_resource = ApiProject.resource.get({id: $scope.project_id});
$scope.project_resource = ApiProject.resource.get($scope.pull_params);
}
// Fix: at first load
// Cannot read property 'from_ref' of null
if (!from_ref) { from_ref = $scope.pull.from_ref.ref; }
$scope.project_resource.$refs({id: $scope.project_id}, function(results) {
$scope.project_resource.$branches($scope.pull_params, function(results) {
var branch = null;
_.each(results.refs_list, function(ref){
var result = new ProjectRef(ref);
if (!result.isTag && result.ref == from_ref) {
branch = result;
_.each(results.refs_list, function(b){
if (b.ref == from_ref) {
branch = new ProjectRef(b);
return true;
}
});
@ -52,15 +50,13 @@ RosaABF.controller('PullRequestController',['$scope', '$http', 'ApiPullRequest',
}
$scope.reopen = function() {
$scope.pull.status = 'reopen';
$scope.pull_resource.$update(function() {
$scope.pull_resource.$update({pull_request_action: 'reopen'}, function() {
$scope.getPullRequest();
});
}
$scope.close = function() {
$scope.pull.status = 'close';
$scope.pull_resource.$update(function() {
$scope.pull_resource.$update({pull_request_action: 'close'}, function() {
$scope.getPullRequest();
});
}
@ -86,10 +82,9 @@ RosaABF.controller('PullRequestController',['$scope', '$http', 'ApiPullRequest',
}
$scope.branch_params = function() {
var project = $scope.pull.from_ref.project;
return {
owner: project.fullname.replace(/\/.*/, ''),
project: project.name,
owner: $scope.pull_params.owner,
project: $scope.pull_params.project,
ref: $scope.pull.from_ref.ref,
sha: $scope.pull.from_ref.sha
}

View File

@ -1,11 +1,16 @@
RosaABF.factory("ApiProject", ['$resource', function($resource) {
var ProjectResource = $resource(
'/api/v1/projects/:id.json',
{id: '@project.id'},
'/:owner/:project',
{owner: '@project.owner.uname', project: '@project.name'},
{
refs: {
url: '/api/v1/projects/:id/refs_list.json',
tags: {
url: '/:owner/:project/tags',
method: 'GET',
isArray : false
},
branches: {
url: '/:owner/:project/branches',
method: 'GET',
isArray : false
},

View File

@ -1,9 +1,10 @@
RosaABF.factory("ApiPullRequest", ['$resource', function($resource) {
var PullRequestResource = $resource(
'/api/v1/projects/:project_id/pull_requests/:serial_id.json',
'/:owner/:project/pull_requests/:serial_id',
{
project_id: '@pull_request.to_ref.project.id',
owner: '@pull_request.to_ref.project.owner_uname',
project: '@pull_request.to_ref.project.name',
serial_id: '@pull_request.number'
},
{
@ -12,7 +13,7 @@ RosaABF.factory("ApiPullRequest", ['$resource', function($resource) {
isArray : false
},
merge: {
url: '/api/v1/projects/:project_id/pull_requests/:serial_id/merge.json',
url: '/:owner/:project/pull_requests/:serial_id/merge',
method: 'PUT',
isArray: false
}

View File

@ -23,8 +23,7 @@ class Api::V1::ProjectsController < Api::V1::BaseController
end
def refs_list
@refs = @project.repo.branches.sort_by(&:name) +
@project.repo.tags.select{ |t| t.commit }.sort_by(&:name).reverse
@refs = @project.repo.branches + @project.repo.tags.select{ |t| t.commit }
end
def update

View File

@ -8,10 +8,12 @@ class Projects::Git::TreesController < Projects::Git::BaseController
before_filter lambda { authorize!(:write, @project) }, :only => [:destroy, :restore_branch, :create]
def show
render('empty') and return if @project.is_empty?
@tree = @tree / @path if @path.present?
@commit = @branch.present? ? @branch.commit() : @project.repo.log(@treeish, @path, :max_count => 1).first
raise Grit::NoSuchPathError unless @commit
unless request.xhr?
render('empty') and return if @project.is_empty?
@tree = @tree / @path if @path.present?
@commit = @branch.present? ? @branch.commit() : @project.repo.log(@treeish, @path, :max_count => 1).first
raise Grit::NoSuchPathError unless @commit
end
end
def archive
@ -32,6 +34,10 @@ class Projects::Git::TreesController < Projects::Git::BaseController
end
def tags
if request.xhr?
@refs = @project.repo.tags.select{ |t| t.commit }.sort_by(&:name).reverse
render :refs_list
end
end
def restore_branch
@ -50,6 +56,10 @@ class Projects::Git::TreesController < Projects::Git::BaseController
end
def branches
if request.xhr?
@refs = @project.repo.branches.sort_by(&:name)
render :refs_list
end
end
end

View File

@ -67,22 +67,31 @@ class Projects::PullRequestsController < Projects::BaseController
end
end
def merge
status = @pull.merge!(current_user) ? 200 : 422
render :nothing => true, :status => status
end
def update
status = 422
if (action = params[:pull_request_action]) && %w(close reopen).include?(params[:pull_request_action])
if @pull.send("can_#{action}?")
@pull.set_user_and_time current_user
@pull.send(action)
@pull.check if @pull.open?
status = 200
end
end
redirect_to project_pull_request_path(@pull.to_project, @pull)
render :nothing => true, :status => status
end
def show
if @pull.nil?
redirect_to project_issue_path(@project, @issue)
else
load_diff_commits_data
unless request.xhr?
if @pull.nil?
redirect_to project_issue_path(@project, @issue)
else
load_diff_commits_data
end
end
end

View File

@ -3,7 +3,6 @@ json.refs_list @refs do |grit|
json.object do
json.type (grit.class.name =~ /Tag/ ? 'tag' : 'commit')
json.sha grit.commit.id
json.authored_date grit.commit.authored_date.to_i
end
end
json.url refs_list_api_v1_project_path(@project.id, :format => :json)

View File

@ -3,8 +3,7 @@
= render 'submenu'
= render 'repo_block', :project => @project
%div{'ng-controller' => 'ProjectRefsController',
'ng-init' => "init('#{@project.id}','#{@branch.try(:name)}')"}
%div{'ng-controller' => 'ProjectBranchesController', 'ng-init' => "init('#{@project.owner.uname}', '#{@project.name}','#{@branch.try(:name)}')"}
%h3= t('layout.projects.branches')
%p{'ng-show' => '!branches.length'}= t('layout.projects.no_branches')

View File

@ -0,0 +1,8 @@
json.refs_list @refs do |grit|
json.ref grit.name
json.object do
json.type (grit.class.name =~ /Tag/ ? 'tag' : 'commit')
json.sha grit.commit.id
json.authored_date grit.commit.authored_date.to_i
end
end

View File

@ -0,0 +1,7 @@
json.project do
json.(@project, :id, :name)
json.owner do
json.(@project.owner, :id, :name, :uname)
end
end

View File

@ -3,7 +3,7 @@
= render 'submenu'
= render 'repo_block', :project => @project
%div{'ng-controller' => 'ProjectRefsController', 'ng-init' => "init('#{@project.id}')"}
%div{'ng-controller' => 'ProjectTagsController', 'ng-init' => "init('#{@project.owner.uname}', '#{@project.name}')"}
%h3= t('layout.projects.tags')
%p{'ng-show' => '!tags.length'}= t('layout.projects.no_tags')

View File

@ -1,4 +1,4 @@
- if can?(:merge, @pull) && @pull.can_merging?
- if can?(:merge, @pull)
%a.button{:href => '', 'ng-click' => 'merge()', 'ng-show' => "pull.status == 'ready'"}
= t 'projects.pull_requests.ready'
.both

View File

@ -2,7 +2,7 @@
-set_meta_tags :title => [title_object(@project), t('.title', :name => @pull.title.truncate(40), :user => @pull.user.try(:uname))]
= render :partial => 'submenu'
%div{'ng-controller' => 'PullRequestController', 'ng-init' => "init('#{@project.id}', '#{@pull.serial_id}')"}
%div{'ng-controller' => 'PullRequestController', 'ng-init' => "init('#{@project.owner.uname}', '#{@project.name}', '#{@pull.serial_id}')"}
%h3.bpadding10
- PullRequest::STATUSES.each do |status|

View File

@ -0,0 +1,39 @@
json.pull_request do
json.number @pull.serial_id
json.(@pull, :status)
json.to_ref do
json.ref @pull.to_ref
json.sha @pull.to_commit.try(:id)
json.project do
json.(@pull.to_project, :id, :name)
json.owner_uname @pull.to_project.owner.uname
end
end
json.from_ref do
json.ref @pull.from_ref
json.sha @pull.from_commit.try(:id)
json.project do
json.(@pull.from_project, :id, :name)
json.owner_uname @pull.to_project.owner.uname
end
end
json.owner do
json.(@pull.user, :id, :name, :uname)
end
json.assignee do
json.(@pull.issue.assignee, :id, :name, :uname)
end if @pull.issue.assignee
json.mergeable @pull.can_merging?
json.merged_at @pull.issue.closed_at.to_i if @pull.merged?
json.closed_at @pull.issue.closed_at.to_i if @pull.merged? || @pull.closed?
json.closed_by do
json.(@pull.issue.closer, :id, :name, :uname)
end if @pull.issue.closer
json.merged_by do
json.(@pull.issue.closer, :id, :name, :uname)
end if @pull.merged?
end

View File

@ -310,6 +310,7 @@ Rosa::Application.routes.draw do
resources :hooks, :except => :show
resources :pull_requests, :except => :destroy do
get :autocomplete_to_project, :on => :collection
put :merge, :on => :member
end
post '/preview' => 'projects#preview', :as => 'md_preview'
post 'refs_list' => 'projects#refs_list', :as => 'refs_list'
@ -334,10 +335,11 @@ Rosa::Application.routes.draw do
# Tags
get '/tags' => "git/trees#tags", :as => :tags
# Branches
get '/branches' => "git/trees#branches", :as => :branches
get '/branches/:treeish' => "git/trees#branches", :as => :branch
delete '/branches/:treeish' => "git/trees#destroy", :as => :branch
put '/branches/:treeish' => "git/trees#restore_branch", :as => :branch
post '/branches' => "git/trees#create", :as => :branchs
post '/branches' => "git/trees#create", :as => :branches
# Commits
get '/commits/:treeish(/*path)' => "git/commits#index", :as => :commits, :format => false
get '/commit/:id(.:format)' => "git/commits#show", :as => :commit

View File

@ -96,7 +96,13 @@ end
shared_examples_for 'user with pull request update rights' do
it 'should be able to perform update action' do
put :update, @update_params
response.should redirect_to(project_pull_request_path(@pull.to_project, @pull))
response.should be_success
end
it 'should be able to perform merge action' do
@pull.check
put :merge, @update_params
response.should be_success
end
let(:pull) { @project.pull_requests.find(@pull) }
@ -146,6 +152,13 @@ shared_examples_for 'user without pull request update rights' do
put :update, @wrong_update_params
pull.issue.body.should_not =='updating'
end
it 'should be able to perform merge action' do
@pull.check
put :merge, @update_params
response.should_not be_success
end
end
shared_examples_for 'pull request when project with issues turned off' do