@@ -3,10 +3,9 @@ use diesel::prelude::*;
33use diesel_async:: { AsyncPgConnection , RunQueryDsl } ;
44use http:: StatusCode ;
55
6- use crate :: app:: App ;
76use crate :: util:: errors:: { bad_request, custom, AppResult } ;
87
9- use crates_io_github:: GitHubError ;
8+ use crates_io_github:: { GitHubClient , GitHubError } ;
109use oauth2:: AccessToken ;
1110
1211use crate :: models:: { Crate , CrateOwner , Owner , OwnerKind , User } ;
@@ -64,7 +63,7 @@ impl Team {
6463 ///
6564 /// This function will panic if login contains less than 2 `:` characters.
6665 pub async fn create_or_update (
67- app : & App ,
66+ gh_client : & dyn GitHubClient ,
6867 conn : & mut AsyncPgConnection ,
6968 login : & str ,
7069 req_user : & User ,
@@ -84,7 +83,7 @@ impl Team {
8483 )
8584 } ) ?;
8685 Team :: create_or_update_github_team (
87- app ,
86+ gh_client ,
8887 conn,
8988 & login. to_lowercase ( ) ,
9089 org,
@@ -104,7 +103,7 @@ impl Team {
104103 /// correctly parsed out of the full `name`. `name` is passed as a
105104 /// convenience to avoid rebuilding it.
106105 async fn create_or_update_github_team (
107- app : & App ,
106+ gh_client : & dyn GitHubClient ,
108107 conn : & mut AsyncPgConnection ,
109108 login : & str ,
110109 org_name : & str ,
@@ -127,7 +126,7 @@ impl Team {
127126 }
128127
129128 let token = AccessToken :: new ( req_user. gh_access_token . clone ( ) ) ;
130- let team = app . github . team_by_name ( org_name, team_name, & token) . await
129+ let team = gh_client . team_by_name ( org_name, team_name, & token) . await
131130 . map_err ( |_| {
132131 bad_request ( format_args ! (
133132 "could not find the github team {org_name}/{team_name}. \
@@ -138,14 +137,14 @@ impl Team {
138137
139138 let org_id = team. organization . id ;
140139
141- if !can_add_team ( app , org_id, team. id , req_user) . await ? {
140+ if !can_add_team ( gh_client , org_id, team. id , req_user) . await ? {
142141 return Err ( custom (
143142 StatusCode :: FORBIDDEN ,
144143 "only members of a team or organization owners can add it as an owner" ,
145144 ) ) ;
146145 }
147146
148- let org = app . github . org_by_name ( org_name, & token) . await ?;
147+ let org = gh_client . org_by_name ( org_name, & token) . await ?;
149148
150149 NewTeam :: builder ( )
151150 . login ( & login. to_lowercase ( ) )
@@ -163,9 +162,15 @@ impl Team {
163162 /// Note that we're assuming that the given user is the one interested in
164163 /// the answer. If this is not the case, then we could accidentally leak
165164 /// private membership information here.
166- pub async fn contains_user ( & self , app : & App , user : & User ) -> AppResult < bool > {
165+ pub async fn contains_user (
166+ & self ,
167+ gh_client : & dyn GitHubClient ,
168+ user : & User ,
169+ ) -> AppResult < bool > {
167170 match self . org_id {
168- Some ( org_id) => team_with_gh_id_contains_user ( app, org_id, self . github_id , user) . await ,
171+ Some ( org_id) => {
172+ team_with_gh_id_contains_user ( gh_client, org_id, self . github_id , user) . await
173+ }
169174 // This means we don't have an org_id on file for the `self` team. It much
170175 // probably was deleted from github by the time we backfilled the database.
171176 // Short-circuiting to false since a non-existent team cannot contain any
@@ -189,17 +194,25 @@ impl Team {
189194 }
190195}
191196
192- async fn can_add_team ( app : & App , org_id : i32 , team_id : i32 , user : & User ) -> AppResult < bool > {
197+ async fn can_add_team (
198+ gh_client : & dyn GitHubClient ,
199+ org_id : i32 ,
200+ team_id : i32 ,
201+ user : & User ,
202+ ) -> AppResult < bool > {
193203 Ok (
194- team_with_gh_id_contains_user ( app , org_id, team_id, user) . await ?
195- || is_gh_org_owner ( app , org_id, user) . await ?,
204+ team_with_gh_id_contains_user ( gh_client , org_id, team_id, user) . await ?
205+ || is_gh_org_owner ( gh_client , org_id, user) . await ?,
196206 )
197207}
198208
199- async fn is_gh_org_owner ( app : & App , org_id : i32 , user : & User ) -> AppResult < bool > {
209+ async fn is_gh_org_owner (
210+ gh_client : & dyn GitHubClient ,
211+ org_id : i32 ,
212+ user : & User ,
213+ ) -> AppResult < bool > {
200214 let token = AccessToken :: new ( user. gh_access_token . clone ( ) ) ;
201- match app
202- . github
215+ match gh_client
203216 . org_membership ( org_id, & user. gh_login , & token)
204217 . await
205218 {
@@ -210,7 +223,7 @@ async fn is_gh_org_owner(app: &App, org_id: i32, user: &User) -> AppResult<bool>
210223}
211224
212225async fn team_with_gh_id_contains_user (
213- app : & App ,
226+ gh_client : & dyn GitHubClient ,
214227 github_org_id : i32 ,
215228 github_team_id : i32 ,
216229 user : & User ,
@@ -219,8 +232,7 @@ async fn team_with_gh_id_contains_user(
219232 // check that "state": "active"
220233
221234 let token = AccessToken :: new ( user. gh_access_token . clone ( ) ) ;
222- let membership = match app
223- . github
235+ let membership = match gh_client
224236 . team_membership ( github_org_id, github_team_id, & user. gh_login , & token)
225237 . await
226238 {
0 commit comments