From 7c1ab2cddf82b97cfd03b3ca3290017129c01b71 Mon Sep 17 00:00:00 2001 From: Jasder <2053003901@@qq.com> Date: Thu, 20 Aug 2020 18:37:53 +0800 Subject: [PATCH] =?UTF-8?q?FIX=20=E9=87=8D=E6=9E=84ci=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 28 +++++++------------ app/controllers/ci/base_controller.rb | 7 +++++ app/controllers/ci/builds_controller.rb | 18 ++++++------ app/helpers/ci/builds_helper.rb | 10 +++++++ app/helpers/ci/languages_helper.rb | 2 ++ app/helpers/dev_ops/builds_helper.rb | 2 -- app/helpers/dev_ops/languages_helper.rb | 2 -- app/libs/ci/drone/server.rb | 10 +++---- app/models/ci.rb | 6 ++-- app/models/ci/build.rb | 6 ++++ app/models/ci/cloud_account.rb | 3 +- app/models/ci/language.rb | 2 +- app/models/ci/local_base.rb | 5 ++++ app/models/ci/remote_base.rb | 7 +++++ app/models/ci/repo.rb | 19 +++++++++++++ app/models/ci/stage.rb | 6 ++++ app/models/ci/step.rb | 5 ++++ app/models/ci/user.rb | 6 ++++ app/queries/ci/builds/list_query.rb | 18 ++++++++++++ app/views/ci/builds/_build.json.jbuilder | 15 ++++++++++ app/views/ci/builds/_stage.json.jbuilder | 22 +++++++++++++++ app/views/ci/builds/_step.json.jbuilder | 8 ++++++ app/views/ci/builds/index.json.jbuilder | 4 +++ app/views/ci/builds/show.json.jbuilder | 4 +++ .../languages/common.json.jbuilder | 0 .../languages/index.json.jbuilder | 0 .../languages/show.json.jbuilder | 0 app/views/dev_ops/builds/index.json.jbuilder | 2 -- app/views/dev_ops/builds/show.json.jbuilder | 2 -- config/routes.rb | 22 +++++++++------ spec/models/ci/build_spec.rb | 5 ++++ spec/models/ci/repo_spec.rb | 5 ++++ spec/models/ci/stage_spec.rb | 5 ++++ spec/models/ci/step_spec.rb | 5 ++++ 34 files changed, 207 insertions(+), 54 deletions(-) create mode 100644 app/helpers/ci/builds_helper.rb create mode 100644 app/helpers/ci/languages_helper.rb delete mode 100644 app/helpers/dev_ops/builds_helper.rb delete mode 100644 app/helpers/dev_ops/languages_helper.rb create mode 100644 app/models/ci/build.rb create mode 100644 app/models/ci/local_base.rb create mode 100644 app/models/ci/remote_base.rb create mode 100644 app/models/ci/repo.rb create mode 100644 app/models/ci/stage.rb create mode 100644 app/models/ci/step.rb create mode 100644 app/models/ci/user.rb create mode 100644 app/queries/ci/builds/list_query.rb create mode 100644 app/views/ci/builds/_build.json.jbuilder create mode 100644 app/views/ci/builds/_stage.json.jbuilder create mode 100644 app/views/ci/builds/_step.json.jbuilder create mode 100644 app/views/ci/builds/index.json.jbuilder create mode 100644 app/views/ci/builds/show.json.jbuilder rename app/views/{dev_ops => ci}/languages/common.json.jbuilder (100%) rename app/views/{dev_ops => ci}/languages/index.json.jbuilder (100%) rename app/views/{dev_ops => ci}/languages/show.json.jbuilder (100%) delete mode 100644 app/views/dev_ops/builds/index.json.jbuilder delete mode 100644 app/views/dev_ops/builds/show.json.jbuilder create mode 100644 spec/models/ci/build_spec.rb create mode 100644 spec/models/ci/repo_spec.rb create mode 100644 spec/models/ci/stage_spec.rb create mode 100644 spec/models/ci/step_spec.rb diff --git a/README.md b/README.md index 422b665f3..d24d3d83f 100644 --- a/README.md +++ b/README.md @@ -2695,22 +2695,15 @@ curl -X GET http://localhost:3000/api/dev_ops/languages/114.json | jq #### 获取构建列表 ``` -GET /api/dev_ops/builds +GET /api/:owner/:repo/builds ``` *示例* ``` curl -X GET \ --d 'project_id=4844' \ -http://localhost:3000/api/dev_ops/builds | jq +http://localhost:3000/api/Jason/forge/builds | jq ``` -*请求参数说明:* - -|参数名|必选|类型|说明| -|-|-|-|-| -|project_id |是|int |project's id | - *返回参数说明:* |参数名|类型|说明| @@ -2745,10 +2738,11 @@ http://localhost:3000/api/dev_ops/builds | jq "author_email": "email.com", "author_avatar": "", "sender": "jasder", - "started": 1595317786, - "finished": 1595318426, - "created": 1595317786, - "updated": 1595317786, + "started": "2020-08-19 06:22", + "finished": "2020-08-19 06:22", + "created": "2020-08-19 06:22", + "updated": "2020-08-19 06:22", + "duration_time": 0, "version": 3 } ] @@ -2757,22 +2751,20 @@ http://localhost:3000/api/dev_ops/builds | jq #### 获取某条构建详情信息 ``` -GET /api/dev_ops/builds/:number +GET /api/:owner/:repo/builds/:build ``` *示例* ``` curl -X GET \ --d 'project_id=4844' \ -http://ocalhost:3000/api/dev_ops/builds/1 | jq +http://ocalhost:3000/api/jasder/forge/builds/1 | jq ``` *请求参数说明:* |参数名|必选|类型|说明| |-|-|-|-| -|project_id |int|project's id| -|number |是|int |build's number | +|build |是|int |build's number | *返回参数说明:* diff --git a/app/controllers/ci/base_controller.rb b/app/controllers/ci/base_controller.rb index edbd5b8a4..8212ff9a1 100644 --- a/app/controllers/ci/base_controller.rb +++ b/app/controllers/ci/base_controller.rb @@ -1,3 +1,10 @@ class Ci::BaseController < ApplicationController before_action :require_login + + def load_repo + namespace = params[:owner] + id = params[:repo] || params[:id] + + @user, @repo = Ci::Repo.find_with_namespace(namespace, id) + end end diff --git a/app/controllers/ci/builds_controller.rb b/app/controllers/ci/builds_controller.rb index 1a5552485..ff49c3ee2 100644 --- a/app/controllers/ci/builds_controller.rb +++ b/app/controllers/ci/builds_controller.rb @@ -2,14 +2,18 @@ class Ci::BuildsController < Ci::BaseController include RepositoriesHelper include Devopsable - before_action :find_project, :find_cloud_account - before_action :find_cloud_account, except: :get_trustie_pipeline - before_action :ci_authorize! + before_action :load_repo def index - result = Ci::Drone::API.new(@cloud_account.drone_token, @cloud_account.drone_url, @project.owner.login, @project.identifier).builds + scope = @repo.builds - render json: result + scope = Ci::Builds::ListQuery.call(@repo, params) + @total_count = scope.map(&:build_id).size + @builds = paginate scope + end + + def show + @build = @repo.builds.includes(stages: [:steps]).find_by(build_number: params[:build]) end def detail @@ -49,10 +53,6 @@ class Ci::BuildsController < Ci::BaseController end private - def find_project - @project = Project.find params[:project_id] - end - def find_cloud_account @cloud_account = @project.ci_cloud_account end diff --git a/app/helpers/ci/builds_helper.rb b/app/helpers/ci/builds_helper.rb new file mode 100644 index 000000000..41c92229a --- /dev/null +++ b/app/helpers/ci/builds_helper.rb @@ -0,0 +1,10 @@ +module Ci::BuildsHelper + def format_utc_time(unix_time) + Rails.logger.info "00000000000000 #{unix_time}" + DateTime.strptime(unix_time.to_s,'%s').strftime("%Y-%m-%d %H:%M") + end + + def render_duartion_time(end_time, start_time) + (end_time - start_time) / 10000 + end +end diff --git a/app/helpers/ci/languages_helper.rb b/app/helpers/ci/languages_helper.rb new file mode 100644 index 000000000..dc4fa8978 --- /dev/null +++ b/app/helpers/ci/languages_helper.rb @@ -0,0 +1,2 @@ +module Ci::LanguagesHelper +end diff --git a/app/helpers/dev_ops/builds_helper.rb b/app/helpers/dev_ops/builds_helper.rb deleted file mode 100644 index 81d98403b..000000000 --- a/app/helpers/dev_ops/builds_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module DevOps::BuildsHelper -end diff --git a/app/helpers/dev_ops/languages_helper.rb b/app/helpers/dev_ops/languages_helper.rb deleted file mode 100644 index 00b1b6183..000000000 --- a/app/helpers/dev_ops/languages_helper.rb +++ /dev/null @@ -1,2 +0,0 @@ -module DevOps::LanguagesHelper -end diff --git a/app/libs/ci/drone/server.rb b/app/libs/ci/drone/server.rb index 60fbfcb8e..2f0dad502 100644 --- a/app/libs/ci/drone/server.rb +++ b/app/libs/ci/drone/server.rb @@ -40,23 +40,23 @@ class Ci::Drone::Server end def database_username - database_config[Rails.env]["ci"]["username"] + database_config[Rails.env]["ci_server_db"]["username"] end def database_password - database_config[Rails.env]["ci"]["password"] + database_config[Rails.env]["ci_server_db"]["password"] end def database_host - database_config[Rails.env]["ci"]["host"] + database_config[Rails.env]["ci_server_db"]["host"] end def database_port - database_config[Rails.env]["ci"]["port"] || 3306 + database_config[Rails.env]["ci_server_db"]["port"] || 3306 end def database - database_config[Rails.env]["ci"]["database"] + database_config[Rails.env]["ci_server_db"]["database"] end def database_config diff --git a/app/models/ci.rb b/app/models/ci.rb index 50a7a6279..2bce3b852 100644 --- a/app/models/ci.rb +++ b/app/models/ci.rb @@ -1,5 +1,5 @@ module Ci - def self.table_name_prefix - 'ci_' - end + # def self.table_name_prefix + # 'ci_' + # end end diff --git a/app/models/ci/build.rb b/app/models/ci/build.rb new file mode 100644 index 000000000..b119c9d98 --- /dev/null +++ b/app/models/ci/build.rb @@ -0,0 +1,6 @@ +class Ci::Build < Ci::RemoteBase + self.primary_key = 'build_id' + + belongs_to :repo, foreign_key: :build_repo_id + has_many :stages, foreign_key: "stage_build_id", dependent: :destroy +end diff --git a/app/models/ci/cloud_account.rb b/app/models/ci/cloud_account.rb index 212aab639..30f490797 100644 --- a/app/models/ci/cloud_account.rb +++ b/app/models/ci/cloud_account.rb @@ -1,4 +1,5 @@ -class Ci::CloudAccount < ApplicationRecord +class Ci::CloudAccount < Local::Base + belongs_to :project belongs_to :user diff --git a/app/models/ci/language.rb b/app/models/ci/language.rb index 0d16e5bd2..97c4a57d2 100644 --- a/app/models/ci/language.rb +++ b/app/models/ci/language.rb @@ -1,4 +1,4 @@ -class Ci::Language < ApplicationRecord +class Ci::Language < Local::Base # before_save :encode_content belongs_to :cover, class_name: "Attachment", foreign_key: :cover_id, optional: true diff --git a/app/models/ci/local_base.rb b/app/models/ci/local_base.rb new file mode 100644 index 000000000..e5d8968de --- /dev/null +++ b/app/models/ci/local_base.rb @@ -0,0 +1,5 @@ +class Local::Base < ApplicationRecord + def self.table_name_prefix + "ci_" + end +end diff --git a/app/models/ci/remote_base.rb b/app/models/ci/remote_base.rb new file mode 100644 index 000000000..ba2f5bbb1 --- /dev/null +++ b/app/models/ci/remote_base.rb @@ -0,0 +1,7 @@ +class Ci::RemoteBase < ApplicationRecord + self.abstract_class = true + + establish_connection Rails.configuration.database_configuration[Rails.env]["ci_server_db"] + + +end diff --git a/app/models/ci/repo.rb b/app/models/ci/repo.rb new file mode 100644 index 000000000..83092d9cd --- /dev/null +++ b/app/models/ci/repo.rb @@ -0,0 +1,19 @@ +class Ci::Repo < Ci::RemoteBase + self.primary_key = 'repo_id' + + belongs_to :user, foreign_key: :repo_user_id + has_many :builds, foreign_key: "build_repo_id", dependent: :destroy + + def self.find_with_namespace(namespace_path, identifier) + logger.info "########namespace_path: #{namespace_path} ########identifier: #{identifier} " + + user = Ci::User.find_by_user_login namespace_path + return nil if user.blank? + + repo = user.repos.find_by(repo_name: identifier) + + return nil if repo.blank? + [user, repo] + end + +end diff --git a/app/models/ci/stage.rb b/app/models/ci/stage.rb new file mode 100644 index 000000000..2309f05d4 --- /dev/null +++ b/app/models/ci/stage.rb @@ -0,0 +1,6 @@ +class Ci::Stage < Ci::RemoteBase + self.primary_key = 'stage_id' + + belongs_to :build, foreign_key: :stage_build_id + has_many :steps, foreign_key: "step_stage_id", dependent: :destroy +end diff --git a/app/models/ci/step.rb b/app/models/ci/step.rb new file mode 100644 index 000000000..23d540314 --- /dev/null +++ b/app/models/ci/step.rb @@ -0,0 +1,5 @@ +class Ci::Step < Ci::RemoteBase + self.primary_key = 'step_id' + + belongs_to :stage, foreign_key: :step_stage_id +end diff --git a/app/models/ci/user.rb b/app/models/ci/user.rb new file mode 100644 index 000000000..90387b492 --- /dev/null +++ b/app/models/ci/user.rb @@ -0,0 +1,6 @@ +class Ci::User < Ci::RemoteBase + self.primary_key = 'user_id' + + has_many :repos, foreign_key: "repo_user_id", dependent: :destroy + +end diff --git a/app/queries/ci/builds/list_query.rb b/app/queries/ci/builds/list_query.rb new file mode 100644 index 000000000..b86fbd7da --- /dev/null +++ b/app/queries/ci/builds/list_query.rb @@ -0,0 +1,18 @@ +class Ci::Builds::ListQuery < ApplicationQuery + include CustomSortable + + attr_reader :params + + sort_columns :build_created, default_by: :build_created, default_direction: :desc + + def initialize(repo, params) + @repo = repo + @params = params + end + + def call + course_lists = @repo.builds + + custom_sort(course_lists, params[:sort_by], params[:sort_direction]) + end +end diff --git a/app/views/ci/builds/_build.json.jbuilder b/app/views/ci/builds/_build.json.jbuilder new file mode 100644 index 000000000..222d922fc --- /dev/null +++ b/app/views/ci/builds/_build.json.jbuilder @@ -0,0 +1,15 @@ +json.id build.build_id +json.repo_id build.build_repo_id +json.number build.build_number +json.status build.build_status +json.event build.build_event +json.action build.build_action +# json.link build.build_link +json.message build.build_message +json.author build.build_author +json.started format_utc_time build.build_started +json.finished format_utc_time build.build_finished +json.created format_utc_time build.build_created +json.updated format_utc_time build.build_updated +json.version build.build_version +json.duration_time render_duartion_time(build.build_finished, build.build_started) diff --git a/app/views/ci/builds/_stage.json.jbuilder b/app/views/ci/builds/_stage.json.jbuilder new file mode 100644 index 000000000..c2a079915 --- /dev/null +++ b/app/views/ci/builds/_stage.json.jbuilder @@ -0,0 +1,22 @@ +json.id stage.stage_id +json.repo_id stage.stage_repo_id +json.build_id stage.stage_build_id +json.name stage.stage_name +json.kind stage.stage_kind +json.type stage.stage_type +json.number stage.stage_number +json.status stage.stage_status +json.errignore stage.stage_errignore +json.exit_code stage.stage_exit_code +json.os stage.stage_os +json.arch stage.stage_arch +json.started format_utc_time(stage.stage_started) +json.stopped format_utc_time(stage.stage_stoped) +json.created format_utc_time(stage.stage_created) +json.updated format_utc_time(stage.stage_updated) +json.version stage.stage_version +json.on_success stage.stage_on_success +json.on_failure stage.stage_on_failure +json.steps build.steps do |stage| + json.partial! "/ci/builds/step", step: step +end diff --git a/app/views/ci/builds/_step.json.jbuilder b/app/views/ci/builds/_step.json.jbuilder new file mode 100644 index 000000000..097b4cf2c --- /dev/null +++ b/app/views/ci/builds/_step.json.jbuilder @@ -0,0 +1,8 @@ +json.id step.step_id +json.number step.step_number +json.name step.step_name +json.status step.step_status +json.exit_code step.step_exit_code +json.started format_utc_time(step.step_started) +json.stopped format_utc_time(step.step_stopped) +json.version step.build_version diff --git a/app/views/ci/builds/index.json.jbuilder b/app/views/ci/builds/index.json.jbuilder new file mode 100644 index 000000000..40fc515c5 --- /dev/null +++ b/app/views/ci/builds/index.json.jbuilder @@ -0,0 +1,4 @@ +json.total_count @total_count +json.builds @builds do |build| + json.partial! "/ci/builds/build", build: build +end diff --git a/app/views/ci/builds/show.json.jbuilder b/app/views/ci/builds/show.json.jbuilder new file mode 100644 index 000000000..0c2b21e2e --- /dev/null +++ b/app/views/ci/builds/show.json.jbuilder @@ -0,0 +1,4 @@ +json.partial! "/ci/builds/build", build: @build +json.stages @build.stages do |stage| + json.partial! "/ci/builds/stage", stage: stage +end diff --git a/app/views/dev_ops/languages/common.json.jbuilder b/app/views/ci/languages/common.json.jbuilder similarity index 100% rename from app/views/dev_ops/languages/common.json.jbuilder rename to app/views/ci/languages/common.json.jbuilder diff --git a/app/views/dev_ops/languages/index.json.jbuilder b/app/views/ci/languages/index.json.jbuilder similarity index 100% rename from app/views/dev_ops/languages/index.json.jbuilder rename to app/views/ci/languages/index.json.jbuilder diff --git a/app/views/dev_ops/languages/show.json.jbuilder b/app/views/ci/languages/show.json.jbuilder similarity index 100% rename from app/views/dev_ops/languages/show.json.jbuilder rename to app/views/ci/languages/show.json.jbuilder diff --git a/app/views/dev_ops/builds/index.json.jbuilder b/app/views/dev_ops/builds/index.json.jbuilder deleted file mode 100644 index b6539104b..000000000 --- a/app/views/dev_ops/builds/index.json.jbuilder +++ /dev/null @@ -1,2 +0,0 @@ -
Find me in app/views/dev_ops/builds/index.html.erb
diff --git a/app/views/dev_ops/builds/show.json.jbuilder b/app/views/dev_ops/builds/show.json.jbuilder deleted file mode 100644 index f25a17b7b..000000000 --- a/app/views/dev_ops/builds/show.json.jbuilder +++ /dev/null @@ -1,2 +0,0 @@ -Find me in app/views/dev_ops/builds/show.html.erb
diff --git a/config/routes.rb b/config/routes.rb index 57f305776..68c4edd19 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -31,15 +31,15 @@ Rails.application.routes.draw do get :common end end - resources :builds, only: :index do - collection do - get 'get_trustie_pipeline', to: 'builds#get_trustie_pipeline', as: 'get_trustie_pipeline' - get ':number', to: 'builds#detail', as: 'detail' - post ':number', to: 'builds#restart', as: 'restart' - delete ':number', to: 'builds#delete', as: 'delete' - get ':number/logs/:stage/:step', to: 'builds#logs', as: 'logs' - end - end + # resources :builds, only: :index do + # collection do + # get 'get_trustie_pipeline', to: 'builds#get_trustie_pipeline', as: 'get_trustie_pipeline' + # get ':number', to: 'builds#detail', as: 'detail' + # post ':number', to: 'builds#restart', as: 'restart' + # delete ':number', to: 'builds#delete', as: 'delete' + # get ':number/logs/:stage/:step', to: 'builds#logs', as: 'logs' + # end + # end end resources :sync_forge, only: [:create] do @@ -338,6 +338,10 @@ Rails.application.routes.draw do resources :issue_tags, :path => :labels, only: [:create, :edit, :update, :destroy, :index] resources :version_releases, :path => :releases, only: [:index,:new, :create, :edit, :update, :destroy] + scope module: :ci do + resources :builds, param: :build + end + scope module: :projects do scope do get( diff --git a/spec/models/ci/build_spec.rb b/spec/models/ci/build_spec.rb new file mode 100644 index 000000000..3246d09ea --- /dev/null +++ b/spec/models/ci/build_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Ci::Build, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/ci/repo_spec.rb b/spec/models/ci/repo_spec.rb new file mode 100644 index 000000000..407536991 --- /dev/null +++ b/spec/models/ci/repo_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Ci::Repo, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/ci/stage_spec.rb b/spec/models/ci/stage_spec.rb new file mode 100644 index 000000000..ba43cca2a --- /dev/null +++ b/spec/models/ci/stage_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Ci::Stage, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/ci/step_spec.rb b/spec/models/ci/step_spec.rb new file mode 100644 index 000000000..46ab7a4a0 --- /dev/null +++ b/spec/models/ci/step_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Ci::Step, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end