Project

General

Profile

Revision abf1341e

Added by Ondřej Pražák almost 8 years ago

working widgets

View differences:

app/assets/javascripts/foreman_pipeline/jobs/details/job-details-environments.controller.js
1 1
angular.module('ForemanPipeline.jobs').controller('JobDetailsEnvironmentsController', 
2
    ['$scope', '$q', 'translate', 'Nutupane', 'Job', 'CurrentOrganization', 'Organization',
3
    function ($scope, $q, translate, Nutupane, Job, CurrentOrganization, Organization) {
2
    ['$scope', '$q', 'translate', 'Nutupane', 'Job', 'CurrentOrganization', 'Organization', 'Path',
3
    function ($scope, $q, translate, Nutupane, Job, CurrentOrganization, Organization, Path) {
4 4

  
5 5
        $scope.successMessages = [];
6 6
        $scope.errorMessages = [];
......
16 16

  
17 17
        $scope.chosenEnvironment = $scope.job.environment;
18 18

  
19
        $scope.environments = Organization.readableEnvironments(params, function () {
19
        $scope.environments = Path.allPaths(params, function () {
20 20
            $scope.loading = false;
21 21
        });
22 22

  
......
31 31
                        $scope.successMessages.push(translate("New Lifecycle Environmnet successfully set."))
32 32
                        $scope.working = false;
33 33
                        $scope.job.environment = $scope.chosenEnvironment;
34
                        $scope.job.to_environments = [];
34 35
                    };
35 36

  
36 37
                    error = function (response) {
app/assets/javascripts/foreman_pipeline/jobs/details/job-details-to-environments.controller.js
19 19
            });
20 20

  
21 21
            $scope.setToEnvironments = function () {
22
                console.log($scope.job)
22 23
                var success, 
23 24
                    error,
24 25
                    deferred = $q.defer();                    
25
                    data = {environment_ids: _.map($scope.toEnvironments, function (env) { return env.id })};
26
                    data = {to_environment_ids: _.map($scope.toEnvironments, function (env) { return env.id })};
26 27

  
27 28
                    success = function (response) {
28 29
                        deferred.resolve(response);
......
110 111
            }
111 112

  
112 113
            function itemsLoad(){
113
                var modelIds = _.map(scope.toEnvironments, function (obj) { return obj.id });
114
                var modelIds = _.pluck(scope.toEnvironments, "id"),
115
                    succIds = _.pluck(scope.job.environment.successors, "id");
114 116
                forEachItem(function (item) {
115 117
                    forEachInModel(function (modelItem) {
116 118
                        if (item.id === modelItem.id) {
......
125 127
                        modelIds = _.union(modelIds, [item.id]);
126 128
                    }
127 129
                    item.flag = false;
130
                    if (_.indexOf(succIds, item.id) < 0) {
131
                        item.disabled = true;
132
                    }           
128 133
                });                
129 134
                scope.toEnvironments = [];
130 135
                forEachItem(function (item) {
app/assets/javascripts/foreman_pipeline/jobs/jobs.factory.js
1
angular.module('ForemanPipeline.jobs').factory('Job',
2
    ['BastionResource', 'CurrentOrganization',
3
    function (BastionResource, CurrentOrganization) {
1
(function () {
4 2

  
5
        var transformPaths = function (data) {
3
    var transformPaths = function (data) {
6 4
            return _.map(angular.fromJson(data)["results"], function (path) {
7 5
                return _.map(path["environments"], function (env) {
8 6
                    return env;
9 7
                });
10
            })
11
            
12
        };
13

  
14
        return BastionResource('/../foreman_pipeline/api/organizations/:organizationId/jobs/:id/:action',
15
         {id: '@id', organizationId: CurrentOrganization}, {
16
          
17
            update: {method: 'PUT'},
18
            setContentView: {method: 'PUT', params: {action: 'set_content_view'}},
19
            setHostgroup: {method: 'PUT', params: {action: 'set_hostgroup'}},
20
            setJenkins: {method: 'PUT', params: {action: 'set_jenkins'}},
21
            setResource: {method: 'PUT', params: {action: 'set_resource'}},
22
            setEnvironment: {method: 'PUT', params: {action: 'set_environment'}},
23
            availableResources: {method: 'GET', params: {action: 'available_resources'}, transformResponse: function (response) {                
24
                return {results: angular.fromJson(response)};
25
            }},
26
            runJob: {method: 'GET', params: {action: 'run_job'}},
27
            projects: {method: 'GET', transformResponse: function (response) {
28
                var job = angular.fromJson(response);
29
                return {results: job.jenkins_projects};
30
            }},
31
            addProjects: {method: 'PUT', params: {action: 'add_projects'}},
32
            removeProjects: {method: 'PUT', params: {action: 'remove_projects'}},
33
            addPaths: {method: 'PUT', params: {action: 'add_paths'}},
34
            removePaths: {method: 'PUT', params: {action: 'remove_paths'}},
35
            availablePaths: {method: 'GET', params: {action: 'available_paths'}, transformResponse: function (response) {
36
                return {results: transformPaths(response)};
37
            }},
38
            currentPaths: {method: 'GET', params: {action: 'current_paths'}, transformResponse: function (response) {
39
                return {results: transformPaths(response)};
40
            }},
41
            availableToEnvironments: {method: 'GET',
42
                                    params: {action: 'available_paths'},
43
                                    isArray: true,
44
                                    transformResponse: function (data) {
45
                                       return transformPaths(data);
46
                                    }
47
            },
48
            setToEnvironments: {method: 'PUT', params: {action: 'set_to_environments'}},
49
        });
50
    }]
51
);
52

  
53
angular.module('ForemanPipeline.jobs').factory('Hostgroup',
54
    ['BastionResource',
55
    function (BastionResource) {
56

  
57
        return BastionResource('/../api/v2/hostgroups/:id/:action',
58
            {id: '@id'}, {});
59
    }]
60
);
8
            })  
9
    };
10

  
11
    angular.module('ForemanPipeline.jobs').factory('Job',
12
        ['BastionResource', 'CurrentOrganization',
13
        function (BastionResource, CurrentOrganization) {
14

  
15
            return BastionResource('/../foreman_pipeline/api/organizations/:organizationId/jobs/:id/:action',
16
             {id: '@id', organizationId: CurrentOrganization}, {
17
              
18
                update: {method: 'PUT'},
19
                setContentView: {method: 'PUT', params: {action: 'set_content_view'}},
20
                setHostgroup: {method: 'PUT', params: {action: 'set_hostgroup'}},
21
                setJenkins: {method: 'PUT', params: {action: 'set_jenkins'}},
22
                setResource: {method: 'PUT', params: {action: 'set_resource'}},
23
                setEnvironment: {method: 'PUT', params: {action: 'set_environment'}},
24
                availableResources: {method: 'GET', params: {action: 'available_resources'}, transformResponse: function (response) {                
25
                    return {results: angular.fromJson(response)};
26
                }},
27
                runJob: {method: 'GET', params: {action: 'run_job'}},
28
                projects: {method: 'GET', transformResponse: function (response) {
29
                    var job = angular.fromJson(response);
30
                    return {results: job.jenkins_projects};
31
                }},
32
                addProjects: {method: 'PUT', params: {action: 'add_projects'}},
33
                removeProjects: {method: 'PUT', params: {action: 'remove_projects'}},
34

  
35
                availableToEnvironments: {method: 'GET',
36
                                        params: {action: 'available_paths'},
37
                                        isArray: true,
38
                                        transformResponse: function (data) {
39
                                           return transformPaths(data);
40
                                        }
41
                },
42
                setToEnvironments: {method: 'PUT', params: {action: 'set_to_environments'}},
43
            });
44
        }]
45
    );
46

  
47
    angular.module('ForemanPipeline.jobs').factory('Hostgroup',
48
        ['BastionResource',
49
        function (BastionResource) {
50

  
51
            return BastionResource('/../api/v2/hostgroups/:id/:action',
52
                {id: '@id'}, {});
53
        }]
54
    );
55

  
56
    angular.module('ForemanPipeline.jobs').factory('Path',
57
        ['BastionResource', 'CurrentOrganization', 
58
        function (BastionResource, CurrentOrganization) {
59

  
60
            return BastionResource('/../foreman_pipeline/api/organizations/:organizationId/paths/:action',
61
                {organizationId: CurrentOrganization}, {
62

  
63
                    allPaths: {method: 'GET',
64
                                        params: {action: 'all_paths'},
65
                                        isArray: true,
66
                                        transformResponse: function (data) {
67
                                           return transformPaths(data);
68
                                        }
69
                    },
70
                });
71
        }]
72
    );
73
})();
74

  
app/controllers/foreman_pipeline/api/jobs_controller.rb
9 9
    before_filter :find_job, :only => [:update, :show, :destroy, :set_content_view,
10 10
                                       :set_hostgroup, :set_resource, :available_resources,
11 11
                                       :set_jenkins, :set_environment, :run_job,
12
                                       :add_projects, :remove_projects, :set_paths,
13
                                       :remove_paths, :add_paths, :current_paths, :available_paths]
12
                                       :add_projects, :remove_projects, :set_to_environments,
13
                                       :available_paths]
14 14

  
15 15
    before_filter :load_search_service, :only => [:index]
16 16

  
......
109 109
    param :environment_id, :number, :desc => N_("Environment id which will be set"), :required => true
110 110
    def set_environment
111 111
      @job.environment = Katello::KTEnvironment.find(params[:environment_id])
112
      @job.to_environments = []
112 113
      @job.save!
113 114
      respond_for_show
114 115
    end    
......
126 127
      end
127 128
    end
128 129

  
129
    api :PUT, "/organizations/:organization_id/jobs/:id/add_paths", N_("Add environment paths for job")
130
    param_group :job_id
131
    param :path_ids, Array, :desc => N_("Identifiers of environments which are library's successors in corresponding paths")
132
    def add_paths
133
      @job.path_ids = (@job.path_ids + params[:path_ids]).uniq
134
      @job.save!
135
      respond_for_show
136
    end
137

  
138
    api :PUT, "/organizations/:organization_id/jobs/:id/remove_paths", N_("Remove environment paths for job")
139
    param_group :job_id
140
    param :path_ids, Array, :desc => N_("Identifiers of environments which are library's successors in corresponding paths")
141
    def remove_paths
142
      @job.path_ids = (@job.path_ids - params[:path_ids]).uniq
143
      @job.environment = nil unless @job.environment_in_paths? @job.environment_id
144
      @job.save!
145
      respond_for_show
130
    # api :PUT, "/organizations/:organization_id/jobs/:id/add_paths", N_("Add environment paths for job")
131
    # param_group :job_id
132
    # param :path_ids, Array, :desc => N_("Identifiers of environments which are library's successors in corresponding paths")
133
    def set_to_environments
134
      is_ok = params[:to_environment_ids].map do |new_id|
135
        @job.environment.successors.map(&:id).include? new_id
136
      end.all?
137
      if is_ok
138
        @job.to_environment_ids
139
        @job.save!
140
        respond_for_show
141
      else
142
        fail Katello::HttpErrors::Conflict, "Only Environments that are direct successors of Job's Environment may be set as target Environments." 
143
      end
146 144
    end
147 145

  
148 146
    api :GET, "/organizations/:organization_id/jobs/:id/available_paths", N_("List environment paths available for a job")
app/controllers/foreman_pipeline/api/paths_controller.rb
1
module ForemanPipeline
2
  class Api::PathsController < Katello::Api::V2::ApiController
3
    respond_to :json
4

  
5
    include Api::Rendering
6

  
7
    before_filter :find_organization, :only => [:all_paths]
8

  
9
    def all_paths
10
      env_paths = if params[:permission_type] == "promotable"
11
                    @organization.promotable_promotion_paths
12
                  else
13
                    @organization.readable_promotion_paths
14
                  end
15

  
16
      paths = env_paths.inject([]) do |result, path|
17
        result << { :environments => [@organization.library] + path }
18
      end
19
      paths = [{ :environments => [@organization.library] }] if paths.empty?
20

  
21
      collection = {
22
        :results => paths,
23
        :total => paths.size,
24
        :subtotal => paths.size
25
      }
26
      respond_for_index(:collection => collection, :template => '../jobs/available_paths')
27
    end 
28
    
29
  end
30
end
app/views/foreman_pipeline/api/environments/show.json.rabl
6 6

  
7 7
extends 'katello/api/v2/common/timestamps'
8 8

  
9
node :prior do |env|
10
  if env.prior
11
    {name: env.prior.name, :id => env.prior.id}
12
  end
13
end
14

  
15
node :successor do |env|
16
  if !env.library && env.successor
17
    {name: env.successor.name, :id => env.successor.id}
18
  end
9
child :successors => :successors do |env|
10
  attributes :name, :id, :label, :library
19 11
end
app/views/foreman_pipeline/api/jobs/current_paths.json.rabl
1
object Katello::Util::Data.ostructize(@collection)
2

  
3
extends "foreman_pipeline/api/environments/index"
config/routes.rb
23 23
            get :run_job
24 24
            put :add_projects
25 25
            put :remove_projects
26
            put :add_paths
27
            put :remove_paths
26
            put :set_to_environments            
28 27
            get :available_paths
29
            get :current_paths
30 28
          end
31 29
        end
32 30

  
......
35 33
            get :check_jenkins
36 34
            put :set_jenkins_user
37 35
          end
38

  
39

  
40 36
        end
41 37

  
42 38
        api_resources :jenkins_projects, :only => [:show, :update]
......
49 45

  
50 46
        api_resources :jenkins_project_params, :only => [:update]
51 47

  
52
        api_resources :jenkins_users, :only => [:index, :create, :destroy, :show, :update]  
48
        api_resources :jenkins_users, :only => [:index, :create, :destroy, :show, :update]
49

  
50
        api_resources :paths, :only => [] do
51
          collection do
52
            get :all_paths
53
          end
54
        end
53 55
        
54 56
      end
55 57
    end

Also available in: Unified diff