issues.rb 3.3 KB
Newer Older
1
module API
N
Nihad Abbasov 已提交
2 3 4 5 6 7 8 9 10 11
  # Issues API
  class Issues < Grape::API
    before { authenticate! }

    resource :issues do
      # Get currently authenticated user's issues
      #
      # Example Request:
      #   GET /issues
      get do
N
Nihad Abbasov 已提交
12
        present paginate(current_user.issues), with: Entities::Issue
N
Nihad Abbasov 已提交
13 14 15 16 17 18 19
      end
    end

    resource :projects do
      # Get a list of project issues
      #
      # Parameters:
20
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
21 22 23
      # Example Request:
      #   GET /projects/:id/issues
      get ":id/issues" do
N
Nihad Abbasov 已提交
24
        present paginate(user_project.issues), with: Entities::Issue
N
Nihad Abbasov 已提交
25 26 27 28 29
      end

      # Get a single project issue
      #
      # Parameters:
30
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
31 32 33 34 35
      #   issue_id (required) - The ID of a project issue
      # Example Request:
      #   GET /projects/:id/issues/:issue_id
      get ":id/issues/:issue_id" do
        @issue = user_project.issues.find(params[:issue_id])
36
        present @issue, with: Entities::Issue
N
Nihad Abbasov 已提交
37 38 39 40 41
      end

      # Create a new project issue
      #
      # Parameters:
42
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
43 44 45 46 47 48 49 50
      #   title (required) - The title of an issue
      #   description (optional) - The description of an issue
      #   assignee_id (optional) - The ID of a user to assign issue
      #   milestone_id (optional) - The ID of a milestone to assign issue
      #   labels (optional) - The labels of an issue
      # Example Request:
      #   POST /projects/:id/issues
      post ":id/issues" do
51
        required_attributes! [:title]
A
Alex Denisov 已提交
52
        attrs = attributes_for_keys [:title, :description, :assignee_id, :milestone_id]
A
Alex Denisov 已提交
53 54
        attrs[:label_list] = params[:labels] if params[:labels].present?
        @issue = user_project.issues.new attrs
N
Nihad Abbasov 已提交
55 56
        @issue.author = current_user
        if @issue.save
57
          present @issue, with: Entities::Issue
N
Nihad Abbasov 已提交
58
        else
59
          not_found!
N
Nihad Abbasov 已提交
60 61 62 63 64 65
        end
      end

      # Update an existing issue
      #
      # Parameters:
66
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
67 68 69 70 71 72
      #   issue_id (required) - The ID of a project issue
      #   title (optional) - The title of an issue
      #   description (optional) - The description of an issue
      #   assignee_id (optional) - The ID of a user to assign issue
      #   milestone_id (optional) - The ID of a milestone to assign issue
      #   labels (optional) - The labels of an issue
73
      #   state_event (optional) - The state event of an issue (close|reopen)
N
Nihad Abbasov 已提交
74 75 76 77
      # Example Request:
      #   PUT /projects/:id/issues/:issue_id
      put ":id/issues/:issue_id" do
        @issue = user_project.issues.find(params[:issue_id])
R
randx 已提交
78 79
        authorize! :modify_issue, @issue

A
Andrew8xx8 已提交
80
        attrs = attributes_for_keys [:title, :description, :assignee_id, :milestone_id, :state_event]
A
Alex Denisov 已提交
81
        attrs[:label_list] = params[:labels] if params[:labels].present?
82
        IssueObserver.current_user = current_user
A
Alex Denisov 已提交
83
        if @issue.update_attributes attrs
84
          present @issue, with: Entities::Issue
N
Nihad Abbasov 已提交
85
        else
86
          not_found!
N
Nihad Abbasov 已提交
87 88 89
        end
      end

90
      # Delete a project issue (deprecated)
N
Nihad Abbasov 已提交
91 92
      #
      # Parameters:
93
      #   id (required) - The ID of a project
N
Nihad Abbasov 已提交
94 95 96 97
      #   issue_id (required) - The ID of a project issue
      # Example Request:
      #   DELETE /projects/:id/issues/:issue_id
      delete ":id/issues/:issue_id" do
98
        not_allowed!
N
Nihad Abbasov 已提交
99 100 101 102
      end
    end
  end
end