Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support custom parameters in workflow run #714

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
3 changes: 2 additions & 1 deletion app/controllers/config/release_platforms_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def config_params
]
],
release_candidate_workflow_attributes: [
:id, :identifier, :artifact_name_pattern
:id, :identifier, :artifact_name_pattern,
parameters_attributes: [:id, :name, :value, :_destroy]
],
production_release_attributes: [
:id,
Expand Down
3 changes: 2 additions & 1 deletion app/libs/installations/bitbucket/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,8 @@ def trigger_pipeline!(repo_slug, pipeline_config, branch_name, inputs, commit_ha
{
key: "VERSION_CODE",
value: inputs[:version_code]
}
},
*inputs[:parameters].map { |key, value| {key:, value:} }
]
}
}
Expand Down
3 changes: 2 additions & 1 deletion app/libs/installations/bitrise/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@
environments: [
{mapped_to: "versionName", value: inputs[:build_version]},
{mapped_to: "versionCode", value: inputs[:version_code]},
{mapped_to: "buildNotes", value: inputs[:build_notes] || ""}
{mapped_to: "buildNotes", value: inputs[:build_notes] || ""},
*inputs[:parameters].map { |mapped_to, value| {mapped_to:, value:} },

Check failure on line 76 in app/libs/installations/bitrise/api.rb

View workflow job for this annotation

GitHub Actions / lint

Style/TrailingCommaInArrayLiteral: Avoid comma after the last item of an array.
]
},

Expand Down
3 changes: 2 additions & 1 deletion app/libs/installations/github/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ def run_workflow!(repo, id, ref, inputs, commit_hash, json_inputs_enabled = fals
inputs
.slice(:version_code, :build_notes)
.merge(version_name: inputs[:build_version], commit_ref: commit_hash)
.merge(inputs[:parameters])
.compact
.to_json
.then { {"tramline-input" => _1} }
Expand All @@ -93,7 +94,7 @@ def run_workflow!(repo, id, ref, inputs, commit_hash, json_inputs_enabled = fals
versionCode: inputs[:version_code],
versionName: inputs[:build_version],
buildNotes: inputs[:build_notes]
}.compact
}.merge(inputs[:parameters]).compact
end

execute do
Expand Down
9 changes: 7 additions & 2 deletions app/models/config/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
self.table_name = "workflow_configs"

belongs_to :release_platform_config, class_name: "Config::ReleasePlatform"
has_many :parameters, class_name: "Config::WorkflowParameter", dependent: :destroy

accepts_nested_attributes_for :parameters, allow_destroy: true

enum :kind, {internal: "internal", release_candidate: "release_candidate"}
validates :identifier, :name, presence: true
Expand All @@ -24,13 +27,15 @@
id: identifier,
name: name,
artifact_name_pattern: artifact_name_pattern,
kind: kind
kind: kind,
parameters: parameters.map(&:as_json),

Check failure on line 31 in app/models/config/workflow.rb

View workflow job for this annotation

GitHub Actions / lint

Style/TrailingCommaInHashLiteral: Avoid comma after the last item of a hash.
}
end

def self.from_json(json)
workflow = new(json.except("id")) # Exclude 'id' to ensure we don't overwrite an existing object
workflow = new(json.except("id", "parameters")) # Exclude 'id' to ensure we don't overwrite an existing object
workflow.identifier = json["id"]
workflow.parameters = json["parameters"].map { |parameter| ::Config::WorkflowParameter.new(parameter) }
workflow
end
end
26 changes: 26 additions & 0 deletions app/models/config/workflow_parameter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# == Schema Information
#
# Table name: workflow_config_parameters
#
# id :bigint not null, primary key
# name :string not null
# value :string not null
# created_at :datetime not null
# updated_at :datetime not null
# workflow_id :bigint not null, indexed
#
class Config::WorkflowParameter < ApplicationRecord
self.table_name = "workflow_config_parameters"
belongs_to :workflow

validates :name, :value, presence: true
validates :name, uniqueness: true

Check failure on line 17 in app/models/config/workflow_parameter.rb

View workflow job for this annotation

GitHub Actions / lint

Rails/UniqueValidationWithoutIndex: Uniqueness validation should have a unique index on the database column.

def as_json(_options = {})
{
name:,
value:,
id:
}
end
end
4 changes: 4 additions & 0 deletions app/models/workflow_run.rb
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ def update_build_number!
def workflow_inputs
data = {version_code: build.build_number, build_version: release_version}
data[:build_notes] = triggering_release.tester_notes if organization.build_notes_in_workflow?
data[:parameters] = {}
conf.parameters.each do |param|
data[:parameters][param.name] = param.value
end
data
end

Expand Down
47 changes: 47 additions & 0 deletions app/views/config/release_platforms/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,53 @@
<%= render partial: "build_name_pattern_info" %>
<div><%= rc_fields.labeled_text_field :artifact_name_pattern, "Build artifact name (optional)", placeholder: "Eg., build-abc.apk" %></div>
</div>

<div class="flex flex-col gap-0.5">
<div data-controller="nested-form-ext" data-nested-form-ext-list-position-outlet="#release_candidate_workflow_additional_parameters" class="grid-cols-2 gap-2 pt-3">
<div class="pb-3">
<%= render V2::ButtonComponent.new(
scheme: :light,
type: :action,
size: :xs,
label: "Add a new parameter",
html_options: { data: { action: "nested-form-ext#add" } },
arrow: :none) do |b|
b.with_icon("plus.svg", rounded: false)
end %>
</div>
<ul data-controller="sortable-list-ext list-position"
data-sortable-list-ext-handle-value=".handle"
data-sortable-list-ext-list-position-outlet="#release_candidate_workflow_parameters"
data-list-position-initial-value=1
class="flex flex-col flex-1 item-gap-default"
id="release_candidate_workflow_parameters">
<% if rc_fields.object.parameters.present? %>
<div class="flex flex-row grid grid-cols-2">
<p class="text-sm">Parameter Name</p>
<p class="text-sm">Parameter Value</p>
</div>
<% end %>

<% rc_fields.object.parameters.each do |parameter| %>
<% rc_fields.fields_for :parameters, parameter do |parameter_form| %>
<li class="nested-form-wrapper" data-new-record="false">
<%= render partial: "parameter_form", object: parameter_form %>
</li>
<% end %>
<% end %>

<template data-nested-form-ext-target="template">
<li class="nested-form-wrapper" data-new-record="true">
<% rc_fields.fields_for :parameters, rc_fields.object.parameters.build, child_index: "NEW_RECORD" do |parameter_form| %>
<%= render partial: "parameter_form", object: parameter_form %>
<% end %>
</li>
</template>

<div data-nested-form-ext-target="target"></div>
</ul>
</div>
</div>
</div>
<% end %>
<% end %>
Expand Down
16 changes: 16 additions & 0 deletions app/views/config/release_platforms/_parameter_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<div class="flex flex-row item-gap-default">
<%= parameter_form.hidden_field :id %>
<%= parameter_form.hidden_field :_destroy %>
<%= parameter_form.text_field_without_label :name, "Parameter name" %>
<%= parameter_form.text_field_without_label :value, "Parameter value" %>

<div class="flex item-gap-default ml-auto">
<%= render V2::ButtonComponent.new(
scheme: :naked_icon,
type: :action,
size: :none,
html_options: {data: {action: "nested-form-ext#remove"}}) do |b|
b.with_icon("v2/trash.svg", size: :md)
end %>
</div>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<% release_type = "#{kind}_release".to_sym %>

<div class="grid gap-4">
<% section.F.fields_for release_type, section.F.object.send(release_type) || Config::ReleaseStep.new(kind: kind, release_platform_config: section.F.object) do |rr| %>
<%= rr.hidden_field :id, value: rr.object.id %>
<div data-controller="nested-form-ext"
data-nested-form-ext-list-position-outlet="#<%= release_type %>_additional_parameters"
class="grid gap-2 pt-3">
<div>
<%= render V2::ButtonComponent.new(
scheme: :light,
type: :action,
size: :xs,
label: "Add a new parameter",
html_options: { data: { action: "nested-form-ext#add" } },
arrow: :none) do |b|
b.with_icon("plus.svg", rounded: false)
end %>
</div>

<ul data-controller="sortable-list-ext list-position"
data-sortable-list-ext-handle-value=".handle"
data-sortable-list-ext-list-position-outlet="#<%= release_type %>_additional_parameters"
data-list-position-initial-value=1
class="flex flex-col item-gap-default"
id="<%= release_type %>_additional_parameters">
<% rr.object.additional_parameters.each_with_index do |submission, _index| %>
<li class="nested-form-wrapper" data-new-record="false">
<% rr.fields_for :submissions, submission do |submission_form| %>
<%= render "submissions_form", form: submission_form, submission_types: submission_types %>
<%= submission_form.hidden_field :number, value: submission.number, data: { list_position_target: "position" } %>
<%= submission_form.hidden_field :id, value: submission.id %>
<%= submission_form.hidden_field :_destroy %>
<% end %>
</li>
<% end %>

<template data-nested-form-ext-target="template">
<li class="nested-form-wrapper" data-new-record="true">
<% rr.fields_for :submissions, rr.object.submissions.build(submission_type: submission_types.first, auto_promote: false), child_index: "NEW_RECORD" do |submission_form| %>
<%= render "submissions_form", form: submission_form, submission_types: submission_types %>
<%= submission_form.hidden_field :number, data: { list_position_target: "position" } %>
<% end %>
</li>
</template>

<div data-nested-form-ext-target="target"></div>
</ul>
</div>
<% end %>
</div>
11 changes: 11 additions & 0 deletions db/migrate/20250105122540_create_workflow_config_parameters.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class CreateWorkflowConfigParameters < ActiveRecord::Migration[7.2]
def change
create_table :workflow_config_parameters do |t|
t.string :name, null: false
t.string :value, null: false
t.references :workflow, null: false, foreign_key: {to_table: :workflow_configs}

t.timestamps
end
end
end
12 changes: 11 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions spec/models/config/workflow_parameter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

Check failure on line 1 in spec/models/config/workflow_parameter_spec.rb

View workflow job for this annotation

GitHub Actions / lint

Style/StringLiterals: Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.

RSpec.describe Config::WorkflowParameter, type: :model do

Check failure on line 3 in spec/models/config/workflow_parameter_spec.rb

View workflow job for this annotation

GitHub Actions / lint

RSpecRails/InferredSpecType: Remove redundant spec type.
pending "add some examples to (or delete) #{__FILE__}"
end
Loading