@loopstack/github-module 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +67 -0
- package/dist/github-oauth.provider.d.ts +19 -0
- package/dist/github-oauth.provider.d.ts.map +1 -0
- package/dist/github-oauth.provider.js +91 -0
- package/dist/github-oauth.provider.js.map +1 -0
- package/dist/github.module.d.ts +3 -0
- package/dist/github.module.d.ts.map +1 -0
- package/dist/github.module.js +75 -0
- package/dist/github.module.js.map +1 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +44 -0
- package/dist/index.js.map +1 -0
- package/dist/tools/actions/github-get-workflow-run.tool.d.ts +13 -0
- package/dist/tools/actions/github-get-workflow-run.tool.d.ts.map +1 -0
- package/dist/tools/actions/github-get-workflow-run.tool.js +106 -0
- package/dist/tools/actions/github-get-workflow-run.tool.js.map +1 -0
- package/dist/tools/actions/github-list-workflow-runs.tool.d.ts +16 -0
- package/dist/tools/actions/github-list-workflow-runs.tool.d.ts.map +1 -0
- package/dist/tools/actions/github-list-workflow-runs.tool.js +129 -0
- package/dist/tools/actions/github-list-workflow-runs.tool.js.map +1 -0
- package/dist/tools/actions/github-trigger-workflow.tool.d.ts +15 -0
- package/dist/tools/actions/github-trigger-workflow.tool.d.ts.map +1 -0
- package/dist/tools/actions/github-trigger-workflow.tool.js +101 -0
- package/dist/tools/actions/github-trigger-workflow.tool.js.map +1 -0
- package/dist/tools/content/github-create-or-update-file.tool.d.ts +17 -0
- package/dist/tools/content/github-create-or-update-file.tool.d.ts.map +1 -0
- package/dist/tools/content/github-create-or-update-file.tool.js +115 -0
- package/dist/tools/content/github-create-or-update-file.tool.js.map +1 -0
- package/dist/tools/content/github-get-commit.tool.d.ts +13 -0
- package/dist/tools/content/github-get-commit.tool.d.ts.map +1 -0
- package/dist/tools/content/github-get-commit.tool.js +113 -0
- package/dist/tools/content/github-get-commit.tool.js.map +1 -0
- package/dist/tools/content/github-get-file-content.tool.d.ts +14 -0
- package/dist/tools/content/github-get-file-content.tool.d.ts.map +1 -0
- package/dist/tools/content/github-get-file-content.tool.js +107 -0
- package/dist/tools/content/github-get-file-content.tool.js.map +1 -0
- package/dist/tools/content/github-list-directory.tool.d.ts +14 -0
- package/dist/tools/content/github-list-directory.tool.d.ts.map +1 -0
- package/dist/tools/content/github-list-directory.tool.js +102 -0
- package/dist/tools/content/github-list-directory.tool.js.map +1 -0
- package/dist/tools/issues/github-create-issue-comment.tool.d.ts +14 -0
- package/dist/tools/issues/github-create-issue-comment.tool.d.ts.map +1 -0
- package/dist/tools/issues/github-create-issue-comment.tool.js +100 -0
- package/dist/tools/issues/github-create-issue-comment.tool.js.map +1 -0
- package/dist/tools/issues/github-create-issue.tool.d.ts +16 -0
- package/dist/tools/issues/github-create-issue.tool.d.ts.map +1 -0
- package/dist/tools/issues/github-create-issue.tool.js +112 -0
- package/dist/tools/issues/github-create-issue.tool.js.map +1 -0
- package/dist/tools/issues/github-get-issue.tool.d.ts +13 -0
- package/dist/tools/issues/github-get-issue.tool.d.ts.map +1 -0
- package/dist/tools/issues/github-get-issue.tool.js +106 -0
- package/dist/tools/issues/github-get-issue.tool.js.map +1 -0
- package/dist/tools/issues/github-list-issues.tool.d.ts +17 -0
- package/dist/tools/issues/github-list-issues.tool.d.ts.map +1 -0
- package/dist/tools/issues/github-list-issues.tool.js +115 -0
- package/dist/tools/issues/github-list-issues.tool.js.map +1 -0
- package/dist/tools/pull-requests/github-create-pull-request.tool.d.ts +17 -0
- package/dist/tools/pull-requests/github-create-pull-request.tool.d.ts.map +1 -0
- package/dist/tools/pull-requests/github-create-pull-request.tool.js +113 -0
- package/dist/tools/pull-requests/github-create-pull-request.tool.js.map +1 -0
- package/dist/tools/pull-requests/github-get-pull-request.tool.d.ts +13 -0
- package/dist/tools/pull-requests/github-get-pull-request.tool.d.ts.map +1 -0
- package/dist/tools/pull-requests/github-get-pull-request.tool.js +113 -0
- package/dist/tools/pull-requests/github-get-pull-request.tool.js.map +1 -0
- package/dist/tools/pull-requests/github-list-pr-reviews.tool.d.ts +13 -0
- package/dist/tools/pull-requests/github-list-pr-reviews.tool.d.ts.map +1 -0
- package/dist/tools/pull-requests/github-list-pr-reviews.tool.js +97 -0
- package/dist/tools/pull-requests/github-list-pr-reviews.tool.js.map +1 -0
- package/dist/tools/pull-requests/github-list-pull-requests.tool.d.ts +16 -0
- package/dist/tools/pull-requests/github-list-pull-requests.tool.d.ts.map +1 -0
- package/dist/tools/pull-requests/github-list-pull-requests.tool.js +113 -0
- package/dist/tools/pull-requests/github-list-pull-requests.tool.js.map +1 -0
- package/dist/tools/pull-requests/github-merge-pull-request.tool.d.ts +16 -0
- package/dist/tools/pull-requests/github-merge-pull-request.tool.d.ts.map +1 -0
- package/dist/tools/pull-requests/github-merge-pull-request.tool.js +108 -0
- package/dist/tools/pull-requests/github-merge-pull-request.tool.js.map +1 -0
- package/dist/tools/repos/github-create-repo.tool.d.ts +14 -0
- package/dist/tools/repos/github-create-repo.tool.d.ts.map +1 -0
- package/dist/tools/repos/github-create-repo.tool.js +108 -0
- package/dist/tools/repos/github-create-repo.tool.js.map +1 -0
- package/dist/tools/repos/github-get-repo.tool.d.ts +12 -0
- package/dist/tools/repos/github-get-repo.tool.d.ts.map +1 -0
- package/dist/tools/repos/github-get-repo.tool.js +107 -0
- package/dist/tools/repos/github-get-repo.tool.js.map +1 -0
- package/dist/tools/repos/github-list-branches.tool.d.ts +13 -0
- package/dist/tools/repos/github-list-branches.tool.d.ts.map +1 -0
- package/dist/tools/repos/github-list-branches.tool.js +97 -0
- package/dist/tools/repos/github-list-branches.tool.js.map +1 -0
- package/dist/tools/repos/github-list-repos.tool.d.ts +14 -0
- package/dist/tools/repos/github-list-repos.tool.d.ts.map +1 -0
- package/dist/tools/repos/github-list-repos.tool.js +107 -0
- package/dist/tools/repos/github-list-repos.tool.js.map +1 -0
- package/dist/tools/search/github-search-code.tool.d.ts +13 -0
- package/dist/tools/search/github-search-code.tool.d.ts.map +1 -0
- package/dist/tools/search/github-search-code.tool.js +100 -0
- package/dist/tools/search/github-search-code.tool.js.map +1 -0
- package/dist/tools/search/github-search-issues.tool.d.ts +14 -0
- package/dist/tools/search/github-search-issues.tool.d.ts.map +1 -0
- package/dist/tools/search/github-search-issues.tool.js +109 -0
- package/dist/tools/search/github-search-issues.tool.js.map +1 -0
- package/dist/tools/search/github-search-repos.tool.d.ts +14 -0
- package/dist/tools/search/github-search-repos.tool.d.ts.map +1 -0
- package/dist/tools/search/github-search-repos.tool.js +106 -0
- package/dist/tools/search/github-search-repos.tool.js.map +1 -0
- package/dist/tools/users/github-get-authenticated-user.tool.d.ts +9 -0
- package/dist/tools/users/github-get-authenticated-user.tool.d.ts.map +1 -0
- package/dist/tools/users/github-get-authenticated-user.tool.js +96 -0
- package/dist/tools/users/github-get-authenticated-user.tool.js.map +1 -0
- package/dist/tools/users/github-list-user-orgs.tool.d.ts +11 -0
- package/dist/tools/users/github-list-user-orgs.tool.d.ts.map +1 -0
- package/dist/tools/users/github-list-user-orgs.tool.js +95 -0
- package/dist/tools/users/github-list-user-orgs.tool.js.map +1 -0
- package/package.json +81 -0
- package/src/github-oauth.provider.ts +85 -0
- package/src/github.module.ts +63 -0
- package/src/index.ts +27 -0
- package/src/tools/actions/github-get-workflow-run.tool.ts +115 -0
- package/src/tools/actions/github-list-workflow-runs.tool.ts +141 -0
- package/src/tools/actions/github-trigger-workflow.tool.ts +97 -0
- package/src/tools/content/github-create-or-update-file.tool.ts +124 -0
- package/src/tools/content/github-get-commit.tool.ts +124 -0
- package/src/tools/content/github-get-file-content.tool.ts +112 -0
- package/src/tools/content/github-list-directory.tool.ts +105 -0
- package/src/tools/issues/github-create-issue-comment.tool.ts +100 -0
- package/src/tools/issues/github-create-issue.tool.ts +114 -0
- package/src/tools/issues/github-get-issue.tool.ts +115 -0
- package/src/tools/issues/github-list-issues.tool.ts +126 -0
- package/src/tools/pull-requests/github-create-pull-request.tool.ts +119 -0
- package/src/tools/pull-requests/github-get-pull-request.tool.ts +128 -0
- package/src/tools/pull-requests/github-list-pr-reviews.tool.ts +99 -0
- package/src/tools/pull-requests/github-list-pull-requests.tool.ts +124 -0
- package/src/tools/pull-requests/github-merge-pull-request.tool.ts +108 -0
- package/src/tools/repos/github-create-repo.tool.ts +111 -0
- package/src/tools/repos/github-get-repo.tool.ts +120 -0
- package/src/tools/repos/github-list-branches.tool.ts +97 -0
- package/src/tools/repos/github-list-repos.tool.ts +115 -0
- package/src/tools/search/github-search-code.tool.ts +105 -0
- package/src/tools/search/github-search-issues.tool.ts +120 -0
- package/src/tools/search/github-search-repos.tool.ts +115 -0
- package/src/tools/users/github-get-authenticated-user.tool.ts +98 -0
- package/src/tools/users/github-list-user-orgs.tool.ts +94 -0
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { Inject, Logger } from '@nestjs/common';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { Input, RunContext, Tool, ToolInterface, ToolResult } from '@loopstack/common';
|
|
4
|
+
import { OAuthTokenStore } from '@loopstack/oauth-module';
|
|
5
|
+
|
|
6
|
+
export type GitHubMergePullRequestArgs = {
|
|
7
|
+
owner: string;
|
|
8
|
+
repo: string;
|
|
9
|
+
pullNumber: number;
|
|
10
|
+
mergeMethod?: string;
|
|
11
|
+
commitTitle?: string;
|
|
12
|
+
commitMessage?: string;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
@Tool({
|
|
16
|
+
config: {
|
|
17
|
+
description: 'Merges a GitHub pull request. Returns { error: "unauthorized" } if no valid token is available.',
|
|
18
|
+
},
|
|
19
|
+
})
|
|
20
|
+
export class GitHubMergePullRequestTool implements ToolInterface {
|
|
21
|
+
private readonly logger = new Logger(GitHubMergePullRequestTool.name);
|
|
22
|
+
|
|
23
|
+
@Inject()
|
|
24
|
+
private tokenStore: OAuthTokenStore;
|
|
25
|
+
|
|
26
|
+
@Input({
|
|
27
|
+
schema: z
|
|
28
|
+
.object({
|
|
29
|
+
owner: z.string(),
|
|
30
|
+
repo: z.string(),
|
|
31
|
+
pullNumber: z.number(),
|
|
32
|
+
mergeMethod: z.enum(['merge', 'squash', 'rebase']).default('merge'),
|
|
33
|
+
commitTitle: z.string().optional(),
|
|
34
|
+
commitMessage: z.string().optional(),
|
|
35
|
+
})
|
|
36
|
+
.strict(),
|
|
37
|
+
})
|
|
38
|
+
args: GitHubMergePullRequestArgs;
|
|
39
|
+
|
|
40
|
+
async execute(args: GitHubMergePullRequestArgs, ctx: RunContext): Promise<ToolResult> {
|
|
41
|
+
const accessToken = await this.tokenStore.getValidAccessToken(ctx.userId, 'github');
|
|
42
|
+
|
|
43
|
+
if (!accessToken) {
|
|
44
|
+
return {
|
|
45
|
+
data: {
|
|
46
|
+
error: 'unauthorized',
|
|
47
|
+
message: 'No valid GitHub token found. Please authenticate first.',
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const requestBody: Record<string, unknown> = {
|
|
53
|
+
merge_method: args.mergeMethod ?? 'merge',
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
if (args.commitTitle) requestBody.commit_title = args.commitTitle;
|
|
57
|
+
if (args.commitMessage) requestBody.commit_message = args.commitMessage;
|
|
58
|
+
|
|
59
|
+
const url = `https://api.github.com/repos/${encodeURIComponent(args.owner)}/${encodeURIComponent(args.repo)}/pulls/${args.pullNumber}/merge`;
|
|
60
|
+
const response = await fetch(url, {
|
|
61
|
+
method: 'PUT',
|
|
62
|
+
headers: {
|
|
63
|
+
Authorization: `Bearer ${accessToken}`,
|
|
64
|
+
Accept: 'application/vnd.github+json',
|
|
65
|
+
'X-GitHub-Api-Version': '2022-11-28',
|
|
66
|
+
'Content-Type': 'application/json',
|
|
67
|
+
},
|
|
68
|
+
body: JSON.stringify(requestBody),
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
if (response.status === 401 || response.status === 403) {
|
|
72
|
+
this.logger.warn(`GitHub API returned ${response.status} for user ${ctx.userId}`);
|
|
73
|
+
return {
|
|
74
|
+
data: {
|
|
75
|
+
error: '401',
|
|
76
|
+
message: 'GitHub token was rejected. Please re-authenticate.',
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (!response.ok) {
|
|
82
|
+
const errorBody = await response.text();
|
|
83
|
+
this.logger.error(`GitHub API error: ${response.status} ${errorBody}`);
|
|
84
|
+
return {
|
|
85
|
+
data: {
|
|
86
|
+
error: 'api_error',
|
|
87
|
+
message: `GitHub API error: ${response.statusText}`,
|
|
88
|
+
},
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const result = (await response.json()) as {
|
|
93
|
+
sha: string;
|
|
94
|
+
merged: boolean;
|
|
95
|
+
message: string;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
data: {
|
|
100
|
+
merge: {
|
|
101
|
+
sha: result.sha,
|
|
102
|
+
merged: result.merged,
|
|
103
|
+
message: result.message,
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { Inject, Logger } from '@nestjs/common';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { Input, RunContext, Tool, ToolInterface, ToolResult } from '@loopstack/common';
|
|
4
|
+
import { OAuthTokenStore } from '@loopstack/oauth-module';
|
|
5
|
+
|
|
6
|
+
export type GitHubCreateRepoArgs = {
|
|
7
|
+
name: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
private?: boolean;
|
|
10
|
+
autoInit?: boolean;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
@Tool({
|
|
14
|
+
config: {
|
|
15
|
+
description:
|
|
16
|
+
'Creates a new GitHub repository for the authenticated user. Returns { error: "unauthorized" } if no valid token is available.',
|
|
17
|
+
},
|
|
18
|
+
})
|
|
19
|
+
export class GitHubCreateRepoTool implements ToolInterface {
|
|
20
|
+
private readonly logger = new Logger(GitHubCreateRepoTool.name);
|
|
21
|
+
|
|
22
|
+
@Inject()
|
|
23
|
+
private tokenStore: OAuthTokenStore;
|
|
24
|
+
|
|
25
|
+
@Input({
|
|
26
|
+
schema: z
|
|
27
|
+
.object({
|
|
28
|
+
name: z.string(),
|
|
29
|
+
description: z.string().optional(),
|
|
30
|
+
private: z.boolean().default(false),
|
|
31
|
+
autoInit: z.boolean().default(false),
|
|
32
|
+
})
|
|
33
|
+
.strict(),
|
|
34
|
+
})
|
|
35
|
+
args: GitHubCreateRepoArgs;
|
|
36
|
+
|
|
37
|
+
async execute(args: GitHubCreateRepoArgs, ctx: RunContext): Promise<ToolResult> {
|
|
38
|
+
const accessToken = await this.tokenStore.getValidAccessToken(ctx.userId, 'github');
|
|
39
|
+
|
|
40
|
+
if (!accessToken) {
|
|
41
|
+
return {
|
|
42
|
+
data: {
|
|
43
|
+
error: 'unauthorized',
|
|
44
|
+
message: 'No valid GitHub token found. Please authenticate first.',
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const body: Record<string, unknown> = {
|
|
50
|
+
name: args.name,
|
|
51
|
+
private: args.private ?? false,
|
|
52
|
+
auto_init: args.autoInit ?? false,
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
if (args.description) body.description = args.description;
|
|
56
|
+
|
|
57
|
+
const response = await fetch('https://api.github.com/user/repos', {
|
|
58
|
+
method: 'POST',
|
|
59
|
+
headers: {
|
|
60
|
+
Authorization: `Bearer ${accessToken}`,
|
|
61
|
+
Accept: 'application/vnd.github+json',
|
|
62
|
+
'X-GitHub-Api-Version': '2022-11-28',
|
|
63
|
+
'Content-Type': 'application/json',
|
|
64
|
+
},
|
|
65
|
+
body: JSON.stringify(body),
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
if (response.status === 401 || response.status === 403) {
|
|
69
|
+
this.logger.warn(`GitHub API returned ${response.status} for user ${ctx.userId}`);
|
|
70
|
+
return {
|
|
71
|
+
data: {
|
|
72
|
+
error: '401',
|
|
73
|
+
message: 'GitHub token was rejected. Please re-authenticate.',
|
|
74
|
+
},
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (!response.ok) {
|
|
79
|
+
const errorBody = await response.text();
|
|
80
|
+
this.logger.error(`GitHub API error: ${response.status} ${errorBody}`);
|
|
81
|
+
return {
|
|
82
|
+
data: {
|
|
83
|
+
error: 'api_error',
|
|
84
|
+
message: `GitHub API error: ${response.statusText}`,
|
|
85
|
+
},
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const repo = (await response.json()) as {
|
|
90
|
+
id: number;
|
|
91
|
+
full_name: string;
|
|
92
|
+
name: string;
|
|
93
|
+
html_url: string;
|
|
94
|
+
private: boolean;
|
|
95
|
+
default_branch: string;
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
data: {
|
|
100
|
+
repo: {
|
|
101
|
+
id: repo.id,
|
|
102
|
+
fullName: repo.full_name,
|
|
103
|
+
name: repo.name,
|
|
104
|
+
htmlUrl: repo.html_url,
|
|
105
|
+
private: repo.private,
|
|
106
|
+
defaultBranch: repo.default_branch,
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
import { Inject, Logger } from '@nestjs/common';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { Input, RunContext, Tool, ToolInterface, ToolResult } from '@loopstack/common';
|
|
4
|
+
import { OAuthTokenStore } from '@loopstack/oauth-module';
|
|
5
|
+
|
|
6
|
+
export type GitHubGetRepoArgs = {
|
|
7
|
+
owner: string;
|
|
8
|
+
repo: string;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
@Tool({
|
|
12
|
+
config: {
|
|
13
|
+
description:
|
|
14
|
+
'Gets detailed information about a specific GitHub repository. Returns { error: "unauthorized" } if no valid token is available.',
|
|
15
|
+
},
|
|
16
|
+
})
|
|
17
|
+
export class GitHubGetRepoTool implements ToolInterface {
|
|
18
|
+
private readonly logger = new Logger(GitHubGetRepoTool.name);
|
|
19
|
+
|
|
20
|
+
@Inject()
|
|
21
|
+
private tokenStore: OAuthTokenStore;
|
|
22
|
+
|
|
23
|
+
@Input({
|
|
24
|
+
schema: z
|
|
25
|
+
.object({
|
|
26
|
+
owner: z.string(),
|
|
27
|
+
repo: z.string(),
|
|
28
|
+
})
|
|
29
|
+
.strict(),
|
|
30
|
+
})
|
|
31
|
+
args: GitHubGetRepoArgs;
|
|
32
|
+
|
|
33
|
+
async execute(args: GitHubGetRepoArgs, ctx: RunContext): Promise<ToolResult> {
|
|
34
|
+
const accessToken = await this.tokenStore.getValidAccessToken(ctx.userId, 'github');
|
|
35
|
+
|
|
36
|
+
if (!accessToken) {
|
|
37
|
+
return {
|
|
38
|
+
data: {
|
|
39
|
+
error: 'unauthorized',
|
|
40
|
+
message: 'No valid GitHub token found. Please authenticate first.',
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const response = await fetch(
|
|
46
|
+
`https://api.github.com/repos/${encodeURIComponent(args.owner)}/${encodeURIComponent(args.repo)}`,
|
|
47
|
+
{
|
|
48
|
+
headers: {
|
|
49
|
+
Authorization: `Bearer ${accessToken}`,
|
|
50
|
+
Accept: 'application/vnd.github+json',
|
|
51
|
+
'X-GitHub-Api-Version': '2022-11-28',
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
if (response.status === 401 || response.status === 403) {
|
|
57
|
+
this.logger.warn(`GitHub API returned ${response.status} for user ${ctx.userId}`);
|
|
58
|
+
return {
|
|
59
|
+
data: {
|
|
60
|
+
error: '401',
|
|
61
|
+
message: 'GitHub token was rejected. Please re-authenticate.',
|
|
62
|
+
},
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (!response.ok) {
|
|
67
|
+
const body = await response.text();
|
|
68
|
+
this.logger.error(`GitHub API error: ${response.status} ${body}`);
|
|
69
|
+
return {
|
|
70
|
+
data: {
|
|
71
|
+
error: 'api_error',
|
|
72
|
+
message: `GitHub API error: ${response.statusText}`,
|
|
73
|
+
},
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const repo = (await response.json()) as {
|
|
78
|
+
id: number;
|
|
79
|
+
full_name: string;
|
|
80
|
+
name: string;
|
|
81
|
+
owner: { login: string; avatar_url: string };
|
|
82
|
+
private: boolean;
|
|
83
|
+
html_url: string;
|
|
84
|
+
description: string | null;
|
|
85
|
+
language: string | null;
|
|
86
|
+
default_branch: string;
|
|
87
|
+
stargazers_count: number;
|
|
88
|
+
forks_count: number;
|
|
89
|
+
open_issues_count: number;
|
|
90
|
+
created_at: string;
|
|
91
|
+
updated_at: string;
|
|
92
|
+
topics: string[];
|
|
93
|
+
license: { spdx_id: string } | null;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
return {
|
|
97
|
+
data: {
|
|
98
|
+
repo: {
|
|
99
|
+
id: repo.id,
|
|
100
|
+
fullName: repo.full_name,
|
|
101
|
+
name: repo.name,
|
|
102
|
+
owner: repo.owner.login,
|
|
103
|
+
ownerAvatar: repo.owner.avatar_url,
|
|
104
|
+
private: repo.private,
|
|
105
|
+
htmlUrl: repo.html_url,
|
|
106
|
+
description: repo.description,
|
|
107
|
+
language: repo.language,
|
|
108
|
+
defaultBranch: repo.default_branch,
|
|
109
|
+
stars: repo.stargazers_count,
|
|
110
|
+
forks: repo.forks_count,
|
|
111
|
+
openIssues: repo.open_issues_count,
|
|
112
|
+
createdAt: repo.created_at,
|
|
113
|
+
updatedAt: repo.updated_at,
|
|
114
|
+
topics: repo.topics,
|
|
115
|
+
license: repo.license?.spdx_id ?? null,
|
|
116
|
+
},
|
|
117
|
+
},
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { Inject, Logger } from '@nestjs/common';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { Input, RunContext, Tool, ToolInterface, ToolResult } from '@loopstack/common';
|
|
4
|
+
import { OAuthTokenStore } from '@loopstack/oauth-module';
|
|
5
|
+
|
|
6
|
+
export type GitHubListBranchesArgs = {
|
|
7
|
+
owner: string;
|
|
8
|
+
repo: string;
|
|
9
|
+
perPage?: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
@Tool({
|
|
13
|
+
config: {
|
|
14
|
+
description:
|
|
15
|
+
'Lists branches for a GitHub repository. Returns { error: "unauthorized" } if no valid token is available.',
|
|
16
|
+
},
|
|
17
|
+
})
|
|
18
|
+
export class GitHubListBranchesTool implements ToolInterface {
|
|
19
|
+
private readonly logger = new Logger(GitHubListBranchesTool.name);
|
|
20
|
+
|
|
21
|
+
@Inject()
|
|
22
|
+
private tokenStore: OAuthTokenStore;
|
|
23
|
+
|
|
24
|
+
@Input({
|
|
25
|
+
schema: z
|
|
26
|
+
.object({
|
|
27
|
+
owner: z.string(),
|
|
28
|
+
repo: z.string(),
|
|
29
|
+
perPage: z.number().default(30),
|
|
30
|
+
})
|
|
31
|
+
.strict(),
|
|
32
|
+
})
|
|
33
|
+
args: GitHubListBranchesArgs;
|
|
34
|
+
|
|
35
|
+
async execute(args: GitHubListBranchesArgs, ctx: RunContext): Promise<ToolResult> {
|
|
36
|
+
const accessToken = await this.tokenStore.getValidAccessToken(ctx.userId, 'github');
|
|
37
|
+
|
|
38
|
+
if (!accessToken) {
|
|
39
|
+
return {
|
|
40
|
+
data: {
|
|
41
|
+
error: 'unauthorized',
|
|
42
|
+
message: 'No valid GitHub token found. Please authenticate first.',
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const params = new URLSearchParams({
|
|
48
|
+
per_page: String(args.perPage ?? 30),
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const url = `https://api.github.com/repos/${encodeURIComponent(args.owner)}/${encodeURIComponent(args.repo)}/branches?${params.toString()}`;
|
|
52
|
+
const response = await fetch(url, {
|
|
53
|
+
headers: {
|
|
54
|
+
Authorization: `Bearer ${accessToken}`,
|
|
55
|
+
Accept: 'application/vnd.github+json',
|
|
56
|
+
'X-GitHub-Api-Version': '2022-11-28',
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
if (response.status === 401 || response.status === 403) {
|
|
61
|
+
this.logger.warn(`GitHub API returned ${response.status} for user ${ctx.userId}`);
|
|
62
|
+
return {
|
|
63
|
+
data: {
|
|
64
|
+
error: '401',
|
|
65
|
+
message: 'GitHub token was rejected. Please re-authenticate.',
|
|
66
|
+
},
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (!response.ok) {
|
|
71
|
+
const body = await response.text();
|
|
72
|
+
this.logger.error(`GitHub API error: ${response.status} ${body}`);
|
|
73
|
+
return {
|
|
74
|
+
data: {
|
|
75
|
+
error: 'api_error',
|
|
76
|
+
message: `GitHub API error: ${response.statusText}`,
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const data = (await response.json()) as Array<{
|
|
82
|
+
name: string;
|
|
83
|
+
commit: { sha: string };
|
|
84
|
+
protected: boolean;
|
|
85
|
+
}>;
|
|
86
|
+
|
|
87
|
+
const branches = data.map((branch) => ({
|
|
88
|
+
name: branch.name,
|
|
89
|
+
commitSha: branch.commit.sha,
|
|
90
|
+
protected: branch['protected'],
|
|
91
|
+
}));
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
data: { branches },
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { Inject, Logger } from '@nestjs/common';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { Input, RunContext, Tool, ToolInterface, ToolResult } from '@loopstack/common';
|
|
4
|
+
import { OAuthTokenStore } from '@loopstack/oauth-module';
|
|
5
|
+
|
|
6
|
+
export type GitHubListReposArgs = {
|
|
7
|
+
visibility?: string;
|
|
8
|
+
sort?: string;
|
|
9
|
+
perPage?: number;
|
|
10
|
+
page?: number;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
@Tool({
|
|
14
|
+
config: {
|
|
15
|
+
description:
|
|
16
|
+
'Lists repositories for the authenticated GitHub user. Returns { error: "unauthorized" } if no valid token is available.',
|
|
17
|
+
},
|
|
18
|
+
})
|
|
19
|
+
export class GitHubListReposTool implements ToolInterface {
|
|
20
|
+
private readonly logger = new Logger(GitHubListReposTool.name);
|
|
21
|
+
|
|
22
|
+
@Inject()
|
|
23
|
+
private tokenStore: OAuthTokenStore;
|
|
24
|
+
|
|
25
|
+
@Input({
|
|
26
|
+
schema: z
|
|
27
|
+
.object({
|
|
28
|
+
visibility: z.enum(['all', 'public', 'private']).default('all'),
|
|
29
|
+
sort: z.enum(['created', 'updated', 'pushed', 'full_name']).default('updated'),
|
|
30
|
+
perPage: z.number().default(30),
|
|
31
|
+
page: z.number().default(1),
|
|
32
|
+
})
|
|
33
|
+
.strict(),
|
|
34
|
+
})
|
|
35
|
+
args: GitHubListReposArgs;
|
|
36
|
+
|
|
37
|
+
async execute(args: GitHubListReposArgs, ctx: RunContext): Promise<ToolResult> {
|
|
38
|
+
const accessToken = await this.tokenStore.getValidAccessToken(ctx.userId, 'github');
|
|
39
|
+
|
|
40
|
+
if (!accessToken) {
|
|
41
|
+
return {
|
|
42
|
+
data: {
|
|
43
|
+
error: 'unauthorized',
|
|
44
|
+
message: 'No valid GitHub token found. Please authenticate first.',
|
|
45
|
+
},
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const params = new URLSearchParams({
|
|
50
|
+
visibility: args.visibility ?? 'all',
|
|
51
|
+
sort: args.sort ?? 'updated',
|
|
52
|
+
per_page: String(args.perPage ?? 30),
|
|
53
|
+
page: String(args.page ?? 1),
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const response = await fetch(`https://api.github.com/user/repos?${params.toString()}`, {
|
|
57
|
+
headers: {
|
|
58
|
+
Authorization: `Bearer ${accessToken}`,
|
|
59
|
+
Accept: 'application/vnd.github+json',
|
|
60
|
+
'X-GitHub-Api-Version': '2022-11-28',
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
if (response.status === 401 || response.status === 403) {
|
|
65
|
+
this.logger.warn(`GitHub API returned ${response.status} for user ${ctx.userId}`);
|
|
66
|
+
return {
|
|
67
|
+
data: {
|
|
68
|
+
error: '401',
|
|
69
|
+
message: 'GitHub token was rejected. Please re-authenticate.',
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (!response.ok) {
|
|
75
|
+
const body = await response.text();
|
|
76
|
+
this.logger.error(`GitHub API error: ${response.status} ${body}`);
|
|
77
|
+
return {
|
|
78
|
+
data: {
|
|
79
|
+
error: 'api_error',
|
|
80
|
+
message: `GitHub API error: ${response.statusText}`,
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const data = (await response.json()) as Array<{
|
|
86
|
+
id: number;
|
|
87
|
+
full_name: string;
|
|
88
|
+
name: string;
|
|
89
|
+
owner: { login: string };
|
|
90
|
+
private: boolean;
|
|
91
|
+
html_url: string;
|
|
92
|
+
description: string | null;
|
|
93
|
+
language: string | null;
|
|
94
|
+
default_branch: string;
|
|
95
|
+
updated_at: string;
|
|
96
|
+
}>;
|
|
97
|
+
|
|
98
|
+
const repos = data.map((repo) => ({
|
|
99
|
+
id: repo.id,
|
|
100
|
+
fullName: repo.full_name,
|
|
101
|
+
name: repo.name,
|
|
102
|
+
owner: repo.owner.login,
|
|
103
|
+
private: repo.private,
|
|
104
|
+
htmlUrl: repo.html_url,
|
|
105
|
+
description: repo.description,
|
|
106
|
+
language: repo.language,
|
|
107
|
+
defaultBranch: repo.default_branch,
|
|
108
|
+
updatedAt: repo.updated_at,
|
|
109
|
+
}));
|
|
110
|
+
|
|
111
|
+
return {
|
|
112
|
+
data: { repos },
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { Inject, Logger } from '@nestjs/common';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import { Input, RunContext, Tool, ToolInterface, ToolResult } from '@loopstack/common';
|
|
4
|
+
import { OAuthTokenStore } from '@loopstack/oauth-module';
|
|
5
|
+
|
|
6
|
+
export type GitHubSearchCodeArgs = {
|
|
7
|
+
query: string;
|
|
8
|
+
perPage?: number;
|
|
9
|
+
page?: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
@Tool({
|
|
13
|
+
config: {
|
|
14
|
+
description:
|
|
15
|
+
'Searches for code across GitHub repositories using the GitHub search syntax. Returns { error: "unauthorized" } if no valid token is available.',
|
|
16
|
+
},
|
|
17
|
+
})
|
|
18
|
+
export class GitHubSearchCodeTool implements ToolInterface {
|
|
19
|
+
private readonly logger = new Logger(GitHubSearchCodeTool.name);
|
|
20
|
+
|
|
21
|
+
@Inject()
|
|
22
|
+
private tokenStore: OAuthTokenStore;
|
|
23
|
+
|
|
24
|
+
@Input({
|
|
25
|
+
schema: z
|
|
26
|
+
.object({
|
|
27
|
+
query: z.string(),
|
|
28
|
+
perPage: z.number().default(30),
|
|
29
|
+
page: z.number().default(1),
|
|
30
|
+
})
|
|
31
|
+
.strict(),
|
|
32
|
+
})
|
|
33
|
+
args: GitHubSearchCodeArgs;
|
|
34
|
+
|
|
35
|
+
async execute(args: GitHubSearchCodeArgs, ctx: RunContext): Promise<ToolResult> {
|
|
36
|
+
const accessToken = await this.tokenStore.getValidAccessToken(ctx.userId, 'github');
|
|
37
|
+
|
|
38
|
+
if (!accessToken) {
|
|
39
|
+
return {
|
|
40
|
+
data: {
|
|
41
|
+
error: 'unauthorized',
|
|
42
|
+
message: 'No valid GitHub token found. Please authenticate first.',
|
|
43
|
+
},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const params = new URLSearchParams({
|
|
48
|
+
q: args.query,
|
|
49
|
+
per_page: String(args.perPage ?? 30),
|
|
50
|
+
page: String(args.page ?? 1),
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const response = await fetch(`https://api.github.com/search/code?${params.toString()}`, {
|
|
54
|
+
headers: {
|
|
55
|
+
Authorization: `Bearer ${accessToken}`,
|
|
56
|
+
Accept: 'application/vnd.github+json',
|
|
57
|
+
'X-GitHub-Api-Version': '2022-11-28',
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
if (response.status === 401 || response.status === 403) {
|
|
62
|
+
this.logger.warn(`GitHub API returned ${response.status} for user ${ctx.userId}`);
|
|
63
|
+
return {
|
|
64
|
+
data: {
|
|
65
|
+
error: '401',
|
|
66
|
+
message: 'GitHub token was rejected. Please re-authenticate.',
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
if (!response.ok) {
|
|
72
|
+
const body = await response.text();
|
|
73
|
+
this.logger.error(`GitHub API error: ${response.status} ${body}`);
|
|
74
|
+
return {
|
|
75
|
+
data: {
|
|
76
|
+
error: 'api_error',
|
|
77
|
+
message: `GitHub API error: ${response.statusText}`,
|
|
78
|
+
},
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
const data = (await response.json()) as {
|
|
83
|
+
total_count: number;
|
|
84
|
+
items: Array<{
|
|
85
|
+
name: string;
|
|
86
|
+
path: string;
|
|
87
|
+
sha: string;
|
|
88
|
+
html_url: string;
|
|
89
|
+
repository: { full_name: string };
|
|
90
|
+
}>;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const results = data.items.map((item) => ({
|
|
94
|
+
name: item.name,
|
|
95
|
+
path: item.path,
|
|
96
|
+
sha: item.sha,
|
|
97
|
+
htmlUrl: item.html_url,
|
|
98
|
+
repository: item.repository.full_name,
|
|
99
|
+
}));
|
|
100
|
+
|
|
101
|
+
return {
|
|
102
|
+
data: { totalCount: data.total_count, results },
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
}
|