Skip to content

Commit

Permalink
Move token preperation logic to a concern
Browse files Browse the repository at this point in the history
The methods to authorize and set the package/project
from a token is also required in the webui controller.
Therefore moving it to a concern in order to reuse them.
  • Loading branch information
krauselukas committed Sep 10, 2021
1 parent 2acf054 commit 8f5a76c
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 42 deletions.
45 changes: 45 additions & 0 deletions src/api/app/controllers/concerns/triggerable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
module Triggerable
extend ActiveSupport::Concern

def set_project
# By default we operate on the package association
@project = @token.package.try(:project)
# If the token has no package, let's find one from the parameters
@project ||= Project.get_by_name(params[:project])
# Remote projects are read-only, can't trigger something for them.
# See https://github.com/openSUSE/open-build-service/wiki/Links#project-links
raise Project::Errors::UnknownObjectError, "Sorry, triggering tokens for remote project \"#{params[:project]}\" is not possible." unless @project.is_a?(Project)
end

def set_package
# By default we operate on the package association
@package = @token.package
# If the token has no package, let's find one from the parameters
@package ||= Package.get_by_project_and_name(@project,
params[:package],
@token.package_find_options)
return unless @project.links_to_remote?

# The token has no package, we did not find a package in the database but the project has a link to remote.
# See https://github.com/openSUSE/open-build-service/wiki/Links#project-links
# In this case, we will try to trigger with the user input, no matter what it is
@package ||= params[:package]

# TODO: This should not happen right? But who knows...
raise ActiveRecord::RecordNotFound unless @package
end

def set_object_to_authorize
@token.object_to_authorize = package_from_project_link? ? @project : @package
end

def set_multibuild_flavor
# Do NOT use @package.multibuild_flavor? here because the flavor need to be checked for the right source revision
@multibuild_container = params[:package].gsub(/.*:/, '') if params[:package].present? && params[:package].include?(':')
end

def package_from_project_link?
# a remote package is always included via project link
!(@package.is_a?(Package) && @package.project == @project)
end
end
46 changes: 4 additions & 42 deletions src/api/app/controllers/trigger_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class TriggerController < ApplicationController
include Triggerable

ALLOWED_GITLAB_EVENTS = ['Push Hook', 'Tag Push Hook', 'Merge Request Hook'].freeze

include Pundit
Expand All @@ -19,10 +21,12 @@ class TriggerController < ApplicationController
before_action :set_object_to_authorize
# set_multibuild_flavor needs to run after the set_object_to_authorize callback
append_before_action :set_multibuild_flavor

include Trigger::Errors

def create
authorize @token, :trigger?

@token.user.run_as do
opts = { project: @project, package: @package, repository: params[:repository], arch: params[:arch] }
opts[:multibuild_flavor] = @multibuild_container if @multibuild_container.present?
Expand Down Expand Up @@ -60,46 +64,4 @@ def set_token
def pundit_user
@token.user
end

def set_project
# By default we operate on the package association
@project = @token.package.try(:project)
# If the token has no package, let's find one from the parameters
@project ||= Project.get_by_name(params[:project])
# Remote projects are read-only, can't trigger something for them.
# See https://github.com/openSUSE/open-build-service/wiki/Links#project-links
raise Project::Errors::UnknownObjectError, "Sorry, triggering tokens for remote project \"#{params[:project]}\" is not possible." unless @project.is_a?(Project)
end

def set_package
# By default we operate on the package association
@package = @token.package
# If the token has no package, let's find one from the parameters
@package ||= Package.get_by_project_and_name(@project,
params[:package],
@token.package_find_options)
return unless @project.links_to_remote?

# The token has no package, we did not find a package in the database but the project has a link to remote.
# See https://github.com/openSUSE/open-build-service/wiki/Links#project-links
# In this case, we will try to trigger with the user input, no matter what it is
@package ||= params[:package]

# TODO: This should not happen right? But who knows...
raise ActiveRecord::RecordNotFound unless @package
end

def set_object_to_authorize
@token.object_to_authorize = package_from_project_link? ? @project : @package
end

def set_multibuild_flavor
# Do NOT use @package.multibuild_flavor? here because the flavor need to be checked for the right source revision
@multibuild_container = params[:package].gsub(/.*:/, '') if params[:package].present? && params[:package].include?(':')
end

def package_from_project_link?
# a remote package is always included via project link
!(@package.is_a?(Package) && @package.project == @project)
end
end

0 comments on commit 8f5a76c

Please sign in to comment.