subspace-client-github 0.1.0

Subspace Framework GitHub client: REST and GraphQL types and requests. Internal crate; apps depend on `subspace-framework`.
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
//! Requests to api.github.com and their error mapping.
//! Every function takes the credential for one attempt; the caller wraps it
//! in [`send_with_retry`](subspace_core::http::send_with_retry) so a 401
//! gets a fresh token once.

use serde::Deserialize;
use serde_json::Value;
use subspace_core::credentials::Credential;
use subspace_core::http::status as http_status;
use subspace_core::http::{Error, agent};
use ureq::Body;
use ureq::http::Response;

const USER_AGENT: &str = "subspace";
const API_VERSION: &str = "2022-11-28";
const ERROR_BODY_LIMIT_BYTES: u64 = subspace_core::http::ERROR_BODY_BYTES;
pub const JSON: &str = "application/vnd.github+json";
pub const RAW: &str = "application/vnd.github.raw+json";

mod graphql;
mod mapping;
pub(crate) use graphql::post_bound_graphql;
pub use graphql::post_graphql;

pub use mapping::{Failure, account_label, http_failure, not_found, rejected, status, unexpected};

use mapping::{is_rate_limit, is_sso};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Api {
    GraphQl,
    Rest,
}

#[derive(Debug, Clone, Default, Deserialize)]
pub struct GraphQlError {
    #[serde(default, rename = "type")]
    pub kind: Option<String>,
    #[serde(default)]
    pub message: Option<String>,
    #[serde(default)]
    pub extensions: Option<Extensions>,
}

#[derive(Debug, Clone, Default, Deserialize)]
pub struct Extensions {
    #[serde(default)]
    pub code: Option<String>,
}

/// Maps GraphQL `errors` to one error.
pub fn graphql_failure(account: &str, errors: &[GraphQlError]) -> Error {
    let message = |error: &GraphQlError| error.message.clone().unwrap_or_default();
    if let Some(sso) = errors.iter().find(|error| is_sso(&message(error))) {
        return status(http_status::FORBIDDEN, message(sso));
    }
    let limited = errors.iter().any(|error| {
        error.kind.as_deref() == Some("RATE_LIMITED") || is_rate_limit(&message(error))
    });
    if limited {
        return status(
            http_status::TOO_MANY_REQUESTS,
            format!("GitHub rate limit reached for {account}; try again in a few minutes."),
        );
    }
    let first = errors.first();
    let text = first
        .and_then(|error| error.message.clone())
        .unwrap_or_else(|| "GitHub returned an error.".to_owned());
    let kind = first.and_then(|error| error.kind.as_deref());
    if kind == Some("FORBIDDEN") {
        return status(http_status::FORBIDDEN, text);
    }
    let has_code = first
        .and_then(|error| error.extensions.as_ref())
        .and_then(|extensions| extensions.code.as_ref())
        .is_some();
    let invalid = matches!(kind, Some("NOT_FOUND" | "UNPROCESSABLE" | "INVALID")) || has_code;
    status(
        if invalid {
            http_status::BAD_REQUEST
        } else {
            http_status::BAD_GATEWAY
        },
        text,
    )
}

#[derive(Debug, Default, Deserialize)]
pub struct GraphQlResponse {
    #[serde(default)]
    pub data: Option<Value>,
    #[serde(default)]
    pub errors: Vec<GraphQlError>,
    #[serde(default)]
    pub message: Option<String>,
}

/// The `data` of a GraphQL answer. With `partial`, data that came with
/// errors (such as null nodes in SSO-protected organisations) is kept.
pub fn graphql_data(
    account: &str,
    response: GraphQlResponse,
    partial: bool,
) -> Result<Value, Error> {
    let data = response.data.filter(|data| !data.is_null());
    if !response.errors.is_empty() && !(partial && data.is_some()) {
        return Err(graphql_failure(account, &response.errors));
    }
    data.ok_or_else(|| status(http_status::BAD_GATEWAY, "GitHub returned no data."))
}

fn header<'a>(response: &'a Response<Body>, name: &str) -> Option<&'a str> {
    response.headers().get(name)?.to_str().ok()
}

fn network(_: ureq::Error) -> Error {
    Error::Network("GitHub could not be reached.".into())
}

/// Maps a response that is not a success; `Ok` passes it through.
fn check(
    credential: &Credential,
    account: &str,
    api: Api,
    mut response: Response<Body>,
) -> Result<Response<Body>, Error> {
    let code = response.status().as_u16();
    if response.status().is_success() {
        return Ok(response);
    }
    if code == http_status::UNAUTHORIZED {
        return Err(rejected(credential, account));
    }
    let text = response
        .body_mut()
        .with_config()
        .limit(ERROR_BODY_LIMIT_BYTES)
        .read_to_string()
        .unwrap_or_default();
    let message = serde_json::from_str::<GraphQlResponse>(&text)
        .ok()
        .and_then(|body| body.message)
        .unwrap_or_default();
    if code == http_status::BAD_REQUEST {
        return Err(status(http_status::BAD_REQUEST, message));
    }
    Err(http_failure(
        account,
        api,
        Failure {
            status: code,
            remaining: header(&response, "x-ratelimit-remaining"),
            reset: header(&response, "x-ratelimit-reset"),
            message: &message,
        },
    ))
}

/// GETs a REST path; the caller reads the body.
pub fn get_rest(
    url: &str,
    credential: &Credential,
    account: &str,
    accept: &str,
) -> Result<Response<Body>, Error> {
    get_rest_version(url, credential, account, accept, API_VERSION)
}

pub fn get_rest_version(
    url: &str,
    credential: &Credential,
    account: &str,
    accept: &str,
    version: &str,
) -> Result<Response<Body>, Error> {
    let authorization = credential.authorization();
    let response = agent()
        .get(url)
        .header("Authorization", authorization.expose())
        .header("Accept", accept)
        .header("X-GitHub-Api-Version", version)
        .header("User-Agent", USER_AGENT)
        .call()
        .map_err(network)?;
    check(credential, account, Api::Rest, response)
}

/// The Copilot agent API names the client that calls it.
pub const COPILOT_INTEGRATION_ID: &str = "copilot-4-cli";
/// The API version the Copilot agent endpoints answer.
pub const COPILOT_API_VERSION: &str = "2026-01-09";
pub const EVENT_STREAM: &str = "text/event-stream";

/// GETs a Copilot agent API path; the caller reads the body. The Copilot
/// endpoints need their own integration id and version, so they do not go
/// through [`get_rest`].
pub fn get_copilot(
    url: &str,
    credential: &Credential,
    account: &str,
    accept: &str,
) -> Result<Response<Body>, Error> {
    let authorization = credential.authorization();
    let response = agent()
        .get(url)
        .header("Authorization", authorization.expose())
        .header("Accept", accept)
        .header("Copilot-Integration-Id", COPILOT_INTEGRATION_ID)
        .header("X-GitHub-Api-Version", COPILOT_API_VERSION)
        .header("User-Agent", USER_AGENT)
        .call()
        .map_err(network)?;
    check(credential, account, Api::Rest, response)
}

pub fn is_api_version_rejection(error: &Error) -> bool {
    matches!(error, Error::Status { status: http_status::BAD_REQUEST, message } if {
        let message = message.to_ascii_lowercase();
        message.contains("api version") || message.contains("api-version") || message.contains("unsupported version")
    })
}

/// POSTs JSON to a REST endpoint. The caller may retry a rejected API version
/// without retrying the operation itself.
pub fn post_rest_json(
    url: &str,
    credential: &Credential,
    account: &str,
    body: &str,
    version: &str,
) -> Result<(u16, String), Error> {
    post_rest_json_request(agent().post(url), credential, account, body, version)
}

/// The same JSON transport with caller-owned request configuration, such as a
/// remaining submission budget. Authentication, limits and status mapping agree.
pub(crate) fn post_rest_json_request(
    request: ureq::RequestBuilder<ureq::typestate::WithBody>,
    credential: &Credential,
    account: &str,
    body: &str,
    version: &str,
) -> Result<(u16, String), Error> {
    let response = request
        .header("Authorization", credential.authorization().expose())
        .header("Content-Type", "application/json")
        .header("Accept", JSON)
        .header("X-GitHub-Api-Version", version)
        .header("User-Agent", USER_AGENT)
        .send(body)
        .map_err(network)?;
    let code = response.status().as_u16();
    let mut response = response;
    let text = response
        .body_mut()
        .with_config()
        .limit(subspace_core::http::MAX_BODY_BYTES)
        .read_to_string()
        .map_err(network)?;
    if !http_status::is_success(code) {
        if code == http_status::UNAUTHORIZED {
            return Err(rejected(credential, account));
        }
        let message = serde_json::from_str::<GraphQlResponse>(&text)
            .ok()
            .and_then(|body| body.message)
            .unwrap_or_default();
        if code == http_status::BAD_REQUEST {
            return Err(status(http_status::BAD_REQUEST, message));
        }
        return Err(http_failure(
            account,
            Api::Rest,
            Failure {
                status: code,
                remaining: None,
                reset: None,
                message: &message,
            },
        ));
    }
    Ok((code, text))
}

/// Reads a body up to `max` bytes; a larger one is a 413.
pub fn read_limited(mut response: Response<Body>, max: usize) -> Result<Vec<u8>, Error> {
    let too_large = || {
        status(
            http_status::PAYLOAD_TOO_LARGE,
            format!("The file is larger than {} MB.", max / 1024 / 1024),
        )
    };
    let declared = header(&response, "content-length").and_then(|value| value.parse::<u64>().ok());
    if declared.is_some_and(|length| length > max as u64) {
        return Err(too_large());
    }
    response
        .body_mut()
        .with_config()
        .limit(max as u64)
        .read_to_vec()
        .map_err(|error| match error {
            ureq::Error::BodyExceedsLimit(_) => too_large(),
            other => network(other),
        })
}

#[cfg(test)]
mod tests {
    use serde_json::json;
    use subspace_core::Secret;
    use subspace_core::credentials::{Credential, Scheme, Source};
    use subspace_core::http::Error;

    use super::mapping::reset_hint;
    use super::{
        Api, Failure, GraphQlError, GraphQlResponse, graphql_data, graphql_failure, http_failure,
        rejected,
    };

    fn code(error: &Error) -> u16 {
        match error {
            Error::Status { status, .. } => *status,
            _ => 0,
        }
    }

    fn failure(
        status: u16,
        remaining: Option<&'static str>,
        message: &'static str,
    ) -> Failure<'static> {
        Failure {
            status,
            remaining,
            reset: Some("1790000000"),
            message,
        }
    }

    #[test]
    fn rate_limits_name_the_reset_time() {
        let error = http_failure("ann", Api::Rest, failure(403, Some("0"), ""));
        assert_eq!(code(&error), 429);
        assert!(
            error
                .to_string()
                .starts_with("GitHub rate limit reached for ann; it resets at ")
        );
        assert!(error.to_string().ends_with(" UTC."));
        let by_message = http_failure(
            "ann",
            Api::GraphQl,
            failure(429, None, "API rate limit exceeded"),
        );
        assert_eq!(code(&by_message), 429);
        assert_eq!(reset_hint(None), "; try again in a few minutes");
        assert_eq!(reset_hint(Some("0")), "; try again in a few minutes");
        assert_eq!(reset_hint(Some("3600")), "; it resets at 01:00 UTC");
    }

    #[test]
    fn sso_timeouts_and_not_found() {
        let sso = http_failure(
            "ann",
            Api::Rest,
            failure(
                403,
                None,
                "Resource protected by organization SAML enforcement.",
            ),
        );
        assert_eq!(code(&sso), 403);
        assert_eq!(
            code(&http_failure("ann", Api::GraphQl, failure(502, None, ""))),
            504
        );
        assert_eq!(
            code(&http_failure("ann", Api::Rest, failure(502, None, ""))),
            502
        );
        assert_eq!(
            code(&http_failure("ann", Api::Rest, failure(404, None, ""))),
            404
        );
        assert_eq!(
            code(&http_failure("ann", Api::GraphQl, failure(404, None, ""))),
            502
        );
        assert_eq!(
            code(&http_failure("ann", Api::Rest, failure(403, None, "nope"))),
            502
        );
    }

    fn errors(value: serde_json::Value) -> Vec<GraphQlError> {
        serde_json::from_value(value).unwrap()
    }

    #[test]
    fn graphql_errors_map_by_type() {
        let cases = [
            (json!([{"type": "FORBIDDEN", "message": "x"}]), 403),
            (json!([{"message": "Resource protected by SSO"}]), 403),
            (json!([{"type": "RATE_LIMITED"}]), 429),
            (json!([{"type": "NOT_FOUND", "message": "gone"}]), 400),
            (
                json!([{"message": "bad", "extensions": {"code": "undefinedField"}}]),
                400,
            ),
            (json!([{"message": "boom"}]), 502),
        ];
        for (value, expected) in cases {
            assert_eq!(
                code(&graphql_failure("ann", &errors(value.clone()))),
                expected,
                "{value}"
            );
        }
    }

    #[test]
    fn partial_data_is_kept_only_when_asked() {
        let response = || GraphQlResponse {
            data: Some(json!({"search": {}})),
            errors: errors(json!([{"type": "FORBIDDEN", "message": "SAML"}])),
            message: None,
        };
        assert!(graphql_data("ann", response(), true).is_ok());
        assert_eq!(
            code(&graphql_data("ann", response(), false).unwrap_err()),
            403
        );
        let empty = GraphQlResponse::default();
        assert_eq!(code(&graphql_data("ann", empty, true).unwrap_err()), 502);
    }

    #[test]
    fn a_rejected_token_names_its_source_and_keeps_401() {
        let credential = |source| Credential {
            value: Secret::new("t"),
            scheme: Scheme::Bearer,
            source,
        };
        let env = rejected(&credential(Source::Environment("GH_TOKEN")), "ann");
        assert_eq!(code(&env), 401);
        assert_eq!(env.to_string(), "GitHub rejected the token in GH_TOKEN.");
        let cli = rejected(
            &credential(Source::GhCli {
                login: "ann".into(),
            }),
            "ann",
        );
        assert_eq!(
            cli.to_string(),
            "GitHub token rejected for ann; run gh auth login."
        );
    }
}