diff options
author | Kamil Trzciński <ayufan@ayufan.eu> | 2016-02-11 12:04:57 +0000 |
---|---|---|
committer | Kamil Trzciński <ayufan@ayufan.eu> | 2016-02-11 12:04:57 +0000 |
commit | 4eaa6a4c4df729bfad296fa69a35d9089f5bc130 (patch) | |
tree | ac04168b9983fec0af96696dbab4dd23b2bd50e9 | |
parent | 9bcc9ec14cf4f120b8e16d5c232f82e1fc5e2cfd (diff) | |
parent | 9e6190485b2213c9dfdd3d5b22853fc6e2263c2a (diff) | |
download | gitlab-ce-4eaa6a4c4df729bfad296fa69a35d9089f5bc130.tar.gz |
Merge branch 'fix/ci-build-status-badge' into 'master'
Fix CI build status badge
Closes #5570, #4935, #4163
See merge request !2704
-rw-r--r-- | CHANGELOG | 1 | ||||
-rw-r--r-- | app/controllers/ci/projects_controller.rb | 6 | ||||
-rw-r--r-- | app/controllers/projects/badges_controller.rb | 11 | ||||
-rw-r--r-- | app/controllers/projects/builds_controller.rb | 15 | ||||
-rw-r--r-- | app/services/ci/image_for_build_service.rb | 19 | ||||
-rw-r--r-- | config/routes.rb | 8 | ||||
-rw-r--r-- | db/fixtures/development/14_builds.rb | 39 | ||||
-rw-r--r-- | doc/ci/quick_start/README.md | 8 | ||||
-rw-r--r-- | features/project/badges/build.feature | 22 | ||||
-rw-r--r-- | features/steps/project/badges/build.rb | 28 | ||||
-rw-r--r-- | features/steps/shared/builds.rb | 16 |
11 files changed, 133 insertions, 40 deletions
diff --git a/CHANGELOG b/CHANGELOG index ceb5c17cae9..e2a501fb826 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -48,6 +48,7 @@ v 8.5.0 (unreleased) - Title for milestones should be unique (Zeger-Jan van de Weg) - Validate correctness of maximum attachment size application setting - Replaces "Create merge request" link with one to the "Merge Request" when one exists + - Fix CI builds badge, add a new link to builds badge, deprecate the old one v 8.4.4 - Update omniauth-saml gem to 1.4.2 diff --git a/app/controllers/ci/projects_controller.rb b/app/controllers/ci/projects_controller.rb index 711c2847d5e..d1824b481d7 100644 --- a/app/controllers/ci/projects_controller.rb +++ b/app/controllers/ci/projects_controller.rb @@ -12,9 +12,13 @@ module Ci # Project status badge # Image with build status for sha or ref + # + # This action in DEPRECATED, this is here only for backwards compatibility + # with projects migrated from GitLab CI. + # def badge + return render_404 unless @project image = Ci::ImageForBuildService.new.execute(@project, params) - send_file image.path, filename: image.name, disposition: 'inline', type:"image/svg+xml" end diff --git a/app/controllers/projects/badges_controller.rb b/app/controllers/projects/badges_controller.rb new file mode 100644 index 00000000000..a4dd94b941c --- /dev/null +++ b/app/controllers/projects/badges_controller.rb @@ -0,0 +1,11 @@ +class Projects::BadgesController < Projects::ApplicationController + def build + respond_to do |format| + format.html { render_404 } + format.svg do + image = Ci::ImageForBuildService.new.execute(project, ref: params[:ref]) + send_file(image.path, filename: image.name, disposition: 'inline', type: 'image/svg+xml') + end + end + end +end diff --git a/app/controllers/projects/builds_controller.rb b/app/controllers/projects/builds_controller.rb index 9e89296e71d..ec379c53b8f 100644 --- a/app/controllers/projects/builds_controller.rb +++ b/app/controllers/projects/builds_controller.rb @@ -1,10 +1,8 @@ class Projects::BuildsController < Projects::ApplicationController before_action :build, except: [:index, :cancel_all] - before_action :authorize_read_build!, except: [:cancel, :cancel_all, :retry] before_action :authorize_update_build!, except: [:index, :show, :status] - - layout "project" + layout 'project' def index @scope = params[:scope] @@ -24,7 +22,6 @@ class Projects::BuildsController < Projects::ApplicationController def cancel_all @project.builds.running_or_pending.each(&:cancel) - redirect_to namespace_project_builds_path(project.namespace, project) end @@ -47,20 +44,18 @@ class Projects::BuildsController < Projects::ApplicationController end build = Ci::Build.retry(@build) - redirect_to build_path(build) end - def status - render json: @build.to_json(only: [:status, :id, :sha, :coverage], methods: :sha) - end - def cancel @build.cancel - redirect_to build_path(@build) end + def status + render json: @build.to_json(only: [:status, :id, :sha, :coverage], methods: :sha) + end + private def build diff --git a/app/services/ci/image_for_build_service.rb b/app/services/ci/image_for_build_service.rb index f469b13e902..005a5c4661c 100644 --- a/app/services/ci/image_for_build_service.rb +++ b/app/services/ci/image_for_build_service.rb @@ -1,28 +1,23 @@ module Ci class ImageForBuildService - def execute(project, params) - sha = params[:sha] - sha ||= - if params[:ref] - project.commit(params[:ref]).try(:sha) - end + def execute(project, opts) + sha = opts[:sha] || ref_sha(project, opts[:ref]) commit = project.ci_commits.ordered.find_by(sha: sha) image_name = image_for_commit(commit) image_path = Rails.root.join('public/ci', image_name) - - OpenStruct.new( - path: image_path, - name: image_name - ) + OpenStruct.new(path: image_path, name: image_name) end private + def ref_sha(project, ref) + project.commit(ref).try(:sha) if ref + end + def image_for_commit(commit) return 'build-unknown.svg' unless commit - 'build-' + commit.status + ".svg" end end diff --git a/config/routes.rb b/config/routes.rb index 3f6561a1fe0..507bcbc53d7 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -608,7 +608,7 @@ Rails.application.routes.draw do resource :variables, only: [:show, :update] resources :triggers, only: [:index, :create, :destroy] - resources :builds, only: [:index, :show] do + resources :builds, only: [:index, :show], constraints: { id: /\d+/ } do collection do post :cancel_all end @@ -697,6 +697,12 @@ Rails.application.routes.draw do end resources :runner_projects, only: [:create, :destroy] + resources :badges, only: [], path: 'badges/*ref', + constraints: { ref: Gitlab::Regex.git_reference_regex } do + collection do + get :build, constraints: { format: /svg/ } + end + end end end end diff --git a/db/fixtures/development/14_builds.rb b/db/fixtures/development/14_builds.rb index 03a12323845..e3ca2b4eea3 100644 --- a/db/fixtures/development/14_builds.rb +++ b/db/fixtures/development/14_builds.rb @@ -1,24 +1,13 @@ class Gitlab::Seeder::Builds - BUILD_STATUSES = %w(running pending success failed canceled) - def initialize(project) @project = project end def seed! ci_commits.each do |ci_commit| - build = Ci::Build.new(build_attributes_for(ci_commit)) - - artifacts_cache_file(artifacts_archive_path) do |file| - build.artifacts_file = file - end - - artifacts_cache_file(artifacts_metadata_path) do |file| - build.artifacts_metadata = file - end - begin - build.save! + build_create!(ci_commit, name: 'test build 1') + build_create!(ci_commit, status: 'success', name: 'test build 2') print '.' rescue ActiveRecord::RecordInvalid print 'F' @@ -36,6 +25,28 @@ class Gitlab::Seeder::Builds [] end + def build_create!(ci_commit, opts = {}) + attributes = build_attributes_for(ci_commit).merge(opts) + build = Ci::Build.new(attributes) + + if %w(success failed).include?(build.status) + artifacts_cache_file(artifacts_archive_path) do |file| + build.artifacts_file = file + end + + artifacts_cache_file(artifacts_metadata_path) do |file| + build.artifacts_metadata = file + end + end + + build.save! + + if %w(running success failed).include?(build.status) + # We need to set build trace after saving a build (id required) + build.trace = FFaker::Lorem.paragraphs(6).join("\n\n") + end + end + def build_attributes_for(ci_commit) { name: 'test build', commands: "$ build command", stage: 'test', stage_idx: 1, ref: 'master', @@ -49,7 +60,7 @@ class Gitlab::Seeder::Builds end def build_status - BUILD_STATUSES.sample + Ci::Build::AVAILABLE_STATUSES.sample end def artifacts_archive_path diff --git a/doc/ci/quick_start/README.md b/doc/ci/quick_start/README.md index a9b36139de9..ae7b760fa67 100644 --- a/doc/ci/quick_start/README.md +++ b/doc/ci/quick_start/README.md @@ -184,6 +184,14 @@ you expected. You are also able to view the status of any commit in the various pages in GitLab, such as **Commits** and **Merge Requests**. +## Builds badge + +You can access a builds badge image using following link: + +``` +http://example.gitlab.com/namespace/project/badges/branch/build.svg +``` + ## Next steps Awesome! You started using CI in GitLab! diff --git a/features/project/badges/build.feature b/features/project/badges/build.feature new file mode 100644 index 00000000000..9417f62d680 --- /dev/null +++ b/features/project/badges/build.feature @@ -0,0 +1,22 @@ +Feature: Project Badges Build + Background: + Given I sign in as a user + And I own a project + And project has CI enabled + And project has a recent build + + Scenario: I want to see a badge for successfully built project + Given recent build is successful + When I display builds badge for a master branch + Then I should see a build success badge + + Scenario: I want to see a badge for project with failed builds + Given recent build failed + When I display builds badge for a master branch + Then I should see a build failed badge + + Scenario: I want to see a badge for project with running builds + Given recent build is successful + And project has another build that is running + When I display builds badge for a master branch + Then I should see a build running badge diff --git a/features/steps/project/badges/build.rb b/features/steps/project/badges/build.rb new file mode 100644 index 00000000000..cbfc35bed65 --- /dev/null +++ b/features/steps/project/badges/build.rb @@ -0,0 +1,28 @@ +class Spinach::Features::ProjectBadgesBuild < Spinach::FeatureSteps + include SharedAuthentication + include SharedProject + include SharedBuilds + include RepoHelpers + + step 'I display builds badge for a master branch' do + visit build_namespace_project_badges_path(@project.namespace, @project, ref: :master, format: :svg) + end + + step 'I should see a build success badge' do + expect_badge('success') + end + + step 'I should see a build failed badge' do + expect_badge('failed') + end + + step 'I should see a build running badge' do + expect_badge('running') + end + + def expect_badge(status) + svg = Nokogiri::XML.parse(page.body) + expect(page.response_headers).to include('Content-Type' => 'image/svg+xml') + expect(svg.at(%Q{text:contains("#{status}")})).to be_truthy + end +end diff --git a/features/steps/shared/builds.rb b/features/steps/shared/builds.rb index 726e2e814ad..fa54c93df0f 100644 --- a/features/steps/shared/builds.rb +++ b/features/steps/shared/builds.rb @@ -6,8 +6,20 @@ module SharedBuilds end step 'project has a recent build' do - ci_commit = create :ci_commit, project: @project, sha: sample_commit.id - @build = create :ci_build, commit: ci_commit + @ci_commit = create(:ci_commit, project: @project, sha: @project.commit.sha) + @build = create(:ci_build, commit: @ci_commit) + end + + step 'recent build is successful' do + @build.update_column(:status, 'success') + end + + step 'recent build failed' do + @build.update_column(:status, 'failed') + end + + step 'project has another build that is running' do + create(:ci_build, commit: @ci_commit, name: 'second build', status: 'running') end step 'I visit recent build details page' do |