1
1
use octocrab:: models:: repos:: ContentItems ;
2
2
3
3
use crate :: config:: get_env_var_or_default;
4
- use crate :: external_services:: github:: github_async_new;
4
+ use crate :: external_services:: github:: { github_async_new, GithubWrapper } ;
5
5
use crate :: { base64:: decode_allocator_model, error:: LDNError } ;
6
6
7
7
use self :: file:: AllocatorModel ;
@@ -12,17 +12,17 @@ pub async fn process_allocator_file(file_name: &str) -> Result<AllocatorModel, L
12
12
13
13
let owner = get_env_var_or_default ( "ALLOCATOR_GOVERNANCE_OWNER" ) ;
14
14
let repo = get_env_var_or_default ( "ALLOCATOR_GOVERNANCE_REPO" ) ;
15
+ let installation_id = get_env_var_or_default ( "GITHUB_INSTALLATION_ID" ) ;
15
16
let branch = "main" ;
16
17
let path = file_name. to_string ( ) ;
17
18
18
- let gh = github_async_new ( owner. to_string ( ) , repo. to_string ( ) ) . await ;
19
+ let gh = GithubWrapper :: new ( owner. clone ( ) , repo. clone ( ) , installation_id ) ;
19
20
let content_items = gh. get_file ( & path, branch) . await . map_err ( |e| LDNError :: Load ( e. to_string ( ) ) ) ?;
20
21
let model = content_items_to_allocator_model ( content_items) . map_err ( |e| LDNError :: Load ( e. to_string ( ) ) ) ?;
21
22
22
23
Ok ( model)
23
24
}
24
25
25
-
26
26
fn content_items_to_allocator_model ( file : ContentItems ) -> Result < AllocatorModel , LDNError > {
27
27
let encoded_content = match file. items . get ( 0 ) . and_then ( |f| f. content . clone ( ) ) {
28
28
Some ( content) => {
@@ -48,4 +48,89 @@ fn content_items_to_allocator_model(file: ContentItems) -> Result<AllocatorModel
48
48
Err ( LDNError :: Load ( "Failed to parse allocator model" . to_string ( ) ) )
49
49
}
50
50
}
51
+ }
52
+
53
+ pub async fn is_allocator_repo_created ( owner : & str , repo : & str ) -> Result < bool , LDNError > {
54
+ let repo_flag_file = "invalisd.md" ;
55
+ let applications_directory = "applications" ;
56
+ let gh = github_async_new ( owner. to_string ( ) , repo. to_string ( ) ) . await ;
57
+ let all_files_result = gh. get_files ( applications_directory) . await . map_err ( |e| {
58
+ LDNError :: Load ( format ! ( "Failed to retrieve all files from GitHub. Reason: {}" , e) )
59
+ } ) ;
60
+
61
+ match all_files_result {
62
+ Ok ( content_items) => {
63
+ let mut is_repo_created = false ;
64
+ for file in content_items. items . iter ( ) {
65
+ if file. name == repo_flag_file {
66
+ is_repo_created = true ;
67
+ break ;
68
+ }
69
+ }
70
+ Ok ( is_repo_created)
71
+ } ,
72
+ Err ( e) => {
73
+ if e. to_string ( ) . contains ( "GitHub: This repository is empty" ) || e. to_string ( ) . contains ( "GitHub: Not Found" ) {
74
+ Ok ( false )
75
+ } else {
76
+ Err ( e)
77
+ }
78
+ } ,
79
+ }
80
+ }
81
+
82
+ pub async fn create_allocator_repo ( owner : & str , repo : & str ) -> Result < ( ) , LDNError > {
83
+ let gh = github_async_new ( owner. to_string ( ) , repo. to_string ( ) ) . await ;
84
+ let mut dirs = Vec :: new ( ) ;
85
+ dirs. push ( "" . to_string ( ) ) ;
86
+
87
+ while dirs. len ( ) > 0 {
88
+ let dir = dirs. pop ( ) . unwrap ( ) ;
89
+ let files_list = gh. get_files_from_public_repo ( "clriesco" , "filplus-allocator-template" , Some ( & dir) ) . await . map_err ( |e| {
90
+ LDNError :: Load ( format ! ( "Failed to retrieve all files from GitHub. Reason: {}" , e) )
91
+ } ) ?;
92
+
93
+ for file in files_list. items . iter ( ) {
94
+ let file_path = file. path . clone ( ) ;
95
+ if file. r#type == "dir" {
96
+ dirs. push ( file_path) ;
97
+ continue ;
98
+ }
99
+ let file = reqwest:: Client :: new ( )
100
+ . get ( & file. download_url . clone ( ) . unwrap ( ) )
101
+ . send ( )
102
+ . await
103
+ . map_err ( |e| LDNError :: Load ( format ! ( "here {}" , e) ) ) ?;
104
+ let file = file
105
+ . text ( )
106
+ . await
107
+ . map_err ( |e| LDNError :: Load ( format ! ( "here1 {}" , e) ) ) ?;
108
+
109
+ //Get file from target repo. If file does not exist or fails to retrieve, create it
110
+ let target_file = gh. get_file ( & file_path, "main" ) . await . map_err ( |e| {
111
+ LDNError :: Load ( format ! ( "Failed to retrieve file from GitHub. Reason: {} in file {}" , e, file_path) )
112
+ } ) ;
113
+
114
+ match target_file {
115
+ Ok ( target_file) => {
116
+ if target_file. items . is_empty ( ) {
117
+ log:: info!( "Creating file in target repo: {}" , file_path) ;
118
+ gh. add_file ( & file_path, & file, "first commit" , "main" ) . await . map_err ( |e| {
119
+ LDNError :: Load ( format ! ( "Failed to create file in GitHub. Reason: {} in file {}" , e, file_path) )
120
+ } ) ?;
121
+ } else {
122
+ log:: info!( "File already exists in target repo: {}" , file_path) ;
123
+ }
124
+ } ,
125
+ Err ( _) => {
126
+ log:: info!( "Creating file in target repo: {}" , file_path) ;
127
+ gh. add_file ( & file_path, & file, "first commit" , "main" ) . await . map_err ( |e| {
128
+ LDNError :: Load ( format ! ( "Failed to create file in GitHub. Reason: {} in file {}" , e, file_path) )
129
+ } ) ?;
130
+ } ,
131
+ }
132
+ }
133
+ }
134
+
135
+ Ok ( ( ) )
51
136
}
0 commit comments