diff --git a/src/api/issues.rs b/src/api/issues.rs index f10494cf..8cfd2580 100644 --- a/src/api/issues.rs +++ b/src/api/issues.rs @@ -19,6 +19,8 @@ pub use self::{ update::UpdateIssueBuilder, }; +use super::repos::RepoRef; + /// Handler for GitHub's issue API. /// /// Note: GitHub's REST API v3 considers every pull request an issue, but not @@ -29,13 +31,12 @@ pub use self::{ /// Created with [`Octocrab::issues`]. pub struct IssueHandler<'octo> { crab: &'octo Octocrab, - owner: String, - repo: String, + repo: RepoRef, } impl<'octo> IssueHandler<'octo> { - pub(crate) fn new(crab: &'octo Octocrab, owner: String, repo: String) -> Self { - Self { crab, owner, repo } + pub(crate) fn new(crab: &'octo Octocrab, repo: RepoRef) -> Self { + Self { crab, repo } } /// Gets an issue from the repository. @@ -47,12 +48,7 @@ impl<'octo> IssueHandler<'octo> { /// # } /// ``` pub async fn get(&self, number: u64) -> Result { - let route = format!( - "/repos/{owner}/{repo}/issues/{number}", - owner = self.owner, - repo = self.repo, - number = number, - ); + let route = format!("/{}/issues/{number}", self.repo, number = number,); self.crab.get(route, None::<&()>).await } @@ -147,12 +143,7 @@ impl<'octo> IssueHandler<'octo> { number: u64, reason: impl Into>, ) -> Result { - let route = format!( - "/repos/{owner}/{repo}/issues/{number}/lock", - owner = self.owner, - repo = self.repo, - number = number, - ); + let route = format!("/{}/issues/{number}/lock", self.repo, number = number,); let uri = Uri::builder() .path_and_query(route) @@ -187,12 +178,7 @@ impl<'octo> IssueHandler<'octo> { /// # } /// ``` pub async fn unlock(&self, number: u64) -> Result { - let route = format!( - "/repos/{owner}/{repo}/issues/{number}/lock", - owner = self.owner, - repo = self.repo, - number = number, - ); + let route = format!("/{}/issues/{number}/lock", self.repo, number = number,); let uri = Uri::builder() .path_and_query(route) @@ -221,12 +207,7 @@ impl<'octo> IssueHandler<'octo> { number: u64, assignees: &[&str], ) -> Result { - let route = format!( - "/repos/{owner}/{repo}/issues/{issue}/assignees", - owner = self.owner, - repo = self.repo, - issue = number - ); + let route = format!("/{}/issues/{issue}/assignees", self.repo, issue = number); self.crab .post(route, Some(&serde_json::json!({ "assignees": assignees }))) @@ -246,12 +227,7 @@ impl<'octo> IssueHandler<'octo> { number: u64, assignees: &[&str], ) -> Result { - let route = format!( - "/repos/{owner}/{repo}/issues/{issue}/assignees", - owner = self.owner, - repo = self.repo, - issue = number - ); + let route = format!("/{}/issues/{issue}/assignees", self.repo, issue = number); self.crab .delete(route, Some(&serde_json::json!({ "assignees": assignees }))) @@ -269,9 +245,8 @@ impl<'octo> IssueHandler<'octo> { /// ``` pub async fn check_assignee(&self, assignee: impl AsRef) -> Result { let route = format!( - "/repos/{owner}/{repo}/assignees/{assignee}", - owner = self.owner, - repo = self.repo, + "/{}/assignees/{assignee}", + self.repo, assignee = assignee.as_ref() ); @@ -344,11 +319,7 @@ impl<'octo, 'r> ListAssigneesBuilder<'octo, 'r> { /// Send the actual request. pub async fn send(self) -> Result> { - let route = format!( - "/repos/{owner}/{repo}/assignees", - owner = self.handler.owner, - repo = self.handler.repo, - ); + let route = format!("/{}/assignees", self.handler.repo); self.handler.crab.get(route, Some(&self)).await } @@ -367,12 +338,7 @@ impl<'octo> IssueHandler<'octo> { /// # } /// ``` pub async fn add_labels(&self, number: u64, labels: &[String]) -> Result> { - let route = format!( - "/repos/{owner}/{repo}/issues/{issue}/labels", - owner = self.owner, - repo = self.repo, - issue = number - ); + let route = format!("/{}/issues/{issue}/labels", self.repo, issue = number); self.crab .post(route, Some(&serde_json::json!({ "labels": labels }))) @@ -395,9 +361,8 @@ impl<'octo> IssueHandler<'octo> { label: impl AsRef, ) -> Result> { let route = format!( - "/repos/{owner}/{repo}/issues/{issue_number}/labels/{name}", - owner = self.owner, - repo = self.repo, + "/{}/issues/{issue_number}/labels/{name}", + self.repo, issue_number = number, name = utf8_percent_encode(label.as_ref(), NON_ALPHANUMERIC), ); @@ -420,12 +385,7 @@ impl<'octo> IssueHandler<'octo> { number: u64, labels: &[String], ) -> Result> { - let route = format!( - "/repos/{owner}/{repo}/issues/{issue}/labels", - owner = self.owner, - repo = self.repo, - issue = number - ); + let route = format!("/{}/issues/{issue}/labels", self.repo, issue = number); self.crab .put(route, Some(&serde_json::json!({ "labels": labels }))) @@ -448,11 +408,7 @@ impl<'octo> IssueHandler<'octo> { color: impl AsRef, description: impl AsRef, ) -> Result { - let route = format!( - "/repos/{owner}/{repo}/labels", - owner = self.owner, - repo = self.repo, - ); + let route = format!("/{}/labels", self.repo); self.crab .post( @@ -477,12 +433,7 @@ impl<'octo> IssueHandler<'octo> { /// # } /// ``` pub async fn get_label(&self, name: impl AsRef) -> Result { - let route = format!( - "/repos/{owner}/{repo}/labels/{name}", - owner = self.owner, - repo = self.repo, - name = name.as_ref(), - ); + let route = format!("/{}/labels/{name}", self.repo, name = name.as_ref(),); self.crab.get(route, None::<&()>).await } @@ -498,12 +449,7 @@ impl<'octo> IssueHandler<'octo> { /// # } /// ``` pub async fn delete_label(&self, name: impl AsRef) -> Result<()> { - let route = format!( - "/repos/{owner}/{repo}/labels/{name}", - owner = self.owner, - repo = self.repo, - name = name.as_ref(), - ); + let route = format!("/{}/labels/{name}", self.repo, name = name.as_ref(),); self.crab._delete(route, None::<&()>).await?; Ok(()) @@ -563,12 +509,7 @@ impl<'octo> IssueHandler<'octo> { number: u64, body: impl AsRef, ) -> Result { - let route = format!( - "/repos/{owner}/{repo}/issues/{issue}/comments", - owner = self.owner, - repo = self.repo, - issue = number - ); + let route = format!("/{}/issues/{issue}/comments", self.repo, issue = number); self.crab .post(route, Some(&serde_json::json!({ "body": body.as_ref() }))) @@ -586,11 +527,7 @@ impl<'octo> IssueHandler<'octo> { /// # } /// ``` pub async fn get_comment(&self, comment_id: CommentId) -> Result { - let route = format!( - "/repos/{owner}/{repo}/issues/comments/{comment_id}", - owner = self.owner, - repo = self.repo, - ); + let route = format!("/{}/issues/comments/{comment_id}", self.repo,); self.crab.get(route, None::<&()>).await } @@ -610,11 +547,7 @@ impl<'octo> IssueHandler<'octo> { comment_id: CommentId, body: impl AsRef, ) -> Result { - let route = format!( - "/repos/{owner}/{repo}/issues/comments/{comment_id}", - owner = self.owner, - repo = self.repo, - ); + let route = format!("/{}/issues/comments/{comment_id}", self.repo,); self.crab .post(route, Some(&serde_json::json!({ "body": body.as_ref() }))) @@ -629,11 +562,7 @@ impl<'octo> IssueHandler<'octo> { /// # } /// ``` pub async fn delete_comment(&self, comment_id: CommentId) -> Result<()> { - let route = format!( - "/repos/{owner}/{repo}/issues/comments/{comment_id}", - owner = self.owner, - repo = self.repo, - ); + let route = format!("/{}/issues/comments/{comment_id}", self.repo,); let uri = Uri::builder() .path_and_query(route) @@ -731,8 +660,7 @@ impl<'octo, 'r> ListCommentsBuilder<'octo, 'r> { /// Send the actual request. pub async fn send(self) -> Result> { let route = format!( - "/repos/{owner}/{repo}/issues/{issue}/comments", - owner = self.handler.owner, + "/{repo}/issues/{issue}/comments", repo = self.handler.repo, issue = self.issue_number, ); @@ -801,11 +729,7 @@ impl<'octo, 'r> ListIssueCommentsBuilder<'octo, 'r> { /// Send the actual request. pub async fn send(self) -> Result> { - let route = format!( - "/repos/{owner}/{repo}/issues/comments", - owner = self.handler.owner, - repo = self.handler.repo, - ); + let route = format!("/{}/issues/comments", self.handler.repo); self.handler.crab.get(route, Some(&self)).await } @@ -847,8 +771,7 @@ impl<'octo, 'r> ListTimelineEventsBuilder<'octo, 'r> { /// Send the actual request. pub async fn send(self) -> Result> { let route = format!( - "/repos/{owner}/{repo}/issues/{issue}/timeline", - owner = self.handler.owner, + "/{repo}/issues/{issue}/timeline", repo = self.handler.repo, issue = self.issue_number, ); @@ -952,8 +875,7 @@ impl<'octo, 'r> ListReactionsBuilder<'octo, 'r> { /// Send the actual request. pub async fn send(self) -> Result> { let route = format!( - "/repos/{owner}/{repo}/issues/{issue}/reactions", - owner = self.handler.owner, + "/{repo}/issues/{issue}/reactions", repo = self.handler.repo, issue = self.issue_number, ); @@ -998,8 +920,7 @@ impl<'octo, 'r> ListCommentReactionsBuilder<'octo, 'r> { /// Send the actual request. pub async fn send(self) -> Result> { let route = format!( - "/repos/{owner}/{repo}/issues/comments/{comment}/reactions", - owner = self.handler.owner, + "/{repo}/issues/comments/{comment}/reactions", repo = self.handler.repo, comment = self.comment_id, ); @@ -1024,11 +945,7 @@ impl<'octo> IssueHandler<'octo> { issue_number: u64, content: models::reactions::ReactionContent, ) -> Result { - let route = format!( - "/repos/{owner}/{repo}/issues/{issue_number}/reactions", - owner = self.owner, - repo = self.repo, - ); + let route = format!("/{}/issues/{issue_number}/reactions", self.repo,); self.crab .post(route, Some(&serde_json::json!({ "content": content }))) @@ -1051,9 +968,8 @@ impl<'octo> IssueHandler<'octo> { content: models::reactions::ReactionContent, ) -> Result { let route = format!( - "/repos/{owner}/{repo}/issues/comments/{comment_id}/reactions", - owner = self.owner, - repo = self.repo, + "/{}/issues/comments/{comment_id}/reactions", + self.repo, comment_id = comment_id.into(), ); @@ -1080,9 +996,8 @@ impl<'octo> IssueHandler<'octo> { reaction_id: impl Into, ) -> Result<()> { let route = format!( - "/repos/{owner}/{repo}/issues/{issue_number}/reactions/{reaction_id}", - owner = self.owner, - repo = self.repo, + "/{}/issues/{issue_number}/reactions/{reaction_id}", + self.repo, reaction_id = reaction_id.into(), ); @@ -1107,9 +1022,8 @@ impl<'octo> IssueHandler<'octo> { reaction_id: impl Into, ) -> Result<()> { let route = format!( - "/repos/{owner}/{repo}/issues/comments/{comment_id}/reactions/{reaction_id}", - owner = self.owner, - repo = self.repo, + "/{}/issues/comments/{comment_id}/reactions/{reaction_id}", + self.repo, comment_id = comment_id.into(), reaction_id = reaction_id.into(), ); diff --git a/src/api/issues/create.rs b/src/api/issues/create.rs index 0ddc577c..a73f9830 100644 --- a/src/api/issues/create.rs +++ b/src/api/issues/create.rs @@ -29,11 +29,7 @@ impl<'octo, 'r> CreateIssueBuilder<'octo, 'r> { /// Sends the actual request. pub async fn send(self) -> crate::Result { - let route = format!( - "/repos/{owner}/{repo}/issues", - owner = self.handler.owner, - repo = self.handler.repo, - ); + let route = format!("/{}/issues", self.handler.repo); self.handler.crab.post(route, Some(&self)).await } diff --git a/src/api/issues/list.rs b/src/api/issues/list.rs index fb14a863..005a3ba6 100644 --- a/src/api/issues/list.rs +++ b/src/api/issues/list.rs @@ -124,11 +124,7 @@ impl<'octo, 'b, 'c, 'd> ListIssuesBuilder<'octo, 'b, 'c, 'd> { /// Sends the actual request. pub async fn send(self) -> crate::Result> { - let route = format!( - "/repos/{owner}/{repo}/issues", - owner = self.handler.owner, - repo = self.handler.repo - ); + let route = format!("/{}/issues", self.handler.repo); self.handler.crab.get(route, Some(&self)).await } } diff --git a/src/api/issues/list_labels.rs b/src/api/issues/list_labels.rs index bd0ecbd0..8c17aa11 100644 --- a/src/api/issues/list_labels.rs +++ b/src/api/issues/list_labels.rs @@ -37,9 +37,8 @@ impl<'octo, 'r> ListLabelsForIssueBuilder<'octo, 'r> { /// Send the actual request. pub async fn send(self) -> Result> { let route = format!( - "/repos/{owner}/{repo}/issues/{number}/labels", - owner = self.handler.owner, - repo = self.handler.repo, + "/{}/issues/{number}/labels", + self.handler.repo, number = self.number, ); @@ -80,11 +79,7 @@ impl<'octo, 'r> ListLabelsForRepoBuilder<'octo, 'r> { /// Send the actual request. pub async fn send(self) -> Result> { - let route = format!( - "/repos/{owner}/{repo}/labels", - owner = self.handler.owner, - repo = self.handler.repo, - ); + let route = format!("/{}/labels", self.handler.repo); self.handler.crab.get(route, Some(&self)).await } diff --git a/src/api/issues/update.rs b/src/api/issues/update.rs index 22618418..86a8070e 100644 --- a/src/api/issues/update.rs +++ b/src/api/issues/update.rs @@ -85,8 +85,7 @@ impl<'octo, 'a, 'b, 'c, 'd, 'e> UpdateIssueBuilder<'octo, 'a, 'b, 'c, 'd, 'e> { /// Send the actual request. pub async fn send(self) -> Result { let route = format!( - "/repos/{owner}/{repo}/issues/{issue}", - owner = self.handler.owner, + "/repos/{repo}/issues/{issue}", repo = self.handler.repo, issue = self.number, ); diff --git a/src/api/repos.rs b/src/api/repos.rs index 3b914237..80c1f41d 100644 --- a/src/api/repos.rs +++ b/src/api/repos.rs @@ -27,7 +27,7 @@ mod teams; use crate::error::HttpSnafu; use crate::models::commits::GitCommitObject; -use crate::models::repos; +use crate::models::{repos, RepositoryId}; use crate::repos::file::GetReadmeBuilder; use crate::{models, params, Octocrab, Result}; pub use branches::ListBranchesBuilder; @@ -46,18 +46,32 @@ pub use status::{CreateStatusBuilder, ListStatusesBuilder}; pub use tags::ListTagsBuilder; pub use teams::ListTeamsBuilder; +#[derive(Clone)] +pub(crate) enum RepoRef { + ByOwnerAndName(String, String), + ById(RepositoryId), +} + +impl std::fmt::Display for RepoRef { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + RepoRef::ByOwnerAndName(owner, name) => write!(f, "repos/{}/{}", owner, name), + RepoRef::ById(id) => write!(f, "repositories/{}", id), + } + } +} + /// Handler for GitHub's repository API. /// /// Created with [`Octocrab::repos`]. pub struct RepoHandler<'octo> { crab: &'octo Octocrab, - owner: String, - repo: String, + repo: RepoRef, } impl<'octo> RepoHandler<'octo> { - pub(crate) fn new(crab: &'octo Octocrab, owner: String, repo: String) -> Self { - Self { crab, owner, repo } + pub(crate) fn new(crab: &'octo Octocrab, repo: RepoRef) -> Self { + Self { crab, repo } } /// Get's a repository's license. @@ -68,11 +82,7 @@ impl<'octo> RepoHandler<'octo> { /// # } /// ``` pub async fn license(&self) -> Result { - let route = format!( - "/repos/{owner}/{repo}/license", - owner = self.owner, - repo = self.repo, - ); + let route = format!("/{}/license", self.repo); self.crab.get(route, None::<&()>).await } @@ -85,11 +95,7 @@ impl<'octo> RepoHandler<'octo> { /// # } /// ``` pub async fn public_key(&self) -> Result { - let route = format!( - "/repos/{owner}/{repo}/actions/secrets/public-key", - owner = self.owner, - repo = self.repo, - ); + let route = format!("/{}/actions/secrets/public-key", self.repo); self.crab.get(route, None::<&()>).await } @@ -105,11 +111,7 @@ impl<'octo> RepoHandler<'octo> { /// # } /// ``` pub async fn get(&self) -> Result { - let route = format!( - "/repos/{owner}/{repo}", - owner = self.owner, - repo = self.repo, - ); + let route = format!("/{}", self.repo); self.crab.get(route, None::<&()>).await } @@ -124,11 +126,7 @@ impl<'octo> RepoHandler<'octo> { /// # } /// ``` pub async fn get_community_profile_metrics(&self) -> Result { - let route = format!( - "/repos/{owner}/{repo}/community/profile", - owner = self.owner, - repo = self.repo, - ); + let route = format!("/{}/community/profile", self.repo); self.crab.get(route, None::<&()>).await } @@ -149,8 +147,7 @@ impl<'octo> RepoHandler<'octo> { reference: ¶ms::repos::Reference, ) -> Result { let route = format!( - "/repos/{owner}/{repo}/git/ref/{reference}", - owner = self.owner, + "/{repo}/git/ref/{reference}", repo = self.repo, reference = reference.ref_url(), ); @@ -171,8 +168,7 @@ impl<'octo> RepoHandler<'octo> { /// ``` pub async fn get_tag(&self, tag_sha: impl Into) -> Result { let route = format!( - "/repos/{owner}/{repo}/git/tags/{tag_sha}", - owner = self.owner, + "/{repo}/git/tags/{tag_sha}", repo = self.repo, tag_sha = tag_sha.into(), ); @@ -198,11 +194,7 @@ impl<'octo> RepoHandler<'octo> { reference: ¶ms::repos::Reference, sha: impl Into, ) -> Result { - let route = format!( - "/repos/{owner}/{repo}/git/refs", - owner = self.owner, - repo = self.repo, - ); + let route = format!("/{}/git/refs", self.repo); self.crab .post( route, @@ -230,8 +222,7 @@ impl<'octo> RepoHandler<'octo> { /// ``` pub async fn delete_ref(&self, reference: ¶ms::repos::Reference) -> Result<()> { let route = format!( - "/repos/{owner}/{repo}/git/refs/{ref}", - owner = self.owner, + "/{repo}/git/refs/{ref}", repo = self.repo, ref = reference.ref_url() ); @@ -514,11 +505,7 @@ impl<'octo> RepoHandler<'octo> { /// # } /// ``` pub async fn list_languages(&self) -> Result { - let route = format!( - "/repos/{owner}/{repo}/languages", - owner = self.owner, - repo = self.repo, - ); + let route = format!("/{}/languages", self.repo); self.crab.get(route, None::<&()>).await } @@ -610,11 +597,7 @@ impl<'octo> RepoHandler<'octo> { &self, hook: crate::models::hooks::Hook, ) -> crate::Result { - let route = format!( - "/repos/{org}/{repo}/hooks", - org = self.owner, - repo = self.repo - ); + let route = format!("/{}/hooks", self.repo); let res = self.crab.post(route, Some(&hook)).await?; Ok(res) @@ -637,8 +620,7 @@ impl<'octo> RepoHandler<'octo> { reference: ¶ms::repos::Reference, ) -> Result { let route = format!( - "/repos/{owner}/{repo}/commits/{reference}/status", - owner = self.owner, + "/{repo}/commits/{reference}/status", repo = self.repo, reference = reference.ref_url(), ); @@ -671,8 +653,7 @@ impl<'octo> RepoHandler<'octo> { path: impl AsRef, ) -> Result>> { let route = format!( - "/repos/{owner}/{repo}/contents/{path}", - owner = self.owner, + "/{repo}/contents/{path}", repo = self.repo, path = path.as_ref(), ); @@ -696,11 +677,7 @@ impl<'octo> RepoHandler<'octo> { /// # } /// ``` pub async fn delete(self) -> Result<()> { - let route = format!( - "/repos/{owner}/{repo}", - owner = self.owner, - repo = self.repo - ); + let route = format!("/{}", self.repo); let uri = Uri::builder() .path_and_query(route) .build() @@ -716,8 +693,7 @@ impl<'octo> RepoHandler<'octo> { reference: impl Into, ) -> Result>> { let route = format!( - "/repos/{owner}/{repo}/tarball/{reference}", - owner = self.owner, + "/{repo}/tarball/{reference}", repo = self.repo, reference = reference.into(), ); @@ -733,8 +709,7 @@ impl<'octo> RepoHandler<'octo> { /// Check if a user is a repository collaborator pub async fn is_collaborator(&self, username: impl AsRef) -> Result { let route = format!( - "/repos/{owner}/{repo}/collaborators/{username}", - owner = self.owner, + "/{repo}/collaborators/{username}", repo = self.repo, username = username.as_ref(), ); @@ -801,7 +776,6 @@ impl<'octo> RepoHandler<'octo> { ) -> CreateGitCommitObjectBuilder<'_, '_> { CreateGitCommitObjectBuilder::new( self, - self.owner.clone(), self.repo.clone(), message.into().to_owned(), tree.into().to_owned(), @@ -813,8 +787,9 @@ impl<'octo> RepoHandler<'octo> { pub struct CreateGitCommitObjectBuilder<'octo, 'req> { #[serde(skip)] handler: &'octo RepoHandler<'req>, - owner: String, - repo: String, + // According to [API reference](https://docs.github.com/en/rest/git/commits?apiVersion=2022-11-28#create-a-commit), repo in body is not required. + #[serde(skip)] + repo: RepoRef, message: String, tree: String, parents: Vec, @@ -829,14 +804,12 @@ pub struct CreateGitCommitObjectBuilder<'octo, 'req> { impl<'octo, 'req> CreateGitCommitObjectBuilder<'octo, 'req> { pub(crate) fn new( handler: &'octo RepoHandler<'req>, - owner: String, - repo: String, + repo: RepoRef, message: String, tree: String, ) -> Self { Self { handler, - owner, repo, message, tree, @@ -873,11 +846,7 @@ impl<'octo, 'req> CreateGitCommitObjectBuilder<'octo, 'req> { /// Sends the request pub async fn send(&self) -> Result { - let route = format!( - "/repos/{owner}/{repo}/git/commits", - owner = self.owner, - repo = self.repo, - ); + let route = format!("/{}/git/commits", self.repo); self.handler.crab.post(route, Some(&self)).await } } diff --git a/src/api/repos/branches.rs b/src/api/repos/branches.rs index eb1532eb..d2637b43 100644 --- a/src/api/repos/branches.rs +++ b/src/api/repos/branches.rs @@ -44,11 +44,7 @@ impl<'octo, 'r> ListBranchesBuilder<'octo, 'r> { /// Sends the actual request. pub async fn send(self) -> Result> { - let route = format!( - "/repos/{owner}/{repo}/branches", - owner = self.handler.owner, - repo = self.handler.repo - ); + let route = format!("/{}/branches", self.handler.repo); self.handler.crab.get(route, Some(&self)).await } } diff --git a/src/api/repos/collaborators.rs b/src/api/repos/collaborators.rs index 8ea0eb81..5efe5cdf 100644 --- a/src/api/repos/collaborators.rs +++ b/src/api/repos/collaborators.rs @@ -45,11 +45,7 @@ impl<'octo, 'r> ListCollaboratorsBuilder<'octo, 'r> { /// Sends the actual request. pub async fn send(self) -> crate::Result> { - let route = format!( - "/repos/{owner}/{repo}/collaborators", - owner = self.handler.owner, - repo = self.handler.repo - ); + let route = format!("/{}/collaborators", self.handler.repo); self.handler.crab.get(route, Some(&self)).await } } diff --git a/src/api/repos/commits.rs b/src/api/repos/commits.rs index 88f5d844..76a15869 100644 --- a/src/api/repos/commits.rs +++ b/src/api/repos/commits.rs @@ -93,11 +93,7 @@ impl<'octo, 'r> ListCommitsBuilder<'octo, 'r> { /// Sends the actual request. pub async fn send(self) -> crate::Result> { - let route = format!( - "/repos/{owner}/{repo}/commits", - owner = self.handler.owner, - repo = self.handler.repo - ); + let route = format!("/{}/commits", self.handler.repo); self.handler.crab.get(route, Some(&self)).await } } diff --git a/src/api/repos/contributors.rs b/src/api/repos/contributors.rs index 31d3e3b8..643d953c 100644 --- a/src/api/repos/contributors.rs +++ b/src/api/repos/contributors.rs @@ -42,11 +42,7 @@ impl<'octo, 'r> ListContributorsBuilder<'octo, 'r> { /// Sends the actual request. pub async fn send(self) -> crate::Result> { - let route = format!( - "/repos/{owner}/{repo}/contributors", - owner = self.handler.owner, - repo = self.handler.repo - ); + let route = format!("/{}/contributors", self.handler.repo); self.handler.crab.get(route, Some(&self)).await } } diff --git a/src/api/repos/events.rs b/src/api/repos/events.rs index 6e3ba593..08a6ec78 100644 --- a/src/api/repos/events.rs +++ b/src/api/repos/events.rs @@ -58,11 +58,7 @@ impl<'octo, 'handler> ListRepoEventsBuilder<'octo, 'handler> { /// Sends the actual request. pub async fn send(self) -> crate::Result>> { - let route = format!( - "/repos/{owner}/{repo}/events", - owner = self.handler.owner, - repo = self.handler.repo - ); + let route = format!("/{}/events", self.handler.repo); let uri = self .handler diff --git a/src/api/repos/file.rs b/src/api/repos/file.rs index d293e88c..f6c3b56c 100644 --- a/src/api/repos/file.rs +++ b/src/api/repos/file.rs @@ -35,12 +35,7 @@ impl<'octo, 'r> GetContentBuilder<'octo, 'r> { /// Sends the actual request. pub async fn send(self) -> Result { let path = self.path.clone().unwrap_or(String::from("")); - let route = format!( - "/repos/{owner}/{repo}/contents/{path}", - owner = self.handler.owner, - repo = self.handler.repo, - path = path, - ); + let route = format!("/{}/contents/{path}", self.handler.repo, path = path,); self.handler.crab.get(route, Some(&self)).await } } @@ -81,12 +76,7 @@ impl<'octo, 'r> GetReadmeBuilder<'octo, 'r> { /// Sends the actual request. pub async fn send(self) -> Result { let path = self.path.clone().unwrap_or(String::from("")); - let route = format!( - "/repos/{owner}/{repo}/readme/{path}", - owner = self.handler.owner, - repo = self.handler.repo, - path = path, - ); + let route = format!("/{}/readme/{path}", self.handler.repo, path = path,); self.handler.crab.get(route, Some(&self)).await } } @@ -149,12 +139,7 @@ impl<'octo, 'r> UpdateFileBuilder<'octo, 'r> { /// Sends the actual request. pub async fn send(self) -> Result { - let route = format!( - "/repos/{owner}/{repo}/contents/{path}", - owner = self.handler.owner, - repo = self.handler.repo, - path = self.path, - ); + let route = format!("/{}/contents/{path}", self.handler.repo, path = self.path,); self.handler.crab.put(route, Some(&self)).await } } @@ -213,12 +198,7 @@ impl<'octo, 'r> DeleteFileBuilder<'octo, 'r> { /// Sends the actual request. pub async fn send(self) -> Result { - let route = format!( - "/repos/{owner}/{repo}/contents/{path}", - owner = self.handler.owner, - repo = self.handler.repo, - path = self.path, - ); + let route = format!("/{}/contents/{path}", self.handler.repo, path = self.path,); self.handler.crab.delete(route, Some(&self)).await } } diff --git a/src/api/repos/forks.rs b/src/api/repos/forks.rs index f3d71934..253a2f42 100644 --- a/src/api/repos/forks.rs +++ b/src/api/repos/forks.rs @@ -43,11 +43,7 @@ impl<'octo, 'r> ListForksBuilder<'octo, 'r> { /// Sends the actual request. pub async fn send(self) -> crate::Result> { - let route = format!( - "/repos/{owner}/{repo}/forks", - owner = self.handler.owner, - repo = self.handler.repo - ); + let route = format!("/{}/forks", self.handler.repo); self.handler.crab.get(route, Some(&self)).await } } @@ -92,11 +88,7 @@ impl<'octo, 'r> CreateForkBuilder<'octo, 'r> { /// Sends the actual request. pub async fn send(self) -> crate::Result { - let route = format!( - "/repos/{owner}/{repo}/forks", - owner = self.handler.owner, - repo = self.handler.repo - ); + let route = format!("/{}/forks", self.handler.repo); self.handler.crab.post(route, Some(&self)).await } } diff --git a/src/api/repos/generate.rs b/src/api/repos/generate.rs index e3a29af1..375e53b7 100644 --- a/src/api/repos/generate.rs +++ b/src/api/repos/generate.rs @@ -57,11 +57,7 @@ impl<'octo, 'r> GenerateRepositoryBuilder<'octo, 'r> { /// Sends the actual request. pub async fn send(self) -> Result<(), Error> { - let route = format!( - "/repos/{owner}/{repo}/generate", - owner = self.handler.owner, - repo = self.handler.repo - ); + let route = format!("/{}/generate", self.handler.repo); let uri = Uri::builder() .path_and_query(route) .build() diff --git a/src/api/repos/merges.rs b/src/api/repos/merges.rs index 5c541ea2..daf850f6 100644 --- a/src/api/repos/merges.rs +++ b/src/api/repos/merges.rs @@ -33,11 +33,7 @@ impl<'octo, 'r> MergeBranchBuilder<'octo, 'r> { /// Sends the actual request. pub async fn send(self) -> Result> { - let route = format!( - "/repos/{owner}/{repo}/merges", - owner = self.handler.owner, - repo = self.handler.repo - ); + let route = format!("/{}/merges", self.handler.repo); let post_response = self.handler.crab._post(route, Some(&self)).await?; if post_response.status() == http::StatusCode::NO_CONTENT { diff --git a/src/api/repos/pulls.rs b/src/api/repos/pulls.rs index 7f3cfafc..7e7ea0f7 100644 --- a/src/api/repos/pulls.rs +++ b/src/api/repos/pulls.rs @@ -36,12 +36,7 @@ impl<'octo, 'r> ListPullsBuilder<'octo, 'r> { /// Sends the actual request. pub async fn send(self) -> crate::Result> { - let route = format!( - "/repos/{owner}/{repo}/commits/{sha}/pulls", - owner = self.handler.owner, - repo = self.handler.repo, - sha = self.sha, - ); + let route = format!("/{}/commits/{sha}/pulls", self.handler.repo, sha = self.sha,); self.handler.crab.get(route, Some(&self)).await } } diff --git a/src/api/repos/release_assets.rs b/src/api/repos/release_assets.rs index b97640c1..57e72c78 100644 --- a/src/api/repos/release_assets.rs +++ b/src/api/repos/release_assets.rs @@ -4,12 +4,12 @@ use super::*; /// /// Created with [`RepoHandler::release_assets`]. pub struct ReleaseAssetsHandler<'octo, 'r> { - parent: &'r RepoHandler<'octo>, + handler: &'r RepoHandler<'octo>, } impl<'octo, 'r> ReleaseAssetsHandler<'octo, 'r> { pub(crate) fn new(parent: &'r RepoHandler<'octo>) -> Self { - Self { parent } + Self { handler: parent } } /// Gets the release asset using its id. /// ```no_run @@ -23,14 +23,9 @@ impl<'octo, 'r> ReleaseAssetsHandler<'octo, 'r> { /// # } /// ``` pub async fn get(&self, id: u64) -> Result { - let route = format!( - "/repos/{owner}/{repo}/releases/assets/{id}", - owner = self.parent.owner, - repo = self.parent.repo, - id = id, - ); + let route = format!("/{}/releases/assets/{id}", self.handler.repo, id = id,); - self.parent.crab.get(route, None::<&()>).await + self.handler.crab.get(route, None::<&()>).await } /// Creates a new [`UpdateReleaseAssetBuilder`] with `asset_id`. /// ```no_run @@ -65,14 +60,9 @@ impl<'octo, 'r> ReleaseAssetsHandler<'octo, 'r> { /// # } /// ``` pub async fn delete(&self, id: u64) -> Result<()> { - let route = format!( - "/repos/{owner}/{repo}/releases/assets/{id}", - owner = self.parent.owner, - repo = self.parent.repo, - id = id, - ); + let route = format!("/{}/releases/assets/{id}", self.handler.repo, id = id,); - self.parent.crab._delete(route, None::<&()>).await?; + self.handler.crab._delete(route, None::<&()>).await?; Ok(()) } @@ -103,7 +93,7 @@ impl<'octo, 'r> ReleaseAssetsHandler<'octo, 'r> { //use snafu::GenerateImplicitData; let route = format!( - "/repos/{owner}/{repo}/releases/assets/{id}", + "/{}/releases/assets/{id}", owner = self.parent.owner, repo = self.parent.repo, id = id, @@ -182,11 +172,10 @@ impl<'octo, 'repos, 'handler, 'name, 'label, 'state> /// Sends the actual request. pub async fn send(self) -> crate::Result { let route = format!( - "/repos/{owner}/{repo}/releases/assets/{asset_id}", - owner = self.handler.parent.owner, - repo = self.handler.parent.repo, + "/{repo}/releases/assets/{asset_id}", + repo = self.handler.handler.repo, asset_id = self.asset_id, ); - self.handler.parent.crab.patch(route, Some(&self)).await + self.handler.handler.crab.patch(route, Some(&self)).await } } diff --git a/src/api/repos/releases.rs b/src/api/repos/releases.rs index 1b5e1070..6e3ee56a 100644 --- a/src/api/repos/releases.rs +++ b/src/api/repos/releases.rs @@ -8,12 +8,12 @@ use std::convert::TryInto; /// /// Created with [`RepoHandler::releases`]. pub struct ReleasesHandler<'octo, 'r> { - parent: &'r RepoHandler<'octo>, + handler: &'r RepoHandler<'octo>, } impl<'octo, 'r> ReleasesHandler<'octo, 'r> { pub(crate) fn new(parent: &'r RepoHandler<'octo>) -> Self { - Self { parent } + Self { handler: parent } } /// Creates a new [`ListReleasesBuilder`] that can be configured to filter @@ -100,7 +100,7 @@ impl<'octo, 'r> ReleasesHandler<'octo, 'r> { /// ``` #[deprecated(note = "use repos::ReleaseAssetsHandler::get instead")] pub async fn get_asset(&self, asset_id: u64) -> crate::Result { - self.parent.release_assets().get(asset_id).await + self.handler.release_assets().get(asset_id).await } /// Gets the latest release. @@ -115,13 +115,9 @@ impl<'octo, 'r> ReleasesHandler<'octo, 'r> { /// # } /// ``` pub async fn get_latest(&self) -> crate::Result { - let route = format!( - "/repos/{owner}/{repo}/releases/latest", - owner = self.parent.owner, - repo = self.parent.repo, - ); + let route = format!("/{}/releases/latest", self.handler.repo,); - self.parent.crab.get(route, None::<&()>).await + self.handler.crab.get(route, None::<&()>).await } /// Gets the release using its tag. @@ -136,14 +132,9 @@ impl<'octo, 'r> ReleasesHandler<'octo, 'r> { /// # } /// ``` pub async fn get_by_tag(&self, tag: &str) -> crate::Result { - let route = format!( - "/repos/{owner}/{repo}/releases/tags/{tag}", - owner = self.parent.owner, - repo = self.parent.repo, - tag = tag, - ); + let route = format!("/{}/releases/tags/{tag}", self.handler.repo, tag = tag,); - self.parent.crab.get(route, None::<&()>).await + self.handler.crab.get(route, None::<&()>).await } /// Gets the release using its id. @@ -158,14 +149,9 @@ impl<'octo, 'r> ReleasesHandler<'octo, 'r> { /// # } /// ``` pub async fn get(&self, number: u64) -> Result { - let route = format!( - "/repos/{owner}/{repo}/releases/{number}", - owner = self.parent.owner, - repo = self.parent.repo, - number = number, - ); + let route = format!("/{}/releases/{number}", self.handler.repo, number = number,); - self.parent.crab.get(route, None::<&()>).await + self.handler.crab.get(route, None::<&()>).await } /// Generates [`crate::models::repos::ReleaseNotes`] which describe @@ -273,14 +259,9 @@ impl<'octo, 'r> ReleasesHandler<'octo, 'r> { /// # } /// ``` pub async fn delete(&self, id: u64) -> Result<()> { - let route = format!( - "/repos/{owner}/{repo}/releases/{id}", - owner = self.parent.owner, - repo = self.parent.repo, - id = id, - ); + let route = format!("/{}/releases/{id}", self.handler.repo, id = id,); - self.parent.crab._delete(route, None::<&()>).await?; + self.handler.crab._delete(route, None::<&()>).await?; Ok(()) } } @@ -321,12 +302,8 @@ impl<'octo, 'r1, 'r2> ListReleasesBuilder<'octo, 'r1, 'r2> { /// Sends the actual request. pub async fn send(self) -> crate::Result> { - let route = format!( - "/repos/{owner}/{repo}/releases", - owner = self.handler.parent.owner, - repo = self.handler.parent.repo - ); - self.handler.parent.crab.get(route, Some(&self)).await + let route = format!("/{}/releases", self.handler.handler.repo); + self.handler.handler.crab.get(route, Some(&self)).await } } @@ -439,12 +416,8 @@ impl<'octo, 'repos, 'handler, 'tag_name, 'target_commitish, 'name, 'body> /// Sends the actual request. pub async fn send(self) -> crate::Result { - let route = format!( - "/repos/{owner}/{repo}/releases", - owner = self.handler.parent.owner, - repo = self.handler.parent.repo - ); - self.handler.parent.crab.post(route, Some(&self)).await + let route = format!("/{}/releases", self.handler.handler.repo); + self.handler.handler.crab.post(route, Some(&self)).await } } @@ -543,12 +516,11 @@ impl<'octo, 'repos, 'handler, 'tag_name, 'target_commitish, 'name, 'body> /// Sends the actual request. pub async fn send(self) -> crate::Result { let route = format!( - "/repos/{owner}/{repo}/releases/{release_id}", - owner = self.handler.parent.owner, - repo = self.handler.parent.repo, + "/{repo}/releases/{release_id}", + repo = self.handler.handler.repo, release_id = self.release_id, ); - self.handler.parent.crab.patch(route, Some(&self)).await + self.handler.handler.crab.patch(route, Some(&self)).await } } @@ -640,14 +612,10 @@ impl< /// Sends the actual request. pub async fn send(self) -> crate::Result { - let route = format!( - "/repos/{owner}/{repo}/releases/generate-notes", - owner = self.handler.parent.owner, - repo = self.handler.parent.repo, - ); + let route = format!("/{}/releases/generate-notes", self.handler.handler.repo,); let result: Result = - self.handler.parent.crab.post(route, Some(&self)).await; + self.handler.handler.crab.post(route, Some(&self)).await; result } } @@ -692,12 +660,11 @@ impl<'octo, 'r1, 'r2> ListReleaseAssetsBuilder<'octo, 'r1, 'r2> { /// Sends the actual request. pub async fn send(self) -> crate::Result> { let route = format!( - "/repos/{owner}/{repo}/releases/{release_id}/assets", - owner = self.handler.parent.owner, - repo = self.handler.parent.repo, + "/{repo}/releases/{release_id}/assets", + repo = self.handler.handler.repo, release_id = self.release_id, ); - self.handler.parent.crab.get(route, Some(&self)).await + self.handler.handler.crab.get(route, Some(&self)).await } } @@ -762,7 +729,7 @@ impl<'octo, 'repos, 'handler, 'name, 'label> .header(http::header::CONTENT_LENGTH, self.body.len()) .body(self.body) .context(HttpSnafu)?; - let response = self.handler.parent.crab.execute(request).await?; + let response = self.handler.handler.crab.execute(request).await?; Asset::from_response(crate::map_github_error(response).await?).await } } diff --git a/src/api/repos/secrets.rs b/src/api/repos/secrets.rs index 71f8fb99..94f127e0 100644 --- a/src/api/repos/secrets.rs +++ b/src/api/repos/secrets.rs @@ -8,12 +8,12 @@ use crate::models::repos::secrets::{CreateRepositorySecret, CreateRepositorySecr /// /// Created with [`Octocrab::repos`]. pub struct RepoSecretsHandler<'octo> { - repo: &'octo RepoHandler<'octo>, + handler: &'octo RepoHandler<'octo>, } impl<'octo> RepoSecretsHandler<'octo> { pub(crate) fn new(repo: &'octo RepoHandler<'octo>) -> Self { - Self { repo } + Self { handler: repo } } /// Lists all secrets available in a repository without revealing their encrypted values. @@ -31,12 +31,8 @@ impl<'octo> RepoSecretsHandler<'octo> { pub async fn get_secrets( &self, ) -> crate::Result { - let route = format!( - "/repos/{owner}/{repo}/actions/secrets", - owner = self.repo.owner, - repo = self.repo.repo - ); - self.repo.crab.get(route, None::<&()>).await + let route = format!("/{}/actions/secrets", self.handler.repo); + self.handler.crab.get(route, None::<&()>).await } /// Gets your public key, which you need to encrypt secrets. @@ -54,12 +50,8 @@ impl<'octo> RepoSecretsHandler<'octo> { /// # Ok(()) /// # } pub async fn get_public_key(&self) -> crate::Result { - let route = format!( - "/repos/{owner}/{repo}/actions/secrets/public-key", - owner = self.repo.owner, - repo = self.repo.repo - ); - self.repo.crab.get(route, None::<&()>).await + let route = format!("/{}/actions/secrets/public-key", self.handler.repo); + self.handler.crab.get(route, None::<&()>).await } /// Gets a single repository secret without revealing its encrypted value. @@ -79,12 +71,11 @@ impl<'octo> RepoSecretsHandler<'octo> { secret_name: impl AsRef, ) -> crate::Result { let route = format!( - "/repos/{owner}/{repo}/actions/secrets/{secret_name}", - owner = self.repo.owner, - repo = self.repo.repo, + "/{}/actions/secrets/{secret_name}", + self.handler.repo, secret_name = secret_name.as_ref() ); - self.repo.crab.get(route, None::<&()>).await + self.handler.crab.get(route, None::<&()>).await } /// Creates or updates a repository secret with an encrypted value. @@ -116,14 +107,13 @@ impl<'octo> RepoSecretsHandler<'octo> { secret: &CreateRepositorySecret<'_>, ) -> crate::Result { let route = format!( - "/repos/{owner}/{repo}/actions/secrets/{secret_name}", - owner = self.repo.owner, - repo = self.repo.repo, + "/{}/actions/secrets/{secret_name}", + self.handler.repo, secret_name = secret_name.as_ref() ); let resp = { - let resp = self.repo.crab._put(route, Some(secret)).await?; + let resp = self.handler.crab._put(route, Some(secret)).await?; crate::map_github_error(resp).await? }; @@ -156,13 +146,12 @@ impl<'octo> RepoSecretsHandler<'octo> { /// # } pub async fn delete_secret(&self, secret_name: impl AsRef) -> crate::Result<()> { let route = format!( - "/repos/{owner}/{repo}/actions/secrets/{secret_name}", - owner = self.repo.owner, - repo = self.repo.repo, + "/{}/actions/secrets/{secret_name}", + self.handler.repo, secret_name = secret_name.as_ref() ); - let resp = self.repo.crab._delete(route, None::<&()>).await?; + let resp = self.handler.crab._delete(route, None::<&()>).await?; crate::map_github_error(resp).await?; Ok(()) } diff --git a/src/api/repos/stargazers.rs b/src/api/repos/stargazers.rs index c5695c14..97cd4846 100644 --- a/src/api/repos/stargazers.rs +++ b/src/api/repos/stargazers.rs @@ -33,11 +33,7 @@ impl<'octo, 'r> ListStarGazersBuilder<'octo, 'r> { /// Sends the actual request. pub async fn send(self) -> crate::Result> { - let route = format!( - "/repos/{owner}/{repo}/stargazers", - owner = self.handler.owner, - repo = self.handler.repo - ); + let route = format!("/{}/stargazers", self.handler.repo); let mut headers = http::header::HeaderMap::new(); headers.insert(ACCEPT, "application/vnd.github.star+json".parse().unwrap()); diff --git a/src/api/repos/status.rs b/src/api/repos/status.rs index d71b8657..d55666a2 100644 --- a/src/api/repos/status.rs +++ b/src/api/repos/status.rs @@ -62,12 +62,7 @@ impl<'octo, 'r> CreateStatusBuilder<'octo, 'r> { /// Sends the actual request. pub async fn send(self) -> Result { - let route = format!( - "/repos/{owner}/{repo}/statuses/{sha}", - owner = self.handler.owner, - repo = self.handler.repo, - sha = self.sha - ); + let route = format!("/{}/statuses/{sha}", self.handler.repo, sha = self.sha); self.handler.crab.post(route, Some(&self)).await } } @@ -109,8 +104,7 @@ impl<'octo, 'r> ListStatusesBuilder<'octo, 'r> { /// Sends the actual request. pub async fn send(self) -> crate::Result> { let route = format!( - "/repos/{owner}/{repo}/commits/{sha}/statuses", - owner = self.handler.owner, + "/{repo}/commits/{sha}/statuses", repo = self.handler.repo, sha = self.sha, ); diff --git a/src/api/repos/tags.rs b/src/api/repos/tags.rs index 4e493433..e9ff01e6 100644 --- a/src/api/repos/tags.rs +++ b/src/api/repos/tags.rs @@ -33,11 +33,7 @@ impl<'octo, 'r> ListTagsBuilder<'octo, 'r> { /// Sends the actual request. pub async fn send(self) -> crate::Result> { - let route = format!( - "/repos/{owner}/{repo}/tags", - owner = self.handler.owner, - repo = self.handler.repo - ); + let route = format!("/{}/tags", self.handler.repo); self.handler.crab.get(route, Some(&self)).await } } diff --git a/src/api/repos/teams.rs b/src/api/repos/teams.rs index 6902ec79..8cf16707 100644 --- a/src/api/repos/teams.rs +++ b/src/api/repos/teams.rs @@ -33,11 +33,7 @@ impl<'octo, 'r> ListTeamsBuilder<'octo, 'r> { /// Sends the actual request. pub async fn send(self) -> crate::Result> { - let route = format!( - "/repos/{owner}/{repo}/teams", - owner = self.handler.owner, - repo = self.handler.repo - ); + let route = format!("/{}/teams", self.handler.repo); self.handler.crab.get(route, Some(&self)).await } } diff --git a/src/api/users.rs b/src/api/users.rs index feb5b3d1..94f154e7 100644 --- a/src/api/users.rs +++ b/src/api/users.rs @@ -6,6 +6,7 @@ use http::StatusCode; use snafu::GenerateImplicitData; use crate::api::users::user_blocks::BlockedUsersBuilder; +use crate::models::UserId; use crate::{error, GitHubError, Octocrab}; pub use self::follow::{ListUserFollowerBuilder, ListUserFollowingBuilder}; @@ -15,20 +16,35 @@ mod follow; mod user_blocks; mod user_repos; +pub(crate) enum UserRef { + ByString(String), + ById(UserId), +} + +impl std::fmt::Display for UserRef { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + UserRef::ByString(str) => write!(f, "users/{}", str), + + UserRef::ById(id) => write!(f, "user/{}", id), + } + } +} + pub struct UserHandler<'octo> { crab: &'octo Octocrab, - user: String, + user: UserRef, } impl<'octo> UserHandler<'octo> { - pub(crate) fn new(crab: &'octo Octocrab, user: String) -> Self { + pub(crate) fn new(crab: &'octo Octocrab, user: UserRef) -> Self { Self { crab, user } } /// Get this users profile info pub async fn profile(&self) -> crate::Result { // build the route to get info on this user - let route = format!("/users/{}", self.user); + let route = format!("/{}", self.user); // get info on the specified user self.crab.get(route, None::<&()>).await } diff --git a/src/api/users/user_repos.rs b/src/api/users/user_repos.rs index abdccdd8..2927908b 100644 --- a/src/api/users/user_repos.rs +++ b/src/api/users/user_repos.rs @@ -66,7 +66,7 @@ impl<'octo, 'b> ListUserReposBuilder<'octo, 'b> { /// Sends the actual request. pub async fn send(self) -> crate::Result> { - let route = format!("/users/{user}/repos", user = self.handler.user); + let route = format!("/{user}/repos", user = self.handler.user); self.handler.crab.get(route, Some(&self)).await } } diff --git a/src/lib.rs b/src/lib.rs index c794dbeb..12a07402 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -193,6 +193,8 @@ pub mod models; pub mod params; pub mod service; +use api::repos::RepoRef; +use api::users::UserRef; use body::OctoBody; use chrono::{DateTime, Utc}; use http::{HeaderMap, HeaderValue, Method, Uri}; @@ -249,7 +251,7 @@ use crate::service::middleware::retry::RetryConfig; use crate::api::{code_scannings, users}; use auth::{AppAuth, Auth}; -use models::{AppId, InstallationId, InstallationToken}; +use models::{AppId, InstallationId, InstallationToken, RepositoryId, UserId}; pub use self::{ api::{ @@ -1077,7 +1079,13 @@ impl Octocrab { owner: impl Into, repo: impl Into, ) -> issues::IssueHandler { - issues::IssueHandler::new(self, owner.into(), repo.into()) + issues::IssueHandler::new(self, RepoRef::ByOwnerAndName(owner.into(), repo.into())) + } + + /// Creates a [`issues::IssueHandler`] for the repo specified at repository ID, + /// that allows you to access GitHub's issues API. + pub fn issues_by_id(&self, id: impl Into) -> issues::IssueHandler { + issues::IssueHandler::new(self, RepoRef::ById(id.into())) } /// Creates a [`code_scanning::CodeSCanningHandler`] for the repo specified at `owner/repo`, @@ -1137,7 +1145,13 @@ impl Octocrab { /// Creates a [`repos::RepoHandler`] for the repo specified at `owner/repo`, /// that allows you to access GitHub's repository API. pub fn repos(&self, owner: impl Into, repo: impl Into) -> repos::RepoHandler { - repos::RepoHandler::new(self, owner.into(), repo.into()) + repos::RepoHandler::new(self, RepoRef::ByOwnerAndName(owner.into(), repo.into())) + } + + /// Creates a [`repos::RepoHandler`] for the repo specified at repository ID, + /// that allows you to access GitHub's repository API. + pub fn repos_by_id(&self, id: impl Into) -> repos::RepoHandler { + repos::RepoHandler::new(self, RepoRef::ById(id.into())) } /// Creates a [`projects::ProjectHandler`] that allows you to access GitHub's @@ -1158,9 +1172,14 @@ impl Octocrab { teams::TeamHandler::new(self, owner.into()) } - /// Creates a [`users::UserHandler`] for the specified user + /// Creates a [`users::UserHandler`] for the specified user using the user name pub fn users(&self, user: impl Into) -> users::UserHandler { - users::UserHandler::new(self, user.into()) + users::UserHandler::new(self, UserRef::ByString(user.into())) + } + + /// Creates a [`users::UserHandler`] for the specified user using the user ID + pub fn users_by_id(&self, user: impl Into) -> users::UserHandler { + users::UserHandler::new(self, UserRef::ById(user.into())) } /// Creates a [`workflows::WorkflowsHandler`] for the specified repository that allows diff --git a/tests/follow_redirect.rs b/tests/follow_redirect.rs index 0dc53639..a421399b 100644 --- a/tests/follow_redirect.rs +++ b/tests/follow_redirect.rs @@ -38,7 +38,7 @@ async fn setup_api(template: ResponseTemplate) -> MockServer { .await; setup_error_handler( &mock_server, - &format!("GET on /repo/{new_owner}/{repo}/stargazers was not received"), + &format!("GET on /repos/{new_owner}/{repo}/stargazers was not received"), ) .await; mock_server diff --git a/tests/user_deserialize_test.rs b/tests/user_deserialize_test.rs index c91d8b26..41839ee6 100644 --- a/tests/user_deserialize_test.rs +++ b/tests/user_deserialize_test.rs @@ -31,7 +31,7 @@ async fn setup_api(template: ResponseTemplate) -> MockServer { } #[tokio::test] -async fn should_return_desirialized_user() { +async fn should_return_deserialized_user() { let mocked_response: UserProfile = serde_json::from_str(include_str!("resources/user_data.json")).unwrap(); let template = ResponseTemplate::new(200).set_body_json(&mocked_response);