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