|
5 | 5 | module Rubyists |
6 | 6 | # Namespace for Linear |
7 | 7 | module Linear |
8 | | - L :api |
9 | | - L :fragments |
10 | | - M :base_model |
11 | | - M :user |
| 8 | + M :base_model, :user |
12 | 9 | Issue = Class.new(BaseModel) |
13 | 10 | # The Issue class represents a Linear issue. |
14 | | - class Issue |
| 11 | + class Issue # rubocop:disable Metrics/ClassLength |
15 | 12 | include SemanticLogger::Loggable |
| 13 | + has_one :assignee, :User |
| 14 | + has_one :team, :Team |
16 | 15 |
|
17 | 16 | BASIC_FILTER = { completedAt: { null: true } }.freeze |
18 | 17 |
|
19 | 18 | Base = fragment('BaseIssue', 'Issue') do |
20 | 19 | id |
21 | 20 | identifier |
22 | 21 | title |
23 | | - assignee { ___ User::Base } |
24 | 22 | branchName |
25 | 23 | description |
26 | 24 | createdAt |
27 | 25 | updatedAt |
28 | 26 | end |
29 | 27 |
|
30 | 28 | class << self |
| 29 | + def base_fragment |
| 30 | + @base_fragment ||= fragment('IssueWithTeams', 'Issue') do |
| 31 | + ___ Base |
| 32 | + assignee { ___ User.base_fragment } |
| 33 | + team { ___ Team.base_fragment } |
| 34 | + end |
| 35 | + end |
| 36 | + |
31 | 37 | def find(slug) |
32 | | - q = query { issue(id: slug) { ___ Base } } |
| 38 | + q = query { issue(id: slug) { ___ Issue.base_fragment } } |
33 | 39 | data = Api.query(q) |
34 | 40 | raise NotFoundError, "Issue not found: #{slug}" if data.nil? |
35 | 41 |
|
36 | 42 | new(data[:issue]) |
37 | 43 | end |
38 | 44 |
|
| 45 | + def find_all(*slugs) |
| 46 | + slugs.flatten.map { |slug| find(slug) } |
| 47 | + end |
| 48 | + |
39 | 49 | def create(title:, description:, team:, labels: []) |
40 | 50 | team_id = team.id |
41 | 51 | label_ids = labels.map(&:id) |
42 | 52 | input = { title:, description:, teamId: team_id } |
43 | | - input.merge!(labelIds: label_ids) unless label_ids.empty? |
44 | | - m = mutation { issueCreate(input:) { issue { ___ Base } } } |
45 | | - data = Api.query(m) |
46 | | - new(data[:issueCreate][:issue]) |
| 53 | + input[:labelIds] = label_ids unless label_ids.empty? |
| 54 | + m = mutation { issueCreate(input:) { issue { ___ Issue.base_fragment } } } |
| 55 | + query_data = Api.query(m) |
| 56 | + new query_data.dig(:issueCreate, :issue) |
47 | 57 | end |
48 | 58 | end |
49 | 59 |
|
50 | | - def assign!(user) |
| 60 | + def comment_fragment |
| 61 | + @comment_fragment ||= fragment('Comment', 'Comment') do |
| 62 | + id |
| 63 | + body |
| 64 | + url |
| 65 | + end |
| 66 | + end |
| 67 | + |
| 68 | + # Reference for this mutation: |
| 69 | + # https://studio.apollographql.com/public/Linear-API/variant/current/schema/reference/inputs/CommentCreateInput |
| 70 | + def add_comment(comment) |
| 71 | + id_for_this = identifier |
| 72 | + comment_frag = comment_fragment |
| 73 | + m = mutation { commentCreate(input: { issueId: id_for_this, body: comment }) { comment { ___ comment_frag } } } |
| 74 | + |
| 75 | + query_data = Api.query(m) |
| 76 | + query_data.dig(:commentCreate, :comment) |
| 77 | + self |
| 78 | + end |
| 79 | + |
| 80 | + def close_mutation(close_state, trash: false) |
51 | 81 | id_for_this = identifier |
52 | | - m = mutation { issueUpdate(id: id_for_this, input: { assigneeId: user.id }) { issue { ___ Base } } } |
53 | | - data = Api.query(m) |
54 | | - updated = data.dig(:issueUpdate, :issue) |
| 82 | + input = { stateId: close_state.id } |
| 83 | + input[:trash] = true if trash |
| 84 | + mutation { issueUpdate(id: id_for_this, input:) { issue { ___ Issue.base_fragment } } } |
| 85 | + end |
| 86 | + |
| 87 | + def close!(state: nil, trash: false) |
| 88 | + logger.warn "Using first completed state found: #{completed_states.first}" if state.nil? |
| 89 | + state ||= completed_states.first |
| 90 | + query_data = Api.query close_mutation(state, trash:) |
| 91 | + updated = query_data.dig(:issueUpdate, :issue) |
| 92 | + raise SmellsBad, "Unknown response for issue close: #{data} (should have :issueUpdate key)" if updated.nil? |
| 93 | + |
| 94 | + @data = @updated_data = updated |
| 95 | + self |
| 96 | + end |
| 97 | + |
| 98 | + def assign!(user) |
| 99 | + this_id = identifier |
| 100 | + m = mutation { issueUpdate(id: this_id, input: { assigneeId: user.id }) { issue { ___ Issue.base_fragment } } } |
| 101 | + query_data = Api.query(m) |
| 102 | + updated = query_data.dig(:issueUpdate, :issue) |
55 | 103 | raise SmellsBad, "Unknown response for issue update: #{data} (should have :issueUpdate key)" if updated.nil? |
56 | 104 |
|
57 | | - Issue.new updated |
| 105 | + @data = @updated_data = updated |
| 106 | + self |
| 107 | + end |
| 108 | + |
| 109 | + def workflow_states |
| 110 | + @workflow_states ||= team.workflow_states |
58 | 111 | end |
59 | 112 |
|
60 | 113 | def inspection |
61 | 114 | format('id: "%<identifier>s" title: "%<title>s"', identifier:, title:) |
62 | 115 | end |
63 | 116 |
|
64 | 117 | def to_s |
65 | | - basic = format('%<id>-12s %<title>s', id: data[:identifier], title: data[:title]) |
| 118 | + basic = format('%<id>-12s %<title>s', id: identifier, title:) |
66 | 119 | return basic unless (name = data.dig(:assignee, :name)) |
67 | 120 |
|
68 | 121 | format('%<basic>s (%<name>s)', basic:, name:) |
69 | 122 | end |
70 | 123 |
|
| 124 | + def parsed_description |
| 125 | + return TTY::Markdown.parse(description) if description && !description.empty? |
| 126 | + |
| 127 | + TTY::Markdown.parse(['# No Description For this issue??', |
| 128 | + 'Issues really need description', |
| 129 | + "## What's up with that?"].join("\n")) |
| 130 | + rescue StandardError => e |
| 131 | + logger.error 'Error parsing description', e |
| 132 | + "Description was unparsable: #{description}\n" |
| 133 | + end |
| 134 | + |
71 | 135 | def full |
72 | 136 | sep = '-' * to_s.length |
73 | | - format("%<to_s>s\n%<sep>s\n%<description>s\n", |
74 | | - sep:, |
75 | | - to_s:, |
76 | | - description: (TTY::Markdown.parse(data[:description]) rescue 'No Description?')) # rubocop:disable Style/RescueModifier |
| 137 | + format("%<to_s>s\n%<sep>s\n%<description>s\n", sep:, to_s:, description: parsed_description) |
77 | 138 | end |
78 | 139 |
|
79 | 140 | def display(options) |
|
0 commit comments