Class: DSPy::Tools::GitHubCLIToolset
- Extended by:
- T::Sig
- Defined in:
- lib/dspy/tools/github_cli_toolset.rb
Overview
GitHub CLI toolset for common GitHub operations
Instance Method Summary collapse
- #api_request(endpoint:, method: 'GET', fields: {}, repo: nil) ⇒ Object
- #comment_on_issue(issue_number:, comment:, repo: nil) ⇒ Object
- #create_issue(title:, body:, labels: [], assignees: [], repo: nil) ⇒ Object
- #create_pr(title:, body:, base:, head:, repo: nil) ⇒ Object
- #get_issue(issue_number:, repo: nil) ⇒ Object
- #get_pr(pr_number:, repo: nil) ⇒ Object
-
#initialize ⇒ GitHubCLIToolset
constructor
A new instance of GitHubCLIToolset.
- #list_issues(state: IssueState::Open, labels: [], assignee: nil, repo: nil, limit: 20) ⇒ Object
- #list_prs(state: PRState::Open, author: nil, base: nil, repo: nil, limit: 20) ⇒ Object
- #review_pr(pr_number:, review_type:, comment: nil, repo: nil) ⇒ Object
Methods inherited from Toolset
schema_for_method, to_tools, tool, toolset_name
Constructor Details
#initialize ⇒ GitHubCLIToolset
Returns a new instance of GitHubCLIToolset.
75 76 77 |
# File 'lib/dspy/tools/github_cli_toolset.rb', line 75 def initialize # No persistent state needed end |
Instance Method Details
#api_request(endpoint:, method: 'GET', fields: {}, repo: nil) ⇒ Object
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 |
# File 'lib/dspy/tools/github_cli_toolset.rb', line 303 def api_request(endpoint:, method: 'GET', fields: {}, repo: nil) cmd = build_gh_command(['api', endpoint]) cmd << ['--method', method.upcase] fields.each do |key, value| cmd << ['-f', "#{key}=#{shell_escape(value)}"] end if repo cmd << ['--repo', shell_escape(repo)] end result = execute_command(cmd.flatten.join(' ')) if result[:success] result[:output] else "API request failed: #{result[:error]}" end rescue => e "Error making API request: #{e.message}" end |
#comment_on_issue(issue_number:, comment:, repo: nil) ⇒ Object
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/dspy/tools/github_cli_toolset.rb', line 249 def comment_on_issue(issue_number:, comment:, repo: nil) cmd = build_gh_command(['issue', 'comment', issue_number.to_s]) cmd << ['--body', shell_escape(comment)] if repo cmd << ['--repo', shell_escape(repo)] end result = execute_command(cmd.flatten.join(' ')) if result[:success] "Comment added successfully to issue ##{issue_number}" else "Failed to add comment: #{result[:error]}" end rescue => e "Error adding comment: #{e.message}" end |
#create_issue(title:, body:, labels: [], assignees: [], repo: nil) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/dspy/tools/github_cli_toolset.rb', line 86 def create_issue(title:, body:, labels: [], assignees: [], repo: nil) cmd = build_gh_command(['issue', 'create']) cmd << ['--title', shell_escape(title)] cmd << ['--body', shell_escape(body)] labels.each { |label| cmd << ['--label', shell_escape(label)] } assignees.each { |assignee| cmd << ['--assignee', shell_escape(assignee)] } if repo cmd << ['--repo', shell_escape(repo)] end result = execute_command(cmd.flatten.join(' ')) if result[:success] "Issue created successfully: #{result[:output].strip}" else "Failed to create issue: #{result[:error]}" end rescue => e "Error creating issue: #{e.message}" end |
#create_pr(title:, body:, base:, head:, repo: nil) ⇒ Object
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/dspy/tools/github_cli_toolset.rb', line 116 def create_pr(title:, body:, base:, head:, repo: nil) cmd = build_gh_command(['pr', 'create']) cmd << ['--title', shell_escape(title)] cmd << ['--body', shell_escape(body)] cmd << ['--base', shell_escape(base)] cmd << ['--head', shell_escape(head)] if repo cmd << ['--repo', shell_escape(repo)] end result = execute_command(cmd.flatten.join(' ')) if result[:success] "Pull request created successfully: #{result[:output].strip}" else "Failed to create pull request: #{result[:error]}" end rescue => e "Error creating pull request: #{e.message}" end |
#get_issue(issue_number:, repo: nil) ⇒ Object
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/dspy/tools/github_cli_toolset.rb', line 207 def get_issue(issue_number:, repo: nil) cmd = build_gh_command(['issue', 'view', issue_number.to_s, '--json', 'number,title,state,body,labels,assignees,url']) if repo cmd << ['--repo', shell_escape(repo)] end result = execute_command(cmd.flatten.join(' ')) if result[:success] parse_issue_details(result[:output]) else "Failed to get issue: #{result[:error]}" end rescue => e "Error getting issue: #{e.message}" end |
#get_pr(pr_number:, repo: nil) ⇒ Object
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/dspy/tools/github_cli_toolset.rb', line 226 def get_pr(pr_number:, repo: nil) cmd = build_gh_command(['pr', 'view', pr_number.to_s, '--json', 'number,title,state,body,baseRefName,headRefName,mergeable,url']) if repo cmd << ['--repo', shell_escape(repo)] end result = execute_command(cmd.flatten.join(' ')) if result[:success] parse_pr_details(result[:output]) else "Failed to get pull request: #{result[:error]}" end rescue => e "Error getting pull request: #{e.message}" end |
#list_issues(state: IssueState::Open, labels: [], assignee: nil, repo: nil, limit: 20) ⇒ Object
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/dspy/tools/github_cli_toolset.rb', line 145 def list_issues(state: IssueState::Open, labels: [], assignee: nil, repo: nil, limit: 20) cmd = build_gh_command(['issue', 'list', '--json', 'number,title,state,labels,assignees,url']) cmd << ['--state', state.serialize] cmd << ['--limit', limit.to_s] labels.each { |label| cmd << ['--label', shell_escape(label)] } if assignee cmd << ['--assignee', shell_escape(assignee)] end if repo cmd << ['--repo', shell_escape(repo)] end result = execute_command(cmd.flatten.join(' ')) if result[:success] parse_issue_list(result[:output]) else "Failed to list issues: #{result[:error]}" end rescue => e "Error listing issues: #{e.message}" end |
#list_prs(state: PRState::Open, author: nil, base: nil, repo: nil, limit: 20) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/dspy/tools/github_cli_toolset.rb', line 178 def list_prs(state: PRState::Open, author: nil, base: nil, repo: nil, limit: 20) cmd = build_gh_command(['pr', 'list', '--json', 'number,title,state,baseRefName,headRefName,url']) cmd << ['--state', state.serialize] cmd << ['--limit', limit.to_s] if cmd << ['--author', shell_escape()] end if base cmd << ['--base', shell_escape(base)] end if repo cmd << ['--repo', shell_escape(repo)] end result = execute_command(cmd.flatten.join(' ')) if result[:success] parse_pr_list(result[:output]) else "Failed to list pull requests: #{result[:error]}" end rescue => e "Error listing pull requests: #{e.message}" end |
#review_pr(pr_number:, review_type:, comment: nil, repo: nil) ⇒ Object
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/dspy/tools/github_cli_toolset.rb', line 274 def review_pr(pr_number:, review_type:, comment: nil, repo: nil) cmd = build_gh_command(['pr', 'review', pr_number.to_s]) cmd << ['--' + review_type.serialize.tr('_', '-')] if comment cmd << ['--body', shell_escape(comment)] end if repo cmd << ['--repo', shell_escape(repo)] end result = execute_command(cmd.flatten.join(' ')) if result[:success] "Review added successfully to PR ##{pr_number}" else "Failed to add review: #{result[:error]}" end rescue => e "Error adding review: #{e.message}" end |