|
31 | 31 | import org.kohsuke.github.GHIssue; |
32 | 32 | import org.kohsuke.github.GHIssueComment; |
33 | 33 | import org.kohsuke.github.GHIssueState; |
| 34 | +import org.kohsuke.github.GHIssueStateReason; |
34 | 35 | import org.kohsuke.github.GHLabel; |
35 | 36 | import org.kohsuke.github.GHPullRequest; |
36 | 37 | import org.kohsuke.github.GHPullRequestCommitDetail; |
|
48 | 49 | * |
49 | 50 | * <p>The tools cover the operations needed by the ADK GitHub automation samples: reading releases, |
50 | 51 | * diffs and file contents; searching code; listing and reading issues; creating issues and pull |
51 | | - * requests; labelling/assigning issues; and reading, labelling and commenting on pull requests. |
| 52 | + * requests; labelling/assigning issues; commenting on or closing issues; and reading, labelling and |
| 53 | + * commenting on pull requests. |
52 | 54 | * |
53 | 55 | * <p>Defense in depth against prompt injection: the agents read untrusted GitHub content (diffs, |
54 | 56 | * file contents, issue/PR titles) and could be steered into harmful writes. Independently of the |
@@ -851,6 +853,73 @@ private static List<Map<String, Object>> collectStatusChecks( |
851 | 853 | return checks; |
852 | 854 | } |
853 | 855 |
|
| 856 | + @Schema( |
| 857 | + name = "add_comment_to_issue", |
| 858 | + description = "Posts a comment on an issue. Returns the created comment's html_url.") |
| 859 | + public static Map<String, Object> addCommentToIssue( |
| 860 | + @Schema(name = "repo_owner", description = "The repository owner.") String repoOwner, |
| 861 | + @Schema(name = "repo_name", description = "The repository name.") String repoName, |
| 862 | + @Schema(name = "issue_number", description = "The issue number to comment on.") |
| 863 | + int issueNumber, |
| 864 | + @Schema(name = "body", description = "The Markdown body of the comment.") String body) { |
| 865 | + if (body == null || body.isEmpty()) { |
| 866 | + return error("Comment body must not be empty."); |
| 867 | + } |
| 868 | + String targetError = writeTargetError(repoOwner, repoName); |
| 869 | + if (targetError != null) { |
| 870 | + return error(targetError); |
| 871 | + } |
| 872 | + if (dryRun) { |
| 873 | + return dryRunPreview( |
| 874 | + "DRY RUN: no comment was posted. Set DRY_RUN=0 to comment on issues for real.", |
| 875 | + "issue_number", |
| 876 | + issueNumber, |
| 877 | + "body", |
| 878 | + body); |
| 879 | + } |
| 880 | + try { |
| 881 | + GHRepository repo = connect().getRepository(repoOwner + "/" + repoName); |
| 882 | + GHIssueComment comment = repo.getIssue(issueNumber).comment(body); |
| 883 | + Map<String, Object> result = new LinkedHashMap<>(); |
| 884 | + result.put("issue_number", issueNumber); |
| 885 | + result.put("html_url", comment.getHtmlUrl() == null ? "" : comment.getHtmlUrl().toString()); |
| 886 | + return success(result); |
| 887 | + } catch (IOException | GHException e) { |
| 888 | + return error("Failed to comment on issue #" + issueNumber + ": " + e.getMessage()); |
| 889 | + } |
| 890 | + } |
| 891 | + |
| 892 | + @Schema( |
| 893 | + name = "close_issue", |
| 894 | + description = |
| 895 | + "Closes an issue as 'not planned' (the state used for stale/won't-do issues). Succeeds as" |
| 896 | + + " a no-op if the issue is already closed.") |
| 897 | + public static Map<String, Object> closeIssue( |
| 898 | + @Schema(name = "repo_owner", description = "The repository owner.") String repoOwner, |
| 899 | + @Schema(name = "repo_name", description = "The repository name.") String repoName, |
| 900 | + @Schema(name = "issue_number", description = "The issue number to close.") int issueNumber) { |
| 901 | + String targetError = writeTargetError(repoOwner, repoName); |
| 902 | + if (targetError != null) { |
| 903 | + return error(targetError); |
| 904 | + } |
| 905 | + if (dryRun) { |
| 906 | + return dryRunPreview( |
| 907 | + "DRY RUN: no issue was closed. Set DRY_RUN=0 to close issues for real.", |
| 908 | + "issue_number", |
| 909 | + issueNumber); |
| 910 | + } |
| 911 | + try { |
| 912 | + GHRepository repo = connect().getRepository(repoOwner + "/" + repoName); |
| 913 | + repo.getIssue(issueNumber).close(GHIssueStateReason.NOT_PLANNED); |
| 914 | + Map<String, Object> result = new LinkedHashMap<>(); |
| 915 | + result.put("issue_number", issueNumber); |
| 916 | + result.put("state", "closed"); |
| 917 | + return success(result); |
| 918 | + } catch (IOException | GHException e) { |
| 919 | + return error("Failed to close issue #" + issueNumber + ": " + e.getMessage()); |
| 920 | + } |
| 921 | + } |
| 922 | + |
854 | 923 | /** Formats an issue into the compact map (number, title, body, html_url, labels, assignees). */ |
855 | 924 | private static Map<String, Object> formatIssue(GHIssue issue) { |
856 | 925 | Map<String, Object> info = new LinkedHashMap<>(); |
|
0 commit comments