Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion repos/rust-lang/crates-io-auth-action.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
org = "rust-lang"
name = "crates-io-auth-action"
description = "Get a crates.io temporary access token"
bots = []
bots = ["renovate"]

[access.teams]
crates-io-infra-admins = "write"
Expand Down
14 changes: 14 additions & 0 deletions sync-team/src/github/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,23 @@ impl fmt::Display for RepoPermission {
}
}

#[derive(serde::Deserialize, Debug)]
pub(crate) struct OrgAppInstallation {
#[serde(rename = "id")]
pub(crate) installation_id: u64,
pub(crate) app_id: u64,
}

#[derive(serde::Deserialize, Debug)]
pub(crate) struct RepoAppInstallation {
pub(crate) name: String,
}

#[derive(serde::Deserialize, Debug, Clone)]
pub(crate) struct Repo {
pub(crate) node_id: String,
#[serde(rename = "id")]
pub(crate) repo_id: u64,
pub(crate) name: String,
#[serde(alias = "owner", deserialize_with = "repo_owner")]
pub(crate) org: String,
Expand Down
62 changes: 60 additions & 2 deletions sync-team/src/github/api/read.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::github::api::Ruleset;
use crate::github::api::{
BranchProtection, GraphNode, GraphNodes, GraphPageInfo, HttpClient, Login, Repo, RepoTeam,
RepoUser, Team, TeamMember, TeamRole, team_node_id, url::GitHubUrl, user_node_id,
BranchProtection, GraphNode, GraphNodes, GraphPageInfo, HttpClient, Login, OrgAppInstallation,
Repo, RepoAppInstallation, RepoTeam, RepoUser, Team, TeamMember, TeamRole, team_node_id,
url::GitHubUrl, user_node_id,
};
use anyhow::Context as _;
use reqwest::Method;
Expand All @@ -20,6 +21,16 @@ pub(crate) trait GithubRead {
/// Get the members of an org
fn org_members(&self, org: &str) -> anyhow::Result<HashMap<u64, String>>;

/// Get the app installations of an org
fn org_app_installations(&self, org: &str) -> anyhow::Result<Vec<OrgAppInstallation>>;

/// Get the repositories enabled for an app installation.
fn app_installation_repos(
&self,
installation_id: u64,
org: &str,
) -> anyhow::Result<Vec<RepoAppInstallation>>;

/// Get all teams associated with a org
///
/// Returns a list of tuples of team name and slug
Expand Down Expand Up @@ -160,6 +171,50 @@ impl GithubRead for GitHubApiRead {
Ok(members)
}

fn org_app_installations(&self, org: &str) -> anyhow::Result<Vec<OrgAppInstallation>> {
#[derive(serde::Deserialize, Debug)]
struct InstallationPage {
installations: Vec<OrgAppInstallation>,
}

let mut installations = Vec::new();
self.client.rest_paginated(
&Method::GET,
&GitHubUrl::orgs(org, "installations")?,
|response: InstallationPage| {
installations.extend(response.installations);
Ok(())
},
)?;
Ok(installations)
}

fn app_installation_repos(
&self,
installation_id: u64,
org: &str,
) -> anyhow::Result<Vec<RepoAppInstallation>> {
#[derive(serde::Deserialize, Debug)]
struct InstallationPage {
repositories: Vec<RepoAppInstallation>,
}

let mut installations = Vec::new();

let url = format!("user/installations/{installation_id}/repositories");
self.client
.rest_paginated(
&Method::GET,
&GitHubUrl::new(&url, org),
|response: InstallationPage| {
installations.extend(response.repositories);
Ok(())
},
)
.with_context(|| format!("failed to send rest paginated request to {url}"))?;
Ok(installations)
}

fn org_teams(&self, org: &str) -> anyhow::Result<Vec<(String, String)>> {
let mut teams = Vec::new();

Expand Down Expand Up @@ -294,6 +349,7 @@ impl GithubRead for GitHubApiRead {
query($owner: String!, $name: String!) {
repository(owner: $owner, name: $name) {
id
databaseId
autoMergeAllowed
description
homepageUrl
Expand All @@ -313,6 +369,7 @@ impl GithubRead for GitHubApiRead {
// Equivalent of `node_id` of the Rest API
id: String,
// Equivalent of `id` of the Rest API
database_id: u64,
auto_merge_allowed: Option<bool>,
description: Option<String>,
homepage_url: Option<String>,
Expand All @@ -332,6 +389,7 @@ impl GithubRead for GitHubApiRead {
.with_context(|| format!("failed to retrieve repo `{org}/{repo}`"))?;

let repo = result.and_then(|r| r.repository).map(|repo_response| Repo {
repo_id: repo_response.database_id,
node_id: repo_response.id,
name: repo.to_string(),
description: repo_response.description.unwrap_or_default(),
Expand Down
49 changes: 49 additions & 0 deletions sync-team/src/github/api/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ impl GitHubWrite {
if self.dry_run {
Ok(Repo {
node_id: String::from("ID"),
repo_id: 0,
name: name.to_string(),
org: org.to_string(),
description: settings.description.clone(),
Expand Down Expand Up @@ -287,6 +288,54 @@ impl GitHubWrite {
Ok(())
}

pub(crate) fn add_repo_to_app_installation(
&self,
installation_id: u64,
repository_id: u64,
org: &str,
) -> anyhow::Result<()> {
debug!("Adding repository {repository_id} to installation {installation_id}");
if !self.dry_run {
self.client
.req(
Method::PUT,
&GitHubUrl::new(
&format!(
"user/installations/{installation_id}/repositories/{repository_id}"
),
org,
),
)?
.send()?
.custom_error_for_status()?;
}
Ok(())
}

pub(crate) fn remove_repo_from_app_installation(
&self,
installation_id: u64,
repository_id: u64,
org: &str,
) -> anyhow::Result<()> {
debug!("Removing repository {repository_id} from installation {installation_id}");
if !self.dry_run {
self.client
.req(
Method::DELETE,
&GitHubUrl::new(
&format!(
"user/installations/{installation_id}/repositories/{repository_id}"
),
org,
),
)?
.send()?
.custom_error_for_status()?;
}
Ok(())
}

/// Update a team's permissions to a repo
pub(crate) fn update_team_repo_permissions(
&self,
Expand Down
Loading