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
mod config;
mod rest;
mod version;

use crate::testing::{decompress, CachedReleases, GitHubApiBuilder, Release};
use assert_fs::{fixture::PathChild, prelude::*};
pub use config::{
    LegacyConfigError, LegacyNodeConfig, LegacyNodeConfigBuilder, LegacyNodeConfigConverter,
    LegacyNodeConfigManager,
};
pub use jormungandr_lib::interfaces::{
    Log, Mempool, NodeConfig, P2p, Policy, Rest, TopicsOfInterest, TrustedPeer,
};
use jortestkit::file;
pub use rest::BackwardCompatibleRest;
use std::path::PathBuf;
pub use version::*;

const GITHUB_TOKEN: &str = "GITHUB_TOKEN";

lazy_static::lazy_static! {
    static ref RELEASES: CachedReleases = {
        let api = GitHubApiBuilder::new().with_token(std::env::var(GITHUB_TOKEN).ok()).build();
        api.describe_releases().unwrap()
    };
}

pub fn download_last_n_releases(n: u32) -> Vec<Release> {
    RELEASES
        .into_iter()
        .cloned()
        .filter(|x| !x.version_str().starts_with("nightly"))
        .take(n as usize)
        .collect()
}

pub fn get_jormungandr_bin(release: &Release, temp_dir: &impl PathChild) -> PathBuf {
    let asset = RELEASES
        .get_asset_for_current_os_by_version(release.version_str())
        .unwrap()
        .unwrap();
    let asset_name = asset.name();
    let output = temp_dir.child(asset_name);
    asset
        .download_to(output.path())
        .expect("cannot download file");
    let release_dir = temp_dir.child(format!("release-{}", release.version()));
    release_dir.create_dir_all().unwrap();
    decompress(output.path(), release_dir.path()).unwrap();
    file::find_file(release_dir.path(), "jormungandr")
        .unwrap()
        .unwrap()
}