linear-tui 0.12.0

A TUI client for Linear.app — manage issues, projects, and cycles from your terminal
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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! The test workspaces: checking that a key points at one, emptying it, and
//! filling it with the data the scenarios name.
//!
//! This talks to Linear directly, not through linear-tui, so a bug in
//! linear-tui cannot hide in the setup. It is destructive: it deletes every
//! issue, project, saved view, and favorite it can see — which is why it
//! refuses to run unless the workspace's URL key is the one the environment
//! names for it.

use std::collections::HashMap;
use std::sync::atomic::{AtomicBool, Ordering};

use serde_json::{Value, json};

const API: &str = "https://api.linear.app/graphql";

/// One test workspace, as the environment describes it.
#[derive(Debug, Clone)]
pub struct Account {
    /// `A` or `B`.
    pub name: &'static str,
    pub api_key: String,
    /// The workspace's URL key (`linear.app/<key>/…`). A key for any other
    /// workspace is refused.
    pub url_key: String,
}

impl Account {
    /// `LINEAR_E2E_API_KEY_<name>` and `LINEAR_E2E_WORKSPACE_<name>`, when
    /// both are set.
    pub fn from_env(name: &'static str) -> Option<Self> {
        let var = |v: &str| std::env::var(v).ok().filter(|s| !s.is_empty());
        Some(Self {
            name,
            api_key: var(&format!("LINEAR_E2E_API_KEY_{name}"))?,
            url_key: var(&format!("LINEAR_E2E_WORKSPACE_{name}"))?,
        })
    }
}

/// Why a run stops when Linear limits the test keys: every scenario after
/// would fail too, each for a reason that is not the real one.
pub const RATE_LIMITED: &str = "Linear is rate limiting the test workspaces' API keys \
     (too many runs within the hour). Wait for the limit to lift — about an hour — \
     and run again; running at once only extends it.";

static LIMITED: AtomicBool = AtomicBool::new(false);

/// Stop here, and make every later scenario stop before it sends anything:
/// retrying at once only prolongs the limit.
pub fn rate_limited() -> ! {
    LIMITED.store(true, Ordering::Relaxed);
    panic!("{RATE_LIMITED}");
}

/// Stop before sending anything when an earlier scenario hit the limit.
pub fn stop_if_rate_limited() {
    assert!(!LIMITED.load(Ordering::Relaxed), "{RATE_LIMITED}");
}

/// A small blocking GraphQL client for the setup.
pub struct Linear {
    http: reqwest::Client,
    api_key: String,
    runtime: tokio::runtime::Runtime,
}

impl Linear {
    pub fn new(account: &Account) -> Self {
        Self {
            http: reqwest::Client::new(),
            api_key: account.api_key.clone(),
            runtime: tokio::runtime::Builder::new_current_thread()
                .enable_all()
                .build()
                .expect("a tokio runtime for the setup"),
        }
    }

    /// Run a query; a GraphQL error is an `Err` naming the query.
    pub fn gql(&self, query: &str, variables: Value) -> Result<Value, String> {
        let request = self
            .http
            .post(API)
            .header("Authorization", &self.api_key)
            .json(&json!({ "query": query, "variables": variables }));
        let body: Value = self
            .runtime
            .block_on(async { request.send().await?.json().await })
            .map_err(|e| format!("{e}"))?;
        if let Some(errors) = body.get("errors") {
            let limited = errors
                .as_array()
                .is_some_and(|all| all.iter().any(|e| e["extensions"]["code"] == "RATELIMITED"));
            if limited {
                rate_limited();
            }
            let first_line = query.lines().next().unwrap_or_default();
            return Err(format!("{first_line}: {errors}"));
        }
        Ok(body["data"].clone())
    }

    /// Run a `<name>(input: …)` mutation and return what it created.
    fn create(
        &self,
        mutation: &str,
        input_type: &str,
        entity: &str,
        input: Value,
    ) -> Result<String, String> {
        let query = format!(
            "mutation($input: {input_type}!) {{ {mutation}(input: $input) {{ success {entity} {{ id }} }} }}"
        );
        let data = self.gql(&query, json!({ "input": input }))?;
        data[mutation][entity]["id"]
            .as_str()
            .map(String::from)
            .ok_or_else(|| format!("{mutation} created nothing"))
    }

    /// Every node of a connection, following its pages.
    fn all(&self, field: &str, selection: &str) -> Result<Vec<Value>, String> {
        let mut nodes = Vec::new();
        let mut after = Value::Null;
        loop {
            let query = format!(
                "query($after: String) {{ {field}(first: 100, after: $after) {{ nodes {{ {selection} }} pageInfo {{ hasNextPage endCursor }} }} }}"
            );
            let data = self.gql(&query, json!({ "after": after }))?;
            nodes.extend(data[field]["nodes"].as_array().cloned().unwrap_or_default());
            if data[field]["pageInfo"]["hasNextPage"] != json!(true) {
                return Ok(nodes);
            }
            after = data[field]["pageInfo"]["endCursor"].clone();
        }
    }
}

/// What the scenarios find in workspace A once it is seeded.
#[derive(Debug, Clone)]
pub struct Seeded {
    pub team_key: String,
    pub team_name: String,
    /// Another team of the workspace, for switching to.
    pub other_team: Option<String>,
    /// The signed-in user as linear-tui names them: display name first.
    pub viewer_name: String,
    /// Issue identifiers by title.
    pub issues: HashMap<String, String>,
}

impl Seeded {
    /// The identifier of the seeded issue titled `title`.
    pub fn issue(&self, title: &str) -> &str {
        self.issues
            .get(title)
            .unwrap_or_else(|| panic!("no seeded issue titled {title:?}"))
    }
}

/// Titles the scenarios name. Each changing scenario has an issue of its
/// own, so the scenarios can run in any order against one seeding.
pub mod titles {
    pub const CRASH: &str = "Crash when the forecast is empty";
    pub const RETRY: &str = "Retry failed forecast fetches";
    pub const CHART: &str = "Hourly chart overlaps on narrow screens";
    pub const RADAR: &str = "Add a radar layer toggle";
    pub const DOCS: &str = "Document the forecast API";
    pub const WIND: &str = "Wind arrows point the wrong way";
    pub const FOR_STATUS: &str = "E2E status change";
    pub const FOR_PRIORITY: &str = "E2E priority change";
    pub const FOR_ASSIGNEE: &str = "E2E assignee change";
    pub const FOR_COMMENT: &str = "E2E comment target";
    pub const FOR_AGENT: &str = "E2E agent target";
    pub const FOR_UPDATE: &str = "E2E update target";
    pub const FOR_THREAD: &str = "E2E thread target";
    pub const SEEDED_COMMENT: &str = "Reproduced on Android too.";
    pub const PROJECT: &str = "Forecast v2";
    pub const ISSUE_VIEW: &str = "Open bugs";
    pub const PROJECT_VIEW: &str = "E2E projects";
    pub const IN_WORKSPACE_B: &str = "Only in workspace B";
}

/// Refuse any workspace but the one the environment names.
pub fn check_workspace(linear: &Linear, account: &Account) -> Result<(), String> {
    let data = linear.gql("query { organization { urlKey name } }", json!({}))?;
    let key = data["organization"]["urlKey"].as_str().unwrap_or_default();
    if key != account.url_key {
        return Err(format!(
            "LINEAR_E2E_API_KEY_{} is for workspace {key:?}, not {:?} (LINEAR_E2E_WORKSPACE_{}); refusing to reset it",
            account.name, account.url_key, account.name
        ));
    }
    Ok(())
}

/// Delete every issue, project, saved view, and favorite.
pub fn reset(linear: &Linear) -> Result<(), String> {
    for favorite in linear.all("favorites", "id")? {
        linear.gql(
            "mutation($id: String!) { favoriteDelete(id: $id) { success } }",
            json!({ "id": favorite["id"] }),
        )?;
    }
    for view in linear.all("customViews", "id")? {
        linear.gql(
            "mutation($id: String!) { customViewDelete(id: $id) { success } }",
            json!({ "id": view["id"] }),
        )?;
    }
    // Sub-issues go with their parents; deleting one twice is harmless.
    for issue in linear.all("issues", "id")? {
        let _ = linear.gql(
            "mutation($id: String!) { issueDelete(id: $id) { success } }",
            json!({ "id": issue["id"] }),
        );
    }
    for project in linear.all("projects", "id")? {
        linear.gql(
            "mutation($id: String!) { projectDelete(id: $id) { success } }",
            json!({ "id": project["id"] }),
        )?;
    }
    Ok(())
}

/// The team the scenarios use: `LINEAR_E2E_TEAM_<name>` by key, or the
/// first team.
fn team(linear: &Linear, account: &Account) -> Result<(Value, Option<String>), String> {
    let teams = linear.gql(
        "query { teams { nodes { id key name cyclesEnabled cycles(first: 10) { nodes { id number } } } } }",
        json!({}),
    )?;
    let teams = teams["teams"]["nodes"]
        .as_array()
        .cloned()
        .unwrap_or_default();
    // CI passes a secret that was never set as an empty variable: no choice.
    let wanted = std::env::var(format!("LINEAR_E2E_TEAM_{}", account.name))
        .ok()
        .filter(|key| !key.is_empty());
    let team = teams
        .iter()
        .find(|t| wanted.as_deref().is_none_or(|k| t["key"] == k))
        .cloned()
        .ok_or_else(|| "no team to test in".to_string())?;
    let other = teams
        .iter()
        .find(|t| t["id"] != team["id"])
        .and_then(|t| t["name"].as_str())
        .map(String::from);
    Ok((team, other))
}

/// The second team workspace A's scenarios switch to, made when missing.
const SECOND_TEAM: (&str, &str) = ("E2E Second", "E2ES");

/// A second team for workspace A: another team it has, or one made now.
fn second_team(linear: &Linear, other: Option<String>) -> Result<String, String> {
    if let Some(other) = other {
        return Ok(other);
    }
    let (name, key) = SECOND_TEAM;
    linear.create(
        "teamCreate",
        "TeamCreateInput",
        "team",
        json!({ "name": name, "key": key }),
    )?;
    Ok(name.to_string())
}

/// A cycle of `team` to put issues in. Cycles are turned on for the team
/// when they are off; Linear then makes the cycles itself (it takes no
/// `cycleCreate`), a moment later.
fn cycle(linear: &Linear, team: &Value) -> Result<String, String> {
    if let Some(id) = team["cycles"]["nodes"][0]["id"].as_str() {
        return Ok(id.to_string());
    }
    if team["cyclesEnabled"] != json!(true) {
        linear.gql(
            "mutation($id: String!, $input: TeamUpdateInput!) { teamUpdate(id: $id, input: $input) { success } }",
            json!({ "id": team["id"],
                    "input": { "cyclesEnabled": true, "cycleDuration": 2, "upcomingCycleCount": 2 } }),
        )?;
    }
    for _ in 0..15 {
        let data = linear.gql(
            "query($id: String!) { team(id: $id) { cycles(first: 10) { nodes { id } } } }",
            json!({ "id": team["id"] }),
        )?;
        if let Some(id) = data["team"]["cycles"]["nodes"][0]["id"].as_str() {
            return Ok(id.to_string());
        }
        std::thread::sleep(std::time::Duration::from_secs(2));
    }
    Err(format!(
        "team {} has cycles on but no cycle yet; try again in a minute",
        team["key"]
    ))
}

/// The id of each of the team's states, by name.
fn states(linear: &Linear, team_id: &str) -> Result<HashMap<String, String>, String> {
    let data = linear.gql(
        "query($id: ID!) { workflowStates(filter: { team: { id: { eq: $id } } }) { nodes { id name } } }",
        json!({ "id": team_id }),
    )?;
    Ok(data["workflowStates"]["nodes"]
        .as_array()
        .cloned()
        .unwrap_or_default()
        .into_iter()
        .map(|s| {
            (
                s["name"].as_str().unwrap_or_default().to_string(),
                s["id"].as_str().unwrap_or_default().to_string(),
            )
        })
        .collect())
}

/// A label of the team, created when missing.
fn label(linear: &Linear, team_id: &str, name: &str, color: &str) -> Result<String, String> {
    let data = linear.gql(
        "query($id: String!) { team(id: $id) { labels(first: 100) { nodes { id name } } } }",
        json!({ "id": team_id }),
    )?;
    if let Some(found) = data["team"]["labels"]["nodes"]
        .as_array()
        .into_iter()
        .flatten()
        .find(|l| l["name"] == name)
    {
        return Ok(found["id"].as_str().unwrap_or_default().to_string());
    }
    linear.create(
        "issueLabelCreate",
        "IssueLabelCreateInput",
        "issueLabel",
        json!({ "teamId": team_id, "name": name, "color": color }),
    )
}

/// Empty workspace A and fill it with what the scenarios name.
pub fn seed_a(linear: &Linear, account: &Account) -> Result<Seeded, String> {
    use titles::*;
    check_workspace(linear, account)?;
    reset(linear)?;
    let (team, other_team) = team(linear, account)?;
    let other_team = Some(second_team(linear, other_team)?);
    let team_id = team["id"].as_str().unwrap_or_default().to_string();
    let viewer =
        linear.gql("query { viewer { id name displayName } }", json!({}))?["viewer"].clone();
    let viewer_id = viewer["id"].as_str().unwrap_or_default().to_string();
    let states = states(linear, &team_id)?;
    let state = |name: &str| {
        states.get(name).cloned().ok_or_else(|| {
            format!(
                "team {} has no state {name:?}; the scenarios expect Linear's defaults",
                team["key"]
            )
        })
    };
    let bug = label(linear, &team_id, "Bug", "#eb5757")?;
    let feature = label(linear, &team_id, "Feature", "#bb87fc")?;
    let project = linear.create(
        "projectCreate",
        "ProjectCreateInput",
        "project",
        json!({ "name": PROJECT, "teamIds": [team_id], "description": "Hourly forecasts, rebuilt on the new API" }),
    )?;
    let cycle = cycle(linear, &team)?;

    let mut issues = HashMap::new();
    let mut create = |title: &str, input: Value| -> Result<String, String> {
        let mut input = input;
        input["teamId"] = json!(team_id);
        input["title"] = json!(title);
        let query = "mutation($input: IssueCreateInput!) { issueCreate(input: $input) { success issue { id identifier } } }";
        let data = linear.gql(query, json!({ "input": input }))?;
        let issue = &data["issueCreate"]["issue"];
        issues.insert(
            title.to_string(),
            issue["identifier"].as_str().unwrap_or_default().to_string(),
        );
        Ok(issue["id"].as_str().unwrap_or_default().to_string())
    };
    // The oldest first, so the list (newest activity first) reads as below.
    create(
        DOCS,
        json!({ "stateId": state("Done")?, "priority": 4, "projectId": project }),
    )?;
    create(
        WIND,
        json!({ "stateId": state("Backlog")?, "priority": 2, "labelIds": [bug] }),
    )?;
    create(
        RADAR,
        json!({ "stateId": state("Backlog")?, "priority": 3, "labelIds": [feature] }),
    )?;
    create(FOR_AGENT, json!({ "stateId": state("Backlog")? }))?;
    create(
        FOR_UPDATE,
        json!({ "stateId": state("Todo")?, "priority": 2 }),
    )?;
    create(FOR_THREAD, json!({ "stateId": state("Todo")? }))?;
    create(FOR_ASSIGNEE, json!({ "stateId": state("Todo")? }))?;
    create(FOR_PRIORITY, json!({ "stateId": state("Todo")? }))?;
    create(FOR_STATUS, json!({ "stateId": state("Todo")? }))?;
    let commented = create(FOR_COMMENT, json!({ "stateId": state("Todo")? }))?;
    let chart = create(
        CHART,
        json!({ "stateId": state("Todo")?, "priority": 2, "labelIds": [bug], "cycleId": cycle,
                "description": "Below 360px the labels collide." }),
    )?;
    let crash = create(
        CRASH,
        json!({
            "stateId": state("In Progress")?, "priority": 1, "labelIds": [bug],
            "assigneeId": viewer_id, "projectId": project, "cycleId": cycle,
            "description": "The app **crashes** when the API returns no hours.\n\n## Steps\n\n1. Pick a city with no data\n2. Open the hourly chart",
        }),
    )?;
    create(
        RETRY,
        json!({ "stateId": state("In Progress")?, "priority": 2, "parentId": crash, "projectId": project }),
    )?;
    linear.create(
        "commentCreate",
        "CommentCreateInput",
        "comment",
        json!({ "issueId": commented, "body": SEEDED_COMMENT }),
    )?;

    let issue_view = linear.create(
        "customViewCreate",
        "CustomViewCreateInput",
        "customView",
        json!({ "name": ISSUE_VIEW, "shared": true, "filterData": { "labels": { "id": { "eq": bug } } } }),
    )?;
    linear.create(
        "customViewCreate",
        "CustomViewCreateInput",
        "customView",
        json!({ "name": PROJECT_VIEW, "shared": true,
                "projectFilterData": { "name": { "eq": PROJECT } } }),
    )?;
    for input in [
        json!({ "projectId": project, "sortOrder": 1 }),
        json!({ "customViewId": issue_view, "sortOrder": 2 }),
        json!({ "issueId": chart, "sortOrder": 3 }),
    ] {
        linear.create("favoriteCreate", "FavoriteCreateInput", "favorite", input)?;
    }

    Ok(Seeded {
        team_key: team["key"].as_str().unwrap_or_default().to_string(),
        team_name: team["name"].as_str().unwrap_or_default().to_string(),
        other_team,
        viewer_name: viewer["displayName"]
            .as_str()
            .or(viewer["name"].as_str())
            .unwrap_or_default()
            .to_string(),
        issues,
    })
}

/// Empty workspace B and give it one issue workspace A does not have.
pub fn seed_b(linear: &Linear, account: &Account) -> Result<Seeded, String> {
    check_workspace(linear, account)?;
    reset(linear)?;
    let (team, other_team) = team(linear, account)?;
    let team_id = team["id"].as_str().unwrap_or_default();
    let todo = states(linear, team_id)?
        .remove("Todo")
        .ok_or("workspace B's team has no Todo state")?;
    let data = linear.gql(
        "mutation($input: IssueCreateInput!) { issueCreate(input: $input) { issue { identifier } } }",
        json!({ "input": { "teamId": team_id, "title": titles::IN_WORKSPACE_B, "stateId": todo } }),
    )?;
    let viewer = linear.gql("query { viewer { name } }", json!({}))?;
    Ok(Seeded {
        team_key: team["key"].as_str().unwrap_or_default().to_string(),
        team_name: team["name"].as_str().unwrap_or_default().to_string(),
        other_team,
        viewer_name: viewer["viewer"]["name"]
            .as_str()
            .unwrap_or_default()
            .to_string(),
        issues: HashMap::from([(
            titles::IN_WORKSPACE_B.to_string(),
            data["issueCreate"]["issue"]["identifier"]
                .as_str()
                .unwrap_or_default()
                .to_string(),
        )]),
    })
}