Skip to content

Commit 75f40b6

Browse files
committed
Add method to transfer an issue.
1 parent 561bfd9 commit 75f40b6

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

src/github.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,63 @@ impl Issue {
877877
));
878878
Ok(client.json(req).await?)
879879
}
880+
881+
/// Returns the GraphQL ID of this issue.
882+
async fn graphql_issue_id(&self, client: &GithubClient) -> anyhow::Result<String> {
883+
let repo = self.repository();
884+
let mut issue_id = client
885+
.graphql_query(
886+
"query($owner:String!, $repo:String!, $issueNum:Int!) {
887+
repository(owner: $owner, name: $repo) {
888+
issue(number: $issueNum) {
889+
id
890+
}
891+
}
892+
}
893+
",
894+
serde_json::json!({
895+
"owner": repo.organization,
896+
"repo": repo.repository,
897+
"issueNum": self.number,
898+
}),
899+
)
900+
.await?;
901+
let serde_json::Value::String(issue_id) =
902+
issue_id["data"]["repository"]["issue"]["id"].take()
903+
else {
904+
anyhow::bail!("expected issue id, got {issue_id}");
905+
};
906+
Ok(issue_id)
907+
}
908+
909+
/// Transfers this issue to the given repository.
910+
pub async fn transfer(
911+
&self,
912+
client: &GithubClient,
913+
owner: &str,
914+
repo: &str,
915+
) -> anyhow::Result<()> {
916+
let issue_id = self.graphql_issue_id(client).await?;
917+
let repo_id = client.graphql_repo_id(owner, repo).await?;
918+
client
919+
.graphql_query(
920+
"mutation ($issueId: ID!, $repoId: ID!) {
921+
transferIssue(
922+
input: {createLabelsIfMissing: true, issueId: $issueId, repositoryId: $repoId}
923+
) {
924+
issue {
925+
id
926+
}
927+
}
928+
}",
929+
serde_json::json!({
930+
"issueId": issue_id,
931+
"repoId": repo_id,
932+
}),
933+
)
934+
.await?;
935+
Ok(())
936+
}
880937
}
881938

882939
#[derive(Debug, serde::Deserialize)]
@@ -2424,6 +2481,27 @@ impl GithubClient {
24242481
.with_context(|| format!("failed to set milestone for {url} to milestone {milestone:?}"))?;
24252482
Ok(())
24262483
}
2484+
2485+
/// Returns the GraphQL ID of the given repository.
2486+
async fn graphql_repo_id(&self, owner: &str, repo: &str) -> anyhow::Result<String> {
2487+
let mut repo_id = self
2488+
.graphql_query(
2489+
"query($owner:String!, $repo:String!) {
2490+
repository(owner: $owner, name: $repo) {
2491+
id
2492+
}
2493+
}",
2494+
serde_json::json!({
2495+
"owner": owner,
2496+
"repo": repo,
2497+
}),
2498+
)
2499+
.await?;
2500+
let serde_json::Value::String(repo_id) = repo_id["data"]["repository"]["id"].take() else {
2501+
anyhow::bail!("expected repo id, got {repo_id}");
2502+
};
2503+
Ok(repo_id)
2504+
}
24272505
}
24282506

24292507
#[derive(Debug, serde::Deserialize)]

0 commit comments

Comments
 (0)