Skip to content

Commit 0b53bf9

Browse files
authored
GitHub: Add support for "Get a repository" requests (#11017)
see https://docs.github.com/en/rest/repos/repos#get-a-repository This can be used in the future to lookup the GitHub ID of repositories to avoid account resurrection attacks.
1 parent ce33ac1 commit 0b53bf9

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

crates/crates_io_github/examples/test_github_client.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ enum Request {
1212
#[clap(long, env = "GITHUB_ACCESS_TOKEN", hide_env_values = true)]
1313
access_token: SecretString,
1414
},
15+
GetRepository {
16+
owner: String,
17+
repo: String,
18+
#[clap(long, env = "GITHUB_ACCESS_TOKEN", hide_env_values = true)]
19+
access_token: SecretString,
20+
},
1521
OrgByName {
1622
org_name: String,
1723
#[clap(long, env = "GITHUB_ACCESS_TOKEN", hide_env_values = true)]
@@ -58,6 +64,17 @@ async fn main() -> Result<()> {
5864
let response = github_client.current_user(&access_token).await?;
5965
println!("{response:#?}");
6066
}
67+
Request::GetRepository {
68+
owner,
69+
repo,
70+
access_token,
71+
} => {
72+
let access_token = AccessToken::new(access_token.expose_secret().into());
73+
let response = github_client
74+
.get_repository(&owner, &repo, &access_token)
75+
.await?;
76+
println!("{response:#?}");
77+
}
6178
Request::OrgByName {
6279
org_name,
6380
access_token,

crates/crates_io_github/src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ type Result<T> = std::result::Result<T, GitHubError>;
2020
#[async_trait]
2121
pub trait GitHubClient: Send + Sync {
2222
async fn current_user(&self, auth: &AccessToken) -> Result<GithubUser>;
23+
async fn get_repository(
24+
&self,
25+
owner: &str,
26+
repo: &str,
27+
auth: &AccessToken,
28+
) -> Result<GitHubRepository>;
2329
async fn org_by_name(&self, org_name: &str, auth: &AccessToken) -> Result<GitHubOrganization>;
2430
async fn team_by_name(
2531
&self,
@@ -102,6 +108,16 @@ impl GitHubClient for RealGitHubClient {
102108
self.request("/user", auth).await
103109
}
104110

111+
async fn get_repository(
112+
&self,
113+
owner: &str,
114+
repo: &str,
115+
auth: &AccessToken,
116+
) -> Result<GitHubRepository> {
117+
let url = format!("/repos/{owner}/{repo}");
118+
self.request(&url, auth).await
119+
}
120+
105121
async fn org_by_name(&self, org_name: &str, auth: &AccessToken) -> Result<GitHubOrganization> {
106122
let url = format!("/orgs/{org_name}");
107123
self.request(&url, auth).await
@@ -191,6 +207,19 @@ pub struct GithubUser {
191207
pub name: Option<String>,
192208
}
193209

210+
#[derive(Debug, Deserialize)]
211+
pub struct GitHubRepository {
212+
pub id: i64,
213+
pub name: String,
214+
pub owner: GitHubRepositoryOwner,
215+
}
216+
217+
#[derive(Debug, Deserialize)]
218+
pub struct GitHubRepositoryOwner {
219+
pub login: String,
220+
pub id: i32,
221+
}
222+
194223
#[derive(Debug, Deserialize)]
195224
pub struct GitHubOrganization {
196225
pub id: i32, // unique GH id (needed for membership queries)

0 commit comments

Comments
 (0)