Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize GitHub app installation #75

Merged
merged 9 commits into from
Mar 26, 2024
Merged
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 17 additions & 4 deletions src/github/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ impl HttpClient {

fn rest_paginated<F, T>(&self, method: &Method, url: String, mut f: F) -> anyhow::Result<()>
where
F: FnMut(Vec<T>) -> anyhow::Result<()>,
F: FnMut(T) -> anyhow::Result<()>,
T: DeserializeOwned,
{
let mut next = Some(url);
Expand All @@ -138,7 +138,7 @@ impl HttpClient {
}
}

f(resp.json().with_context(|| {
f(resp.json_annotated().with_context(|| {
format!("Failed to deserialize response body for {method} request to '{next_url}'")
})?)?;
}
Expand Down Expand Up @@ -248,10 +248,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)]
pub(crate) struct Repo {
#[serde(rename = "node_id")]
pub(crate) id: String,
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
51 changes: 50 additions & 1 deletion src/github/api/read.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::github::api::{
team_node_id, user_node_id, BranchProtection, GraphNode, GraphNodes, GraphPageInfo, HttpClient,
Login, Repo, RepoTeam, RepoUser, Team, TeamMember, TeamRole,
Login, OrgAppInstallation, Repo, RepoAppInstallation, RepoTeam, RepoUser, Team, TeamMember,
TeamRole,
};
use reqwest::Method;
use std::collections::{HashMap, HashSet};
Expand All @@ -12,6 +13,15 @@ pub(crate) trait GithubRead {
/// Get the owners of an org
fn org_owners(&self, org: &str) -> anyhow::Result<HashSet<u64>>;

/// 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,
) -> 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 @@ -110,6 +120,45 @@ impl GithubRead for GitHubApiRead {
Ok(owners)
}

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,
format!("orgs/{org}/installations"),
|response: InstallationPage| {
installations.extend(response.installations);
Ok(())
},
)?;
Ok(installations)
}

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

let mut installations = Vec::new();
self.client.rest_paginated(
&Method::GET,
format!("user/installations/{installation_id}/repositories"),
|response: InstallationPage| {
installations.extend(response.repositories);
Ok(())
},
)?;
Ok(installations)
}

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

Expand Down
39 changes: 38 additions & 1 deletion src/github/api/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,8 @@ impl GitHubWrite {
debug!("Creating the repo {org}/{name} with {req:?}");
if self.dry_run {
Ok(Repo {
id: String::from("ID"),
node_id: String::from("ID"),
repo_id: 0,
name: name.to_string(),
org: org.to_string(),
description: settings.description.clone(),
Expand Down Expand Up @@ -273,6 +274,42 @@ impl GitHubWrite {
Ok(())
}

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

pub(crate) fn remove_repo_from_app_installation(
&self,
installation_id: u64,
repository_id: u64,
) -> anyhow::Result<()> {
debug!("Removing repository {repository_id} from installation {installation_id}");
if !self.dry_run {
self.client
.req(
Method::DELETE,
&format!("user/installations/{installation_id}/repositories/{repository_id}"),
)?
.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
Loading