Skip to content

Commit

Permalink
Merge pull request #11625 from danidoni/refactor-workflow-step-calls
Browse files Browse the repository at this point in the history
Refactor the Workflow step calling responsibility
  • Loading branch information
danidoni committed Sep 20, 2021
2 parents 3f8bf8a + 369bcde commit b74e17f
Show file tree
Hide file tree
Showing 5 changed files with 568 additions and 135 deletions.
60 changes: 1 addition & 59 deletions src/api/app/models/token/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,68 +13,10 @@ def call(options)

yaml_file = Workflows::YAMLDownloader.new(scm_webhook.payload, token: self).call
workflows = Workflows::YAMLToWorkflowsService.new(yaml_file: yaml_file, scm_webhook: scm_webhook, token: self).call

case
when scm_webhook.closed_merged_pull_request?
destroy_target_projects(workflows)
when scm_webhook.reopened_pull_request?
restore_target_projects(workflows)
when scm_webhook.new_pull_request?, scm_webhook.updated_pull_request?
call_steps(workflows)
end
workflows.each(&:call)
rescue Octokit::Unauthorized, Gitlab::Error::Unauthorized => e
raise Token::Errors::SCMTokenInvalid, e.message
end

private

def destroy_target_projects(workflows)
workflows.each do |workflow|
# Do not process steps for which there's nothing to do
workflow_steps = workflow.steps.reject { |step| step.instance_of?(::Workflow::Step::ConfigureRepositories) }
target_packages = workflow_steps.map(&:target_package).uniq.compact
delete_subscriptions(target_packages)

target_project_names = workflow_steps.map(&:target_project_name).uniq.compact
destroy_all_target_projects(target_project_names)
end
end

# We want the callbacks to run after destroy, so we can't use delete_all
def destroy_all_target_projects(target_project_names)
Project.where(name: target_project_names).destroy_all
end

def delete_subscriptions(packages)
EventSubscription.where(channel: 'scm', token: self, package: packages).delete_all
end

def restore_target_projects(workflows)
token_user_login = user.login

workflows.each do |workflow|
# Do not process steps for which there's nothing to do
workflow_steps = workflow.steps.reject { |step| step.instance_of?(::Workflow::Step::ConfigureRepositories) }
target_project_names = workflow_steps.map(&:target_project_name).uniq.compact
target_project_names.each do |target_project_name|
Project.restore(target_project_name, user: token_user_login)
end

target_packages = workflow_steps.map(&:target_package).uniq.compact
target_packages.each do |target_package|
# FIXME: We shouldn't rely on a workflow step to be able to create/update subscriptions
workflow_steps.first.create_or_update_subscriptions(target_package, workflow.filters)
end
end
end

def call_steps(workflows)
workflows.each do |workflow|
workflow.steps.each do |step|
step.call({ workflow_filters: workflow.filters })
end
end
end
end

# == Schema Information
Expand Down
41 changes: 41 additions & 0 deletions src/api/app/models/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ def initialize(attributes = {})
validates_with WorkflowStepsValidator
validates_with WorkflowFiltersValidator

def call
case
when scm_webhook.closed_merged_pull_request?
destroy_target_projects
when scm_webhook.reopened_pull_request?
restore_target_projects
when scm_webhook.new_pull_request?, scm_webhook.updated_pull_request?
steps.each do |step|
step.call({ workflow_filters: filters })
end
end
end

def steps
return [] if workflow_steps.blank?

Expand Down Expand Up @@ -68,4 +81,32 @@ def initialize_step(step_name, step_instructions)
def supported_filters
@supported_filters ||= workflow_instructions[:filters]&.select { |key, _value| SUPPORTED_FILTERS.include?(key.to_sym) }
end

def destroy_target_projects
# Do not process steps for which there's nothing to do
processable_steps = steps.reject { |step| step.instance_of?(::Workflow::Step::ConfigureRepositories) }
target_packages = steps.map(&:target_package).uniq.compact
EventSubscription.where(channel: 'scm', token: self, package: target_packages).delete_all

target_project_names = processable_steps.map(&:target_project_name).uniq.compact
# We want the callbacks to run after destroy, so we can't use delete_all
Project.where(name: target_project_names).destroy_all
end

def restore_target_projects
token_user_login = token.user.login

# Do not process steps for which there's nothing to do
processable_steps = steps.reject { |step| step.instance_of?(::Workflow::Step::ConfigureRepositories) }
target_project_names = processable_steps.map(&:target_project_name).uniq.compact
target_project_names.each do |target_project_name|
Project.restore(target_project_name, user: token_user_login)
end

target_packages = processable_steps.map(&:target_package).uniq.compact
target_packages.each do |target_package|
# FIXME: We shouldn't rely on a workflow step to be able to create/update subscriptions
processable_steps.first.create_or_update_subscriptions(target_package, filters)
end
end
end
Loading

0 comments on commit b74e17f

Please sign in to comment.