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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use clap::{Args, Subcommand};
use serde_json::json;
use crate::client::LinearClient;
#[derive(Args, Debug)]
pub struct RelationsArgs {
#[command(subcommand)]
pub command: RelationsCommand,
}
#[derive(Subcommand, Debug)]
pub enum RelationsCommand {
/// List all relations for an issue
List {
/// Issue identifier (e.g., ENG-123)
identifier: String,
},
/// Create "A blocks B" relation
Blocks {
/// Blocking issue identifier
issue_a: String,
/// Blocked issue identifier
issue_b: String,
},
/// Create "A is blocked by B" relation
BlockedBy {
/// Blocked issue identifier
issue_a: String,
/// Blocking issue identifier
issue_b: String,
},
/// Create "related" relation
Relates {
/// First issue identifier
issue_a: String,
/// Second issue identifier
issue_b: String,
},
/// Create "duplicate" relation
Duplicate {
/// Duplicate issue identifier
issue_a: String,
/// Original issue identifier
issue_b: String,
},
/// Remove a relation
Remove {
/// Relation ID
relation_id: String,
},
}
pub async fn execute(
args: &RelationsArgs,
json: bool,
debug: bool,
workspace: Option<&str>,
) -> anyhow::Result<()> {
let client = LinearClient::new(None, debug, workspace)?;
match &args.command {
RelationsCommand::List { identifier } => {
let query = r#"
query($id: String!) {
issue(id: $id) {
identifier title
relations {
nodes {
id type
relatedIssue {
identifier title
state { name }
}
}
}
inverseRelations {
nodes {
id type
issue {
identifier title
state { name }
}
}
}
}
}
"#;
let variables = json!({ "id": identifier });
let result = client.query_raw(query, Some(variables)).await?;
if json {
crate::output::print_json(&result);
} else {
let issue = result
.pointer("/data/issue")
.ok_or_else(|| anyhow::anyhow!("Issue not found: {identifier}"))?;
let ident = issue
.get("identifier")
.and_then(|v| v.as_str())
.unwrap_or(identifier);
println!("\n Relations for {}", crate::output::color::bold(ident));
println!();
let mut found = false;
if let Some(rels) = issue.pointer("/relations/nodes").and_then(|v| v.as_array()) {
for rel in rels {
found = true;
let rel_type = rel.get("type").and_then(|v| v.as_str()).unwrap_or("?");
let rel_id = rel.get("id").and_then(|v| v.as_str()).unwrap_or("");
let related = rel
.pointer("/relatedIssue/identifier")
.and_then(|v| v.as_str())
.unwrap_or("?");
let title = rel
.pointer("/relatedIssue/title")
.and_then(|v| v.as_str())
.unwrap_or("");
println!(
" {} {} {} {}",
crate::output::color::bold(related),
crate::output::color::cyan(rel_type),
title,
crate::output::color::dim(rel_id),
);
}
}
if let Some(inv) = issue
.pointer("/inverseRelations/nodes")
.and_then(|v| v.as_array())
{
for rel in inv {
found = true;
let rel_type = rel.get("type").and_then(|v| v.as_str()).unwrap_or("?");
let rel_id = rel.get("id").and_then(|v| v.as_str()).unwrap_or("");
let related = rel
.pointer("/issue/identifier")
.and_then(|v| v.as_str())
.unwrap_or("?");
let title = rel
.pointer("/issue/title")
.and_then(|v| v.as_str())
.unwrap_or("");
let inverse_label = match rel_type {
"blocks" => "blocked by",
"duplicate" => "duplicate of",
_ => rel_type,
};
println!(
" {} {} {} {}",
crate::output::color::bold(related),
crate::output::color::cyan(inverse_label),
title,
crate::output::color::dim(rel_id),
);
}
}
if !found {
println!(" No relations found.");
}
}
}
RelationsCommand::Blocks { issue_a, issue_b } => {
create_relation(&client, issue_a, issue_b, "blocks", json).await?;
}
RelationsCommand::BlockedBy { issue_a, issue_b } => {
// "A blocked-by B" means B blocks A
create_relation(&client, issue_b, issue_a, "blocks", json).await?;
}
RelationsCommand::Relates { issue_a, issue_b } => {
create_relation(&client, issue_a, issue_b, "related", json).await?;
}
RelationsCommand::Duplicate { issue_a, issue_b } => {
create_relation(&client, issue_a, issue_b, "duplicate", json).await?;
}
RelationsCommand::Remove { relation_id } => {
let query = r#"
mutation($id: String!) {
issueRelationDelete(id: $id) {
success
}
}
"#;
let variables = json!({ "id": relation_id });
let result = client.query_raw(query, Some(variables)).await?;
if json {
crate::output::print_json(&result);
} else {
let success = result
.pointer("/data/issueRelationDelete/success")
.and_then(|v| v.as_bool())
.unwrap_or(false);
if success {
println!(
" {} Removed relation {}",
crate::output::color::green("OK"),
crate::output::color::bold(relation_id),
);
} else {
println!(
" {} Failed to remove relation",
crate::output::color::red("ERROR")
);
}
}
}
}
Ok(())
}
async fn create_relation(
client: &LinearClient,
issue_id: &str,
related_issue_id: &str,
rel_type: &str,
json_mode: bool,
) -> anyhow::Result<()> {
let query = r#"
mutation($input: IssueRelationCreateInput!) {
issueRelationCreate(input: $input) {
success
issueRelation { id type }
}
}
"#;
let variables = json!({
"input": {
"issueId": issue_id,
"relatedIssueId": related_issue_id,
"type": rel_type,
}
});
let result = client.query_raw(query, Some(variables)).await?;
if json_mode {
crate::output::print_json(&result);
} else {
let success = result
.pointer("/data/issueRelationCreate/success")
.and_then(|v| v.as_bool())
.unwrap_or(false);
if success {
println!(
" {} Created {} relation: {} -> {}",
crate::output::color::green("OK"),
rel_type,
crate::output::color::bold(issue_id),
crate::output::color::bold(related_issue_id),
);
} else {
println!(
" {} Failed to create relation",
crate::output::color::red("ERROR")
);
}
}
Ok(())
}