|
|
@@ -11,29 +11,34 @@ class Api::V1::Projects::PipelinesController < Api::V1::BaseController |
|
|
tip_exception("已经存在#{params[:pipeline_name]}流水线!") if size > 0 |
|
|
tip_exception("已经存在#{params[:pipeline_name]}流水线!") if size > 0 |
|
|
@pipeline = Action::Pipeline.new(pipeline_name: params[:pipeline_name], project_id: @project.id) |
|
|
@pipeline = Action::Pipeline.new(pipeline_name: params[:pipeline_name], project_id: @project.id) |
|
|
@pipeline.file_name = ".gitea/workflows/#{@pipeline.pipeline_name}.yaml" |
|
|
@pipeline.file_name = ".gitea/workflows/#{@pipeline.pipeline_name}.yaml" |
|
|
@pipeline.json = demo.to_json |
|
|
|
|
|
# @pipeline.json = params[:json] if params[:json] |
|
|
|
|
|
@pipeline.yaml = build_pipeline_yaml(@pipeline) |
|
|
|
|
|
|
|
|
@pipeline.json = params[:pipeline_json].to_json |
|
|
|
|
|
pipeline_yaml = build_pipeline_yaml(params[:pipeline_name], params[:pipeline_json]) |
|
|
|
|
|
tip_exception("流水线yaml内空不能为空") if pipeline_yaml.blank? |
|
|
|
|
|
@pipeline.yaml = pipeline_yaml |
|
|
@pipeline.save! |
|
|
@pipeline.save! |
|
|
sha = get_pipeline_file_sha(@pipeline.file_name, @pipeline.branch) |
|
|
sha = get_pipeline_file_sha(@pipeline.file_name, @pipeline.branch) |
|
|
tip_exception("#{@pipeline.file_name}已存在") if sha |
|
|
tip_exception("#{@pipeline.file_name}已存在") if sha |
|
|
interactor = Gitea::UpdateFileInteractor.call(current_user.gitea_token, @owner.login, content_params) |
|
|
|
|
|
|
|
|
interactor = Gitea::CreateFileInteractor.call(current_user.gitea_token, @owner.login, content_params) |
|
|
tip_exception(interactor.error) unless interactor.success? |
|
|
tip_exception(interactor.error) unless interactor.success? |
|
|
render_ok({ id: @pipeline.id }) |
|
|
render_ok({ id: @pipeline.id }) |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def save_yaml |
|
|
|
|
|
@pipeline = Action::Pipeline.new(pipeline_name: params[:pipeline_name], project_id: @project.id) |
|
|
|
|
|
@pipeline.file_name = ".gitea/workflows/#{@pipeline.pipeline_name}.yaml" |
|
|
|
|
|
@pipeline.json = params[:pipeline_json].to_json |
|
|
|
|
|
pipeline_yaml = build_pipeline_yaml(params[:pipeline_name], params[:pipeline_json]) |
|
|
|
|
|
tip_exception("流水线yaml内空不能为空") if pipeline_yaml.blank? |
|
|
|
|
|
@pipeline.yaml = pipeline_yaml |
|
|
|
|
|
sha = get_pipeline_file_sha(@pipeline.file_name, @pipeline.branch) |
|
|
|
|
|
interactor = sha.present? ? Gitea::UpdateFileInteractor.call(current_user.gitea_token, @owner.login, content_params) : Gitea::CreateFileInteractor.call(current_user.gitea_token, @owner.login, content_params) |
|
|
|
|
|
tip_exception(interactor.error) unless interactor.success? |
|
|
|
|
|
render_ok |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
def build_yaml |
|
|
def build_yaml |
|
|
Rails.logger.info("pipeline===========#{params[:pipeline_json]}") |
|
|
|
|
|
if params[:pipeline_json].present? |
|
|
if params[:pipeline_json].present? |
|
|
pipeline = params[:pipeline_json] |
|
|
|
|
|
@name = params[:name] |
|
|
|
|
|
params_nodes = JSON.parse(pipeline.to_json)["nodes"].select { |node| !["on-push", "on-schedule"].include?(node["component_name"]) } |
|
|
|
|
|
on_nodes = JSON.parse(pipeline.to_json)["nodes"].select { |node| ["on-push", "on-schedule"].include?(node["component_name"]) } |
|
|
|
|
|
@on_nodes = build_nodes(on_nodes) |
|
|
|
|
|
@steps_nodes = build_nodes(params_nodes) |
|
|
|
|
|
yaml = ERB.new(File.read(File.join(Rails.root, "app/views/api/v1/projects/pipelines", "build_pipeline.yaml.erb"))).result(binding) |
|
|
|
|
|
# # 删除空行内容 |
|
|
|
|
|
@pipeline_yaml = yaml.gsub(/^\s*\n/, "") |
|
|
|
|
|
|
|
|
@pipeline_yaml = build_pipeline_yaml(params[:pipeline_name], params[:pipeline_json]) |
|
|
else |
|
|
else |
|
|
@pipeline_yaml = build_test_yaml |
|
|
@pipeline_yaml = build_test_yaml |
|
|
end |
|
|
end |
|
|
@@ -63,24 +68,24 @@ class Api::V1::Projects::PipelinesController < Api::V1::BaseController |
|
|
@pipeline = Action::Pipeline.new(id: 0, pipeline_name: "test-ss", yaml: build_test_yaml) if @pipeline.blank? |
|
|
@pipeline = Action::Pipeline.new(id: 0, pipeline_name: "test-ss", yaml: build_test_yaml) if @pipeline.blank? |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def build_pipeline_yaml(pipeline) |
|
|
|
|
|
if pipeline.json.present? |
|
|
|
|
|
@name = pipeline.pipeline_name |
|
|
|
|
|
params_nodes = JSON.parse(pipeline.json)["nodes"].select { |node| !["on-push", "on-schedule"].include?(node["component_name"]) } |
|
|
|
|
|
on_nodes = JSON.parse(pipeline.json)["nodes"].select { |node| ["on-push", "on-schedule"].include?(node["component_name"]) } |
|
|
|
|
|
|
|
|
def build_pipeline_yaml(pipeline_name, pipeline_json) |
|
|
|
|
|
if pipeline_json.present? |
|
|
|
|
|
@pipeline_name = pipeline_name |
|
|
|
|
|
params_nodes = pipeline_json["nodes"].select { |node| !["on-push", "on-schedule"].include?(node["component_name"]) } |
|
|
|
|
|
on_nodes = pipeline_json["nodes"].select { |node| ["on-push", "on-schedule"].include?(node["component_name"]) } |
|
|
@on_nodes = build_nodes(on_nodes) |
|
|
@on_nodes = build_nodes(on_nodes) |
|
|
@steps_nodes = build_nodes(params_nodes) |
|
|
@steps_nodes = build_nodes(params_nodes) |
|
|
yaml = ERB.new(File.read(File.join(Rails.root, "app/views/api/v1/projects/pipelines", "build_pipeline.yaml.erb"))).result(binding) |
|
|
yaml = ERB.new(File.read(File.join(Rails.root, "app/views/api/v1/projects/pipelines", "build_pipeline.yaml.erb"))).result(binding) |
|
|
# 删除空行内容 |
|
|
# 删除空行内容 |
|
|
@pipeline_yaml = yaml.gsub(/^\s*\n/, "") |
|
|
@pipeline_yaml = yaml.gsub(/^\s*\n/, "") |
|
|
else |
|
|
else |
|
|
@pipeline_yaml = params[:yaml] |
|
|
|
|
|
|
|
|
@pipeline_yaml = params[:pipeline_yaml] |
|
|
end |
|
|
end |
|
|
@pipeline_yaml |
|
|
@pipeline_yaml |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def build_test_yaml |
|
|
def build_test_yaml |
|
|
@name = "love me" |
|
|
|
|
|
|
|
|
@pipeline_name = "I like it" |
|
|
params_nodes = JSON.parse(demo.to_json)["nodes"].select { |node| !["on-push", "on-schedule"].include?(node["component_name"]) } |
|
|
params_nodes = JSON.parse(demo.to_json)["nodes"].select { |node| !["on-push", "on-schedule"].include?(node["component_name"]) } |
|
|
on_nodes = JSON.parse(demo.to_json)["nodes"].select { |node| ["on-push", "on-schedule"].include?(node["component_name"]) } |
|
|
on_nodes = JSON.parse(demo.to_json)["nodes"].select { |node| ["on-push", "on-schedule"].include?(node["component_name"]) } |
|
|
@on_nodes = build_nodes(on_nodes) |
|
|
@on_nodes = build_nodes(on_nodes) |
|
|
@@ -135,7 +140,7 @@ class Api::V1::Projects::PipelinesController < Api::V1::BaseController |
|
|
filepath: ".gitea/workflows/#{@pipeline.pipeline_name}.yaml", |
|
|
filepath: ".gitea/workflows/#{@pipeline.pipeline_name}.yaml", |
|
|
branch: @pipeline.branch, |
|
|
branch: @pipeline.branch, |
|
|
new_branch: @pipeline.branch, |
|
|
new_branch: @pipeline.branch, |
|
|
content: build_pipeline_yaml(@pipeline), |
|
|
|
|
|
|
|
|
content: @pipeline.yaml, |
|
|
message: 'create pipeline', |
|
|
message: 'create pipeline', |
|
|
committer: { |
|
|
committer: { |
|
|
email: current_user.mail, |
|
|
email: current_user.mail, |
|
|
@@ -154,8 +159,8 @@ class Api::V1::Projects::PipelinesController < Api::V1::BaseController |
|
|
run_values = {} |
|
|
run_values = {} |
|
|
input_values = {} |
|
|
input_values = {} |
|
|
if input_node["in_parameters"].present? |
|
|
if input_node["in_parameters"].present? |
|
|
# Rails.logger.info "@in_parameters=====11===#{input_node["component_name"]}======#{input_node["in_parameters"]}" |
|
|
|
|
|
input_node["in_parameters"].each_key do |input_key| |
|
|
|
|
|
|
|
|
Rails.logger.info "@in_parameters=====11===#{input_node["component_name"]}======#{input_node["in_parameters"].keys}" |
|
|
|
|
|
input_node["in_parameters"].keys.each do |input_key| |
|
|
# Rails.logger.info "@in_parameters.input_key===#{input_key}" |
|
|
# Rails.logger.info "@in_parameters.input_key===#{input_key}" |
|
|
# Rails.logger.info "@in_parameters.input_value===#{input_node["in_parameters"][input_key]["value"]}" |
|
|
# Rails.logger.info "@in_parameters.input_value===#{input_node["in_parameters"][input_key]["value"]}" |
|
|
if input_key.to_s.gsub("--", "") == "run" |
|
|
if input_key.to_s.gsub("--", "") == "run" |
|
|
@@ -185,7 +190,7 @@ class Api::V1::Projects::PipelinesController < Api::V1::BaseController |
|
|
"working_directory": "", |
|
|
"working_directory": "", |
|
|
"command": "", |
|
|
"command": "", |
|
|
"in_parameters": { |
|
|
"in_parameters": { |
|
|
"--cro": { |
|
|
|
|
|
|
|
|
"--cron": { |
|
|
"type": "str", |
|
|
"type": "str", |
|
|
"item_type": "", |
|
|
"item_type": "", |
|
|
"label": "push代码", |
|
|
"label": "push代码", |
|
|
|