diff options
22 files changed, 439 insertions, 224 deletions
diff --git a/app/assets/javascripts/application.js.coffee b/app/assets/javascripts/application.js.coffee index 75df6e2..297f000 100644 --- a/app/assets/javascripts/application.js.coffee +++ b/app/assets/javascripts/application.js.coffee @@ -19,17 +19,13 @@ # # -$ -> - $('.sync-now').on 'click', -> - $(this).addClass('icon-spin') +$(document).on 'click', '.edit-runner-link', -> + descr = $(this).closest('.runner-description').first() + descr.hide() + descr.next('.runner-description-form').show() - $('.edit-runner-link').on 'click', -> - descr = $(this).closest('.runner-description').first() - descr.hide() - descr.next('.runner-description-form').show() - - $('.assign-all-runner').on 'click', -> - $(this).replaceWith('<i class="icon-refresh icon-spin"></i> Assign in progress..') +$(document).on 'click', '.assign-all-runner', -> + $(this).replaceWith('<i class="icon-refresh icon-spin"></i> Assign in progress..') window.startSpinner = -> $('.turbolink-spinner').fadeIn() diff --git a/app/assets/javascripts/projects.js.coffee b/app/assets/javascripts/projects.js.coffee index 41ee955..4ad500c 100644 --- a/app/assets/javascripts/projects.js.coffee +++ b/app/assets/javascripts/projects.js.coffee @@ -1,6 +1,5 @@ -$ -> - $('.badge-codes-toggle').on 'click', -> - $('.badge-codes-block').toggle() +$(document).on 'click', '.badge-codes-toggle', -> + $('.badge-codes-block').toggle() - $('body').on 'click', '.sync-now', -> - $(this).find('i').addClass('icon-spin') +$(document).on 'click', '.sync-now', -> + $(this).find('i').addClass('icon-spin') diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index b17ba6b..6da86a9 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -45,28 +45,7 @@ class ProjectsController < ApplicationController end def create - @project = Project.parse(params[:project]) - - Project.transaction do - # Send emails only on broken builds be default - @project.email_all_broken_builds = true - - # Disable committer notification by defualt to prevent spamming - @project.email_add_committer = false - - @project.save! - - opts = { - token: @project.token, - project_url: project_url(@project), - } - - if Network.new.enable_ci(current_user.url, @project.gitlab_id, opts, current_user.private_token) - true - else - raise ActiveRecord::Rollback - end - end + @project = CreateProjectService.new.execute(current_user, params[:project], project_url(":project_id")) if @project.persisted? redirect_to project_path(@project, show_guide: true), notice: 'Project was successfully created.' @@ -94,33 +73,21 @@ class ProjectsController < ApplicationController end def build - # Ignore remove branch push - return head(200) if params[:after] =~ /^00000000/ - - build_params = params.dup - @build = @project.register_build(build_params) + @build = CreateBuildService.new.execute(@project, params.dup) - if @build - head 200 - else - head 500 - end - rescue - head 500 + if @build.persisted? + head 201 + else + head 400 + end end # Project status badge # Image with build status for sha or ref def badge - image_name = if params[:sha] - @project.sha_status_image(params[:sha]) - elsif params[:ref] - @project.status_image(params[:ref]) - else - 'unknown.png' - end - - send_file Rails.root.join('public', image_name), filename: image_name, disposition: 'inline' + image = ImageForBuildService.new.execute(@project, params) + + send_file image.path, filename: image.name, disposition: 'inline' end protected diff --git a/app/models/build.rb b/app/models/build.rb index 436479d..6977a2a 100644 --- a/app/models/build.rb +++ b/app/models/build.rb @@ -7,7 +7,7 @@ # ref :string(255) # status :string(255) # finished_at :datetime -# trace :text(2147483647) +# trace :text # created_at :datetime not null # updated_at :datetime not null # sha :string(255) @@ -27,9 +27,11 @@ class Build < ActiveRecord::Base attr_accessible :project_id, :ref, :sha, :before_sha, :status, :finished_at, :trace, :started_at, :push_data, :runner_id, :project_name + validates :before_sha, presence: true validates :sha, presence: true validates :ref, presence: true validates :status, presence: true + validate :valid_commit_sha scope :running, ->() { where(status: "running") } scope :pending, ->() { where(status: "pending") } @@ -91,6 +93,12 @@ class Build < ActiveRecord::Base state :canceled, value: 'canceled' end + def valid_commit_sha + if self.sha =~ /\A00000000/ + self.errors.add(:sha, " cant be 00000000 (branch removal)") + end + end + def compare? gitlab? && before_sha end diff --git a/app/models/project.rb b/app/models/project.rb index 1a72f7e..8b21958 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -2,24 +2,24 @@ # # Table name: projects # -# id :integer not null, primary key -# name :string(255) not null -# timeout :integer default(1800), not null -# scripts :text default(""), not null -# created_at :datetime not null -# updated_at :datetime not null -# token :string(255) -# default_ref :string(255) -# gitlab_url :string(255) -# always_build :boolean default(FALSE), not null -# polling_interval :integer -# public :boolean default(FALSE), not null -# ssh_url_to_repo :string(255) -# gitlab_id :integer -# allow_git_fetch :boolean default(TRUE), not null -# email_recipients :string(255) -# email_add_committer :boolean default(TRUE), not null -# email_all_broken_builds :boolean default(TRUE), not null +# id :integer not null, primary key +# name :string(255) not null +# timeout :integer default(1800), not null +# scripts :text not null +# created_at :datetime not null +# updated_at :datetime not null +# token :string(255) +# default_ref :string(255) +# gitlab_url :string(255) +# always_build :boolean default(FALSE), not null +# polling_interval :integer +# public :boolean default(FALSE), not null +# ssh_url_to_repo :string(255) +# gitlab_id :integer +# allow_git_fetch :boolean default(TRUE), not null +# email_recipients :string(255) default(""), not null +# email_add_committer :boolean default(TRUE), not null +# email_all_broken_builds :boolean default(TRUE), not null # class Project < ActiveRecord::Base @@ -89,29 +89,6 @@ class Project < ActiveRecord::Base self.token = SecureRandom.hex(15) if self.token.blank? end - def register_build(opts={}) - ref = opts[:ref] - - raise 'ref is not defined' unless ref - - if ref.include? 'heads' - ref = ref.scan(/heads\/(.*)$/).flatten[0] - end - - before_sha = opts[:before] - sha = opts[:after] - - data = { - project_id: self.id, - ref: ref, - sha: sha, - before_sha: before_sha, - push_data: opts - } - - @build = Build.create(data) - end - def gitlab? gitlab_url.present? end @@ -144,34 +121,10 @@ class Project < ActiveRecord::Base status end - def status_image ref = 'master' - build = self.builds.where(ref: ref).last - image_for_build build - end - - def last_build_for_sha sha + def last_build_for_sha(sha) builds.where(sha: sha).order('id DESC').limit(1).first end - def sha_status_image sha - build = last_build_for_sha(sha) - image_for_build build - end - - def image_for_build build - return 'unknown.png' unless build - - if build.success? - 'success.png' - elsif build.failed? - 'failed.png' - elsif build.active? - 'running.png' - else - 'unknown.png' - end - end - def tracked_refs @tracked_refs ||= default_ref.split(",").map{|ref| ref.strip} end diff --git a/app/services/create_build_service.rb b/app/services/create_build_service.rb new file mode 100644 index 0000000..3fdf767 --- /dev/null +++ b/app/services/create_build_service.rb @@ -0,0 +1,20 @@ +class CreateBuildService + def execute(project, params) + before_sha = params[:before] + sha = params[:after] + ref = params[:ref] + + if ref && ref.include?('refs/heads/') + ref = ref.scan(/heads\/(.*)$/).flatten[0] + end + + data = { + ref: ref, + sha: sha, + before_sha: before_sha, + push_data: params + } + + project.builds.create(data) + end +end diff --git a/app/services/create_project_service.rb b/app/services/create_project_service.rb new file mode 100644 index 0000000..f21796f --- /dev/null +++ b/app/services/create_project_service.rb @@ -0,0 +1,30 @@ +class CreateProjectService + include Rails.application.routes.url_helpers + + def execute(current_user, params, project_route) + @project = Project.parse(params) + + Project.transaction do + # Send emails only on broken builds be default + @project.email_all_broken_builds = true + + # Disable committer notification by defualt to prevent spamming + @project.email_add_committer = false + + @project.save! + + opts = { + token: @project.token, + project_url: project_route.gsub(":project_id", @project.id.to_s), + } + + if Network.new.enable_ci(current_user.url, @project.gitlab_id, opts, current_user.private_token) + true + else + raise ActiveRecord::Rollback + end + end + + @project + end +end diff --git a/app/services/image_for_build_service.rb b/app/services/image_for_build_service.rb new file mode 100644 index 0000000..425a020 --- /dev/null +++ b/app/services/image_for_build_service.rb @@ -0,0 +1,38 @@ +class ImageForBuildService + def execute(project, params) + if params[:sha] + # Look for last build if commit sha provided + build = project.last_build_for_sha(params[:sha]) + image_name = image_for_build(build) + elsif params[:ref] + # Look for last build per branch + build = project.builds.where(ref: params[:ref]).last + image_name = image_for_build(build) + else + image_name = 'unknown.png' + end + + image_path = Rails.root.join('public', image_name) + + OpenStruct.new( + path: image_path, + name: image_name + ) + end + + private + + def image_for_build(build) + return 'unknown.png' unless build + + if build.success? + 'success.png' + elsif build.failed? + 'failed.png' + elsif build.active? + 'running.png' + else + 'unknown.png' + end + end +end diff --git a/app/views/projects/show.html.haml b/app/views/projects/show.html.haml index f098c29..85574f9 100644 --- a/app/views/projects/show.html.haml +++ b/app/views/projects/show.html.haml @@ -17,13 +17,18 @@ - if @ref %p Paste build status image for #{@ref} with next link - = link_to '#', class: 'badge-codes-toggle btn btn-default btn-sm' do + = link_to '#', class: 'badge-codes-toggle btn btn-default btn-xs' do Status Badge - .badge-codes-block.alert.alert-info - %label Markdown: - = text_field_tag 'badge_md', markdown_badge_code(@project, @ref), readonly: true, class: 'form-control' - %label Html: - = text_field_tag 'badge_html', html_badge_code(@project, @ref), readonly: true, class: 'form-control' + .badge-codes-block.bs-callout.bs-callout-info.hide + %p + Status badge for + %span.label.label-info #{@ref} + branch + %div + %label Markdown: + = text_field_tag 'badge_md', markdown_badge_code(@project, @ref), readonly: true, class: 'form-control' + %label Html: + = text_field_tag 'badge_html', html_badge_code(@project, @ref), readonly: true, class: 'form-control' diff --git a/lib/api/builds.rb b/lib/api/builds.rb index 0982d7d..45b2d35 100644 --- a/lib/api/builds.rb +++ b/lib/api/builds.rb @@ -80,7 +80,7 @@ module API required_attributes! [:project_id, :data, :project_token] project = Project.find(params[:project_id]) authenticate_project_token!(project) - build = project.register_build(params[:data]) + build = CreateBuildService.new.execute(project, params[:data]) if build.persisted? present build, with: Entities::Build diff --git a/spec/controllers/projects_controller_spec.rb b/spec/controllers/projects_controller_spec.rb index 730a9af..b5ec2c2 100644 --- a/spec/controllers/projects_controller_spec.rb +++ b/spec/controllers/projects_controller_spec.rb @@ -15,24 +15,24 @@ describe ProjectsController do expect(response).to be_success - expect(response.code).to eq('200') + expect(response.code).to eq('201') end - it 'should respond 200 if push about removed branch' do + it 'should respond 400 if push about removed branch' do post :build, id: @project.id, ref: 'master', before: '2aa371379db71ac89ae20843fcff3b3477cf1a1d', after: '000000000000000000000000000000000000000', token: @project.token - expect(response).to be_success - expect(response.code).to eq('200') + expect(response).not_to be_success + expect(response.code).to eq('400') end - it 'should respond 500 if something wrong' do + it 'should respond 400 if some params missed' do post :build, id: @project.id, token: @project.token expect(response).not_to be_success - expect(response.code).to eq('500') + expect(response.code).to eq('400') end it 'should respond 403 if token is wrong' do diff --git a/spec/factories/builds.rb b/spec/factories/builds.rb index a313275..2b754af 100644 --- a/spec/factories/builds.rb +++ b/spec/factories/builds.rb @@ -1,8 +1,29 @@ +# == Schema Information +# +# Table name: builds +# +# id :integer not null, primary key +# project_id :integer +# ref :string(255) +# status :string(255) +# finished_at :datetime +# trace :text +# created_at :datetime not null +# updated_at :datetime not null +# sha :string(255) +# started_at :datetime +# tmp_file :string(255) +# before_sha :string(255) +# push_data :text +# runner_id :integer +# + # Read about factories at https://github.com/thoughtbot/factory_girl FactoryGirl.define do factory :build do ref 'master' + before_sha '76de212e80737a608d939f648d959671fb0a0142' sha '97de212e80737a608d939f648d959671fb0a0142' started_at 'Di 29. Okt 09:51:28 CET 2013' finished_at 'Di 29. Okt 09:53:28 CET 2013' diff --git a/spec/factories/projects.rb b/spec/factories/projects.rb index 41c2506..1d4b912 100644 --- a/spec/factories/projects.rb +++ b/spec/factories/projects.rb @@ -1,3 +1,27 @@ +# == Schema Information +# +# Table name: projects +# +# id :integer not null, primary key +# name :string(255) not null +# timeout :integer default(1800), not null +# scripts :text not null +# created_at :datetime not null +# updated_at :datetime not null +# token :string(255) +# default_ref :string(255) +# gitlab_url :string(255) +# always_build :boolean default(FALSE), not null +# polling_interval :integer +# public :boolean default(FALSE), not null +# ssh_url_to_repo :string(255) +# gitlab_id :integer +# allow_git_fetch :boolean default(TRUE), not null +# email_recipients :string(255) default(""), not null +# email_add_committer :boolean default(TRUE), not null +# email_all_broken_builds :boolean default(TRUE), not null +# + # Read about factories at https://github.com/thoughtbot/factory_girl FactoryGirl.define do diff --git a/spec/factories/runner_projects.rb b/spec/factories/runner_projects.rb index e43adcf..d72c525 100644 --- a/spec/factories/runner_projects.rb +++ b/spec/factories/runner_projects.rb @@ -1,3 +1,14 @@ +# == Schema Information +# +# Table name: runner_projects +# +# id :integer not null, primary key +# runner_id :integer not null +# project_id :integer not null +# created_at :datetime not null +# updated_at :datetime not null +# + # Read about factories at https://github.com/thoughtbot/factory_girl FactoryGirl.define do diff --git a/spec/factories/runners.rb b/spec/factories/runners.rb index de4c6cc..9233ee3 100644 --- a/spec/factories/runners.rb +++ b/spec/factories/runners.rb @@ -1,3 +1,15 @@ +# == Schema Information +# +# Table name: runners +# +# id :integer not null, primary key +# token :string(255) +# public_key :text +# created_at :datetime not null +# updated_at :datetime not null +# description :string(255) +# + # Read about factories at https://github.com/thoughtbot/factory_girl FactoryGirl.define do diff --git a/spec/models/build_spec.rb b/spec/models/build_spec.rb index 32a0446..2e2dded 100644 --- a/spec/models/build_spec.rb +++ b/spec/models/build_spec.rb @@ -7,7 +7,7 @@ # ref :string(255) # status :string(255) # finished_at :datetime -# trace :text(2147483647) +# trace :text # created_at :datetime not null # updated_at :datetime not null # sha :string(255) @@ -24,6 +24,7 @@ describe Build do subject { Build.new } it { should belong_to(:project) } + it { should validate_presence_of :before_sha } it { should validate_presence_of :sha } it { should validate_presence_of :ref } it { should validate_presence_of :status } @@ -39,7 +40,7 @@ describe Build do describe "#ci_skip?" do let(:project) { FactoryGirl.create(:project) } - let(:build) { project.register_build(ref: 'master') } + let(:build) { FactoryGirl.create(:build, project: project) } it 'true if commit message contains [ci skip]' do build.stub(:git_commit_message) { 'Small typo [ci skip]' } diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index df45bea..62696f6 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -2,24 +2,24 @@ # # Table name: projects # -# id :integer not null, primary key -# name :string(255) not null -# timeout :integer default(1800), not null -# scripts :text default(""), not null -# created_at :datetime not null -# updated_at :datetime not null -# token :string(255) -# default_ref :string(255) -# gitlab_url :string(255) -# always_build :boolean default(FALSE), not null -# polling_interval :integer -# public :boolean default(FALSE), not null -# ssh_url_to_repo :string(255) -# gitlab_id :integer -# allow_git_fetch :boolean default(TRUE), not null -# email_recipients :string(255) -# email_add_committer :boolean default(TRUE), not null -# email_only_breaking_build :boolean default(TRUE), not null +# id :integer not null, primary key +# name :string(255) not null +# timeout :integer default(1800), not null +# scripts :text not null +# created_at :datetime not null +# updated_at :datetime not null +# token :string(255) +# default_ref :string(255) +# gitlab_url :string(255) +# always_build :boolean default(FALSE), not null +# polling_interval :integer +# public :boolean default(FALSE), not null +# ssh_url_to_repo :string(255) +# gitlab_id :integer +# allow_git_fetch :boolean default(TRUE), not null +# email_recipients :string(255) default(""), not null +# email_add_committer :boolean default(TRUE), not null +# email_all_broken_builds :boolean default(TRUE), not null # require 'spec_helper' @@ -29,6 +29,11 @@ describe Project do it { should have_many(:builds) } + it { should validate_presence_of :name } + it { should validate_presence_of :scripts } + it { should validate_presence_of :timeout } + it { should validate_presence_of :default_ref } + describe 'before_validation' do it 'should set an random token if none provided' do project = FactoryGirl.create :project_without_token @@ -41,30 +46,15 @@ describe Project do end end - it { should validate_presence_of :name } - it { should validate_presence_of :scripts } - it { should validate_presence_of :timeout } - it { should validate_presence_of :default_ref } - context :valid_project do let(:project) { FactoryGirl.create :project } - describe :register_build do - let(:build) { project.register_build(ref: 'master', after: '31das312') } - - it { build.should be_kind_of(Build) } - it { build.should be_pending } - it { build.should be_valid } - it { build.should == project.last_build } - end - context :project_with_build do - before { project.register_build ref: 'master', after: '31das312' } + before { FactoryGirl.create(:build, project: project) } it { project.status.should == 'pending' } it { project.last_build.should be_kind_of(Build) } it { project.human_status.should == 'pending' } - it { project.status_image.should == 'running.png' } end end @@ -86,7 +76,6 @@ describe Project do end describe '#broken_or_success?' do - it { project = FactoryGirl.create :project, email_add_committer: true project.stub(:broken?).and_return(true) @@ -114,7 +103,18 @@ describe Project do project.stub(:success?).and_return(false) project.broken_or_success?.should == false } - end + end + + describe 'Project.parse' do + let(:project_dump) { File.read(Rails.root.join('spec/support/gitlab_stubs/raw_project.yml')) } + let(:parsed_project) { Project.parse(project_dump) } + + it { parsed_project.should be_valid } + it { parsed_project.should be_kind_of(Project) } + it { parsed_project.name.should eq("GitLab / api.gitlab.org") } + it { parsed_project.gitlab_id.should eq(189) } + it { parsed_project.gitlab_url.should eq("http://localhost:3000/gitlab/api-gitlab-org") } + end end # == Schema Information diff --git a/spec/services/create_build_service_spec.rb b/spec/services/create_build_service_spec.rb new file mode 100644 index 0000000..11b4588 --- /dev/null +++ b/spec/services/create_build_service_spec.rb @@ -0,0 +1,27 @@ +require 'spec_helper' + +describe CreateBuildService do + let(:service) { CreateBuildService.new } + let(:project) { FactoryGirl.create(:project) } + + describe :execute do + context 'valid params' do + let(:build) { service.execute(project, ref: 'refs/heads/master', before: '00000000', after: '31das312') } + + it { build.should be_kind_of(Build) } + it { build.should be_pending } + it { build.should be_valid } + it { build.should be_persisted } + it { build.should == project.last_build } + end + + context 'without params' do + let(:build) { service.execute(project, {}) } + + it { build.should be_kind_of(Build) } + it { build.should be_pending } + it { build.should_not be_valid } + it { build.should_not be_persisted } + end + end +end diff --git a/spec/services/create_project_service_spec.rb b/spec/services/create_project_service_spec.rb new file mode 100644 index 0000000..7046830 --- /dev/null +++ b/spec/services/create_project_service_spec.rb @@ -0,0 +1,24 @@ +require 'spec_helper' + +describe CreateProjectService do + let(:service) { CreateProjectService.new } + let(:current_user) { double.as_null_object } + let(:project_dump) { File.read(Rails.root.join('spec/support/gitlab_stubs/raw_project.yml')) } + + before { Network.any_instance.stub(enable_ci: true) } + + describe :execute do + context 'valid params' do + let(:project) { service.execute(current_user, project_dump, 'http://localhost/projects/:project_id') } + + it { project.should be_kind_of(Project) } + it { project.should be_persisted } + end + + context 'without project dump' do + it 'should raise exception' do + expect { service.execute(current_user, '', '') }.to raise_error + end + end + end +end diff --git a/spec/services/image_for_build_service_spec.rb b/spec/services/image_for_build_service_spec.rb new file mode 100644 index 0000000..2a65c48 --- /dev/null +++ b/spec/services/image_for_build_service_spec.rb @@ -0,0 +1,43 @@ +require 'spec_helper' + +describe ImageForBuildService do + let(:service) { ImageForBuildService.new } + let(:project) { FactoryGirl.create(:project) } + let(:build) { FactoryGirl.create(:build, project: project, ref: 'master') } + + describe :execute do + before { build } + + context 'branch name' do + let(:image) { service.execute(project, ref: 'master') } + + it { image.should be_kind_of(OpenStruct) } + it { image.path.to_s.should include('public/running.png') } + it { image.name.should == 'running.png' } + end + + context 'unknown branch name' do + let(:image) { service.execute(project, ref: 'feature') } + + it { image.should be_kind_of(OpenStruct) } + it { image.path.to_s.should include('public/unknown.png') } + it { image.name.should == 'unknown.png' } + end + + context 'commit sha' do + let(:image) { service.execute(project, sha: build.sha) } + + it { image.should be_kind_of(OpenStruct) } + it { image.path.to_s.should include('public/running.png') } + it { image.name.should == 'running.png' } + end + + context 'unknown commit sha' do + let(:image) { service.execute(project, sha: '0000000') } + + it { image.should be_kind_of(OpenStruct) } + it { image.path.to_s.should include('public/unknown.png') } + it { image.name.should == 'unknown.png' } + end + end +end diff --git a/spec/support/gitlab_stubs/raw_project.yml b/spec/support/gitlab_stubs/raw_project.yml new file mode 100644 index 0000000..df2ce22 --- /dev/null +++ b/spec/support/gitlab_stubs/raw_project.yml @@ -0,0 +1,36 @@ +--- !ruby/object:OpenStruct +table: + :id: 189 + :description: Website at http://api.gitlab.org/ + :default_branch: master + :public: false + :visibility_level: 0 + :ssh_url_to_repo: dzaporozhets@localhost:gitlab/api-gitlab-org.git + :http_url_to_repo: http://localhost:3000/gitlab/api-gitlab-org.git + :web_url: http://localhost:3000/gitlab/api-gitlab-org + :owner: + id: 1 + name: GitLab + created_at: '2012-10-03T09:59:57.000Z' + :name: api.gitlab.org + :name_with_namespace: GitLab / api.gitlab.org + :path: api-gitlab-org + :path_with_namespace: gitlab/api-gitlab-org + :issues_enabled: true + :merge_requests_enabled: true + :wall_enabled: false + :wiki_enabled: false + :snippets_enabled: false + :created_at: '2013-06-06T12:29:39.000Z' + :last_activity_at: '2013-12-06T20:29:42.000Z' + :namespace: + id: 1 + name: GitLab + path: gitlab + owner_id: 1 + created_at: '2012-10-03T09:59:57.000Z' + updated_at: '2014-01-28T08:49:53.000Z' + description: Self hosted Git management software + avatar: + url: /uploads/group/avatar/1/0-vader-profile.jpg + diff --git a/spec/support/stub_gitlab_calls.rb b/spec/support/stub_gitlab_calls.rb index cb92bc8..25ff1a1 100644 --- a/spec/support/stub_gitlab_calls.rb +++ b/spec/support/stub_gitlab_calls.rb @@ -6,57 +6,57 @@ module StubGitlabCalls stub_projects stub_projects_owned end - + def stub_js_gitlab_calls Network.any_instance.stub(:projects) { project_hash_array } end private - def gitlab_url - GitlabCi.config.allowed_gitlab_urls.first - end - - def stub_session - f = File.read(Rails.root.join('spec/support/gitlab_stubs/session.json')) - - stub_request(:post, "#{gitlab_url}api/v3/session.json"). - with(:body => "{\"email\":\"test@test.com\",\"password\":\"123456\"}", - :headers => {'Content-Type'=>'application/json'}). - to_return(:status => 201, :body => f, :headers => {'Content-Type'=>'application/json'}) - end - - def stub_user - f = File.read(Rails.root.join('spec/support/gitlab_stubs/user.json')) - - stub_request(:get, "#{gitlab_url}api/v3/user.json?private_token=Wvjy2Krpb7y8xi93owUz"). - with(:headers => {'Content-Type'=>'application/json'}). - to_return(:status => 200, :body => f, :headers => {'Content-Type'=>'application/json'}) - end - - def stub_project_8 - f = File.read(Rails.root.join('spec/support/gitlab_stubs/project_8.json')) - - stub_request(:get, "#{gitlab_url}api/v3/projects/8.json?private_token=Wvjy2Krpb7y8xi93owUz"). - with(:headers => {'Content-Type'=>'application/json'}). - to_return(:status => 200, :body => f, :headers => {'Content-Type'=>'application/json'}) - end - - def stub_projects - f = File.read(Rails.root.join('spec/support/gitlab_stubs/projects.json')) - stub_request(:get, "#{gitlab_url}api/v3/projects.json?private_token=Wvjy2Krpb7y8xi93owUz"). - with(:headers => {'Content-Type'=>'application/json'}). - to_return(:status => 200, :body => f, :headers => {'Content-Type'=>'application/json'}) - end - - def stub_projects_owned - stub_request(:get, "#{gitlab_url}api/v3/projects/owned.json?private_token=Wvjy2Krpb7y8xi93owUz"). - with(:headers => {'Content-Type'=>'application/json'}). - to_return(:status => 200, :body => "", :headers => {}) - end - - def project_hash_array - f = File.read(Rails.root.join('spec/support/gitlab_stubs/projects.json')) - return JSON.parse f - end -end
\ No newline at end of file + def gitlab_url + GitlabCi.config.allowed_gitlab_urls.first + end + + def stub_session + f = File.read(Rails.root.join('spec/support/gitlab_stubs/session.json')) + + stub_request(:post, "#{gitlab_url}api/v3/session.json"). + with(:body => "{\"email\":\"test@test.com\",\"password\":\"123456\"}", + :headers => {'Content-Type'=>'application/json'}). + to_return(:status => 201, :body => f, :headers => {'Content-Type'=>'application/json'}) + end + + def stub_user + f = File.read(Rails.root.join('spec/support/gitlab_stubs/user.json')) + + stub_request(:get, "#{gitlab_url}api/v3/user.json?private_token=Wvjy2Krpb7y8xi93owUz"). + with(:headers => {'Content-Type'=>'application/json'}). + to_return(:status => 200, :body => f, :headers => {'Content-Type'=>'application/json'}) + end + + def stub_project_8 + f = File.read(Rails.root.join('spec/support/gitlab_stubs/project_8.json')) + + stub_request(:get, "#{gitlab_url}api/v3/projects/8.json?private_token=Wvjy2Krpb7y8xi93owUz"). + with(:headers => {'Content-Type'=>'application/json'}). + to_return(:status => 200, :body => f, :headers => {'Content-Type'=>'application/json'}) + end + + def stub_projects + f = File.read(Rails.root.join('spec/support/gitlab_stubs/projects.json')) + stub_request(:get, "#{gitlab_url}api/v3/projects.json?private_token=Wvjy2Krpb7y8xi93owUz"). + with(:headers => {'Content-Type'=>'application/json'}). + to_return(:status => 200, :body => f, :headers => {'Content-Type'=>'application/json'}) + end + + def stub_projects_owned + stub_request(:get, "#{gitlab_url}api/v3/projects/owned.json?private_token=Wvjy2Krpb7y8xi93owUz"). + with(:headers => {'Content-Type'=>'application/json'}). + to_return(:status => 200, :body => "", :headers => {}) + end + + def project_hash_array + f = File.read(Rails.root.join('spec/support/gitlab_stubs/projects.json')) + return JSON.parse f + end +end |