Skip to content

Commit

Permalink
Merge pull request #11448 from krauselukas/feature/token_ui_trigger
Browse files Browse the repository at this point in the history
Trigger token through the webui
  • Loading branch information
krauselukas committed Sep 10, 2021
2 parents b72fc95 + 1aa38e3 commit c726c88
Show file tree
Hide file tree
Showing 50 changed files with 2,430 additions and 2,303 deletions.
4 changes: 2 additions & 2 deletions src/api/app/assets/javascripts/webui/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ $(document).ready(function() {
});
});

$('#linked_project, #review_project, #project_name').on('autocompletechange', function() {
$('#linked_project, #review_project, #project_name, #project').on('autocompletechange', function() {
var projectName = $(this).val(),
packageInput = $('#linked_package, #review_package, #package_name');
packageInput = $('#linked_package, #review_package, #package_name, #package');

if (!packageInput.is(':visible')) return;

Expand Down
44 changes: 44 additions & 0 deletions src/api/app/controllers/concerns/triggerable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
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
52 changes: 52 additions & 0 deletions src/api/app/controllers/webui/users/token_triggers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
class Webui::Users::TokenTriggersController < Webui::WebuiController
include Pundit
include Triggerable

before_action :set_token
before_action :set_project, except: [:show]
before_action :set_package, except: [:show]
before_action :set_object_to_authorize, except: [:show]
# set_multibuild_flavor needs to run after the set_object_to_authorize callback
append_before_action :set_multibuild_flavor, except: [:show]

rescue_from 'Project::Errors::UnknownObjectError' do |exception|
flash[:error] = "#{exception.message}"
redirect_to tokens_url
end

rescue_from 'Package::Errors::UnknownObjectError' do |exception|
flash[:error] = "#{exception.message}"
redirect_to tokens_url
end

def show
authorize @token, :show?
end

def update
authorize @token, :webui_trigger?

opts = { project: @project, package: @package, repository: params[:repository], arch: params[:arch] }
opts[:multibuild_flavor] = @multibuild_container if @multibuild_container.present?

begin
@token.call(opts)
flash[:success] = "Token with id #{@token.id} successfully triggered!"
rescue Token::Errors::NoReleaseTargetFound, Project::Errors::WritePermissionError => e
flash[:error] = "Failed to trigger token: #{e.message}"
rescue Backend::NotFoundError => e
flash[:error] = "Failed to trigger token: #{e.summary}"
end

redirect_to tokens_url
end

private

def set_token
@token = Token.find(params[:id])
rescue ActiveRecord::RecordNotFound => e
flash[:error] = e.message
redirect_to tokens_url
end
end
8 changes: 8 additions & 0 deletions src/api/app/policies/token_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,12 @@ def create?
def destroy?
create?
end

def webui_trigger?
record.user == user && !record.type.in?(['Token::Workflow', 'Token::Rss'])
end

def show?
webui_trigger?
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
%li.breadcrumb-item
= link_to('Your Profile', user_path(User.session!))

- if action_name == 'show'
%li.breadcrumb-item.active{ 'aria-current' => 'page' }
Trigger Token
34 changes: 34 additions & 0 deletions src/api/app/views/webui/users/token_triggers/show.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
- @pagetitle = 'Trigger Token'

.card
.card-body
%h3.mb-3= @pagetitle

-# "as: :token" to have a consistent name across all various token classes
= form_for(@token, as: :token, url: token_trigger_path, method: :put, local: false) do |f|
.form-row
.col-12.col-md-10.col-lg-9
.form-group
= f.label(:id, 'Id:')
= @token.id
.form-group
= f.label(:type, 'Operation:')
= @token.class.token_name.capitalize
- token_package = @token.package
- if token_package.present?
= fields_for(:package, token_package) do |token_fields|
.form-group
= token_fields.label(:project, 'Project:')
= token_package.project
.form-group
= token_fields.label(:package, 'Package:')
= token_package.name
- else
.form-group.row
.col-sm
= render partial: 'webui/shared/autocomplete', locals: { html_id: 'project', label: 'Project:', required: true,
data: { source: autocomplete_projects_path(local: true) } }
.col-sm
= render partial: 'webui/shared/autocomplete', locals: { html_id: 'package', label: 'Package:', required: true,
data: { source: autocomplete_packages_path }, disabled: true }
= f.submit('Trigger', class: 'btn btn-primary px-4 mt-3 mt-sm-0')
3 changes: 3 additions & 0 deletions src/api/app/views/webui/users/tokens/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
- if policy(token).edit?
= link_to(edit_token_path(token), title: 'Edit Token') do
%i.fas.fa-edit
- if policy(token).show?
= link_to(token_trigger_path(token), title: 'Trigger Token') do
%i.fas.fa-project-diagram
= link_to '#', title: 'Delete Token',
data: { toggle: 'modal', target: '#delete-token-modal', token_id: token.id, action: token_path(token) } do
%i.fas.fa-times-circle.text-danger
Expand Down
1 change: 1 addition & 0 deletions src/api/config/routes/webui_routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,7 @@
post 'status_messages/:id' => :acknowledge, controller: 'webui/status_messages', as: :acknowledge_status_message

resources :tokens, controller: 'webui/users/tokens'
resources :token_triggers, only: [:show, :update], controller: 'webui/users/token_triggers'
end

get 'home', to: 'webui/webui#home', as: :home
Expand Down
Loading

0 comments on commit c726c88

Please sign in to comment.