lineark-sdk 2.1.1

Typed, async-first Rust SDK for the Linear GraphQL API
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
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
//! HTTP helpers for Linear file operations.
//!
//! Linear's file handling works outside the GraphQL API: uploads go to Google
//! Cloud Storage via signed URLs, and downloads fetch from Linear's CDN. These
//! helpers use the SDK's internal HTTP client so consumers don't need a separate
//! `reqwest` dependency.

use crate::client::Client;
use crate::error::LinearError;

/// Metadata about a successfully downloaded file.
#[derive(Debug, Clone)]
pub struct DownloadResult {
    /// The raw file bytes.
    pub bytes: Vec<u8>,
    /// Content-Type header from the response, if present.
    pub content_type: Option<String>,
}

/// Metadata returned after a successful two-step file upload.
#[derive(Debug, Clone)]
pub struct UploadResult {
    /// The permanent asset URL for referencing this file in comments, descriptions, etc.
    pub asset_url: String,
}

impl Client {
    /// Download a file from a URL.
    ///
    /// Handles Linear's signed/expiring CDN URLs (e.g. `https://uploads.linear.app/...`)
    /// as well as any other publicly accessible URL. Returns the raw bytes and
    /// content type so the caller can write them to disk or process them further.
    ///
    /// # Errors
    ///
    /// Returns [`LinearError::HttpError`] if the server responds with a non-2xx status,
    /// or [`LinearError::Network`] if the request fails at the transport level.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # async fn example() -> Result<(), lineark_sdk::LinearError> {
    /// let client = lineark_sdk::Client::from_env()?;
    /// let result = client.download_url("https://uploads.linear.app/...").await?;
    /// std::fs::write("output.png", &result.bytes).unwrap();
    /// # Ok(())
    /// # }
    /// ```
    pub async fn download_url(&self, url: &str) -> Result<DownloadResult, LinearError> {
        let is_linear_url = url::Url::parse(url)
            .map(|u| u.host_str().is_some_and(|h| h.ends_with(".linear.app")))
            .unwrap_or(false);

        let mut request = self.http().get(url);
        if is_linear_url {
            request = request.header("Authorization", self.token());
        }
        let response = request.send().await?;

        let status = response.status();
        if !status.is_success() {
            let body = response.text().await.unwrap_or_default();
            return Err(LinearError::HttpError {
                status: status.as_u16(),
                body,
            });
        }

        let content_type = response
            .headers()
            .get("content-type")
            .and_then(|v| v.to_str().ok())
            .map(|s| s.to_string());

        let bytes = response.bytes().await?.to_vec();

        Ok(DownloadResult {
            bytes,
            content_type,
        })
    }

    /// Upload a file to Linear's cloud storage.
    ///
    /// This is a two-step process:
    /// 1. Call the [`fileUpload`](Client::file_upload) GraphQL mutation to obtain
    ///    a signed upload URL and required headers from Linear.
    /// 2. `PUT` the raw file bytes to that signed URL (a Google Cloud Storage endpoint).
    ///
    /// On success, returns an [`UploadResult`] containing the permanent `asset_url`
    /// that can be referenced in issue descriptions, comments, or attachments.
    ///
    /// # Arguments
    ///
    /// * `filename` — The original filename (e.g. `"screenshot.png"`). Linear uses this
    ///   for display and content-type inference on its side.
    /// * `content_type` — MIME type of the file (e.g. `"image/png"`).
    /// * `bytes` — The raw file content.
    /// * `make_public` — If `true`, the uploaded file will be publicly accessible
    ///   without authentication.
    ///
    /// # Errors
    ///
    /// Returns an error if the `fileUpload` mutation fails, if the signed URL
    /// upload fails, or if the response is missing expected fields.
    ///
    /// # Example
    ///
    /// ```no_run
    /// # async fn example() -> Result<(), lineark_sdk::LinearError> {
    /// let client = lineark_sdk::Client::from_env()?;
    /// let bytes = std::fs::read("screenshot.png").unwrap();
    /// let result = client
    ///     .upload_file("screenshot.png", "image/png", bytes, false)
    ///     .await?;
    /// println!("Uploaded to: {}", result.asset_url);
    /// # Ok(())
    /// # }
    /// ```
    pub async fn upload_file(
        &self,
        filename: &str,
        content_type: &str,
        bytes: Vec<u8>,
        make_public: bool,
    ) -> Result<UploadResult, LinearError> {
        let size = bytes.len() as i64;

        // Step 1: Request a signed upload URL from Linear's API.
        // We use a custom query instead of the generated `file_upload` method
        // because we need the nested `headers { key value }` field which the
        // codegen omits (it only includes scalar fields).
        let variables = serde_json::json!({
            "metaData": null,
            "makePublic": if make_public { Some(true) } else { None::<bool> },
            "size": size,
            "contentType": content_type,
            "filename": filename,
        });
        let payload = self
            .execute::<serde_json::Value>(
                "mutation FileUpload($metaData: JSON, $makePublic: Boolean, $size: Int!, \
                 $contentType: String!, $filename: String!) { \
                 fileUpload(metaData: $metaData, makePublic: $makePublic, size: $size, \
                 contentType: $contentType, filename: $filename) { \
                 success uploadFile { filename contentType size uploadUrl assetUrl \
                 headers { key value } } } }",
                variables,
                "fileUpload",
            )
            .await?;

        if payload.get("success").and_then(|v| v.as_bool()) != Some(true) {
            return Err(LinearError::MissingData(format!(
                "fileUpload mutation failed: {}",
                serde_json::to_string(&payload).unwrap_or_default()
            )));
        }

        let upload_file = payload.get("uploadFile").ok_or_else(|| {
            LinearError::MissingData("No 'uploadFile' in fileUpload response".to_string())
        })?;

        let upload_url = upload_file
            .get("uploadUrl")
            .and_then(|v| v.as_str())
            .ok_or_else(|| {
                LinearError::MissingData("No 'uploadUrl' in fileUpload response".to_string())
            })?;

        let asset_url = upload_file
            .get("assetUrl")
            .and_then(|v| v.as_str())
            .ok_or_else(|| {
                LinearError::MissingData("No 'assetUrl' in fileUpload response".to_string())
            })?
            .to_string();

        // Collect upload headers prescribed by Linear.
        let headers: Vec<(String, String)> = upload_file
            .get("headers")
            .and_then(|v| v.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|h| {
                        let key = h.get("key")?.as_str()?.to_string();
                        let val = h.get("value")?.as_str()?.to_string();
                        Some((key, val))
                    })
                    .collect()
            })
            .unwrap_or_default();

        // Step 2: PUT the file bytes to the signed upload URL.
        let mut request = self
            .http()
            .put(upload_url)
            .header("Content-Type", content_type)
            .body(bytes);

        for (key, value) in &headers {
            request = request.header(key.as_str(), value.as_str());
        }

        let response = request.send().await?;

        if !response.status().is_success() {
            let status = response.status();
            let body = response.text().await.unwrap_or_default();
            return Err(LinearError::HttpError {
                status: status.as_u16(),
                body,
            });
        }

        Ok(UploadResult { asset_url })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use wiremock::matchers::{method, path};
    use wiremock::{Mock, MockServer, ResponseTemplate};

    fn test_client_with_base(base_url: &str) -> Client {
        let mut client = Client::from_token("test-token").unwrap();
        client.set_base_url(base_url.to_string());
        client
    }

    // ── download_url ────────────────────────────────────────────────────────

    #[tokio::test]
    async fn download_url_sends_auth_for_linear_urls() {
        let server = MockServer::start().await;
        // This test verifies that for non-Linear URLs, no Authorization header is sent.
        // We can check by mounting a mock that requires NO auth header.
        Mock::given(method("GET"))
            .and(path("/external/file.png"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_bytes(vec![1, 2, 3])
                    .insert_header("content-type", "image/png"),
            )
            .mount(&server)
            .await;

        let client = test_client_with_base(&server.uri());
        let url = format!("{}/external/file.png", server.uri());
        let result = client.download_url(&url).await.unwrap();
        assert_eq!(result.bytes, vec![1, 2, 3]);

        // Verify the request did NOT include an Authorization header
        // (since the URL is not a *.linear.app domain)
        let requests = server.received_requests().await.unwrap();
        assert_eq!(requests.len(), 1);
        let auth_header = requests[0].headers.get("authorization");
        assert!(
            auth_header.is_none(),
            "Authorization header should not be sent to non-Linear URLs"
        );
    }

    #[tokio::test]
    async fn download_url_returns_bytes_and_content_type() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/files/test.png"))
            .respond_with(
                ResponseTemplate::new(200)
                    .set_body_bytes(vec![0x89, 0x50, 0x4E, 0x47]) // PNG magic bytes
                    .insert_header("content-type", "image/png"),
            )
            .mount(&server)
            .await;

        let client = test_client_with_base(&server.uri());
        let url = format!("{}/files/test.png", server.uri());
        let result = client.download_url(&url).await.unwrap();

        assert_eq!(result.bytes, vec![0x89, 0x50, 0x4E, 0x47]);
        assert_eq!(result.content_type, Some("image/png".to_string()));
    }

    #[tokio::test]
    async fn download_url_without_content_type_header() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/files/raw"))
            .respond_with(ResponseTemplate::new(200).set_body_bytes(b"raw data".to_vec()))
            .mount(&server)
            .await;

        let client = test_client_with_base(&server.uri());
        let url = format!("{}/files/raw", server.uri());
        let result = client.download_url(&url).await.unwrap();

        assert_eq!(result.bytes, b"raw data");
        assert_eq!(result.content_type, None);
    }

    #[tokio::test]
    async fn download_url_404_returns_http_error() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/files/missing"))
            .respond_with(ResponseTemplate::new(404).set_body_string("Not Found"))
            .mount(&server)
            .await;

        let client = test_client_with_base(&server.uri());
        let url = format!("{}/files/missing", server.uri());
        let result = client.download_url(&url).await;

        assert!(result.is_err());
        match result.unwrap_err() {
            LinearError::HttpError { status, body } => {
                assert_eq!(status, 404);
                assert_eq!(body, "Not Found");
            }
            other => panic!("expected HttpError, got: {:?}", other),
        }
    }

    #[tokio::test]
    async fn download_url_500_returns_http_error() {
        let server = MockServer::start().await;
        Mock::given(method("GET"))
            .and(path("/files/error"))
            .respond_with(ResponseTemplate::new(500).set_body_string("Internal Server Error"))
            .mount(&server)
            .await;

        let client = test_client_with_base(&server.uri());
        let url = format!("{}/files/error", server.uri());
        let result = client.download_url(&url).await;

        assert!(result.is_err());
        match result.unwrap_err() {
            LinearError::HttpError { status, .. } => assert_eq!(status, 500),
            other => panic!("expected HttpError, got: {:?}", other),
        }
    }

    // ── upload_file ─────────────────────────────────────────────────────────

    #[tokio::test]
    async fn upload_file_two_step_flow() {
        let server = MockServer::start().await;
        let upload_url = format!("{}/upload-target", server.uri());
        let asset_url = "https://linear-uploads.example.com/asset/test.png";

        // Step 1: Mock the fileUpload GraphQL mutation.
        Mock::given(method("POST"))
            .and(path("/"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": {
                    "fileUpload": {
                        "success": true,
                        "uploadFile": {
                            "uploadUrl": upload_url,
                            "assetUrl": asset_url,
                            "filename": "test.png",
                            "contentType": "image/png",
                            "size": 4,
                            "headers": [
                                { "key": "x-goog-meta-test", "value": "123" }
                            ]
                        }
                    }
                }
            })))
            .mount(&server)
            .await;

        // Step 2: Mock the PUT to the signed upload URL.
        Mock::given(method("PUT"))
            .and(path("/upload-target"))
            .respond_with(ResponseTemplate::new(200))
            .mount(&server)
            .await;

        let mut client = Client::from_token("test-token").unwrap();
        // Point GraphQL calls at the mock server.
        client.set_base_url(server.uri());

        let bytes = vec![0x89, 0x50, 0x4E, 0x47]; // PNG magic
        let result = client
            .upload_file("test.png", "image/png", bytes, false)
            .await
            .unwrap();

        assert_eq!(result.asset_url, asset_url);

        // Verify both requests were made.
        let requests = server.received_requests().await.unwrap();
        assert_eq!(
            requests.len(),
            2,
            "should have made 2 requests (mutation + PUT)"
        );
        assert_eq!(requests[0].method.as_str(), "POST"); // GraphQL mutation
        assert_eq!(requests[1].method.as_str(), "PUT"); // File upload
    }

    #[tokio::test]
    async fn upload_file_mutation_failure_returns_error() {
        let server = MockServer::start().await;

        Mock::given(method("POST"))
            .and(path("/"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": {
                    "fileUpload": {
                        "success": false
                    }
                }
            })))
            .mount(&server)
            .await;

        let mut client = Client::from_token("test-token").unwrap();
        client.set_base_url(server.uri());

        let result = client
            .upload_file("test.png", "image/png", vec![1, 2, 3], false)
            .await;

        assert!(result.is_err());
        match result.unwrap_err() {
            LinearError::MissingData(msg) => {
                assert!(msg.contains("fileUpload mutation failed"), "got: {msg}");
            }
            other => panic!("expected MissingData, got: {:?}", other),
        }
    }

    #[tokio::test]
    async fn upload_file_put_failure_returns_http_error() {
        let server = MockServer::start().await;
        let upload_url = format!("{}/upload-target", server.uri());

        Mock::given(method("POST"))
            .and(path("/"))
            .respond_with(ResponseTemplate::new(200).set_body_json(serde_json::json!({
                "data": {
                    "fileUpload": {
                        "success": true,
                        "uploadFile": {
                            "uploadUrl": upload_url,
                            "assetUrl": "https://example.com/asset.png",
                            "headers": []
                        }
                    }
                }
            })))
            .mount(&server)
            .await;

        Mock::given(method("PUT"))
            .and(path("/upload-target"))
            .respond_with(ResponseTemplate::new(403).set_body_string("Forbidden"))
            .mount(&server)
            .await;

        let mut client = Client::from_token("test-token").unwrap();
        client.set_base_url(server.uri());

        let result = client
            .upload_file("test.png", "image/png", vec![1, 2, 3], false)
            .await;

        assert!(result.is_err());
        match result.unwrap_err() {
            LinearError::HttpError { status, body } => {
                assert_eq!(status, 403);
                assert_eq!(body, "Forbidden");
            }
            other => panic!("expected HttpError, got: {:?}", other),
        }
    }
}