summaryrefslogtreecommitdiff
path: root/lib/api/issue_links.rb
blob: 6cc5b344f47e5782c80df5b278e26e6b7e6b5c62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# frozen_string_literal: true

module API
  class IssueLinks < Grape::API::Instance
    include PaginationParams

    before { authenticate! }

    params do
      requires :id, type: String, desc: 'The ID of a project'
      requires :issue_iid, type: Integer, desc: 'The internal ID of a project issue'
    end
    resource :projects, requirements: { id: %r{[^/]+} } do
      desc 'Get related issues' do
        success Entities::RelatedIssue
      end
      get ':id/issues/:issue_iid/links' do
        source_issue = find_project_issue(params[:issue_iid])
        related_issues = source_issue.related_issues(current_user)

        present related_issues,
                with: Entities::RelatedIssue,
                current_user: current_user,
                project: user_project
      end

      desc 'Relate issues' do
        success Entities::IssueLink
      end
      params do
        requires :target_project_id, type: String, desc: 'The ID of the target project'
        requires :target_issue_iid, type: Integer, desc: 'The IID of the target issue'
        optional :link_type, type: String, values: IssueLink.link_types.keys,
          desc: 'The type of the relation'
      end
      # rubocop: disable CodeReuse/ActiveRecord
      post ':id/issues/:issue_iid/links' do
        source_issue = find_project_issue(params[:issue_iid])
        target_issue = find_project_issue(declared_params[:target_issue_iid],
                                          declared_params[:target_project_id])

        create_params = { target_issuable: target_issue, link_type: declared_params[:link_type] }

        result = ::IssueLinks::CreateService
                   .new(source_issue, current_user, create_params)
                   .execute

        if result[:status] == :success
          issue_link = IssueLink.find_by!(source: source_issue, target: target_issue)

          present issue_link, with: Entities::IssueLink
        else
          render_api_error!(result[:message], result[:http_status])
        end
      end
      # rubocop: enable CodeReuse/ActiveRecord

      desc 'Remove issues relation' do
        success Entities::IssueLink
      end
      params do
        requires :issue_link_id, type: Integer, desc: 'The ID of an issue link'
      end
      delete ':id/issues/:issue_iid/links/:issue_link_id' do
        issue_link = IssueLink.find(declared_params[:issue_link_id])

        find_project_issue(params[:issue_iid])
        find_project_issue(issue_link.target.iid.to_s, issue_link.target.project_id.to_s)

        result = ::IssueLinks::DestroyService
                   .new(issue_link, current_user)
                   .execute

        if result[:status] == :success
          present issue_link, with: Entities::IssueLink
        else
          render_api_error!(result[:message], result[:http_status])
        end
      end
    end
  end
end