-
Notifications
You must be signed in to change notification settings - Fork 640
/
Copy pathteam.rs
265 lines (236 loc) · 8.11 KB
/
team.rs
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
use diesel::prelude::*;
use crate::app::App;
use crate::github::{github_api, team_url};
use crate::util::errors::{cargo_err, AppResult, NotFound};
use oauth2::AccessToken;
use crate::models::{Crate, CrateOwner, Owner, OwnerKind, User};
use crate::schema::{crate_owners, teams};
use crate::views::EncodableTeam;
/// For now, just a Github Team. Can be upgraded to other teams
/// later if desirable.
#[derive(Queryable, Identifiable, Serialize, Deserialize, Debug)]
pub struct Team {
/// Unique table id
pub id: i32,
/// "github:org:team"
/// An opaque unique ID, that was at one point parsed out to query Github.
/// We only query membership with github using the github_id, though.
/// This is the only name we should ever talk to Cargo about.
pub login: String,
/// The GitHub API works on team ID numbers. This can change, if a team
/// is deleted and then recreated with the same name!!!
pub github_id: i32,
/// Sugary goodness
pub name: Option<String>,
pub avatar: Option<String>,
/// The GitHub Organization ID this team sits under
pub org_id: Option<i32>,
}
#[derive(Insertable, AsChangeset, Debug)]
#[table_name = "teams"]
pub struct NewTeam<'a> {
pub login: &'a str,
pub github_id: i32,
pub name: Option<String>,
pub avatar: Option<String>,
pub org_id: i32,
}
impl<'a> NewTeam<'a> {
pub fn new(
login: &'a str,
org_id: i32,
github_id: i32,
name: Option<String>,
avatar: Option<String>,
) -> Self {
NewTeam {
login,
github_id,
name,
avatar,
org_id,
}
}
pub fn create_or_update(&self, conn: &PgConnection) -> QueryResult<Team> {
use crate::schema::teams::dsl::*;
use diesel::insert_into;
insert_into(teams)
.values(self)
.on_conflict(login)
.do_update()
.set(self)
.get_result(conn)
}
}
impl Team {
/// Tries to create the Team in the DB (assumes a `:` has already been found).
///
/// # Panics
///
/// This function will panic if login contains less than 2 `:` characters.
pub fn create_or_update(
app: &App,
conn: &PgConnection,
login: &str,
req_user: &User,
) -> AppResult<Self> {
// must look like system:xxxxxxx
let mut chunks = login.split(':');
// unwrap is okay, split on an empty string still has 1 chunk
match chunks.next().unwrap() {
// github:rust-lang:owners
"github" => {
// unwrap is documented above as part of the calling contract
let org = chunks.next().unwrap();
let team = chunks.next().ok_or_else(|| {
cargo_err(
"missing github team argument; \
format is github:org:team",
)
})?;
Team::create_or_update_github_team(
app,
conn,
&login.to_lowercase(),
org,
team,
req_user,
)
}
_ => Err(cargo_err(
"unknown organization handler, \
only 'github:org:team' is supported",
)),
}
}
/// Tries to create or update a Github Team. Assumes `org` and `team` are
/// correctly parsed out of the full `name`. `name` is passed as a
/// convenience to avoid rebuilding it.
fn create_or_update_github_team(
app: &App,
conn: &PgConnection,
login: &str,
org_name: &str,
team_name: &str,
req_user: &User,
) -> AppResult<Self> {
// GET orgs/:org/teams
// check that `team` is the `slug` in results, and grab its data
// "sanitization"
fn is_allowed_char(c: char) -> bool {
matches!(c, 'a'..='z' | 'A'..='Z' | '0'..='9' | '-' | '_')
}
if let Some(c) = org_name.chars().find(|c| !is_allowed_char(*c)) {
return Err(cargo_err(&format_args!(
"organization cannot contain special \
characters like {}",
c
)));
}
#[derive(Deserialize)]
struct GithubOrganization {
id: i32, // unique GH id (needed for membership queries)
}
#[derive(Deserialize)]
struct GithubTeam {
id: i32, // unique GH id (needed for membership queries)
name: Option<String>, // Pretty name
organization: GithubOrganization,
}
let url = format!("/orgs/{}/teams/{}", org_name, team_name);
let token = AccessToken::new(req_user.gh_access_token.clone());
let team = github_api::<GithubTeam>(app, &url, &token).map_err(|_| {
cargo_err(&format_args!(
"could not find the github team {}/{}",
org_name, team_name
))
})?;
let org_id = team.organization.id;
if !team_with_gh_id_contains_user(app, org_id, team.id, req_user)? {
return Err(cargo_err("only members of a team can add it as an owner"));
}
#[derive(Deserialize)]
struct Org {
avatar_url: Option<String>,
}
let url = format!("/orgs/{}", org_name);
let org = github_api::<Org>(app, &url, &token)?;
NewTeam::new(
&login.to_lowercase(),
org_id,
team.id,
team.name,
org.avatar_url,
)
.create_or_update(conn)
.map_err(Into::into)
}
/// Phones home to Github to ask if this User is a member of the given team.
/// Note that we're assuming that the given user is the one interested in
/// the answer. If this is not the case, then we could accidentally leak
/// private membership information here.
pub fn contains_user(&self, app: &App, user: &User) -> AppResult<bool> {
match self.org_id {
Some(org_id) => team_with_gh_id_contains_user(app, org_id, self.github_id, user),
// This means we don't have an org_id on file for the `self` team. It much
// probably was deleted from github by the time we backfilled the database.
// Short-circuiting to false since a non-existent team cannot contain any
// user
None => Ok(false),
}
}
pub fn owning(krate: &Crate, conn: &PgConnection) -> QueryResult<Vec<Owner>> {
let base_query = CrateOwner::belonging_to(krate).filter(crate_owners::deleted.eq(false));
let teams = base_query
.inner_join(teams::table)
.select(teams::all_columns)
.filter(crate_owners::owner_kind.eq(OwnerKind::Team as i32))
.load(conn)?
.into_iter()
.map(Owner::Team);
Ok(teams.collect())
}
pub fn encodable(self) -> EncodableTeam {
let Team {
id,
name,
login,
avatar,
..
} = self;
let url = team_url(&login);
EncodableTeam {
id,
login,
name,
avatar,
url: Some(url),
}
}
}
fn team_with_gh_id_contains_user(
app: &App,
github_org_id: i32,
github_team_id: i32,
user: &User,
) -> AppResult<bool> {
// GET /organizations/:org_id/team/:team_id/memberships/:username
// check that "state": "active"
#[derive(Deserialize)]
struct Membership {
state: String,
}
let url = format!(
"/organizations/{}/team/{}/memberships/{}",
&github_org_id, &github_team_id, &user.gh_login
);
let token = AccessToken::new(user.gh_access_token.clone());
let membership = match github_api::<Membership>(app, &url, &token) {
// Officially how `false` is returned
Err(ref e) if e.is::<NotFound>() => return Ok(false),
x => x?,
};
// There is also `state: pending` for which we could possibly give
// some feedback, but it's not obvious how that should work.
Ok(membership.state == "active")
}