diff --git a/api_document.md b/api_document.md index 4d8a6d5b..b8376b32 100644 --- a/api_document.md +++ b/api_document.md @@ -3966,13 +3966,12 @@ http://localhost:3000/api/ci/pipelines/list.json | jq *返回参数说明:* -| 参数名 | 类型 | 说明 | -| ------------- | ------ | ---------------------------------------------- | -| id | int | 流水线id | -| pipeline_name | string | 流水线名称 | -| file_name | string | 流水线文件名 | -| created_at | string | 创建时间 | -| stages | arr | 流水线包含的阶段数组(字段详情看阶段查询接口) | +| 参数名 | 类型 | 说明 | +| ------------- | ------ | ------------ | +| id | int | 流水线id | +| pipeline_name | string | 流水线名称 | +| file_name | string | 流水线文件名 | +| created_at | string | 创建时间 | 返回值 @@ -3984,27 +3983,7 @@ http://localhost:3000/api/ci/pipelines/list.json | jq "pipeline_name": "2020-01-08 流水线", "file_name": ".trustie.pipeline.yaml", "created_at": "2021-01-08 04:16:24", - "updated_at": "2021-01-08 04:16:24", - "stages": [ - { - "id": 1, - "stage_name": "初始化", - "stage_type": "init", - "pipeline_id": 1, - "show_index": 0, - "created_at": "2021-01-08 04:16:24", - "updated_at": "2021-01-08 04:16:24" - }, - { - "id": 2, - "stage_name": "编译构建", - "stage_type": "build", - "pipeline_id": 1, - "show_index": 1, - "created_at": "2021-01-08 04:16:24", - "updated_at": "2021-01-11 04:16:24" - } - ] + "updated_at": "2021-01-08 04:16:24" } ] } @@ -4014,8 +3993,6 @@ http://localhost:3000/api/ci/pipelines/list.json | jq #### 流水线新增 -点击"新增流水线"按钮时调用。 - ``` POST /api/ci/pipelines ``` @@ -4035,22 +4012,24 @@ curl --location --request POST 'http://localhost:3000/api/ci/pipelines' \ | 参数名 | 必选 | 类型 | 说明 | | ------------- | ---- | ------ | ---------------------------------------------- | -| pipeline_name | 是 | string | 流水线名称(默认初始值:流水线 yyyy-mm-dd) | +| pipeline_name | 是 | string | 流水线名称 | | file_name | 是 | string | 文件名称(默认初始值:.trustie.pipeline.yaml) | *返回参数说明:* -| 参数名 | 类型 | 说明 | -| ------- | ------ | ------------ | -| status | int | 状态码 0成功 | -| message | string | 返回消息 | +| 参数名 | 类型 | 说明 | +| ------- | ------ | -------------- | +| status | int | 状态码 0成功 | +| message | string | 返回消息 | +| id | int | 新增流水线的id | 返回值 ```json { "status": 0, - "message": "success" + "message": "success", + "id": 18 } ``` @@ -4136,56 +4115,64 @@ https://localhost:3000/api/ci/pipelines/1 | jq ------ -#### 流水线阶段排序 - -当新增阶段或者删除阶段时重新给受影响的stage设置show_index。 +#### 流水线的阶段查询 ``` -PUT /api/ci/pipelines/{id}/sort_stage +GET /api/ci/pipelines/{id}/stages ``` *示例* ```bash -curl --location --request PUT 'http://localhost:3000/api/ci/pipelines/1/sort_stage.json' \ ---header 'Content-Type: application/json' \ ---data-raw ' {"stage_index":[ - {"id": 1,"show_index": 0}, - {"id": 2,"show_index": 1}, - {"id": 3,"show_index": 2}, - {"id": 4,"show_index": 3}, - {"id": 7,"show_index": 4} - ] - }' +curl --location --request GET 'http://localhost:3000/api/ci/pipelines/19/stages.json' ``` *请求参数说明:* -| 参数名 | 必选 | 类型 | 说明 | -| ----------- | ---- | ---- | ---------- | -| stage_index | 是 | arr | 参数数组 | -| id | 是 | int | 阶段id | -| show_index | 是 | int | 阶段的序号 | +| 参数名 | 必选 | 类型 | 说明 | +| ------ | ---- | ---- | -------- | +| id | 是 | int | 流水线id | *返回参数说明:* -| 参数名 | 类型 | 说明 | -| ------- | ------ | ------------ | -| status | int | 状态码 0成功 | -| message | string | 返回消息 | +| 参数名 | 类型 | 说明 | +| ----------- | ------ | -------- | +| stages | arr | 阶段数组 | +| stage_name | string | 阶段名称 | +| stage_type | string | 阶段类型 | +| pipeline_id | int | 流水线id | +| show_index | int | 排序 | 返回值 ```json { - "status": 0, - "message": "success" + "stages": [ + { + "id": 37, + "stage_name": "初始化", + "stage_type": "init", + "pipeline_id": 19, + "show_index": 1, + "created_at": "2021-01-12T15:18:00.000+08:00", + "updated_at": "2021-01-12T15:18:00.000+08:00" + }, + { + "id": 38, + "stage_name": "编译构建", + "stage_type": "build", + "pipeline_id": 19, + "show_index": 2, + "created_at": "2021-01-12T15:18:00.000+08:00", + "updated_at": "2021-01-12T15:18:00.000+08:00" + } + ] } ``` ------ -#### 确认阶段流水线内容查询 +#### 确认阶段流水线完整内容查询 ``` GET /api/ci/pipelines/{id}/content @@ -4223,30 +4210,21 @@ POST /api/ci/pipelines/{id}/create_stage *示例* ```bash -curl --location --request POST 'http://localhost:3000/api/ci/pipelines/14/create_stage.json' \ +curl --location --request POST 'http://localhost:3000/api/ci/pipelines/19/create_stage.json' \ --header 'Content-Type: application/json' \ ---data-raw ' {"stages":[{ - "stage_name": "新阶段1", - "stage_type": "customize", - "show_index": 5 -}, -{ +--data-raw '{ "stage_name": "新阶段2", - "stage_type": "customize", - "show_index": 5 -} -] - }' + "show_index": 2 +}' ``` *请求参数说明:* -| 参数名 | 必选 | 类型 | 说明 | -| ---------- | ---- | ------ | -------------------------------- | -| stages | 是 | arr | 新增的阶段数组 | -| id | 是 | int | 流水线id | -| stage_name | 是 | string | 阶段名称(默认为 阶段名-模板名) | -| show_index | 是 | int | 阶段排序 | +| 参数名 | 必选 | 类型 | 说明 | +| ---------- | ---- | ------ | -------- | +| id | 是 | int | 流水线id | +| show_index | 是 | int | 阶段排序 | +| stage_name | 是 | string | 阶段名称 | *返回参数说明:* @@ -4269,7 +4247,7 @@ curl --location --request POST 'http://localhost:3000/api/ci/pipelines/14/create #### 流水线阶段更新 ``` -PUT /api/ci/pipelines/{id}/update_stage +PUT /api/ci/pipelines/{id}/{stage_id}/update_stage ``` *示例* @@ -4278,9 +4256,7 @@ PUT /api/ci/pipelines/{id}/update_stage curl --location --request PUT 'http://localhost:3000/api/ci/pipelines/1/5/update_stage.json' \ --header 'Content-Type: application/json' \ --data-raw ' { - "stage_name": "新阶段-更新", - "stage_type": "customize", - "show_index": 10 + "stage_name": "新阶段-更新" }' ``` @@ -4290,7 +4266,6 @@ curl --location --request PUT 'http://localhost:3000/api/ci/pipelines/1/5/update | ---------- | ---- | ------ | -------------------------------- | | id | 是 | int | 流水线id | | stage_name | 是 | string | 阶段名称(默认为 阶段名-模板名) | -| show_index | 是 | int | 阶段排序 | *返回参数说明:* @@ -4313,22 +4288,22 @@ curl --location --request PUT 'http://localhost:3000/api/ci/pipelines/1/5/update #### 流水线阶段删除 ``` -DELETE /api/ci/pipelines/{id}/{stage_id}/delete_stage +DELETE /api/ci/pipelines/{id}/{stage_id}/delete_stage?show_index={index} ``` *示例* ```bash -curl -X DELETE \ -https://localhost:3000/api/ci/pipelines/1/6/delete_stage.json | jq +curl --location --request DELETE 'http://localhost:3000/api/ci/pipelines/19/42/delete_stage.json?show_index=2' \ ``` *请求参数说明:* -| 参数名 | 必选 | 类型 | 说明 | -| -------- | ---- | ---- | -------- | -| id | 是 | int | 流水线id | -| stage_id | 是 | int | 阶段id | +| 参数名 | 必选 | 类型 | 说明 | +| ---------- | ---- | ---- | ---------------------- | +| id | 是 | int | 流水线id | +| stage_id | 是 | int | 阶段id | +| show_index | 是 | int | 被删除阶段的show_index | *返回参数说明:* @@ -4370,14 +4345,14 @@ http://localhost:3000/api/ci/pipelines/1/2/steps.json | jq *返回参数说明:* -| 参数名 | 类型 | 说明 | -| ----------- | ------ | ---------- | -| id | int | 步骤id | -| step_name | string | 步骤名称 | -| stage_id | int | 所属阶段id | -| template_id | int | 模板id | -| show_index | int | 显示顺序 | -| content | String | 步骤内容 | +| 参数名 | 类型 | 说明 | +| ---------- | ------ | ------------------ | +| id | int | 步骤id | +| step_name | string | 步骤名称 | +| stage_id | int | 所属阶段id | +| show_index | int | 显示顺序 | +| content | String | 步骤内容 | +| template | Object | 步骤对应的模板对象 | 返回值 @@ -4388,21 +4363,19 @@ http://localhost:3000/api/ci/pipelines/1/2/steps.json | jq "id": 1, "step_name": "编译构建-maven", "stage_id": 2, - "template_id": null, "show_index": 0, - "content": "xxxxxxxxxxx", + "content": "- name: Maven编译\r\n image: arm64v8/maven\r\n", "created_at": "2021-01-11T09:57:17.000+08:00", - "updated_at": "2021-01-11T09:57:17.000+08:00" - }, - { - "id": 2, - "step_name": "编译构建-maven-更新", - "stage_id": 2, - "template_id": 2, - "show_index": 2, - "content": "xxxx====xxxxxxx", - "created_at": "2021-01-11T10:12:58.000+08:00", - "updated_at": "2021-01-11T10:40:54.000+08:00" + "updated_at": "2021-01-11T09:57:17.000+08:00", + "template": { + "id": 3, + "template_name": "maven", + "stage_type": "build", + "category": "java", + "content": "- name: maven\r\n image: maven:3-jdk-10\r\n", + "created_at": "2021-01-11T17:28:34.000+08:00", + "updated_at": "2021-01-11T17:28:36.000+08:00" + } } ] } @@ -4578,9 +4551,9 @@ http://localhost:3000/api/ci/templates/templates_by_stage.json?stage_type=build *请求参数说明:* -| 参数名 | 必选 | 类型 | 说明 | -| ---------- | ---- | ------ | --------------------------------------------- | -| stage_type | 是 | string | 阶段类型:init/build/deploy/customize/confirm | +| 参数名 | 必选 | 类型 | 说明 | +| ---------- | ---- | ------ | ------------------------------------- | +| stage_type | 是 | string | 阶段类型:init/build/deploy/customize | *返回参数说明:* diff --git a/app/controllers/ci/pipelines_controller.rb b/app/controllers/ci/pipelines_controller.rb index 0ac53d80..28b6029b 100644 --- a/app/controllers/ci/pipelines_controller.rb +++ b/app/controllers/ci/pipelines_controller.rb @@ -12,7 +12,7 @@ class Ci::PipelinesController < ApplicationController # 默认创建四个初始阶段 init_stages = Ci::PipelineStage::INIT_STAGES - index = 0 + index = 1 init_stages.each do |type, name| pipeline.pipeline_stages.build( stage_name: name, @@ -21,7 +21,7 @@ class Ci::PipelinesController < ApplicationController ).save! index += 1 end - render_ok + render_ok({id: pipeline.id}) end rescue Exception => ex render_error(ex.message) @@ -74,15 +74,12 @@ class Ci::PipelinesController < ApplicationController def create_stage ActiveRecord::Base.transaction do - stages = params[:stages] - unless stages.empty? - stages.each do |stage| - pipeline_stage = Ci::PipelineStage.new(stage_name: stage[:stage_name], - stage_type: stage[:stage_type].blank? ? 'customize' : stage[:stage_type], - pipeline_id: params[:id], show_index: stage[:show_index]) - pipeline_stage.save! - end - end + # 修改stage排序 + update_stage_index(params[:id], params[:show_index], 1) + pipeline_stage = Ci::PipelineStage.new(stage_name: params[:stage_name], + stage_type: params[:stage_type].blank? ? 'customize' : params[:stage_type], + pipeline_id: params[:id], show_index: params[:show_index]) + pipeline_stage.save! render_ok end rescue Exception => ex @@ -92,7 +89,7 @@ class Ci::PipelinesController < ApplicationController def update_stage pipeline_stage = Ci::PipelineStage.find(params[:stage_id]) if pipeline_stage - pipeline_stage.update!(stage_name: params[:stage_name], show_index: params[:show_index]) + pipeline_stage.update!(stage_name: params[:stage_name]) end render_ok rescue Exception => ex @@ -100,28 +97,25 @@ class Ci::PipelinesController < ApplicationController end def delete_stage - pipeline_stage = Ci::PipelineStage.find(params[:stage_id]) - if pipeline_stage - pipeline_stage.destroy! + ActiveRecord::Base.transaction do + update_stage_index(params[:id], params[:show_index].to_i, -1) + pipeline_stage = Ci::PipelineStage.find(params[:stage_id]) + if pipeline_stage + pipeline_stage.destroy! + end + render_ok end - render_ok rescue Exception => ex render_error(ex.message) end - def sort_stage - stages = params[:stage_index] - if stages && !stages.empty? - stages.each do |stage| - pipeline_stage = Ci::PipelineStage.find(stage[:id]) - if pipeline_stage - pipeline_stage.update!(show_index: stage[:show_index]) - end + def update_stage_index(pipeline_id, show_index, diff) + stages = Ci::Pipeline.find(pipeline_id).pipeline_stages + stages.each do |stage| + if stage.show_index >= show_index + stage.update!(show_index: stage.show_index + diff) end end - render_ok - rescue Exception => ex - render_error(ex.message) end # ========步骤相关接口========= # diff --git a/app/models/ci/pipeline_stage_step.rb b/app/models/ci/pipeline_stage_step.rb index 5925d908..48a6b332 100644 --- a/app/models/ci/pipeline_stage_step.rb +++ b/app/models/ci/pipeline_stage_step.rb @@ -18,5 +18,5 @@ class Ci::PipelineStageStep < Ci::LocalBase validates :stage_id, presence: {message: "阶段id不能为空"} belongs_to :pipeline_stage, foreign_key: :stage_id, :class_name => 'Ci::PipelineStage' - has_one :template, :class_name => 'Ci::Template', foreign_key: :template_id + end diff --git a/app/views/ci/pipeline_stage_steps/_list.json.jbuilder b/app/views/ci/pipeline_stage_steps/_list.json.jbuilder index a3d592fb..98acdc52 100644 --- a/app/views/ci/pipeline_stage_steps/_list.json.jbuilder +++ b/app/views/ci/pipeline_stage_steps/_list.json.jbuilder @@ -1,9 +1,11 @@ json.id pipeline_stage_step.id json.step_name pipeline_stage_step.step_name json.stage_id pipeline_stage_step.stage_id -json.template_id pipeline_stage_step.template_id json.show_index pipeline_stage_step.show_index json.content pipeline_stage_step.content json.created_at pipeline_stage_step.created_at json.updated_at pipeline_stage_step.updated_at +json.template do + json.partial! "/ci/templates/list", template: Ci::Template.find(pipeline_stage_step.template_id) +end diff --git a/config/routes.rb b/config/routes.rb index 9b2f09d2..4de53af5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -46,7 +46,6 @@ Rails.application.routes.draw do get :content get :stages post :create_stage - put :sort_stage delete :delete_stage, :path => ":stage_id/delete_stage", to: 'pipelines#delete_stage' put :update_stage, :path => ":stage_id/update_stage", to: 'pipelines#update_stage' get :stage_steps, :path => ":stage_id/steps", to: 'pipelines#steps'