gr/
remote.rs

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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
use std::fmt::{self, Display, Formatter};
use std::fs::File;
use std::path::Path;

use crate::api_traits::{
    Cicd, CicdJob, CicdRunner, CodeGist, CommentMergeRequest, ContainerRegistry, Deploy,
    DeployAsset, MergeRequest, ProjectMember, RemoteProject, RemoteTag, TrendingProjectURL,
    UserInfo,
};
use crate::cache::{filesystem::FileCache, nocache::NoCache};
use crate::config::{env_token, ConfigFile, NoConfig};
use crate::display::Format;
use crate::error::GRError;
use crate::github::Github;
use crate::gitlab::Gitlab;
use crate::io::{CmdInfo, HttpRunner, Response, TaskRunner};
use crate::time::Milliseconds;
use crate::{cli, error, http, log_debug, log_info};
use crate::{git, Result};
use std::sync::Arc;

pub mod query;

/// List cli args can be used across multiple APIs that support pagination.
#[derive(Builder, Clone)]
pub struct ListRemoteCliArgs {
    #[builder(default)]
    pub from_page: Option<i64>,
    #[builder(default)]
    pub to_page: Option<i64>,
    #[builder(default)]
    pub num_pages: bool,
    #[builder(default)]
    pub num_resources: bool,
    #[builder(default)]
    pub page_number: Option<i64>,
    #[builder(default)]
    pub created_after: Option<String>,
    #[builder(default)]
    pub created_before: Option<String>,
    #[builder(default)]
    pub sort: ListSortMode,
    #[builder(default)]
    pub flush: bool,
    #[builder(default)]
    pub throttle_time: Option<Milliseconds>,
    #[builder(default)]
    pub throttle_range: Option<(Milliseconds, Milliseconds)>,
    #[builder(default)]
    pub get_args: GetRemoteCliArgs,
}

impl ListRemoteCliArgs {
    pub fn builder() -> ListRemoteCliArgsBuilder {
        ListRemoteCliArgsBuilder::default()
    }
}

#[derive(Builder, Clone, Default)]
pub struct GetRemoteCliArgs {
    #[builder(default)]
    pub no_headers: bool,
    #[builder(default)]
    pub format: Format,
    #[builder(default)]
    pub cache_args: CacheCliArgs,
    #[builder(default)]
    pub display_optional: bool,
    #[builder(default)]
    pub backoff_max_retries: u32,
    #[builder(default)]
    pub backoff_retry_after: u64,
}

impl GetRemoteCliArgs {
    pub fn builder() -> GetRemoteCliArgsBuilder {
        GetRemoteCliArgsBuilder::default()
    }
}

#[derive(Builder, Clone, Default)]
pub struct CacheCliArgs {
    #[builder(default)]
    pub refresh: bool,
    #[builder(default)]
    pub no_cache: bool,
}

impl CacheCliArgs {
    pub fn builder() -> CacheCliArgsBuilder {
        CacheCliArgsBuilder::default()
    }
}

/// List body args is a common structure that can be used across multiple APIs
/// that support pagination. `list` operations in traits that accept some sort
/// of List related arguments can encapsulate this structure. Example of those
/// is `MergeRequestListBodyArgs`. This can be consumed by Github and Gitlab
/// clients when executing HTTP requests.
#[derive(Builder, Clone)]
pub struct ListBodyArgs {
    #[builder(setter(strip_option), default)]
    pub page: Option<i64>,
    #[builder(setter(strip_option), default)]
    pub max_pages: Option<i64>,
    #[builder(default)]
    pub created_after: Option<String>,
    #[builder(default)]
    pub created_before: Option<String>,
    #[builder(default)]
    pub sort_mode: ListSortMode,
    #[builder(default)]
    pub flush: bool,
    #[builder(default)]
    pub throttle_time: Option<Milliseconds>,
    #[builder(default)]
    pub throttle_range: Option<(Milliseconds, Milliseconds)>,
    // Carry display format for flush operations
    #[builder(default)]
    pub get_args: GetRemoteCliArgs,
}

impl ListBodyArgs {
    pub fn builder() -> ListBodyArgsBuilder {
        ListBodyArgsBuilder::default()
    }
}

pub fn validate_from_to_page(remote_cli_args: &ListRemoteCliArgs) -> Result<Option<ListBodyArgs>> {
    if remote_cli_args.page_number.is_some() {
        return Ok(Some(
            ListBodyArgs::builder()
                .page(remote_cli_args.page_number.unwrap())
                .max_pages(1)
                .sort_mode(remote_cli_args.sort.clone())
                .created_after(remote_cli_args.created_after.clone())
                .created_before(remote_cli_args.created_before.clone())
                .build()
                .unwrap(),
        ));
    }
    // TODO - this can probably be validated at the CLI level
    let body_args = match (remote_cli_args.from_page, remote_cli_args.to_page) {
        (Some(from_page), Some(to_page)) => {
            if from_page < 0 || to_page < 0 {
                return Err(GRError::PreconditionNotMet(
                    "from_page and to_page must be a positive number".to_string(),
                )
                .into());
            }
            if from_page >= to_page {
                return Err(GRError::PreconditionNotMet(
                    "from_page must be less than to_page".to_string(),
                )
                .into());
            }

            let max_pages = to_page - from_page + 1;
            Some(
                ListBodyArgs::builder()
                    .page(from_page)
                    .max_pages(max_pages)
                    .sort_mode(remote_cli_args.sort.clone())
                    .flush(remote_cli_args.flush)
                    .throttle_time(remote_cli_args.throttle_time)
                    .throttle_range(remote_cli_args.throttle_range)
                    .get_args(remote_cli_args.get_args.clone())
                    .build()
                    .unwrap(),
            )
        }
        (Some(_), None) => {
            return Err(
                GRError::PreconditionNotMet("from_page requires the to_page".to_string()).into(),
            );
        }
        (None, Some(to_page)) => {
            if to_page < 0 {
                return Err(GRError::PreconditionNotMet(
                    "to_page must be a positive number".to_string(),
                )
                .into());
            }
            Some(
                ListBodyArgs::builder()
                    .page(1)
                    .max_pages(to_page)
                    .sort_mode(remote_cli_args.sort.clone())
                    .flush(remote_cli_args.flush)
                    .throttle_time(remote_cli_args.throttle_time)
                    .throttle_range(remote_cli_args.throttle_range)
                    .get_args(remote_cli_args.get_args.clone())
                    .build()
                    .unwrap(),
            )
        }
        (None, None) => None,
    };
    match (
        remote_cli_args.created_after.clone(),
        remote_cli_args.created_before.clone(),
    ) {
        (Some(created_after), Some(created_before)) => {
            if let Some(body_args) = &body_args {
                return Ok(Some(
                    ListBodyArgs::builder()
                        .page(body_args.page.unwrap())
                        .max_pages(body_args.max_pages.unwrap())
                        .created_after(Some(created_after.to_string()))
                        .created_before(Some(created_before.to_string()))
                        .sort_mode(remote_cli_args.sort.clone())
                        .flush(remote_cli_args.flush)
                        .throttle_time(remote_cli_args.throttle_time)
                        .throttle_range(remote_cli_args.throttle_range)
                        .get_args(remote_cli_args.get_args.clone())
                        .build()
                        .unwrap(),
                ));
            }
            return Ok(Some(
                ListBodyArgs::builder()
                    .created_after(Some(created_after.to_string()))
                    .created_before(Some(created_before.to_string()))
                    .sort_mode(remote_cli_args.sort.clone())
                    .flush(remote_cli_args.flush)
                    .throttle_time(remote_cli_args.throttle_time)
                    .throttle_range(remote_cli_args.throttle_range)
                    .get_args(remote_cli_args.get_args.clone())
                    .build()
                    .unwrap(),
            ));
        }
        (Some(created_after), None) => {
            if let Some(body_args) = &body_args {
                return Ok(Some(
                    ListBodyArgs::builder()
                        .page(body_args.page.unwrap())
                        .max_pages(body_args.max_pages.unwrap())
                        .created_after(Some(created_after.to_string()))
                        .sort_mode(remote_cli_args.sort.clone())
                        .flush(remote_cli_args.flush)
                        .throttle_time(remote_cli_args.throttle_time)
                        .throttle_range(remote_cli_args.throttle_range)
                        .get_args(remote_cli_args.get_args.clone())
                        .build()
                        .unwrap(),
                ));
            }
            return Ok(Some(
                ListBodyArgs::builder()
                    .created_after(Some(created_after.to_string()))
                    .sort_mode(remote_cli_args.sort.clone())
                    .flush(remote_cli_args.flush)
                    .throttle_time(remote_cli_args.throttle_time)
                    .throttle_range(remote_cli_args.throttle_range)
                    .get_args(remote_cli_args.get_args.clone())
                    .build()
                    .unwrap(),
            ));
        }
        (None, Some(created_before)) => {
            if let Some(body_args) = &body_args {
                return Ok(Some(
                    ListBodyArgs::builder()
                        .page(body_args.page.unwrap())
                        .max_pages(body_args.max_pages.unwrap())
                        .created_before(Some(created_before.to_string()))
                        .sort_mode(remote_cli_args.sort.clone())
                        .flush(remote_cli_args.flush)
                        .throttle_time(remote_cli_args.throttle_time)
                        .throttle_range(remote_cli_args.throttle_range)
                        .get_args(remote_cli_args.get_args.clone())
                        .build()
                        .unwrap(),
                ));
            }
            return Ok(Some(
                ListBodyArgs::builder()
                    .created_before(Some(created_before.to_string()))
                    .sort_mode(remote_cli_args.sort.clone())
                    .flush(remote_cli_args.flush)
                    .throttle_time(remote_cli_args.throttle_time)
                    .throttle_range(remote_cli_args.throttle_range)
                    .get_args(remote_cli_args.get_args.clone())
                    .build()
                    .unwrap(),
            ));
        }
        (None, None) => {
            if let Some(body_args) = &body_args {
                return Ok(Some(
                    ListBodyArgs::builder()
                        .page(body_args.page.unwrap())
                        .max_pages(body_args.max_pages.unwrap())
                        .sort_mode(remote_cli_args.sort.clone())
                        .flush(remote_cli_args.flush)
                        .throttle_time(remote_cli_args.throttle_time)
                        .throttle_range(remote_cli_args.throttle_range)
                        .get_args(remote_cli_args.get_args.clone())
                        .build()
                        .unwrap(),
                ));
            }
            return Ok(Some(
                ListBodyArgs::builder()
                    .sort_mode(remote_cli_args.sort.clone())
                    .flush(remote_cli_args.flush)
                    .throttle_time(remote_cli_args.throttle_time)
                    .throttle_range(remote_cli_args.throttle_range)
                    .get_args(remote_cli_args.get_args.clone())
                    .build()
                    .unwrap(),
            ));
        }
    }
}

pub struct URLQueryParamBuilder {
    url: String,
}

impl URLQueryParamBuilder {
    pub fn new(url: &str) -> Self {
        URLQueryParamBuilder {
            url: url.to_string(),
        }
    }

    pub fn add_param(&mut self, key: &str, value: &str) -> &mut Self {
        if self.url.contains('?') {
            self.url.push_str(&format!("&{}={}", key, value));
        } else {
            self.url.push_str(&format!("?{}={}", key, value));
        }
        self
    }

    pub fn build(&self) -> String {
        self.url.clone()
    }
}

#[derive(Clone, Debug, Default, PartialEq)]
pub enum ListSortMode {
    #[default]
    Asc,
    Desc,
}

#[derive(Clone, Debug, PartialEq)]
pub enum CacheType {
    File,
    None,
}

use crate::config::ConfigProperties;
macro_rules! get {
    ($func_name:ident, $trait_name:ident) => {
        paste::paste! {
            pub fn $func_name(
                domain: String,
                path: String,
                config: Arc<dyn ConfigProperties + Send + Sync + 'static>,
                cache_args: Option<&CacheCliArgs>,
                cache_type: CacheType,
            ) -> Result<Arc<dyn $trait_name + Send + Sync + 'static>> {
                let refresh_cache = cache_args.map_or(false, |args| args.refresh);
                let no_cache_args = cache_args.map_or(false, |args| args.no_cache);

                log_debug!("cache_type: {:?}", cache_type);
                log_debug!("no_cache_args: {:?}", no_cache_args);
                log_debug!("cache location: {:?}", config.cache_location());

                if cache_type == CacheType::None || no_cache_args || config.cache_location().is_none() {
                    log_info!("No cache used for {}", stringify!($func_name));
                    let runner = Arc::new(http::Client::new(NoCache, config.clone(), refresh_cache));
                    [<create_remote_ $func_name>](domain, path, config, runner)
                } else {
                    log_info!("File cache used for {}", stringify!($func_name));
                    let file_cache = FileCache::new(config.clone());
                    file_cache.validate_cache_location()?;
                    let runner = Arc::new(http::Client::new(file_cache, config.clone(), refresh_cache));
                    [<create_remote_ $func_name>](domain, path, config, runner)
                }
            }

            fn [<create_remote_ $func_name>]<R>(
                domain: String,
                path: String,
                config: Arc<dyn ConfigProperties>,
                runner: Arc<R>,
            ) -> Result<Arc<dyn $trait_name + Send + Sync + 'static>>
            where
                R: HttpRunner<Response = Response> + Send + Sync + 'static,
            {
                let github_domain_regex = regex::Regex::new(r"^github").unwrap();
                let gitlab_domain_regex = regex::Regex::new(r"^gitlab").unwrap();
                let remote: Arc<dyn $trait_name + Send + Sync + 'static> =
                    if github_domain_regex.is_match(&domain) {
                        Arc::new(Github::new(config, &domain, &path, runner))
                    } else if gitlab_domain_regex.is_match(&domain) {
                        Arc::new(Gitlab::new(config, &domain, &path, runner))
                    } else {
                        return Err(error::gen(format!("Unsupported domain: {}", &domain)));
                    };
                Ok(remote)
            }
        }
    };
}

get!(get_mr, MergeRequest);
get!(get_cicd, Cicd);
get!(get_project, RemoteProject);
get!(get_tag, RemoteTag);
get!(get_user, UserInfo);
get!(get_project_member, ProjectMember);
get!(get_registry, ContainerRegistry);
get!(get_deploy, Deploy);
get!(get_deploy_asset, DeployAsset);
get!(get_auth_user, UserInfo);
get!(get_cicd_runner, CicdRunner);
get!(get_comment_mr, CommentMergeRequest);
get!(get_trending, TrendingProjectURL);
get!(get_gist, CodeGist);
get!(get_cicd_job, CicdJob);

pub fn extract_domain_path(repo_cli: &str) -> (String, String) {
    let parts: Vec<&str> = repo_cli.split('/').collect();
    let domain = parts[0].to_string();
    let path = parts[1..].join("/");
    (domain, path)
}

/// Given a CLI command, the command can work as long as user is in a cd
/// repository, user passes --domain flag (DomainArgs) or --repo flag
/// (RepoArgs). Some CLI commands might work with one variant, with both, with
/// all or might have no requirement at all.
pub enum CliDomainRequirements {
    CdInLocalRepo,
    DomainArgs,
    RepoArgs,
}

#[derive(Clone, Debug)]
pub struct RemoteURL {
    /// Domain of the project. Ex github.com
    domain: String,
    /// Path to the project. Ex jordilin/gitar
    path: String,
    /// Config encoded project path. Ex jordilin_gitar
    /// This is used as a key in TOML configuration in order to retrieve project
    /// specific configuration that overrides its domain specific one.
    config_encoded_project_path: String,
    config_encoded_domain: String,
}

impl RemoteURL {
    pub fn new(domain: String, path: String) -> Self {
        let config_encoded_project_path = path.replace("/", "_");
        let config_encoded_domain = domain.replace(".", "_");
        RemoteURL {
            domain,
            path,
            config_encoded_project_path,
            config_encoded_domain,
        }
    }

    pub fn domain(&self) -> &str {
        &self.domain
    }

    pub fn path(&self) -> &str {
        &self.path
    }

    pub fn config_encoded_project_path(&self) -> &str {
        &self.config_encoded_project_path
    }

    pub fn config_encoded_domain(&self) -> &str {
        &self.config_encoded_domain
    }
}

impl CliDomainRequirements {
    pub fn check<R: TaskRunner<Response = Response>>(
        &self,
        cli_args: &cli::CliArgs,
        runner: &R,
    ) -> Result<RemoteURL> {
        match self {
            CliDomainRequirements::CdInLocalRepo => match git::remote_url(runner) {
                Ok(CmdInfo::RemoteUrl(url)) => Ok(url),
                Err(err) => Err(GRError::GitRemoteUrlNotFound(format!("{}", err)).into()),
                _ => Err(GRError::ApplicationError(
                    "Could not get remote url during startup. \
                        main::get_config_domain_path - Please open a bug to \
                        https://github.com/jordilin/gitar"
                        .to_string(),
                )
                .into()),
            },
            CliDomainRequirements::DomainArgs => {
                if cli_args.domain.is_some() {
                    Ok(RemoteURL::new(
                        cli_args.domain.as_ref().unwrap().to_string(),
                        "".to_string(),
                    ))
                } else {
                    Err(GRError::DomainExpected("Missing domain information".to_string()).into())
                }
            }
            CliDomainRequirements::RepoArgs => {
                if cli_args.repo.is_some() {
                    let (domain, path) = extract_domain_path(cli_args.repo.as_ref().unwrap());
                    Ok(RemoteURL::new(domain, path))
                } else {
                    Err(GRError::RepoExpected("Missing repository information".to_string()).into())
                }
            }
        }
    }
}

impl Display for CliDomainRequirements {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match self {
            CliDomainRequirements::CdInLocalRepo => write!(f, "cd to a git repository"),
            CliDomainRequirements::DomainArgs => write!(f, "provide --domain option"),
            CliDomainRequirements::RepoArgs => write!(f, "provide --repo option"),
        }
    }
}

pub fn url<R: TaskRunner<Response = Response>>(
    cli_args: &cli::CliArgs,
    requirements: &[CliDomainRequirements],
    runner: &R,
) -> Result<RemoteURL> {
    let mut errors = Vec::new();
    for requirement in requirements {
        match requirement.check(cli_args, runner) {
            Ok(url) => return Ok(url),
            Err(err) => {
                errors.push(err);
            }
        }
    }
    let trace = errors
        .iter()
        .map(|e| format!("{}", e))
        .collect::<Vec<String>>()
        .join("\n");

    let expectations_missed_trace = requirements
        .iter()
        .map(|r| format!("{}", r))
        .collect::<Vec<String>>()
        .join(" OR ");

    Err(GRError::PreconditionNotMet(format!(
        "\n\nMissed requirements: {}\n\n Errors:\n\n {}",
        expectations_missed_trace, trace
    ))
    .into())
}

pub fn read_config(config_file: &Path, url: &RemoteURL) -> Result<Arc<dyn ConfigProperties>> {
    match File::open(config_file) {
        Ok(f) => {
            let config = ConfigFile::new(f, url, env_token)?;
            Ok(Arc::new(config))
        }
        Err(_) => {
            let config = NoConfig::new(url.domain(), env_token)?;
            Ok(Arc::new(config))
        }
    }
}

#[cfg(test)]
mod test {
    use cli::CliArgs;

    use crate::test::utils::MockRunner;

    use super::*;

    #[test]
    fn test_cli_from_to_pages_valid_range() {
        let from_page = Option::Some(1);
        let to_page = Option::Some(3);
        let args = ListRemoteCliArgs::builder()
            .from_page(from_page)
            .to_page(to_page)
            .build()
            .unwrap();
        let args = validate_from_to_page(&args).unwrap().unwrap();
        assert_eq!(args.page, Some(1));
        assert_eq!(args.max_pages, Some(3));
    }

    #[test]
    fn test_cli_from_to_pages_invalid_range() {
        let from_page = Some(5);
        let to_page = Some(2);
        let args = ListRemoteCliArgs::builder()
            .from_page(from_page)
            .to_page(to_page)
            .build()
            .unwrap();
        let args = validate_from_to_page(&args);
        match args {
            Err(err) => match err.downcast_ref::<error::GRError>() {
                Some(error::GRError::PreconditionNotMet(_)) => (),
                _ => panic!("Expected error::GRError::PreconditionNotMet"),
            },
            _ => panic!("Expected error"),
        }
    }

    #[test]
    fn test_cli_from_page_negative_number_is_error() {
        let from_page = Some(-5);
        let to_page = Some(5);
        let args = ListRemoteCliArgs::builder()
            .from_page(from_page)
            .to_page(to_page)
            .build()
            .unwrap();
        let args = validate_from_to_page(&args);
        match args {
            Err(err) => match err.downcast_ref::<error::GRError>() {
                Some(error::GRError::PreconditionNotMet(_)) => (),
                _ => panic!("Expected error::GRError::PreconditionNotMet"),
            },
            _ => panic!("Expected error"),
        }
    }

    #[test]
    fn test_cli_to_page_negative_number_is_error() {
        let from_page = Some(5);
        let to_page = Some(-5);
        let args = ListRemoteCliArgs::builder()
            .from_page(from_page)
            .to_page(to_page)
            .build()
            .unwrap();
        let args = validate_from_to_page(&args);
        match args {
            Err(err) => match err.downcast_ref::<error::GRError>() {
                Some(error::GRError::PreconditionNotMet(_)) => (),
                _ => panic!("Expected error::GRError::PreconditionNotMet"),
            },
            _ => panic!("Expected error"),
        }
    }

    #[test]
    fn test_cli_from_page_without_to_page_is_error() {
        let from_page = Some(5);
        let to_page = None;
        let args = ListRemoteCliArgs::builder()
            .from_page(from_page)
            .to_page(to_page)
            .build()
            .unwrap();
        let args = validate_from_to_page(&args);
        match args {
            Err(err) => match err.downcast_ref::<error::GRError>() {
                Some(error::GRError::PreconditionNotMet(_)) => (),
                _ => panic!("Expected error::GRError::PreconditionNotMet"),
            },
            _ => panic!("Expected error"),
        }
    }

    #[test]
    fn test_if_from_and_to_provided_must_be_positive() {
        let from_page = Some(-5);
        let to_page = Some(-5);
        let args = ListRemoteCliArgs::builder()
            .from_page(from_page)
            .to_page(to_page)
            .build()
            .unwrap();
        let args = validate_from_to_page(&args);
        match args {
            Err(err) => match err.downcast_ref::<error::GRError>() {
                Some(error::GRError::PreconditionNotMet(_)) => (),
                _ => panic!("Expected error::GRError::PreconditionNotMet"),
            },
            _ => panic!("Expected error"),
        }
    }

    #[test]
    fn test_if_page_number_provided_max_pages_is_1() {
        let page_number = Some(5);
        let args = ListRemoteCliArgs::builder()
            .page_number(page_number)
            .build()
            .unwrap();
        let args = validate_from_to_page(&args).unwrap().unwrap();
        assert_eq!(args.page, Some(5));
        assert_eq!(args.max_pages, Some(1));
    }

    #[test]
    fn test_include_created_after_in_list_body_args() {
        let created_after = "2021-01-01T00:00:00Z";
        let args = ListRemoteCliArgs::builder()
            .created_after(Some(created_after.to_string()))
            .build()
            .unwrap();
        let args = validate_from_to_page(&args).unwrap().unwrap();
        assert_eq!(args.created_after.unwrap(), created_after);
    }

    #[test]
    fn test_includes_from_to_page_and_created_after_in_list_body_args() {
        let from_page = Some(1);
        let to_page = Some(3);
        let created_after = "2021-01-01T00:00:00Z";
        let args = ListRemoteCliArgs::builder()
            .from_page(from_page)
            .to_page(to_page)
            .created_after(Some(created_after.to_string()))
            .build()
            .unwrap();
        let args = validate_from_to_page(&args).unwrap().unwrap();
        assert_eq!(args.page, Some(1));
        assert_eq!(args.max_pages, Some(3));
        assert_eq!(args.created_after.unwrap(), created_after);
    }

    #[test]
    fn test_includes_sort_mode_in_list_body_args_used_with_created_after() {
        let args = ListRemoteCliArgs::builder()
            .created_after(Some("2021-01-01T00:00:00Z".to_string()))
            .sort(ListSortMode::Desc)
            .build()
            .unwrap();
        let args = validate_from_to_page(&args).unwrap().unwrap();
        assert_eq!(args.sort_mode, ListSortMode::Desc);
    }

    #[test]
    fn test_includes_sort_mode_in_list_body_args_used_with_from_to_page() {
        let from_page = Some(1);
        let to_page = Some(3);
        let args = ListRemoteCliArgs::builder()
            .from_page(from_page)
            .to_page(to_page)
            .sort(ListSortMode::Desc)
            .build()
            .unwrap();
        let args = validate_from_to_page(&args).unwrap().unwrap();
        assert_eq!(args.sort_mode, ListSortMode::Desc);
    }

    #[test]
    fn test_includes_sort_mode_in_list_body_args_used_with_page_number() {
        let page_number = Some(1);
        let args = ListRemoteCliArgs::builder()
            .page_number(page_number)
            .sort(ListSortMode::Desc)
            .build()
            .unwrap();
        let args = validate_from_to_page(&args).unwrap().unwrap();
        assert_eq!(args.sort_mode, ListSortMode::Desc);
    }

    #[test]
    fn test_add_created_after_with_page_number() {
        let page_number = Some(1);
        let created_after = "2021-01-01T00:00:00Z";
        let args = ListRemoteCliArgs::builder()
            .page_number(page_number)
            .created_after(Some(created_after.to_string()))
            .build()
            .unwrap();
        let args = validate_from_to_page(&args).unwrap().unwrap();
        assert_eq!(args.page.unwrap(), 1);
        assert_eq!(args.max_pages.unwrap(), 1);
        assert_eq!(args.created_after.unwrap(), created_after);
    }

    #[test]
    fn test_add_created_before_with_page_number() {
        let page_number = Some(1);
        let created_before = "2021-01-01T00:00:00Z";
        let args = ListRemoteCliArgs::builder()
            .page_number(page_number)
            .created_before(Some(created_before.to_string()))
            .build()
            .unwrap();
        let args = validate_from_to_page(&args).unwrap().unwrap();
        assert_eq!(args.page.unwrap(), 1);
        assert_eq!(args.max_pages.unwrap(), 1);
        assert_eq!(args.created_before.unwrap(), created_before);
    }

    #[test]
    fn test_add_created_before_with_from_to_page() {
        let from_page = Some(1);
        let to_page = Some(3);
        let created_before = "2021-01-01T00:00:00Z";
        let args = ListRemoteCliArgs::builder()
            .from_page(from_page)
            .to_page(to_page)
            .created_before(Some(created_before.to_string()))
            .sort(ListSortMode::Desc)
            .build()
            .unwrap();
        let args = validate_from_to_page(&args).unwrap().unwrap();
        assert_eq!(args.page.unwrap(), 1);
        assert_eq!(args.max_pages.unwrap(), 3);
        assert_eq!(args.created_before.unwrap(), created_before);
        assert_eq!(args.sort_mode, ListSortMode::Desc);
    }

    #[test]
    fn test_add_crated_before_with_no_created_after_option_and_no_page_number() {
        let created_before = "2021-01-01T00:00:00Z";
        let args = ListRemoteCliArgs::builder()
            .created_before(Some(created_before.to_string()))
            .sort(ListSortMode::Desc)
            .build()
            .unwrap();
        let args = validate_from_to_page(&args).unwrap().unwrap();
        assert_eq!(args.created_before.unwrap(), created_before);
        assert_eq!(args.sort_mode, ListSortMode::Desc);
    }

    #[test]
    fn test_adds_created_after_and_created_before_with_from_to_page() {
        let from_page = Some(1);
        let to_page = Some(3);
        let created_after = "2021-01-01T00:00:00Z";
        let created_before = "2021-01-02T00:00:00Z";
        let args = ListRemoteCliArgs::builder()
            .from_page(from_page)
            .to_page(to_page)
            .created_after(Some(created_after.to_string()))
            .created_before(Some(created_before.to_string()))
            .sort(ListSortMode::Desc)
            .build()
            .unwrap();
        let args = validate_from_to_page(&args).unwrap().unwrap();
        assert_eq!(args.page.unwrap(), 1);
        assert_eq!(args.max_pages.unwrap(), 3);
        assert_eq!(args.created_after.unwrap(), created_after);
        assert_eq!(args.created_before.unwrap(), created_before);
        assert_eq!(args.sort_mode, ListSortMode::Desc);
    }

    #[test]
    fn test_add_created_after_and_before_no_from_to_page_options() {
        let created_after = "2021-01-01T00:00:00Z";
        let created_before = "2021-01-02T00:00:00Z";
        let args = ListRemoteCliArgs::builder()
            .created_after(Some(created_after.to_string()))
            .created_before(Some(created_before.to_string()))
            .sort(ListSortMode::Desc)
            .build()
            .unwrap();
        let args = validate_from_to_page(&args).unwrap().unwrap();
        assert_eq!(args.created_after.unwrap(), created_after);
        assert_eq!(args.created_before.unwrap(), created_before);
        assert_eq!(args.sort_mode, ListSortMode::Desc);
    }

    #[test]
    fn test_if_only_to_page_provided_max_pages_is_to_page() {
        let to_page = Some(3);
        let args = ListRemoteCliArgs::builder()
            .to_page(to_page)
            .build()
            .unwrap();
        let args = validate_from_to_page(&args).unwrap().unwrap();
        assert_eq!(args.page, Some(1));
        assert_eq!(args.max_pages, Some(3));
    }

    #[test]
    fn test_if_only_to_page_provided_and_negative_number_is_error() {
        let to_page = Some(-3);
        let args = ListRemoteCliArgs::builder()
            .to_page(to_page)
            .build()
            .unwrap();
        let args = validate_from_to_page(&args);
        match args {
            Err(err) => match err.downcast_ref::<error::GRError>() {
                Some(error::GRError::PreconditionNotMet(_)) => (),
                _ => panic!("Expected error::GRError::PreconditionNotMet"),
            },
            _ => panic!("Expected error"),
        }
    }

    #[test]
    fn test_if_sort_provided_use_it() {
        let args = ListRemoteCliArgs::builder()
            .sort(ListSortMode::Desc)
            .build()
            .unwrap();
        let args = validate_from_to_page(&args).unwrap().unwrap();
        assert_eq!(args.sort_mode, ListSortMode::Desc);
    }

    #[test]
    fn test_if_flush_option_provided_use_it() {
        let args = ListRemoteCliArgs::builder().flush(true).build().unwrap();
        let args = validate_from_to_page(&args).unwrap().unwrap();
        assert!(args.flush);
    }

    #[test]
    fn test_query_param_builder_no_params() {
        let url = "https://example.com";
        let url = URLQueryParamBuilder::new(url).build();
        assert_eq!(url, "https://example.com");
    }

    #[test]
    fn test_query_param_builder_with_params() {
        let url = "https://example.com";
        let url = URLQueryParamBuilder::new(url)
            .add_param("key", "value")
            .add_param("key2", "value2")
            .build();
        assert_eq!(url, "https://example.com?key=value&key2=value2");
    }

    #[test]
    fn test_retrieve_domain_path_from_repo_cli_flag() {
        let repo_cli = "github.com/jordilin/gitar";
        let (domain, path) = extract_domain_path(repo_cli);
        assert_eq!("github.com", domain);
        assert_eq!("jordilin/gitar", path);
    }

    #[test]
    fn test_cli_requires_cd_local_repo_run_git_remote() {
        let cli_args = CliArgs::new(0, None, None, None);
        let response = Response::builder()
            .body("git@github.com:jordilin/gitar.git".to_string())
            .build()
            .unwrap();
        let runner = MockRunner::new(vec![response]);
        let requirements = vec![CliDomainRequirements::CdInLocalRepo];
        let url = url(&cli_args, &requirements, &runner).unwrap();
        assert_eq!("github.com", url.domain());
        assert_eq!("jordilin/gitar", url.path());
    }

    #[test]
    fn test_cli_requires_cd_local_repo_run_git_remote_error() {
        let cli_args = CliArgs::new(0, None, None, None);
        let response = Response::builder().body("".to_string()).build().unwrap();
        let runner = MockRunner::new(vec![response]);
        let requirements = vec![CliDomainRequirements::CdInLocalRepo];
        let result = url(&cli_args, &requirements, &runner);
        match result {
            Err(err) => match err.downcast_ref::<error::GRError>() {
                Some(error::GRError::PreconditionNotMet(_)) => (),
                _ => panic!("Expected error::GRError::GitRemoteUrlNotFound"),
            },
            _ => panic!("Expected error"),
        }
    }

    #[test]
    fn test_cli_requires_repo_args_or_cd_repo_fails_on_cd_repo() {
        let cli_args = CliArgs::new(0, Some("github.com/jordilin/gitar".to_string()), None, None);
        let requirements = vec![
            CliDomainRequirements::CdInLocalRepo,
            CliDomainRequirements::RepoArgs,
        ];
        let response = Response::builder().body("".to_string()).build().unwrap();
        let url = url(&cli_args, &requirements, &MockRunner::new(vec![response])).unwrap();
        assert_eq!("github.com", url.domain());
        assert_eq!("jordilin/gitar", url.path());
        assert_eq!("jordilin_gitar", url.config_encoded_project_path());
    }

    #[test]
    fn test_cli_requires_domain_args_or_cd_repo_fails_on_cd_repo() {
        let cli_args = CliArgs::new(0, None, Some("github.com".to_string()), None);
        let requirements = vec![
            CliDomainRequirements::CdInLocalRepo,
            CliDomainRequirements::DomainArgs,
        ];
        let response = Response::builder().body("".to_string()).build().unwrap();
        let url = url(&cli_args, &requirements, &MockRunner::new(vec![response])).unwrap();
        assert_eq!("github.com", url.domain());
        assert_eq!("", url.path());
    }

    #[test]
    fn test_remote_url() {
        let remote_url = RemoteURL::new("github.com".to_string(), "jordilin/gitar".to_string());
        assert_eq!("github.com", remote_url.domain());
        assert_eq!("jordilin/gitar", remote_url.path());
    }

    #[test]
    fn test_get_config_encoded_project_path() {
        let remote_url = RemoteURL::new("github.com".to_string(), "jordilin/gitar".to_string());
        assert_eq!("jordilin_gitar", remote_url.config_encoded_project_path());
    }

    #[test]
    fn test_get_config_encoded_project_path_multiple_groups() {
        let remote_url = RemoteURL::new(
            "gitlab.com".to_string(),
            "team/subgroup/project".to_string(),
        );
        assert_eq!(
            "team_subgroup_project",
            remote_url.config_encoded_project_path()
        );
    }

    #[test]
    fn test_get_config_encoded_domain() {
        let remote_url = RemoteURL::new("github.com".to_string(), "jordilin/gitar".to_string());
        assert_eq!("github_com", remote_url.config_encoded_domain());
    }
}