cliban-sync 0.3.0

cliban issue-tracker bridges: import from and push to Linear
Documentation
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
//! Typed GraphQL operations, generic over [`GraphQl`] so tests can drive them
//! with canned JSON.
//!
//! The field selections here and the structs in [`super::model`] have to agree.
//! [`ISSUE_FIELDS`] is shared by every query and mutation that returns an issue
//! so there is exactly one place where that agreement is maintained.

use serde_json::{json, Value};

use super::client::GraphQl;
use super::model::{Issue, Nodes, Team};
use crate::error::{Error, Result};

/// The issue selection, shared by every operation returning an issue.
pub const ISSUE_FIELDS: &str = r#"
    id
    identifier
    title
    description
    url
    updatedAt
    priority
    dueDate
    state { id name type position }
    team { id key name }
    labels { nodes { name } }
"#;

/// Look an issue up by its human key, e.g. `ENG-412`.
///
/// Filtering on team key + number rather than passing the identifier to
/// `issue(id:)`: the identifier form is not documented to work for every
/// workspace, whereas the filter form is plain schema.
pub async fn issue_by_key<G: GraphQl>(api: &G, team_key: &str, number: i64) -> Result<Issue> {
    let doc = format!(
        r#"query IssueByKey($team: String!, $number: Float!) {{
            issues(filter: {{ team: {{ key: {{ eq: $team }} }}, number: {{ eq: $number }} }}, first: 1) {{
                nodes {{ {ISSUE_FIELDS} }}
            }}
        }}"#
    );
    let data = api
        .query(&doc, json!({ "team": team_key, "number": number as f64 }))
        .await?;
    let nodes: Nodes<Issue> = take(data, "issues")?;
    nodes
        .first()
        .ok_or_else(|| Error::NotFound(format!("Linear issue {team_key}-{number}")))
}

/// Fetch an issue by its Linear UUID. Used on push, where the link already
/// records the stable id.
pub async fn issue_by_id<G: GraphQl>(api: &G, id: &str) -> Result<Issue> {
    let doc = format!(
        r#"query IssueById($id: String!) {{
            issue(id: $id) {{ {ISSUE_FIELDS} }}
        }}"#
    );
    let data = api.query(&doc, json!({ "id": id })).await?;
    let issue = data.get("issue").cloned().unwrap_or(Value::Null);
    if issue.is_null() {
        return Err(Error::NotFound(format!("Linear issue {id}")));
    }
    Ok(serde_json::from_value(issue)?)
}

/// A team and its workflow states, for status mapping and issue creation.
pub async fn team_by_key<G: GraphQl>(api: &G, key: &str) -> Result<Team> {
    let doc = r#"query TeamByKey($key: String!) {
        teams(filter: { key: { eq: $key } }, first: 1) {
            nodes {
                id
                key
                name
                states { nodes { id name type position } }
            }
        }
    }"#;
    let data = api.query(doc, json!({ "key": key })).await?;
    let nodes: Nodes<Team> = take(data, "teams")?;
    nodes
        .first()
        .ok_or_else(|| Error::NotFound(format!("Linear team {key}")))
}

/// Fields for a new Linear issue. Only what `push --create` sets.
#[derive(Debug, Clone)]
pub struct NewIssue {
    pub team_id: String,
    pub title: String,
    pub description: String,
    pub state_id: Option<String>,
    pub priority: f64,
}

pub async fn create_issue<G: GraphQl>(api: &G, new: NewIssue) -> Result<Issue> {
    let doc = format!(
        r#"mutation IssueCreate($input: IssueCreateInput!) {{
            issueCreate(input: $input) {{
                success
                issue {{ {ISSUE_FIELDS} }}
            }}
        }}"#
    );
    let mut input = json!({
        "teamId": new.team_id,
        "title": new.title,
        "description": new.description,
        "priority": new.priority,
    });
    if let Some(state_id) = new.state_id {
        input["stateId"] = json!(state_id);
    }
    let data = api.query(&doc, json!({ "input": input })).await?;
    payload_issue(data, "issueCreate")
}

/// Fields `push` may change on an existing Linear issue. `None` means "leave
/// it alone" — the bridge never writes a field the user did not ask for.
#[derive(Debug, Clone, Default)]
pub struct IssuePatch {
    pub state_id: Option<String>,
    pub description: Option<String>,
}

impl IssuePatch {
    pub fn is_empty(&self) -> bool {
        self.state_id.is_none() && self.description.is_none()
    }
}

pub async fn update_issue<G: GraphQl>(api: &G, id: &str, patch: IssuePatch) -> Result<Issue> {
    let doc = format!(
        r#"mutation IssueUpdate($id: String!, $input: IssueUpdateInput!) {{
            issueUpdate(id: $id, input: $input) {{
                success
                issue {{ {ISSUE_FIELDS} }}
            }}
        }}"#
    );
    let mut input = json!({});
    if let Some(state_id) = patch.state_id {
        input["stateId"] = json!(state_id);
    }
    if let Some(description) = patch.description {
        input["description"] = json!(description);
    }
    let data = api.query(&doc, json!({ "id": id, "input": input })).await?;
    payload_issue(data, "issueUpdate")
}

/// Post a comment. Returns nothing useful — a comment either lands or errors.
pub async fn create_comment<G: GraphQl>(api: &G, issue_id: &str, body: &str) -> Result<()> {
    let doc = r#"mutation CommentCreate($input: CommentCreateInput!) {
        commentCreate(input: $input) { success }
    }"#;
    let data = api
        .query(
            doc,
            json!({ "input": { "issueId": issue_id, "body": body } }),
        )
        .await?;
    require_success(&data, "commentCreate")
}

/// Split `ENG-412` into `("ENG", 412)`.
///
/// Team keys can contain digits and hyphens (`WEB-2`, `A-B-12`), so the split
/// is on the *last* hyphen, and the tail has to be entirely numeric.
pub fn parse_issue_key(key: &str) -> Result<(String, i64)> {
    let trimmed = key.trim();
    let (team, number) = trimmed
        .rsplit_once('-')
        .ok_or_else(|| Error::NotFound(format!("{trimmed:?} is not a Linear issue key")))?;
    let number: i64 = number.parse().map_err(|_| {
        Error::NotFound(format!(
            "{trimmed:?} is not a Linear issue key (expected TEAM-123)"
        ))
    })?;
    if team.is_empty() {
        return Err(Error::NotFound(format!(
            "{trimmed:?} is not a Linear issue key (no team)"
        )));
    }
    Ok((team.to_uppercase(), number))
}

// ---- envelope helpers ----

fn take<T: serde::de::DeserializeOwned>(data: Value, field: &str) -> Result<T> {
    let raw = data
        .get(field)
        .cloned()
        .ok_or_else(|| Error::Unexpected(format!("response had no `{field}` field")))?;
    Ok(serde_json::from_value(raw)?)
}

/// Pull the issue out of a mutation payload, checking `success` first so a
/// refusal is reported as a refusal rather than as a parse failure.
fn payload_issue(data: Value, field: &str) -> Result<Issue> {
    require_success(&data, field)?;
    let raw = data
        .get(field)
        .and_then(|p| p.get("issue"))
        .cloned()
        .unwrap_or(Value::Null);
    if raw.is_null() {
        return Err(Error::Unexpected(format!(
            "{field} reported success but returned no issue"
        )));
    }
    Ok(serde_json::from_value(raw)?)
}

fn require_success(data: &Value, field: &str) -> Result<()> {
    let payload = data
        .get(field)
        .ok_or_else(|| Error::Unexpected(format!("response had no `{field}` field")))?;
    match payload.get("success").and_then(Value::as_bool) {
        Some(true) => Ok(()),
        Some(false) => Err(Error::Api(vec![format!("{field} reported success: false")])),
        None => Err(Error::Unexpected(format!(
            "{field} payload had no `success` field"
        ))),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::cell::RefCell;

    /// Records what was asked and replies with whatever was queued.
    struct Fake {
        replies: RefCell<Vec<Value>>,
        seen: RefCell<Vec<(String, Value)>>,
    }

    impl Fake {
        fn new(replies: Vec<Value>) -> Self {
            Self {
                replies: RefCell::new(replies),
                seen: RefCell::new(Vec::new()),
            }
        }
        fn last_vars(&self) -> Value {
            self.seen.borrow().last().unwrap().1.clone()
        }
        fn last_doc(&self) -> String {
            self.seen.borrow().last().unwrap().0.clone()
        }
    }

    impl GraphQl for Fake {
        async fn query(&self, doc: &str, vars: Value) -> Result<Value> {
            self.seen.borrow_mut().push((doc.to_string(), vars));
            let mut replies = self.replies.borrow_mut();
            if replies.is_empty() {
                return Err(Error::Unexpected("fake ran out of replies".into()));
            }
            Ok(replies.remove(0))
        }
    }

    fn issue_json() -> Value {
        json!({
            "id": "uuid-1",
            "identifier": "ENG-412",
            "title": "Fix the thing",
            "description": "spec",
            "url": "https://linear.app/acme/issue/ENG-412",
            "updatedAt": "2026-07-29T12:00:00.000Z",
            "priority": 2,
            "dueDate": null,
            "state": {"id": "s1", "name": "Todo", "type": "unstarted", "position": 1.0},
            "team": {"id": "t1", "key": "ENG", "name": "Engineering"},
            "labels": {"nodes": [{"name": "bug"}]}
        })
    }

    #[test]
    fn parse_issue_key_splits_on_the_last_hyphen() {
        assert_eq!(parse_issue_key("ENG-412").unwrap(), ("ENG".into(), 412));
        // Team keys can contain hyphens; splitting on the first would break.
        assert_eq!(parse_issue_key("A-B-12").unwrap(), ("A-B".into(), 12));
        assert_eq!(parse_issue_key("  eng-7  ").unwrap(), ("ENG".into(), 7));
    }

    #[test]
    fn parse_issue_key_rejects_things_that_are_not_keys() {
        for bad in ["ENG", "ENG-", "-12", "ENG-abc", ""] {
            assert!(parse_issue_key(bad).is_err(), "{bad:?} should not parse");
        }
    }

    #[tokio::test]
    async fn issue_by_key_sends_team_and_number_and_parses_the_node() {
        let fake = Fake::new(vec![json!({"issues": {"nodes": [issue_json()]}})]);
        let issue = issue_by_key(&fake, "ENG", 412).await.unwrap();
        assert_eq!(issue.identifier, "ENG-412");
        assert_eq!(issue.label_names(), vec!["bug"]);
        let vars = fake.last_vars();
        assert_eq!(vars["team"], "ENG");
        assert_eq!(vars["number"], 412.0);
    }

    #[tokio::test]
    async fn issue_by_key_reports_not_found_for_an_empty_node_list() {
        let fake = Fake::new(vec![json!({"issues": {"nodes": []}})]);
        let err = issue_by_key(&fake, "ENG", 999).await.unwrap_err();
        assert!(err.to_string().contains("ENG-999"), "{err}");
    }

    #[tokio::test]
    async fn issue_by_id_reports_not_found_for_a_null_issue() {
        let fake = Fake::new(vec![json!({"issue": null})]);
        let err = issue_by_id(&fake, "uuid-nope").await.unwrap_err();
        assert!(err.to_string().contains("uuid-nope"), "{err}");
    }

    #[tokio::test]
    async fn team_by_key_parses_the_state_list() {
        let fake = Fake::new(vec![json!({"teams": {"nodes": [{
            "id": "t1", "key": "ENG", "name": "Engineering",
            "states": {"nodes": [
                {"id": "s1", "name": "Todo", "type": "unstarted", "position": 1.0},
                {"id": "s2", "name": "Done", "type": "completed", "position": 2.0}
            ]}
        }]}})]);
        let team = team_by_key(&fake, "ENG").await.unwrap();
        assert_eq!(team.states.nodes.len(), 2);
        assert_eq!(team.states.nodes[1].name, "Done");
    }

    #[tokio::test]
    async fn update_issue_omits_fields_the_caller_left_unset() {
        let fake = Fake::new(vec![
            json!({"issueUpdate": {"success": true, "issue": issue_json()}}),
        ]);
        let patch = IssuePatch {
            state_id: Some("s2".into()),
            description: None,
        };
        update_issue(&fake, "uuid-1", patch).await.unwrap();
        let input = &fake.last_vars()["input"];
        assert_eq!(input["stateId"], "s2");
        assert!(
            input.get("description").is_none(),
            "an unset field must not be sent: {input}"
        );
    }

    #[tokio::test]
    async fn create_issue_sends_the_state_only_when_given() {
        let fake = Fake::new(vec![
            json!({"issueCreate": {"success": true, "issue": issue_json()}}),
        ]);
        create_issue(
            &fake,
            NewIssue {
                team_id: "t1".into(),
                title: "T".into(),
                description: "D".into(),
                state_id: None,
                priority: 2.0,
            },
        )
        .await
        .unwrap();
        let input = &fake.last_vars()["input"];
        assert_eq!(input["teamId"], "t1");
        assert!(input.get("stateId").is_none());
    }

    #[tokio::test]
    async fn a_mutation_reporting_success_false_is_an_error() {
        let fake = Fake::new(vec![
            json!({"issueUpdate": {"success": false, "issue": null}}),
        ]);
        let err = update_issue(&fake, "uuid-1", IssuePatch::default())
            .await
            .unwrap_err();
        assert!(err.to_string().contains("success: false"), "{err}");
    }

    #[tokio::test]
    async fn success_true_with_no_issue_is_reported_rather_than_parsed_as_garbage() {
        let fake = Fake::new(vec![
            json!({"issueCreate": {"success": true, "issue": null}}),
        ]);
        let err = create_issue(
            &fake,
            NewIssue {
                team_id: "t1".into(),
                title: "T".into(),
                description: "D".into(),
                state_id: None,
                priority: 0.0,
            },
        )
        .await
        .unwrap_err();
        assert!(err.to_string().contains("returned no issue"), "{err}");
    }

    #[tokio::test]
    async fn create_comment_checks_success() {
        let fake = Fake::new(vec![json!({"commentCreate": {"success": true}})]);
        create_comment(&fake, "uuid-1", "hello").await.unwrap();
        assert_eq!(fake.last_vars()["input"]["issueId"], "uuid-1");
        assert_eq!(fake.last_vars()["input"]["body"], "hello");
        assert!(fake.last_doc().contains("commentCreate"));
    }

    #[test]
    fn issue_fields_selects_everything_the_model_requires() {
        // `model::Issue` has no `#[serde(default)]` on these, so a selection
        // that dropped one would fail at runtime on every operation at once.
        for field in [
            "id",
            "identifier",
            "title",
            "description",
            "url",
            "updatedAt",
            "priority",
            "dueDate",
            "state {",
            "team {",
            "labels {",
        ] {
            assert!(
                ISSUE_FIELDS.contains(field),
                "{field} missing from ISSUE_FIELDS"
            );
        }
    }
}