-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11448 from krauselukas/feature/token_ui_trigger
Trigger token through the webui
- Loading branch information
Showing
50 changed files
with
2,430 additions
and
2,303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/api/app/controllers/webui/users/token_triggers_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/api/app/views/webui/users/token_triggers/_breadcrumb_items.html.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
src/api/app/views/webui/users/token_triggers/show.html.haml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.