Skip to main content

dxm_artifacts/
github.rs

1//! GitHub-specific code for finding information about any FXServer version.
2
3use reqwest::blocking::Client;
4use serde::Deserialize;
5
6const GITHUB_TAG_REF_API_URL: &str =
7    "https://api.github.com/repos/citizenfx/fivem/git/ref/tags/v1.0.0.{version}";
8
9#[derive(Deserialize)]
10struct Ref {
11    object: RefObject,
12}
13
14#[derive(Deserialize)]
15struct RefObject {
16    url: String,
17}
18
19#[derive(Deserialize)]
20struct Tag {
21    object: TagObject,
22}
23
24#[derive(Deserialize)]
25struct TagObject {
26    sha: String,
27}
28
29/// Fetches and returns the git commit SHA using the given FXServer version.
30pub fn get_version_commit_sha<S>(client: &Client, version: S) -> Result<String, reqwest::Error>
31where
32    S: AsRef<str>,
33{
34    let version = version.as_ref();
35
36    log::trace!("getting github tag ref");
37    let github_ref = client
38        .get(tag_api_url(version))
39        .send()?
40        .error_for_status()?
41        .json::<Ref>()?;
42
43    log::trace!("getting github tag");
44    let tag_url = github_ref.object.url;
45    let github_tag = client
46        .get(tag_url)
47        .send()?
48        .error_for_status()?
49        .json::<Tag>()?;
50
51    Ok(github_tag.object.sha)
52}
53
54fn tag_api_url<S>(version: S) -> String
55where
56    S: AsRef<str>,
57{
58    GITHUB_TAG_REF_API_URL.replace("{version}", version.as_ref())
59}
60
61#[cfg(test)]
62mod tests {
63    use super::*;
64
65    #[test]
66    fn returns_tag_api_url() {
67        assert_eq!(
68            tag_api_url("1234"),
69            "https://api.github.com/repos/citizenfx/fivem/git/ref/tags/v1.0.0.1234"
70        );
71    }
72}