diff --git a/api_document.md b/api_document.md index 3c6c90fd1..35618f799 100644 --- a/api_document.md +++ b/api_document.md @@ -4203,7 +4203,8 @@ http://localhost:3000/api/ci/pipelines/1/content.json?owner=xx&repo=xx | jq ```json { "content": "#pipeline \nkind: pipeline\r\nname: maven项目-镜像仓库\r\n\r\nplatform:\r\n os: linux\r\n arch: arm64\nsteps:\n- name: Maven编译\r\n image: arm64v8/maven\r\n commands:\r\n - mvn install\n- name: 编译镜像-推送到仓库\r\n image: plugins/docker\r\n settings:\r\n username: moshenglv\r\n password: RL9UB5P7Jtzukka\r\n repo: docker.io/moshenglv/demo\r\n tags: latest\n", - "sync": 1 + "sync": 1, + "sha":"xxxxx" } ``` diff --git a/app/controllers/ci/pipelines_controller.rb b/app/controllers/ci/pipelines_controller.rb index bb5bcc0a7..dd1fe989e 100644 --- a/app/controllers/ci/pipelines_controller.rb +++ b/app/controllers/ci/pipelines_controller.rb @@ -2,7 +2,8 @@ class Ci::PipelinesController < Ci::BaseController before_action :require_login, only: %i[list create] skip_before_action :connect_to_ci_db - before_action :load_project, only: %i[content] + before_action :load_project, only: %i[content, create_trustie_pipeline] + before_action :load_repository, only: %i[create_trustie_pipeline] # ======流水线相关接口========== # def list @@ -84,6 +85,47 @@ class Ci::PipelinesController < Ci::BaseController return file['sha'] end end + + def create_trustie_pipeline + pipeline = Ci::Pipeline.find(params[:id]) + sha = get_pipeline_file_sha(pipeline.file_name) + if sha + pipeline.update!(sync: 1) + return update_trustie_pipeline(sha) + else + interactor = Gitea::CreateFileInteractor.call(current_user.gitea_token, @owner.login, content_params) + if interactor.success? + pipeline.update!(sync: 1) + else + render_error(interactor.error) + end + end + end + + def update_trustie_pipeline(sha) + interactor = Gitea::UpdateFileInteractor.call(current_user.gitea_token, params[:owner], params.merge(identifier: @project.identifier,sha: sha)) + if interactor.success? + return render_ok("更新成功") + else + return render_error(interactor.error) + end + end + + def content_params + { + filepath: params[:filepath], + branch: params[:branch], + new_branch: params[:new_branch], + content: params[:content], + message: params[:message], + committer: { + email: current_user.mail, + name: current_user.login + }, + identifier: @project.identifier + } + end + # =========阶段相关接口========= # def stages pipeline_id = params[:id] diff --git a/config/routes.rb b/config/routes.rb index 1281d16b9..058dfec02 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -46,6 +46,7 @@ Rails.application.routes.draw do get :content get :stages post :create_stage + post :create_trustie_pipeline 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'