use crate::Response;
use crate::internal::request::json_header;
use crate::types::Issue;
use super::IssuesApi;
impl<'a> IssuesApi<'a> {
pub async fn list_repo_pinned_issues(
&self,
owner: &str,
repo: &str,
) -> crate::Result<(Vec<Issue>, Response)> {
let escaped = crate::internal::escape::validate_and_escape_segments(&[owner, repo])?;
let path = format!("/repos/{}/{}/issues/pinned", escaped[0], escaped[1]);
self.client()
.get_parsed_response(
reqwest::Method::GET,
&path,
Some(&json_header()),
None::<&str>,
)
.await
}
pub async fn pin_issue(&self, owner: &str, repo: &str, index: i64) -> crate::Result<Response> {
let escaped = crate::internal::escape::validate_and_escape_segments(&[owner, repo])?;
let path = format!("/repos/{}/{}/issues/{index}/pin", escaped[0], escaped[1]);
let (status, resp) = self
.client()
.get_status_code(
reqwest::Method::POST,
&path,
Some(&json_header()),
None::<&str>,
)
.await?;
if status != 204 {
return Err(crate::Error::UnknownApi {
status,
body: format!("unexpected status: {status}"),
});
}
Ok(resp)
}
pub async fn unpin_issue(
&self,
owner: &str,
repo: &str,
index: i64,
) -> crate::Result<Response> {
let escaped = crate::internal::escape::validate_and_escape_segments(&[owner, repo])?;
let path = format!("/repos/{}/{}/issues/{index}/pin", escaped[0], escaped[1]);
let (status, resp) = self
.client()
.get_status_code(
reqwest::Method::DELETE,
&path,
Some(&json_header()),
None::<&str>,
)
.await?;
if status != 204 {
return Err(crate::Error::UnknownApi {
status,
body: format!("unexpected status: {status}"),
});
}
Ok(resp)
}
pub async fn move_issue_pin(
&self,
owner: &str,
repo: &str,
index: i64,
position: i64,
) -> crate::Result<Response> {
let escaped = crate::internal::escape::validate_and_escape_segments(&[owner, repo])?;
let path = format!(
"/repos/{}/{}/issues/{index}/pin/{position}",
escaped[0], escaped[1]
);
let (status, resp) = self
.client()
.get_status_code(
reqwest::Method::PATCH,
&path,
Some(&json_header()),
None::<&str>,
)
.await?;
if status != 204 {
return Err(crate::Error::UnknownApi {
status,
body: format!("unexpected status: {status}"),
});
}
Ok(resp)
}
}
#[cfg(test)]
mod tests {
use super::super::test_helpers::*;
use wiremock::matchers::{method, path_regex};
use wiremock::{Mock, MockServer, ResponseTemplate};
#[tokio::test]
async fn test_list_repo_pinned_issues_happy() {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path_regex(r"/api/v1/repos/[^/]+/[^/]+/issues/pinned"))
.respond_with(
ResponseTemplate::new(200)
.set_body_json(serde_json::json!([issue_json(1, 1, "Pinned")])),
)
.mount(&server)
.await;
let client = create_test_client(&server);
let (issues, resp) = client
.issues()
.list_repo_pinned_issues("owner", "repo")
.await
.unwrap();
assert_eq!(issues.len(), 1);
assert_eq!(resp.status, 200);
}
#[tokio::test]
async fn test_list_repo_pinned_issues_error() {
let server = MockServer::start().await;
Mock::given(method("GET"))
.and(path_regex(r"/api/v1/repos/[^/]+/[^/]+/issues/pinned"))
.respond_with(ResponseTemplate::new(500).set_body_json(error_body()))
.mount(&server)
.await;
let client = create_test_client(&server);
let result = client
.issues()
.list_repo_pinned_issues("owner", "repo")
.await;
assert!(result.is_err());
}
#[tokio::test]
async fn test_pin_issue_happy() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path_regex(r"/api/v1/repos/[^/]+/[^/]+/issues/\d+/pin"))
.respond_with(ResponseTemplate::new(204))
.mount(&server)
.await;
let client = create_test_client(&server);
let resp = client.issues().pin_issue("owner", "repo", 1).await.unwrap();
assert_eq!(resp.status, 204);
}
#[tokio::test]
async fn test_pin_issue_error() {
let server = MockServer::start().await;
Mock::given(method("POST"))
.and(path_regex(r"/api/v1/repos/[^/]+/[^/]+/issues/\d+/pin"))
.respond_with(ResponseTemplate::new(500).set_body_json(error_body()))
.mount(&server)
.await;
let client = create_test_client(&server);
let result = client.issues().pin_issue("owner", "repo", 1).await;
assert!(result.is_err());
}
#[tokio::test]
async fn test_unpin_issue_happy() {
let server = MockServer::start().await;
Mock::given(method("DELETE"))
.and(path_regex(r"/api/v1/repos/[^/]+/[^/]+/issues/\d+/pin"))
.respond_with(ResponseTemplate::new(204))
.mount(&server)
.await;
let client = create_test_client(&server);
let resp = client
.issues()
.unpin_issue("owner", "repo", 1)
.await
.unwrap();
assert_eq!(resp.status, 204);
}
#[tokio::test]
async fn test_unpin_issue_error() {
let server = MockServer::start().await;
Mock::given(method("DELETE"))
.and(path_regex(r"/api/v1/repos/[^/]+/[^/]+/issues/\d+/pin"))
.respond_with(ResponseTemplate::new(500).set_body_json(error_body()))
.mount(&server)
.await;
let client = create_test_client(&server);
let result = client.issues().unpin_issue("owner", "repo", 1).await;
assert!(result.is_err());
}
#[tokio::test]
async fn test_move_issue_pin_happy() {
let server = MockServer::start().await;
Mock::given(method("PATCH"))
.and(path_regex(r"/api/v1/repos/[^/]+/[^/]+/issues/\d+/pin/\d+"))
.respond_with(ResponseTemplate::new(204))
.mount(&server)
.await;
let client = create_test_client(&server);
let resp = client
.issues()
.move_issue_pin("owner", "repo", 1, 2)
.await
.unwrap();
assert_eq!(resp.status, 204);
}
#[tokio::test]
async fn test_move_issue_pin_error() {
let server = MockServer::start().await;
Mock::given(method("PATCH"))
.and(path_regex(r"/api/v1/repos/[^/]+/[^/]+/issues/\d+/pin/\d+"))
.respond_with(ResponseTemplate::new(500).set_body_json(error_body()))
.mount(&server)
.await;
let client = create_test_client(&server);
let result = client.issues().move_issue_pin("owner", "repo", 1, 2).await;
assert!(result.is_err());
}
}