github_release_check 0.2.1

Check latest GitHub release version
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
484
485
486
487
488
489
490
491
492
493
494
//! Retrieve releases versions of a repository from GitHub.
//!
//! Two functions are exposed by this crate: one to get the latest (Semantic Versioned) version, and
//! another to get all of the release versions (as `String`s).
//!
//! This crate works for public and private GitHub repositories on public GitHub or GitHub enterprise
//! when supplied with a valid [access token] for that repository / environment.
//!
//! The simplest use case is a public repository on github.com:
//!
//! ```rust,no_run
//! use github_release_check::GitHub;
//!
//! let github = GitHub::new().unwrap();
//! let versions = github.get_all_versions("celeo/github_release_check").unwrap();
//! ```
//!
//! If you want to access a private repository on github.com, you'll need an access token for
//! a user who can view that repository:
//!
//! ```rust,no_run
//! use github_release_check::{GitHub, DEFAULT_API_ROOT};
//!
//! let github = GitHub::from_custom(DEFAULT_API_ROOT, "your-access-token").unwrap();
//! let versions = github.get_all_versions("you/private-repo").unwrap();
//! ```
//!
//! If you are using a private GitHub enterprise environment:
//!
//! ```rust,no_run
//! use github_release_check::GitHub;
//!
//! let github = GitHub::from_custom("https://github.your_domain.com/api/v3/", "your-access-token").unwrap();
//! let versions = github.get_all_versions("you/private-repo").unwrap();
//! ```
//!
//! Of course, handling these `Result`s with something other than just unwrapping them is a good idea.
//!
//! If you wish to gain more information on each release, use the `query` function:
//!
//! ```rust,no_run
//! use github_release_check::GitHub;
//!
//! let github = GitHub::new().unwrap();
//! let versions = github.query("celeo/github_release_check").unwrap();
//! ```
//!
//! [access token]: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token

#![deny(
    clippy::all,
    clippy::pedantic,
    missing_debug_implementations,
    missing_docs,
    trivial_casts,
    trivial_numeric_casts,
    unsafe_code,
    unused_extern_crates,
    unused_import_braces,
    unused_qualifications,
    unused_results
)]

use log::debug;
use once_cell::sync::Lazy;
use regex::Regex;
use reqwest::{
    blocking::{Client, ClientBuilder},
    header::{self, HeaderMap},
};
use semver::Version;
use serde::Deserialize;
use thiserror::Error;

/// Errors that may be raised by this crate.
#[derive(Debug, Error)]
pub enum LookupError {
    /// May arise from working with the HTTP client.
    #[error("HTTP client error")]
    HttpClient(#[from] reqwest::Error),
    /// May arise from working with the HTTP client.
    #[error("invalid header value")]
    HeaderValue(#[from] reqwest::header::InvalidHeaderValue),
    /// May arise from working with the HTTP client.
    #[error("could not get header value")]
    HeaderToString(#[from] reqwest::header::ToStrError),
    /// May arise if the repository does not have any releases.
    #[error("no release found")]
    NoReleases,
    /// May arise from a mis-supplied repository, or from not having access.
    #[error("repository not found")]
    RepositoryNotFound,
    /// May arise from GitHub API missing or incorrect authentication.
    #[error("authentication error")]
    AuthenticationError(u16),
    /// May arise if GitHub returns an error code from the lookup.
    #[error("received error HTTP response code")]
    ErrorHttpResponse(u16),
}

type Result<T> = std::result::Result<T, LookupError>;

const DEFAULT_USER_AGENT: &str = "github.com/celeo/github_version_check";
const DEFAULT_ACCEPT_HEADER: &str = "application/vnd.github.v3+json";
const PAGINATION_REQUEST_AMOUNT: usize = 100;
static PAGE_EXTRACT_REGEX: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"(\w*)page=(\d+)").expect("Could not compile regex"));

/// The default GitHub instance API root endpoint.
///
/// You can use this exported `String` if you want to query
/// a private repository on <https://github.com>.
pub const DEFAULT_API_ROOT: &str = "https://api.github.com/";

/// Generate the headers required to send HTTP requests to GitHub.
fn generate_headers(token: Option<&str>) -> Result<HeaderMap> {
    let mut headers = HeaderMap::new();
    let _prev = headers.insert(
        header::USER_AGENT,
        header::HeaderValue::from_str(DEFAULT_USER_AGENT)?,
    );
    let _prev = headers.insert(
        header::ACCEPT,
        header::HeaderValue::from_str(DEFAULT_ACCEPT_HEADER)?,
    );
    if let Some(t) = token {
        let _prev = headers.insert(
            header::AUTHORIZATION,
            header::HeaderValue::from_str(&format!("Bearer {t}"))?,
        );
    }
    Ok(headers)
}

/// Data for a release in the GitHub API response.
///
/// For information on the struct keys, see [the GitHub docs].
///
/// [the GitHub docs]: https://docs.github.com/en/rest/releases/releases#list-releases
#[derive(Debug, Deserialize, Clone)]
#[allow(missing_docs)]
pub struct GitHubReleaseItem {
    pub url: String,
    pub html_url: String,
    pub assets_url: String,
    pub upload_url: String,
    pub id: usize,
    pub tag_name: String,
    pub name: Option<String>,
    pub draft: bool,
    pub prerelease: bool,
    pub created_at: String,
    pub published_at: String,
    pub body: Option<String>,
}

/// Struct to communicate with the GitHub REST API.
#[derive(Debug)]
pub struct GitHub {
    client: Client,
    api_root: String,
}

impl GitHub {
    /// Create a new instance of the struct suitable for public GitHub.
    ///
    /// The struct created by this function does not set an access token
    /// and as such can only get information on public GitHub repositories.
    ///
    /// If you need to access information for private repositories or any
    /// information from a custom GitHub enterprise instance, use the
    /// `from_custom` function.
    ///
    /// This function may return an `Error` if the HTTP client could
    /// not be constructed or headers initialized. It should be safe
    /// to unwrap the `Result`.
    ///
    /// # Example
    ///
    /// ```rust
    /// use github_release_check::GitHub;
    /// let github = GitHub::new().unwrap();
    /// ```
    ///
    /// # Errors
    ///
    /// This function fails if the headers cannot be constructed.
    pub fn new() -> Result<Self> {
        let client = ClientBuilder::new()
            .default_headers(generate_headers(None)?)
            .build()?;
        Ok(Self {
            client,
            api_root: DEFAULT_API_ROOT.to_owned(),
        })
    }

    /// Create a new instance of the struct suitable for accessing any GitHub repository
    /// that can be viewed with the access key on the GitHub instance.
    ///
    /// This function has to be used to construct the struct instance whenever the repository
    /// that you want to get information from is on a custom GitHub enterprise instance and/or
    /// is private. The access token passed to this function should be a [GitHub personal access token]
    /// that has the access to view the repository on that GitHub instance.
    ///
    /// For the `api_endpoint` argument, pass in the REST API root of the GitHub instance. For public
    /// GitHub, this can be found in [`DEFAULT_API_ROOT`].
    /// Your GitHub enterprise may use a subdomain like `"https://api.github.your_domain_root.com/"`, or
    /// perhaps something like `"https://github.your_domain_root.com/api/v3/"`. Specify the API root that
    /// you can otherwise send requests to. Note that this URL should end in a trailing slash.
    ///
    /// # Example
    ///
    /// ```rust
    /// use github_release_check::GitHub;
    /// let github = GitHub::from_custom("https://github.example.com/api/v3/", "abcdef").unwrap();
    /// ```
    ///
    /// # Errors
    ///
    /// This function fails if the headers cannot be constructed.
    ///
    /// [GitHub personal access token]: https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/creating-a-personal-access-token
    pub fn from_custom(api_endpoint: &str, access_token: &str) -> Result<Self> {
        let client = ClientBuilder::new()
            .default_headers(generate_headers(Some(access_token))?)
            .build()?;
        Ok(Self {
            client,
            api_root: api_endpoint.to_owned(),
        })
    }

    /// Get all release versions from the repository.
    ///
    /// Note that `repository` should be in the format "owner/repo",
    /// like `"celeo/github_release_check"`.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use github_release_check::GitHub;
    /// let github = GitHub::new().unwrap();
    /// let versions_result = github.get_all_versions("celeo/github_release_check");
    /// ```
    ///
    /// # Errors
    ///
    /// This function fails if the HTTP request cannot be sent, the API returns
    /// a status code indicating something other than a success (outside of the
    /// 2xx range), of if the returned data does not match the expected model.
    pub fn query(&self, repository: &str) -> Result<Vec<GitHubReleaseItem>> {
        let mut page = 1usize;
        let mut pages = Vec::<Vec<GitHubReleaseItem>>::new();
        let mut last_page: Option<usize> = None;

        loop {
            let query = vec![("per_page", PAGINATION_REQUEST_AMOUNT), ("page", page)];
            let url = format!("{}repos/{}/releases", self.api_root, repository);
            debug!(
                "Querying GitHub at {}, page {} of {}",
                url,
                page,
                last_page.map_or_else(|| String::from("?"), |p| p.to_string())
            );
            let request = self
                .client
                .request(reqwest::Method::GET, &url)
                .query(&query)
                .build()?;
            let response = self.client.execute(request)?;
            if !response.status().is_success() {
                debug!(
                    "Got status \"{}\" from GitHub release check",
                    response.status()
                );
                let stat = response.status().as_u16();
                if stat == 404 {
                    return Err(LookupError::RepositoryNotFound);
                }
                if stat == 401 || stat == 403 {
                    return Err(LookupError::AuthenticationError(stat));
                }
                return Err(LookupError::ErrorHttpResponse(stat));
            }
            if last_page.is_none() {
                debug!("Determining last page from response headers");
                last_page = get_last_page(response.headers())?;
            }
            pages.push(response.json()?);
            page += 1;
            if let Some(last) = last_page {
                if page >= last {
                    break;
                }
            } else {
                debug!("No pagination header found (fewer than 100 releases)");
                break;
            }
        }

        Ok(pages.iter().flatten().cloned().collect())
    }

    /// Get all release version strings from the repository.
    ///
    /// Note that `repository` should be in the format "owner/repo",
    /// like `"celeo/github_release_check"`.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use github_release_check::GitHub;
    /// let github = GitHub::new().unwrap();
    /// let versions_result = github.get_all_versions("celeo/github_release_check");
    /// ```
    ///
    /// # Errors
    ///
    /// This function fails if the HTTP request cannot be sent, the API returns
    /// a status code indicating something other than a success (outside of the
    /// 2xx range), of if the returned data does not match the expected model.
    pub fn get_all_versions(&self, repository: &str) -> Result<Vec<String>> {
        Ok(self
            .query(repository)?
            .iter()
            .map(|release| release.tag_name.clone())
            .collect())
    }

    /// Get the latest release version from the repository.
    ///
    /// Note that `repository` should be in the format "owner/repo",
    /// like `"celeo/github_release_check"`.
    ///
    /// As this function needs to select and return the latest release version,
    /// it makes use of the "semver" crate's `Version` [parse function]. As there's
    /// no requirement for repositories to use Semantic Versioning, this function may
    /// not suitable for every repository (thus the `get_all_versions` function which
    /// just works with `String`s).
    ///
    /// A leading `'v'` character is stripped from the versions
    /// in order to make more repositories work. For any version string that is not
    /// able to be loaded into a `Version` struct, it is skipped. Note that this
    /// may result in no or missing versions.
    ///
    /// Effectively, for repositories that are using Semantic Versioning correctly,
    /// this will work. For those that are not, it's a bit of a toss-up.
    ///
    /// Since this call can fail for a number of reasons including anything related to
    /// the network at the time of the call, the `Result` from this function should
    /// be handled appropriately.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use github_release_check::GitHub;
    /// let github = GitHub::new().unwrap();
    /// let version_result = github.get_latest_version("celeo/github_release_check");
    /// ```
    ///
    /// # Errors
    ///
    /// This function fails for any of the reasons in `get_all_versions`, or
    /// if no versions are returned from the API.
    ///
    /// [parse function]: https://docs.rs/semver/latest/semver/struct.Version.html#method.parse
    pub fn get_latest_version(&self, repository: &str) -> Result<Version> {
        let versions = self.get_all_versions(repository)?;
        let latest = versions
            .iter()
            .map(|s| {
                let mut s = s.clone();
                if s.starts_with('v') {
                    s = s.chars().skip(1).collect();
                }
                Version::parse(&s)
            })
            .filter_map(std::result::Result::ok)
            .max()
            .ok_or(LookupError::NoReleases)?;
        Ok(latest)
    }
}

/// Determine the last page (if any) from the GitHub response headers.
///
/// # Errors
///
/// This function fails if the the values in the "link" header
/// are not valid ASCII.
fn get_last_page(headers: &HeaderMap) -> Result<Option<usize>> {
    let links = match headers.get("link") {
        Some(l) => l.to_str()?,
        None => return Ok(None),
    };
    for page_ref in links.split(',') {
        if !page_ref.contains("rel=\"last\"") {
            continue;
        }
        for cap_part in PAGE_EXTRACT_REGEX.captures_iter(page_ref) {
            if cap_part[1].is_empty() {
                let page = cap_part[2]
                    .parse::<usize>()
                    .expect("Could not get page version from regex");
                return Ok(Some(page));
            }
        }
    }
    Ok(None)
}

#[cfg(test)]
mod tests {
    use super::{get_last_page, GitHub};
    use mockito::mock;
    use reqwest::header::{HeaderMap, HeaderName, HeaderValue};

    #[test]
    fn test_get_last_page_none() {
        let map = HeaderMap::new();
        let last = get_last_page(&map).unwrap();
        assert!(last.is_none());
    }

    #[test]
    fn test_get_last_page_some() {
        let mut map = HeaderMap::new();
        let _ = map.insert(
            HeaderName::from_static("link"),
            HeaderValue::from_static(r#"<https://api.github.com/repositories/275449421/releases?per_page=1&page=2>; rel="next", <https://api.github.com/repositories/275449421/releases?per_page=1&page=10>; rel="last""#)
        );
        let last = get_last_page(&map).unwrap();
        assert_eq!(last, Some(10));
    }

    #[test]
    fn test_get_all_versions_none() {
        let _m = mock("GET", "/repos/foo/bar/releases")
            .match_query(mockito::Matcher::Any)
            .with_body("[]")
            .create();
        let github = GitHub::from_custom(&format!("{}/", mockito::server_url()), "").unwrap();
        let versions = github.get_all_versions("foo/bar").unwrap();
        assert!(versions.is_empty());
    }

    #[test]
    fn test_get_all_versions_valid() {
        let rest = r#", "url": "", "assets_url": "", "upload_url": "", "html_url": "", "id": 1, "name": "", "draft": false, "prerelease": false, "created_at": "", "published_at": "", "body": """#;
        let _m = mock("GET", "/repos/foo/bar/releases")
            .match_query(mockito::Matcher::Any)
            .with_body(format!(
                r#"[
                {{ "tag_name": "v1.0.0"  {rest}}},
                {{ "tag_name": "v1.9.10"  {rest}}},
                {{ "tag_name": "v0.3.0" {rest}}}
            ]"#
            ))
            .create();
        let github = GitHub::from_custom(&format!("{}/", mockito::server_url()), "").unwrap();
        let versions = github.get_all_versions("foo/bar").unwrap();
        assert_eq!(versions.len(), 3);
    }

    #[test]
    fn test_get_latest_version_none() {
        let _m = mock("GET", "/repos/foo/bar/releases")
            .match_query(mockito::Matcher::Any)
            .with_body("[]")
            .create();
        let github = GitHub::from_custom(&format!("{}/", mockito::server_url()), "").unwrap();
        let version_res = github.get_latest_version("foo/bar");
        assert!(version_res.is_err());
    }

    #[test]
    fn test_get_latest_version_bad_semvers() {
        let rest = r#", "url": "", "assets_url": "", "upload_url": "", "html_url": "", "id": 1, "name": "", "draft": false, "prerelease": false, "created_at": "", "published_at": "", "body": """#;
        let _m = mock("GET", "/repos/foo/bar/releases")
            .match_query(mockito::Matcher::Any)
            .with_body(format!(
                r#"[
                {{ "tag_name": "uhhhh" {rest}}},
                {{ "tag_name": "v3.0.0-alpha" {rest}}},
                {{ "tag_name": "v1.9.10" {rest}}}
            ]"#
            ))
            .create();
        let github = GitHub::from_custom(&format!("{}/", mockito::server_url()), "").unwrap();
        let version = github.get_latest_version("foo/bar").unwrap();
        assert_eq!(version, semver::Version::parse("3.0.0-alpha").unwrap());
    }
}