@@ -7,7 +7,7 @@ use chrono::{DateTime, FixedOffset};
77use cmd_lib:: { run_cmd, run_fun} ;
88use jane_eyre:: eyre:: { self , Context } ;
99use serde:: { Deserialize , Serialize } ;
10- use settings:: TOML ;
10+ use settings:: { DOTENV , TOML } ;
1111use tracing:: trace;
1212
1313#[ derive( Clone , Debug , Deserialize , Serialize ) ]
@@ -107,10 +107,19 @@ impl<Response: Clone + Debug> Cache<Response> {
107107}
108108
109109fn list_registered_runners ( ) -> eyre:: Result < Vec < ApiRunner > > {
110- let github_api_scope = & TOML . github_api_scope ;
111- let result = run_fun ! ( gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28"
112- "$github_api_scope/actions/runners" --paginate -q ".runners[]"
113- | jq -s . ) ?;
110+ let github_or_forgejo_token = & DOTENV . github_or_forgejo_token ;
111+ let github_api_scope_url = & TOML . github_api_scope_url ;
112+ let result = if TOML . github_api_is_forgejo {
113+ // FIXME: this leaks the token in logs when the command fails
114+ run_fun ! ( curl -fsSH "Authorization: token $github_or_forgejo_token"
115+ "$github_api_scope_url/actions/runners" // TODO: pagination?
116+ | jq -er ".runners" ) ?
117+ } else {
118+ run_fun ! ( GITHUB_TOKEN =$github_or_forgejo_token gh api
119+ -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28"
120+ "$github_api_scope_url/actions/runners" --paginate -q ".runners[]"
121+ | jq -s . ) ?
122+ } ;
114123
115124 Ok ( serde_json:: from_str ( & result) . wrap_err ( "Failed to parse JSON" ) ?)
116125}
@@ -127,20 +136,37 @@ pub fn list_registered_runners_for_host() -> eyre::Result<Vec<ApiRunner>> {
127136}
128137
129138pub fn register_runner ( runner_name : & str , label : & str , work_folder : & str ) -> eyre:: Result < String > {
139+ let github_or_forgejo_token = & DOTENV . github_or_forgejo_token ;
140+ let github_api_scope_url = & TOML . github_api_scope_url ;
130141 let github_api_suffix = & TOML . github_api_suffix ;
131- let github_api_scope = & TOML . github_api_scope ;
132- let result = run_fun ! ( gh api --method POST -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28"
133- "$github_api_scope/actions/runners/generate-jitconfig"
134- -f "name=$runner_name@$github_api_suffix" -F "runner_group_id=1" -f "work_folder=$work_folder"
135- -f "labels[]=self-hosted" -f "labels[]=X64" -f "labels[]=$label" ) ?;
142+ let result = if TOML . github_api_is_forgejo {
143+ // FIXME: this leaks the token in logs when the command fails
144+ // TODO: this doesn’t actually register a runner, it just gets a registration token
145+ run_fun ! ( curl -fsSH "Authorization: token $github_or_forgejo_token"
146+ -X POST "$github_api_scope_url/actions/runners/registration-token/TODO" ) ?
147+ } else {
148+ run_fun ! ( GITHUB_TOKEN =$github_or_forgejo_token gh api
149+ -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28"
150+ --method POST "$github_api_scope_url/actions/runners/generate-jitconfig"
151+ -f "name=$runner_name@$github_api_suffix" -F "runner_group_id=1" -f "work_folder=$work_folder"
152+ -f "labels[]=self-hosted" -f "labels[]=X64" -f "labels[]=$label" ) ?
153+ } ;
136154
137155 Ok ( result)
138156}
139157
140158pub fn unregister_runner ( id : usize ) -> eyre:: Result < ( ) > {
141- let github_api_scope = & TOML . github_api_scope ;
142- run_cmd ! ( gh api --method DELETE -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28"
143- "$github_api_scope/actions/runners/$id" ) ?;
159+ let github_or_forgejo_token = & DOTENV . github_or_forgejo_token ;
160+ let github_api_scope_url = & TOML . github_api_scope_url ;
161+ if TOML . github_api_is_forgejo {
162+ // FIXME: this leaks the token in logs when the command fails
163+ run_cmd ! ( curl -fsSH "Authorization: token $github_or_forgejo_token"
164+ -X DELETE "$github_api_scope_url/actions/runners/$id" ) ?;
165+ } else {
166+ run_cmd ! ( GITHUB_TOKEN =$github_or_forgejo_token gh api
167+ -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28"
168+ --method DELETE "$github_api_scope_url/actions/runners/$id" ) ?;
169+ }
144170
145171 Ok ( ( ) )
146172}
@@ -151,13 +177,19 @@ pub fn reserve_runner(
151177 reserved_since : SystemTime ,
152178 reserved_by : & str ,
153179) -> eyre:: Result < ( ) > {
154- let github_api_scope = & TOML . github_api_scope ;
180+ let github_or_forgejo_token = & DOTENV . github_or_forgejo_token ;
181+ let github_api_scope_url = & TOML . github_api_scope_url ;
155182 let reserved_since = reserved_since. duration_since ( UNIX_EPOCH ) ?. as_secs ( ) ;
156- run_cmd ! ( gh api --method POST -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28"
157- "$github_api_scope/actions/runners/$id/labels"
158- -f "labels[]=reserved-for:$unique_id"
159- -f "labels[]=reserved-since:$reserved_since"
160- -f "labels[]=reserved-by:$reserved_by" ) ?;
183+ if TOML . github_api_is_forgejo {
184+ todo ! ( )
185+ } else {
186+ run_cmd ! ( GITHUB_TOKEN =$github_or_forgejo_token gh api
187+ -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28"
188+ --method POST "$github_api_scope_url/actions/runners/$id/labels"
189+ -f "labels[]=reserved-for:$unique_id"
190+ -f "labels[]=reserved-since:$reserved_since"
191+ -f "labels[]=reserved-by:$reserved_by" ) ?;
192+ }
161193
162194 Ok ( ( ) )
163195}
0 commit comments