- def index
- want_ancestor = @where[:script_version_descends_from]
- if want_ancestor
- # Check for missing commit_ancestor rows, and create them if
- # possible.
- @objects.
- dup.
- includes(:commit_ancestors). # I wish Rails would let me
- # specify here which
- # commit_ancestors I am
- # interested in.
- each do |o|
- if o.commit_ancestors.
- select { |ca| ca.ancestor == want_ancestor }.
- empty? and !o.script_version.nil?
- begin
- o.commit_ancestors << CommitAncestor.find_or_create_by_descendant_and_ancestor(o.script_version, want_ancestor)
- rescue
+ def create
+ [:repository, :script, :script_version, :script_parameters].each do |r|
+ if !resource_attrs[r]
+ return render json: {
+ :errors => ["#{r} attribute must be specified"]
+ }, status: :unprocessable_entity
+ end
+ end
+ load_filters_param
+
+ # We used to ask for the minimum_, exclude_, and no_reuse params
+ # in the job resource. Now we advertise them as flags that alter
+ # the behavior of the create action.
+ [:minimum_script_version, :exclude_script_versions].each do |attr|
+ if resource_attrs.has_key? attr
+ params[attr] = resource_attrs.delete attr
+ end
+ end
+ if resource_attrs.has_key? :no_reuse
+ params[:find_or_create] = !resource_attrs.delete(:no_reuse)
+ end
+
+ if params[:find_or_create]
+ # Convert old special-purpose creation parameters to the new
+ # filters-based method.
+ minimum_script_version = params[:minimum_script_version]
+ exclude_script_versions = params.fetch(:exclude_script_versions, [])
+ @filters.select do |(col_name, operand, operator)|
+ case col_name
+ when "script_version"
+ case operand
+ when "in range"
+ minimum_script_version = operator
+ false
+ when "not in", "not in range"
+ begin
+ exclude_script_versions += operator
+ rescue TypeError
+ exclude_script_versions << operator
+ end
+ false
+ else
+ true