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
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
|
//! ref: composer/src/Composer/Repository/Vcs/GitLabDriver.php
use crate::io::io_interface;
use anyhow::Result;
use chrono::{DateTime, Utc};
use indexmap::IndexMap;
use shirabe_external_packages::composer::pcre::{CaptureKey, Preg};
use shirabe_php_shim::{
InvalidArgumentException, LogicException, PhpMixed, RuntimeException, array_search_mixed,
array_shift, ctype_alnum, empty, explode, extension_loaded, implode, in_array, is_array,
is_string, ord, sprintf, strpos, strtolower,
};
use crate::cache::Cache;
use crate::config::Config;
use crate::downloader::TransportException;
use crate::io::IOInterface;
use crate::io::IOInterfaceImmutable;
use crate::json::JsonFile;
use crate::repository::vcs::GitDriver;
use crate::repository::vcs::VcsDriverBase;
use crate::repository::vcs::VcsDriverInterface;
use crate::util::GitLab;
use crate::util::HttpDownloader;
use crate::util::http::Response;
/// Driver for GitLab API, use the Git driver for local checkouts.
#[derive(Debug)]
pub struct GitLabDriver {
pub(crate) inner: VcsDriverBase,
/// @phpstan-var 'https'|'http'
scheme: String,
namespace: String,
repository: String,
/// @var mixed[] Project data returned by GitLab API
project: Option<IndexMap<String, PhpMixed>>,
/// @var array<string|int, mixed[]> Keeps commits returned by GitLab API as commit id => info
commits: IndexMap<String, IndexMap<String, PhpMixed>>,
/// @var array<int|string, string> Map of tag name to identifier
tags: Option<IndexMap<String, String>>,
/// @var array<int|string, string> Map of branch name to identifier
branches: Option<IndexMap<String, String>>,
/// Git Driver
pub(crate) git_driver: Option<GitDriver>,
/// Protocol to force use of for repository URLs.
/// @var string One of ssh, http
pub(crate) protocol: String,
/// Defaults to true unless we can make sure it is public
/// @var bool defines whether the repo is private or not
is_private: bool,
/// @var bool true if the origin has a port number or a path component in it
has_nonstandard_origin: bool,
}
impl GitLabDriver {
pub const URL_REGEX: &'static str = r##"#^(?:(?P<scheme>https?)://(?P<domain>.+?)(?::(?P<port>[0-9]+))?/|git@(?P<domain2>[^:]+):)(?P<parts>.+)/(?P<repo>[^/]+?)(?:\.git|/)?$#"##;
/// Extracts information from the repository url.
///
/// SSH urls use https by default. Set "secure-http": false on the repository config to use http instead.
pub fn initialize(&mut self) -> Result<()> {
let mut match_: IndexMap<CaptureKey, String> = IndexMap::new();
if !Preg::is_match_strict_groups3(Self::URL_REGEX, &self.inner.url, Some(&mut match_))
.unwrap_or(false)
{
return Err(InvalidArgumentException {
message: sprintf(
"The GitLab repository URL %s is invalid. It must be the HTTP URL of a GitLab project.",
&[PhpMixed::String(self.inner.url.clone())],
),
code: 0,
}
.into());
}
let guessed_domain = match_
.get(&CaptureKey::ByName("domain".to_string()))
.cloned()
.filter(|s| !s.is_empty())
.unwrap_or_else(|| {
match_
.get(&CaptureKey::ByName("domain2".to_string()))
.cloned()
.unwrap_or_default()
});
let configured_domains = self.inner.config.borrow_mut().get("gitlab-domains");
let mut url_parts: Vec<String> = explode(
"/",
&match_
.get(&CaptureKey::ByName("parts".to_string()))
.cloned()
.unwrap_or_default(),
);
let scheme_match = match_
.get(&CaptureKey::ByName("scheme".to_string()))
.cloned()
.unwrap_or_default();
self.scheme = if in_array(
PhpMixed::String(scheme_match.clone()),
&PhpMixed::List(vec![
Box::new(PhpMixed::String("https".to_string())),
Box::new(PhpMixed::String("http".to_string())),
]),
true,
) {
scheme_match
} else if self
.inner
.repo_config
.get("secure-http")
.and_then(|v| v.as_bool())
== Some(false)
{
"http".to_string()
} else {
"https".to_string()
};
let port = match_.get(&CaptureKey::ByName("port".to_string())).cloned();
let origin = Self::determine_origin(
&configured_domains,
guessed_domain,
&mut url_parts,
port.clone(),
);
let origin = match origin {
Some(o) => o,
None => {
return Err(LogicException {
message: format!(
"It should not be possible to create a gitlab driver with an unparsable origin URL ({})",
self.inner.url
),
code: 0,
}
.into());
}
};
self.inner.origin_url = origin;
let protocol_value = self.inner.config.borrow_mut().get("gitlab-protocol");
if let Some(protocol) = protocol_value
.as_string()
.filter(|_| is_string(&protocol_value))
{
// https treated as a synonym for http.
if !in_array(
PhpMixed::String(protocol.to_string()),
&PhpMixed::List(vec![
Box::new(PhpMixed::String("git".to_string())),
Box::new(PhpMixed::String("http".to_string())),
Box::new(PhpMixed::String("https".to_string())),
]),
true,
) {
return Err(RuntimeException {
message: "gitlab-protocol must be one of git, http.".to_string(),
code: 0,
}
.into());
}
self.protocol = if protocol == "git" {
"ssh".to_string()
} else {
"http".to_string()
};
}
if strpos(&self.inner.origin_url, ":").is_some()
|| strpos(&self.inner.origin_url, "/").is_some()
{
self.has_nonstandard_origin = true;
}
self.namespace = implode("/", &url_parts);
self.repository = Preg::replace(
r"#(\.git)$#",
"",
&match_
.get(&CaptureKey::ByName("repo".to_string()))
.cloned()
.unwrap_or_default(),
)
.unwrap_or_default();
self.inner.cache = Some(Cache::new(
self.inner.io.clone(),
&format!(
"{}/{}/{}/{}",
self.inner
.config
.borrow_mut()
.get("cache-repo-dir")
.as_string()
.unwrap_or(""),
self.inner.origin_url,
self.namespace,
self.repository,
),
None,
None,
false,
));
self.inner.cache.as_mut().map(|c| {
c.set_read_only(
self.inner
.config
.borrow_mut()
.get("cache-read-only")
.as_bool()
.unwrap_or(false),
)
});
self.fetch_project()?;
Ok(())
}
/// Updates the HttpDownloader instance.
/// Mainly useful for tests.
///
/// @internal
pub fn set_http_downloader(
&mut self,
http_downloader: std::rc::Rc<std::cell::RefCell<HttpDownloader>>,
) {
self.inner.http_downloader = http_downloader;
}
pub fn get_composer_information(
&mut self,
identifier: &str,
) -> Result<Option<IndexMap<String, PhpMixed>>> {
if let Some(ref mut git_driver) = self.git_driver {
return git_driver.get_composer_information(identifier);
}
if !self.inner.info_cache.contains_key(identifier) {
let composer = if self.inner.should_cache(identifier)
&& self
.inner
.cache
.as_mut()
.and_then(|c| c.read(identifier))
.is_some()
{
let res = self
.inner
.cache
.as_mut()
.and_then(|c| c.read(identifier))
.unwrap_or_default();
// TODO(phase-b): cached payload is wrapped to satisfy outer Option type
Some(
JsonFile::parse_json(Some(&res), None)?
.as_array()
.cloned()
.map(|m| {
m.into_iter()
.map(|(k, v)| (k, *v))
.collect::<IndexMap<String, PhpMixed>>()
})
.unwrap_or_default(),
)
} else {
let file_content = self.get_file_content("composer.json", identifier)?;
let composer = VcsDriverBase::finish_base_composer_information(
identifier,
file_content,
|| self.get_change_date(identifier),
)?;
if self.inner.should_cache(identifier) {
if let Some(ref composer_map) = composer {
self.inner.cache.as_mut().map(|c| {
c.write(
identifier,
&JsonFile::encode(
&PhpMixed::Array(
composer_map
.clone()
.into_iter()
.map(|(k, v)| (k, Box::new(v)))
.collect(),
),
shirabe_php_shim::JSON_UNESCAPED_UNICODE
| shirabe_php_shim::JSON_UNESCAPED_SLASHES,
),
)
});
}
}
composer
};
let mut composer = composer;
if let Some(ref mut composer) = composer {
// specials for gitlab (this data is only available if authentication is provided)
if composer.contains_key("support")
&& !is_array(&composer.get("support").cloned().unwrap_or(PhpMixed::Null))
{
composer.insert("support".to_string(), PhpMixed::Array(IndexMap::new()));
}
let project = self.project.clone().unwrap_or_default();
let has_web_url = project.contains_key("web_url");
let support_source_missing = !composer
.get("support")
.and_then(|v| v.as_array())
.map(|m| m.contains_key("source"))
.unwrap_or(false);
if support_source_missing && has_web_url {
let label = array_search_mixed(
&PhpMixed::String(identifier.to_string()),
&PhpMixed::Array(
self.get_tags()?
.into_iter()
.map(|(k, v)| (k, Box::new(PhpMixed::String(v))))
.collect(),
),
true,
)
.filter(|v| !matches!(v, PhpMixed::Bool(false) | PhpMixed::Null))
.or_else(|| {
array_search_mixed(
&PhpMixed::String(identifier.to_string()),
&PhpMixed::Array(
self.get_branches()
.unwrap_or_default()
.into_iter()
.map(|(k, v)| (k, Box::new(PhpMixed::String(v))))
.collect(),
),
true,
)
})
.filter(|v| !matches!(v, PhpMixed::Bool(false) | PhpMixed::Null))
.unwrap_or_else(|| PhpMixed::String(identifier.to_string()));
let label_str = label.as_string().unwrap_or(identifier).to_string();
let web_url = project
.get("web_url")
.and_then(|v| v.as_string())
.unwrap_or("")
.to_string();
if let Some(support) = composer.get_mut("support").and_then(|v| match v {
PhpMixed::Array(m) => Some(m),
_ => None,
}) {
support.insert(
"source".to_string(),
Box::new(PhpMixed::String(sprintf(
"%s/-/tree/%s",
&[PhpMixed::String(web_url), PhpMixed::String(label_str)],
))),
);
}
}
let issues_missing = !composer
.get("support")
.and_then(|v| v.as_array())
.map(|m| m.contains_key("issues"))
.unwrap_or(false);
let issues_enabled = !empty(
&project
.get("issues_enabled")
.cloned()
.unwrap_or(PhpMixed::Null),
);
if issues_missing && issues_enabled && has_web_url {
let web_url = project
.get("web_url")
.and_then(|v| v.as_string())
.unwrap_or("")
.to_string();
if let Some(support) = composer.get_mut("support").and_then(|v| match v {
PhpMixed::Array(m) => Some(m),
_ => None,
}) {
support.insert(
"issues".to_string(),
Box::new(PhpMixed::String(sprintf(
"%s/-/issues",
&[PhpMixed::String(web_url)],
))),
);
}
}
if !composer.contains_key("abandoned")
&& !empty(&project.get("archived").cloned().unwrap_or(PhpMixed::Null))
{
composer.insert("abandoned".to_string(), PhpMixed::Bool(true));
}
}
self.inner
.info_cache
.insert(identifier.to_string(), composer);
}
Ok(self
.inner
.info_cache
.get(identifier)
.cloned()
.unwrap_or(None))
}
pub fn get_file_content(&mut self, file: &str, identifier: &str) -> Result<Option<String>> {
if let Some(ref mut git_driver) = self.git_driver {
return git_driver.get_file_content(file, identifier);
}
// Convert the root identifier to a cacheable commit id
let mut identifier = identifier.to_string();
if !Preg::is_match(r"{[a-f0-9]{40}}i", &identifier).unwrap_or(false) {
let branches = self.get_branches()?;
if let Some(sha) = branches.get(&identifier) {
identifier = sha.clone();
}
}
let resource = format!(
"{}/repository/files/{}/raw?ref={}",
self.get_api_url(),
self.url_encode_all(file),
identifier,
);
let content = match self.get_contents(&resource, false) {
Ok(response) => response.get_body().map(|s| s.to_string()),
Err(e) => {
if e.code != 404 {
return Err(e.into());
}
return Ok(None);
}
};
Ok(content)
}
pub fn get_change_date(&mut self, identifier: &str) -> Result<Option<DateTime<Utc>>> {
if let Some(ref mut git_driver) = self.git_driver {
return git_driver.get_change_date(identifier);
}
if let Some(commit) = self.commits.get(identifier) {
let committed_date = commit
.get("committed_date")
.and_then(|v| v.as_string())
.unwrap_or("");
return Ok(Some(
DateTime::parse_from_rfc3339(committed_date)
.map(|dt| dt.with_timezone(&Utc))
.unwrap_or_else(|_| Utc::now()),
));
}
Ok(None)
}
pub fn get_repository_url(&self) -> String {
let project = self.project.clone().unwrap_or_default();
if !self.protocol.is_empty() {
return project
.get(&format!("{}_url_to_repo", self.protocol))
.and_then(|v| v.as_string())
.unwrap_or("")
.to_string();
}
if self.is_private {
project
.get("ssh_url_to_repo")
.and_then(|v| v.as_string())
.unwrap_or("")
.to_string()
} else {
project
.get("http_url_to_repo")
.and_then(|v| v.as_string())
.unwrap_or("")
.to_string()
}
}
pub fn get_url(&self) -> String {
if let Some(ref git_driver) = self.git_driver {
return git_driver.get_url();
}
self.project
.as_ref()
.and_then(|p| p.get("web_url"))
.and_then(|v| v.as_string())
.unwrap_or("")
.to_string()
}
pub fn get_dist(&self, identifier: &str) -> Option<IndexMap<String, PhpMixed>> {
let url = format!(
"{}/repository/archive.zip?sha={}",
self.get_api_url(),
identifier
);
let mut result = IndexMap::new();
result.insert("type".to_string(), PhpMixed::String("zip".to_string()));
result.insert("url".to_string(), PhpMixed::String(url));
result.insert(
"reference".to_string(),
PhpMixed::String(identifier.to_string()),
);
result.insert("shasum".to_string(), PhpMixed::String(String::new()));
Some(result)
}
pub fn get_source(&self, identifier: &str) -> IndexMap<String, PhpMixed> {
if let Some(ref git_driver) = self.git_driver {
return git_driver
.get_source(identifier)
.into_iter()
.map(|(k, v)| (k, PhpMixed::String(v)))
.collect();
}
let mut result = IndexMap::new();
result.insert("type".to_string(), PhpMixed::String("git".to_string()));
result.insert(
"url".to_string(),
PhpMixed::String(self.get_repository_url()),
);
result.insert(
"reference".to_string(),
PhpMixed::String(identifier.to_string()),
);
result
}
pub fn get_root_identifier(&mut self) -> Result<String> {
if let Some(ref mut git_driver) = self.git_driver {
return git_driver.get_root_identifier();
}
Ok(self
.project
.as_ref()
.and_then(|p| p.get("default_branch"))
.and_then(|v| v.as_string())
.unwrap_or("")
.to_string())
}
pub fn get_branches(&mut self) -> Result<IndexMap<String, String>> {
if let Some(ref mut git_driver) = self.git_driver {
return git_driver.get_branches();
}
if self.branches.is_none() {
self.branches = Some(self.get_references("branches")?);
}
Ok(self.branches.clone().unwrap_or_default())
}
pub fn get_tags(&mut self) -> Result<IndexMap<String, String>> {
if let Some(ref mut git_driver) = self.git_driver {
return git_driver.get_tags();
}
if self.tags.is_none() {
self.tags = Some(self.get_references("tags")?);
}
Ok(self.tags.clone().unwrap_or_default())
}
/// @return string Base URL for GitLab API v3
pub fn get_api_url(&self) -> String {
format!(
"{}://{}/api/v4/projects/{}%2F{}",
self.scheme,
self.inner.origin_url,
self.url_encode_all(&self.namespace),
self.url_encode_all(&self.repository),
)
}
/// Urlencode all non alphanumeric characters. rawurlencode() can not be used as it does not encode `.`
fn url_encode_all(&self, string: &str) -> String {
let mut encoded = String::new();
let bytes: Vec<char> = string.chars().collect();
for i in 0..bytes.len() {
let character = bytes[i].to_string();
let final_character = if !ctype_alnum(&character)
&& !in_array(
PhpMixed::String(character.clone()),
&PhpMixed::List(vec![
Box::new(PhpMixed::String("-".to_string())),
Box::new(PhpMixed::String("_".to_string())),
]),
true,
) {
format!("%{}", sprintf("%02X", &[PhpMixed::Int(ord(&character))]))
} else {
character
};
encoded.push_str(&final_character);
}
encoded
}
/// @return string[] where keys are named references like tags or branches and the value a sha
pub(crate) fn get_references(&mut self, r#type: &str) -> Result<IndexMap<String, String>> {
let per_page = 100;
let mut resource: Option<String> = Some(format!(
"{}/repository/{}?per_page={}",
self.get_api_url(),
r#type,
per_page
));
let mut references: IndexMap<String, String> = IndexMap::new();
loop {
let response = self
.get_contents(resource.as_deref().unwrap_or(""), false)
.map_err(|e| anyhow::anyhow!("{}", e.message))?;
let data = response.decode_json()?;
if let PhpMixed::List(ref list) = data {
for datum in list {
if let PhpMixed::Array(ref datum_map) = **datum {
let name = datum_map
.get("name")
.and_then(|v| v.as_string())
.unwrap_or("")
.to_string();
let commit_id = datum_map
.get("commit")
.and_then(|v| v.as_array())
.and_then(|m| m.get("id"))
.and_then(|v| v.as_string())
.unwrap_or("")
.to_string();
references.insert(name, commit_id.clone());
// Keep the last commit date of a reference to avoid
// unnecessary API call when retrieving the composer file.
let commit_data = datum_map
.get("commit")
.and_then(|v| v.as_array())
.cloned()
.unwrap_or_default()
.into_iter()
.map(|(k, v)| (k, *v))
.collect();
self.commits.insert(commit_id, commit_data);
}
}
}
let len = match data {
PhpMixed::List(ref l) => l.len() as i64,
PhpMixed::Array(ref a) => a.len() as i64,
_ => 0,
};
if len >= per_page {
resource = self.get_next_page(&response);
} else {
resource = None;
}
if resource.is_none() {
break;
}
}
Ok(references)
}
pub(crate) fn fetch_project(&mut self) -> Result<()> {
if self.project.is_some() {
return Ok(());
}
// we need to fetch the default branch from the api
let resource = self.get_api_url();
let project = self
.get_contents(&resource, true)
.map_err(|e| anyhow::anyhow!("{}", e.message))?
.decode_json()?;
self.project = match project {
PhpMixed::Array(m) => Some(m.into_iter().map(|(k, v)| (k, *v)).collect()),
_ => None,
};
let project = self.project.clone().unwrap_or_default();
if project.contains_key("visibility") {
self.is_private = project
.get("visibility")
.and_then(|v| v.as_string())
.map(|s| s != "public")
.unwrap_or(true);
} else {
// client is not authenticated, therefore repository has to be public
self.is_private = false;
}
Ok(())
}
/// @phpstan-impure
///
/// @return true
/// @throws \RuntimeException
pub(crate) fn attempt_clone_fallback(&mut self) -> Result<bool> {
let url = if !self.is_private {
self.generate_public_url()
} else {
self.generate_ssh_url()
};
// If this repository may be private and we
// cannot ask for authentication credentials (because we
// are not interactive) then we fallback to GitDriver.
match self.setup_git_driver(&url) {
Ok(()) => Ok(true),
Err(e) => {
self.git_driver = None;
self.inner.io.write_error3(
&format!(
"<error>Failed to clone the {} repository, try running in interactive mode so that you can enter your credentials</error>",
url
),
true,
io_interface::NORMAL,
);
Err(e)
}
}
}
/// Generate an SSH URL
pub(crate) fn generate_ssh_url(&self) -> String {
if self.has_nonstandard_origin {
return format!(
"ssh://git@{}/{}/{}.git",
self.inner.origin_url, self.namespace, self.repository
);
}
format!(
"git@{}:{}/{}.git",
self.inner.origin_url, self.namespace, self.repository
)
}
pub(crate) fn generate_public_url(&self) -> String {
format!(
"{}://{}/{}/{}.git",
self.scheme, self.inner.origin_url, self.namespace, self.repository
)
}
pub(crate) fn setup_git_driver(&mut self, url: &str) -> Result<()> {
let mut repo_config: IndexMap<String, PhpMixed> = IndexMap::new();
repo_config.insert("url".to_string(), PhpMixed::String(url.to_string()));
let mut git_driver = GitDriver::new(
repo_config,
self.inner.io.clone(),
self.inner.config.clone(),
self.inner.http_downloader.clone(),
self.inner.process.clone(),
);
git_driver.initialize()?;
self.git_driver = Some(git_driver);
Ok(())
}
pub(crate) fn get_contents(
&mut self,
url: &str,
fetching_repo_data: bool,
) -> Result<Response, TransportException> {
let response_result = self.inner.get_contents(url);
match response_result {
Ok(response) => {
if fetching_repo_data {
let json = response
.decode_json()
.map_err(|e| TransportException::new(e.to_string(), 0))?;
let json_map = match json {
PhpMixed::Array(ref m) => m.clone(),
_ => IndexMap::new(),
};
// Accessing the API with a token with Guest (10) or Planner (15) access will return
// more data than unauthenticated access but no default_branch data
// accessing files via the API will then also fail
if !json_map.contains_key("default_branch")
&& json_map.contains_key("permissions")
{
self.is_private = json_map
.get("visibility")
.and_then(|v| v.as_string())
.map(|s| s != "public")
.unwrap_or(true);
let mut more_than_guest_access = false;
// Check both access levels (e.g. project, group)
// - value will be null if no access is set
// - value will be array with key access_level if set
if let Some(permissions) =
json_map.get("permissions").and_then(|v| v.as_array())
{
for (_, permission) in permissions {
if let Some(perm_map) = permission.as_array() {
if let Some(level) =
perm_map.get("access_level").and_then(|v| v.as_int())
{
if level >= 20 {
more_than_guest_access = true;
}
}
}
}
}
if !more_than_guest_access {
self.inner.io.write_error3(
"<warning>GitLab token with Guest or Planner only access detected</warning>",
true,
io_interface::NORMAL,
);
self.attempt_clone_fallback()
.map_err(|e| TransportException::new(e.to_string(), 0))?;
let mut req = IndexMap::new();
req.insert("url".to_string(), PhpMixed::String("dummy".to_string()));
return Ok(Response::new(
req,
Some(200),
vec![],
Some("null".to_string()),
)
.unwrap()
.unwrap());
}
}
// force auth as the unauthenticated version of the API is broken
if !json_map.contains_key("default_branch") {
// GitLab allows you to disable the repository inside a project to use a project only for issues and wiki
if json_map
.get("repository_access_level")
.and_then(|v| v.as_string())
== Some("disabled")
{
return Err(TransportException::new(
"The GitLab repository is disabled in the project".to_string(),
400,
));
}
if !empty(
&*json_map
.get("id")
.cloned()
.unwrap_or(Box::new(PhpMixed::Null)),
) {
self.is_private = false;
}
return Err(TransportException::new(
"GitLab API seems to not be authenticated as it did not return a default_branch"
.to_string(),
401,
));
}
}
Ok(response)
}
Err(e) => {
let mut git_lab_util = GitLab::new(
self.inner.io.clone(),
self.inner.config.clone(),
Some(self.inner.process.clone()),
Some(self.inner.http_downloader.clone()),
)
.map_err(|err| TransportException::new(err.to_string(), 0))?;
match e.code {
401 | 404 => {
// try to authorize only if we are fetching the main /repos/foo/bar data, otherwise it must be a real 404
if !fetching_repo_data {
return Err(e);
}
if git_lab_util.authorize_oauth(&self.inner.origin_url) {
return self.inner.get_contents(url);
}
if git_lab_util.is_oauth_expired(&self.inner.origin_url)
&& git_lab_util
.authorize_oauth_refresh(&self.scheme, &self.inner.origin_url)
.map_err(|err| TransportException::new(err.to_string(), 0))?
{
return self.inner.get_contents(url);
}
if !self.inner.io.is_interactive() {
self.attempt_clone_fallback()
.map_err(|err| TransportException::new(err.to_string(), 0))?;
let mut req = IndexMap::new();
req.insert("url".to_string(), PhpMixed::String("dummy".to_string()));
return Ok(Response::new(
req,
Some(200),
vec![],
Some("null".to_string()),
)
.unwrap()
.unwrap());
}
self.inner.io.write_error3(
&format!(
"<warning>Failed to download {}/{}:{}</warning>",
self.namespace, self.repository, e.message
),
true,
io_interface::NORMAL,
);
git_lab_util.authorize_oauth_interactively(
&self.scheme,
&self.inner.origin_url,
Some(&format!(
"Your credentials are required to fetch private repository metadata (<info>{}</info>)",
self.inner.url
)),
);
self.inner.get_contents(url)
}
403 => {
if !self.inner.io.has_authentication(&self.inner.origin_url)
&& git_lab_util.authorize_oauth(&self.inner.origin_url)
{
return self.inner.get_contents(url);
}
if !self.inner.io.is_interactive() && fetching_repo_data {
self.attempt_clone_fallback()
.map_err(|err| TransportException::new(err.to_string(), 0))?;
let mut req = IndexMap::new();
req.insert("url".to_string(), PhpMixed::String("dummy".to_string()));
return Ok(Response::new(
req,
Some(200),
vec![],
Some("null".to_string()),
)
.unwrap()
.unwrap());
}
Err(e)
}
_ => Err(e),
}
}
}
}
/// Uses the config `gitlab-domains` to see if the driver supports the url for the
/// repository given.
pub fn supports(io: &dyn IOInterface, config: &Config, url: &str, _deep: bool) -> bool {
let mut match_: IndexMap<CaptureKey, String> = IndexMap::new();
if !Preg::is_match_strict_groups3(Self::URL_REGEX, url, Some(&mut match_)).unwrap_or(false)
{
return false;
}
let scheme = match_
.get(&CaptureKey::ByName("scheme".to_string()))
.cloned()
.unwrap_or_default();
let guessed_domain = match_
.get(&CaptureKey::ByName("domain".to_string()))
.cloned()
.filter(|s| !s.is_empty())
.unwrap_or_else(|| {
match_
.get(&CaptureKey::ByName("domain2".to_string()))
.cloned()
.unwrap_or_default()
});
let mut url_parts: Vec<String> = explode(
"/",
&match_
.get(&CaptureKey::ByName("parts".to_string()))
.cloned()
.unwrap_or_default(),
);
if Self::determine_origin(
&config.get("gitlab-domains"),
guessed_domain,
&mut url_parts,
match_.get(&CaptureKey::ByName("port".to_string())).cloned(),
)
.is_none()
{
return false;
}
if scheme == "https" && !extension_loaded("openssl") {
io.write_error3(
&format!(
"Skipping GitLab driver for {} because the OpenSSL PHP extension is missing.",
url
),
true,
io_interface::VERBOSE,
);
return false;
}
true
}
/// Gives back the loaded <gitlab-api>/projects/<owner>/<repo> result
///
/// @return mixed[]|null
pub fn get_repo_data(&mut self) -> Result<Option<IndexMap<String, PhpMixed>>> {
self.fetch_project()?;
Ok(self.project.clone())
}
pub(crate) fn get_next_page(&self, response: &Response) -> Option<String> {
let header = response.get_header("link").unwrap_or_default();
let links = explode(",", &header);
for link in &links {
let mut match_: IndexMap<CaptureKey, String> = IndexMap::new();
if Preg::is_match_strict_groups3(r#"{<(.+?)>; *rel="next"}"#, link, Some(&mut match_))
.unwrap_or(false)
{
return Some(
match_
.get(&CaptureKey::ByIndex(1))
.cloned()
.unwrap_or_default(),
);
}
}
None
}
/// @param array<string> $configuredDomains
/// @param array<string> $urlParts
///
/// @return string|false
fn determine_origin(
configured_domains: &PhpMixed,
guessed_domain: String,
url_parts: &mut Vec<String>,
port_number: Option<String>,
) -> Option<String> {
let mut guessed_domain = strtolower(&guessed_domain);
if in_array(
PhpMixed::String(guessed_domain.clone()),
configured_domains,
false,
) || (port_number.is_some()
&& in_array(
PhpMixed::String(format!(
"{}:{}",
guessed_domain,
port_number.as_deref().unwrap_or("")
)),
configured_domains,
false,
))
{
if let Some(ref port) = port_number {
return Some(format!("{}:{}", guessed_domain, port));
}
return Some(guessed_domain);
}
if let Some(ref port) = port_number {
guessed_domain.push_str(&format!(":{}", port));
}
while let Some(part) = array_shift(url_parts) {
guessed_domain.push_str(&format!("/{}", part));
if in_array(
PhpMixed::String(guessed_domain.clone()),
configured_domains,
false,
) || (port_number.is_some()
&& in_array(
PhpMixed::String(
Preg::replace(r"{:\d+}", "", &guessed_domain).unwrap_or_default(),
),
configured_domains,
false,
))
{
return Some(guessed_domain);
}
}
None
}
}
|