Skip to content

Commit

Permalink
Add get_org_installation to AppsRequestHandler
Browse files Browse the repository at this point in the history
Similarly to get_repository_installation, this fetches the installation
for the given organization.

Sample response payload copied from the API docs: https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#get-an-organization-installation-for-the-authenticated-app
  • Loading branch information
marcusirgens authored and XAMPPRocky committed Aug 3, 2023
1 parent a8530a0 commit 37a7fcc
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/api/apps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,14 @@ impl<'octo> AppsRequestHandler<'octo> {

self.crab.get(&route, None::<&()>).await
}

/// Get an organization installation for the authenticated app.
pub async fn get_org_installation(
&self,
owner: impl AsRef<str>,
) -> crate::Result<crate::models::Installation> {
let route = format!("/orgs/{owner}/installation", owner = owner.as_ref(),);

self.crab.get(&route, None::<&()>).await
}
}
57 changes: 57 additions & 0 deletions tests/org_installations_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Tests for calls to the /orgs/{org}/installation endpoint.
mod mock_error;

use mock_error::setup_error_handler;
use octocrab::models::{Author, Installation, InstallationId};
use octocrab::Octocrab;
use wiremock::{
matchers::{method, path},
Mock, MockServer, ResponseTemplate,
};

async fn setup_api(template: ResponseTemplate) -> MockServer {
let mock_server = MockServer::start().await;

Mock::given(method("GET"))
.and(path("/orgs/github/installation"))
.respond_with(template)
.mount(&mock_server)
.await;
setup_error_handler(
&mock_server,
"GET on /orgs/github/installation was not received",
)
.await;
mock_server
}

fn setup_octocrab(uri: &str) -> Octocrab {
Octocrab::builder().base_uri(uri).unwrap().build().unwrap()
}

#[tokio::test]
async fn should_return_org_installation() {
let org_installation_response: Installation =
serde_json::from_str(include_str!("resources/orgs_installation_event.json")).unwrap();
let template = ResponseTemplate::new(200).set_body_json(&org_installation_response);
let mock_server = setup_api(template).await;
let client = setup_octocrab(&mock_server.uri());

let result = client.apps().get_org_installation("github").await;

assert!(
result.is_ok(),
"expected successful result, got error: {:#?}",
result
);

let Installation {
id: installation_id,
account: Author { login, .. },
..
} = result.unwrap();
{
assert_eq!(installation_id, InstallationId(1));
assert_eq!(login, "github");
}
}
50 changes: 50 additions & 0 deletions tests/resources/orgs_installation_event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"id": 1,
"account": {
"login": "github",
"id": 1,
"node_id": "MDEyOk9yZ2FuaXphdGlvbjE=",
"avatar_url": "https://github.com/images/error/hubot_happy.gif",
"gravatar_id": "",
"url": "https://api.github.com/orgs/github",
"html_url": "https://github.com/github",
"followers_url": "https://api.github.com/users/github/followers",
"following_url": "https://api.github.com/users/github/following{/other_user}",
"gists_url": "https://api.github.com/users/github/gists{/gist_id}",
"starred_url": "https://api.github.com/users/github/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/github/subscriptions",
"organizations_url": "https://api.github.com/users/github/orgs",
"repos_url": "https://api.github.com/orgs/github/repos",
"events_url": "https://api.github.com/orgs/github/events",
"received_events_url": "https://api.github.com/users/github/received_events",
"type": "Organization",
"site_admin": false
},
"repository_selection": "all",
"access_tokens_url": "https://api.github.com/installations/1/access_tokens",
"repositories_url": "https://api.github.com/installation/repositories",
"html_url": "https://github.com/organizations/github/settings/installations/1",
"app_id": 1,
"target_id": 1,
"target_type": "Organization",
"permissions": {
"checks": "write",
"metadata": "read",
"contents": "read"
},
"events": [
"push",
"pull_request"
],
"created_at": "2018-02-09T20:51:14Z",
"updated_at": "2018-02-09T20:51:14Z",
"single_file_name": "config.yml",
"has_multiple_single_files": true,
"single_file_paths": [
"config.yml",
".github/issue_TEMPLATE.md"
],
"app_slug": "github-actions",
"suspended_at": null,
"suspended_by": null
}

0 comments on commit 37a7fcc

Please sign in to comment.