-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Staging::StagedRequests create & destroy
These actions will assign / unassign the request to the provided staging project and branch / destroy the package in the staging project. Currently the branching / destroying will block the request, we plan to move this to a background job in the future when we decided on a way to provide users feedback about the status of a job. One limitation is that this currently only supports submit actions. For delete actions we added a TODO for the future.
- Loading branch information
Showing
23 changed files
with
9,324 additions
and
33 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
80 changes: 78 additions & 2 deletions
80
src/api/app/controllers/staging/staged_requests_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 |
---|---|---|
@@ -1,5 +1,81 @@ | ||
class Staging::StagedRequestsController < Staging::ProjectsController | ||
class Staging::StagedRequestsController < ApplicationController | ||
before_action :require_login, except: [:index] | ||
before_action :set_staging_project | ||
before_action :set_staging_workflow, :set_project, :set_xml_hash, only: [:create, :destroy] | ||
|
||
def index | ||
@requests = @project.staged_requests | ||
@requests = @staging_project.staged_requests | ||
end | ||
|
||
def create | ||
authorize @staging_project, :update? | ||
requests = @staging_workflow.unassigned_requests.where(number: request_numbers) | ||
|
||
result = ::Staging::StageRequests.new(requests: requests, staging_project_name: @staging_project.name).perform | ||
@staging_project.staged_requests << result | ||
unassigned_requests = request_numbers - result.pluck(:number).map(&:to_s) | ||
|
||
if unassigned_requests.empty? | ||
render_ok | ||
else | ||
render_error( | ||
status: 400, | ||
errorcode: 'invalid_request', | ||
message: "Could not assign requests #{unassigned_requests.to_sentence} to #{@staging_project}." | ||
) | ||
end | ||
end | ||
|
||
def destroy | ||
authorize @staging_project, :update? | ||
requests = @staging_project.staged_requests.where(number: request_numbers) | ||
package_names = requests.joins(:bs_request_actions).pluck('bs_request_actions.target_package') | ||
|
||
@staging_project.staged_requests.delete(requests) | ||
not_unassigned_requests = request_numbers - requests.pluck(:number).map(&:to_s) | ||
|
||
result = @staging_project.packages.where(name: package_names).destroy_all | ||
not_deleted_packages = package_names - result.pluck(:name) | ||
|
||
if not_unassigned_requests.empty? && not_deleted_packages.empty? | ||
render_ok | ||
else | ||
message = 'Error while unassigning requests: ' | ||
message << "Requests with number #{not_unassigned_requests.to_sentence} not found. " unless not_unassigned_requests.empty? | ||
message << "Could not delete packages #{not_deleted_packages.to_sentence}." unless not_deleted_packages.empty? | ||
render_error( | ||
status: 400, | ||
errorcode: 'invalid_request', | ||
message: message | ||
) | ||
end | ||
end | ||
|
||
private | ||
|
||
def set_xml_hash | ||
@xml_hash = (Xmlhash.parse(request.body.read) || {}).with_indifferent_access | ||
end | ||
|
||
def request_numbers | ||
[@xml_hash[:number]].flatten | ||
end | ||
|
||
def set_staging_project | ||
@staging_project = Staging::StagingProject.find_by!(name: params[:staging_project_name]) | ||
end | ||
|
||
def set_staging_workflow | ||
@staging_workflow = @staging_project.staging_workflow | ||
return if @staging_workflow | ||
render_error( | ||
status: 422, | ||
errorcode: 'invalid_request', | ||
message: "#{@staging_project} is not a valid staging project, can't assign requests to it." | ||
) | ||
end | ||
|
||
def set_project | ||
@project = @staging_workflow.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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
class Staging::StageRequests | ||
include ActiveModel::Model | ||
attr_accessor :requests, :staging_project_name | ||
|
||
def perform | ||
self.result = [] | ||
requests.each do |request| | ||
bs_request_action = request.bs_request_actions.first | ||
if bs_request_action.is_submit? | ||
branch_package(bs_request_action) | ||
elsif bs_request_action.is_delete? | ||
# TODO: implement delete requests | ||
end | ||
result | ||
end | ||
end | ||
|
||
private | ||
|
||
attr_accessor :result | ||
|
||
def branch_package(bs_request_action) | ||
request = bs_request_action.bs_request | ||
BranchPackage.new( | ||
target_project: staging_project_name, | ||
target_package: bs_request_action.target_package, | ||
project: bs_request_action.source_project, | ||
package: bs_request_action.source_package, | ||
extend_package_names: false | ||
).branch | ||
result << request | ||
rescue BranchPackage::DoubleBranchPackageError | ||
# we leave the package there and do not report as success | ||
# because packages might differ | ||
rescue APIError, Backend::Error => e | ||
Airbrake.notify(e, bs_request: request.number) | ||
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
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,23 @@ | ||
class Staging::StagingProjectPolicy < ApplicationPolicy | ||
def initialize(user, record) | ||
raise Pundit::NotAuthorizedError, 'staging workflow does not exist' unless record | ||
@user = user | ||
@record = record | ||
end | ||
|
||
def create? | ||
ProjectPolicy.new(@user, @record).create? | ||
end | ||
|
||
def update? | ||
ProjectPolicy.new(@user, @record).update? | ||
end | ||
|
||
def edit? | ||
update? | ||
end | ||
|
||
def destroy? | ||
ProjectPolicy.new(@user, @record).destroy? | ||
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
Oops, something went wrong.