Skip to content

Commit 0806d4f

Browse files
committed
feat: add api-based github tool
Signed-off-by: Nick Hale <[email protected]>
1 parent 6c96ea8 commit 0806d4f

File tree

9 files changed

+415
-0
lines changed

9 files changed

+415
-0
lines changed

apis/github/code/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
.env

apis/github/code/README.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# GitHub Integration
2+
3+
This is a collection of tools that interact with GitHub repositories.
4+
5+
## Usage
6+
7+
These tools should not be used directly. Instead, use one of the following:
8+
9+
- github.com/gptscript-ai/tools/apis/github/read
10+
- github.com/gptscript-ai/tools/apis/github/write
11+
12+
## Environment Variables
13+
14+
The following environment variables must be set to use these tools:
15+
16+
- `GITHUB_TOKEN`: Your GitHub personal access token.
17+
- `REPO_OWNER`: The owner of the repository (user or organization).
18+
- `REPO_NAME`: The name of the repository.

apis/github/code/index.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { Octokit } from '@octokit/rest';
2+
import { listIssues, listPRs, createIssue, modifyIssue, searchIssues } from './src/tools.js';
3+
4+
if (process.argv.length < 3) {
5+
console.error('Usage: node index.js <command> [args]');
6+
process.exit(1);
7+
}
8+
9+
const command = process.argv[2];
10+
const token = process.env.GITHUB_TOKEN;
11+
const owner = process.env.REPO_OWNER;
12+
const repo = process.env.REPO_NAME;
13+
14+
if (!token || !owner || !repo) {
15+
console.error('GITHUB_TOKEN, REPO_OWNER, and REPO_NAME environment variables must be set.');
16+
process.exit(1);
17+
}
18+
19+
const octokit = new Octokit({ auth: token });
20+
21+
switch (command) {
22+
case 'listIssues':
23+
await listIssues(octokit, owner, repo);
24+
break;
25+
case 'listPRs':
26+
await listPRs(octokit, owner, repo);
27+
break;
28+
case 'createIssue':
29+
const title = process.argv[3];
30+
const body = process.argv[4];
31+
await createIssue(octokit, owner, repo, title, body);
32+
break;
33+
case 'modifyIssue':
34+
const issueNumber = process.argv[3];
35+
const newTitle = process.argv[4];
36+
const newBody = process.argv[5];
37+
await modifyIssue(octokit, owner, repo, issueNumber, newTitle, newBody);
38+
break;
39+
case 'searchIssues':
40+
const query = process.argv[3];
41+
await searchIssues(octokit, owner, repo, query);
42+
break;
43+
default:
44+
console.error(`Unknown command: ${command}`);
45+
process.exit(1);
46+
}

apis/github/code/package-lock.json

+231
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apis/github/code/package.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "github-cli",
3+
"version": "1.0.0",
4+
"description": "A CLI tool to list issues and pull requests from a GitHub repository",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js"
8+
},
9+
"dependencies": {
10+
"@octokit/rest": "^18.0.0"
11+
}
12+
}

apis/github/code/src/tools.js

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
export async function listIssues(octokit, owner, repo) {
2+
const issues = await octokit.issues.listForRepo({
3+
owner,
4+
repo,
5+
state: 'open',
6+
per_page: 100
7+
});
8+
9+
issues.data.forEach(issue => {
10+
console.log(`#${issue.number} - ${issue.title} (ID: ${issue.id})`);
11+
});
12+
}
13+
14+
export async function listPRs(octokit, owner, repo) {
15+
const prs = await octokit.pulls.list({
16+
owner,
17+
repo,
18+
state: 'open',
19+
per_page: 100
20+
});
21+
22+
prs.data.forEach(pr => {
23+
console.log(`#${pr.number} - ${pr.title} (ID: ${pr.id})`);
24+
});
25+
}
26+
27+
export async function createIssue(octokit, owner, repo, title, body) {
28+
const issue = await octokit.issues.create({
29+
owner,
30+
repo,
31+
title,
32+
body
33+
});
34+
35+
console.log(`Created issue #${issue.data.number} - ${issue.data.title} (ID: ${issue.data.id})`);
36+
}
37+
38+
export async function modifyIssue(octokit, owner, repo, issueNumber, title, body) {
39+
const issue = await octokit.issues.update({
40+
owner,
41+
repo,
42+
issue_number: issueNumber,
43+
title,
44+
body
45+
});
46+
47+
console.log(`Modified issue #${issue.data.number} - ${issue.data.title} (ID: ${issue.data.id})`);
48+
}
49+
50+
export async function searchIssues(octokit, owner, repo, query) {
51+
const issues = await octokit.search.issuesAndPullRequests({
52+
q: `repo:${owner}/${repo} ${query}`
53+
});
54+
55+
issues.data.items.forEach(issue => {
56+
console.log(`#${issue.number} - ${issue.title} (ID: ${issue.id})`);
57+
});
58+
}

0 commit comments

Comments
 (0)