@loopstack/github-module 0.2.2 → 0.2.4
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 +1 -1
- package/package.json +7 -14
- package/src/github-oauth.provider.ts +0 -85
- package/src/github.module.ts +0 -63
- package/src/index.ts +0 -27
- package/src/tools/actions/github-get-workflow-run.tool.ts +0 -109
- package/src/tools/actions/github-list-workflow-runs.tool.ts +0 -132
- package/src/tools/actions/github-trigger-workflow.tool.ts +0 -89
- package/src/tools/content/github-create-or-update-file.tool.ts +0 -114
- package/src/tools/content/github-get-commit.tool.ts +0 -118
- package/src/tools/content/github-get-file-content.tool.ts +0 -105
- package/src/tools/content/github-list-directory.tool.ts +0 -98
- package/src/tools/issues/github-create-issue-comment.tool.ts +0 -93
- package/src/tools/issues/github-create-issue.tool.ts +0 -105
- package/src/tools/issues/github-get-issue.tool.ts +0 -109
- package/src/tools/issues/github-list-issues.tool.ts +0 -116
- package/src/tools/pull-requests/github-create-pull-request.tool.ts +0 -109
- package/src/tools/pull-requests/github-get-pull-request.tool.ts +0 -122
- package/src/tools/pull-requests/github-list-pr-reviews.tool.ts +0 -93
- package/src/tools/pull-requests/github-list-pull-requests.tool.ts +0 -115
- package/src/tools/pull-requests/github-merge-pull-request.tool.ts +0 -99
- package/src/tools/repos/github-create-repo.tool.ts +0 -104
- package/src/tools/repos/github-get-repo.tool.ts +0 -115
- package/src/tools/repos/github-list-branches.tool.ts +0 -91
- package/src/tools/repos/github-list-repos.tool.ts +0 -108
- package/src/tools/search/github-search-code.tool.ts +0 -99
- package/src/tools/search/github-search-issues.tool.ts +0 -113
- package/src/tools/search/github-search-repos.tool.ts +0 -108
- package/src/tools/users/github-get-authenticated-user.tool.ts +0 -96
- package/src/tools/users/github-list-user-orgs.tool.ts +0 -90
package/README.md
CHANGED
package/package.json
CHANGED
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
"module",
|
|
10
10
|
"oauth"
|
|
11
11
|
],
|
|
12
|
-
"version": "0.2.
|
|
13
|
-
"license": "
|
|
12
|
+
"version": "0.2.4",
|
|
13
|
+
"license": "MIT",
|
|
14
14
|
"author": {
|
|
15
15
|
"name": "Jakob Klippel",
|
|
16
16
|
"url": "https://www.linkedin.com/in/jakob-klippel/"
|
|
@@ -18,8 +18,7 @@
|
|
|
18
18
|
"main": "dist/index.js",
|
|
19
19
|
"types": "dist/index.d.ts",
|
|
20
20
|
"exports": {
|
|
21
|
-
".": "./dist/index.js"
|
|
22
|
-
"./src/*": "./src/*"
|
|
21
|
+
".": "./dist/index.js"
|
|
23
22
|
},
|
|
24
23
|
"scripts": {
|
|
25
24
|
"build": "nest build",
|
|
@@ -30,12 +29,12 @@
|
|
|
30
29
|
"watch": "nest build --watch"
|
|
31
30
|
},
|
|
32
31
|
"dependencies": {
|
|
33
|
-
"@loopstack/
|
|
32
|
+
"@loopstack/common": "^0.28.0",
|
|
33
|
+
"@loopstack/oauth-module": "^0.2.6",
|
|
34
34
|
"zod": "^4.3.6"
|
|
35
35
|
},
|
|
36
36
|
"files": [
|
|
37
|
-
"dist"
|
|
38
|
-
"src"
|
|
37
|
+
"dist"
|
|
39
38
|
],
|
|
40
39
|
"jest": {
|
|
41
40
|
"moduleFileExtensions": [
|
|
@@ -46,12 +45,7 @@
|
|
|
46
45
|
"rootDir": "src",
|
|
47
46
|
"testRegex": ".*\\.spec\\.ts$",
|
|
48
47
|
"transform": {
|
|
49
|
-
"^.+\\.ts$":
|
|
50
|
-
"ts-jest",
|
|
51
|
-
{
|
|
52
|
-
"tsconfig": "tsconfig.spec.json"
|
|
53
|
-
}
|
|
54
|
-
]
|
|
48
|
+
"^.+\\.ts$": "ts-jest"
|
|
55
49
|
},
|
|
56
50
|
"collectCoverageFrom": [
|
|
57
51
|
"**/*.(t|j)s"
|
|
@@ -79,7 +73,6 @@
|
|
|
79
73
|
}
|
|
80
74
|
],
|
|
81
75
|
"installModes": [
|
|
82
|
-
"add",
|
|
83
76
|
"install"
|
|
84
77
|
]
|
|
85
78
|
}
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { Injectable, Logger, OnModuleInit } from '@nestjs/common';
|
|
2
|
-
import { ConfigService } from '@nestjs/config';
|
|
3
|
-
import { OAuthProviderInterface, OAuthProviderRegistry, OAuthTokenSet } from '@loopstack/oauth-module';
|
|
4
|
-
|
|
5
|
-
@Injectable()
|
|
6
|
-
export class GitHubOAuthProvider implements OAuthProviderInterface, OnModuleInit {
|
|
7
|
-
private readonly logger = new Logger(GitHubOAuthProvider.name);
|
|
8
|
-
|
|
9
|
-
readonly providerId = 'github';
|
|
10
|
-
readonly defaultScopes = ['repo', 'user', 'workflow', 'read:org'];
|
|
11
|
-
|
|
12
|
-
constructor(
|
|
13
|
-
private readonly configService: ConfigService,
|
|
14
|
-
private readonly providerRegistry: OAuthProviderRegistry,
|
|
15
|
-
) {}
|
|
16
|
-
|
|
17
|
-
onModuleInit(): void {
|
|
18
|
-
this.providerRegistry.register(this);
|
|
19
|
-
this.logger.log('GitHub OAuth provider registered');
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
private get clientId(): string {
|
|
23
|
-
const id = this.configService.get<string>('GITHUB_CLIENT_ID');
|
|
24
|
-
if (!id) throw new Error('GITHUB_CLIENT_ID is not configured');
|
|
25
|
-
return id;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
private get clientSecret(): string {
|
|
29
|
-
const secret = this.configService.get<string>('GITHUB_CLIENT_SECRET');
|
|
30
|
-
if (!secret) throw new Error('GITHUB_CLIENT_SECRET is not configured');
|
|
31
|
-
return secret;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
private get redirectUri(): string {
|
|
35
|
-
return this.configService.get<string>('GITHUB_OAUTH_REDIRECT_URI', '/oauth/callback');
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
buildAuthUrl(scopes: string[], state: string): string {
|
|
39
|
-
const params = new URLSearchParams({
|
|
40
|
-
client_id: this.clientId,
|
|
41
|
-
redirect_uri: this.redirectUri,
|
|
42
|
-
scope: scopes.join(' '),
|
|
43
|
-
state,
|
|
44
|
-
});
|
|
45
|
-
return `https://github.com/login/oauth/authorize?${params.toString()}`;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
async exchangeCode(code: string): Promise<OAuthTokenSet> {
|
|
49
|
-
const response = await fetch('https://github.com/login/oauth/access_token', {
|
|
50
|
-
method: 'POST',
|
|
51
|
-
headers: {
|
|
52
|
-
'Content-Type': 'application/x-www-form-urlencoded',
|
|
53
|
-
Accept: 'application/json',
|
|
54
|
-
},
|
|
55
|
-
body: new URLSearchParams({
|
|
56
|
-
code,
|
|
57
|
-
client_id: this.clientId,
|
|
58
|
-
client_secret: this.clientSecret,
|
|
59
|
-
redirect_uri: this.redirectUri,
|
|
60
|
-
}),
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
if (!response.ok) {
|
|
64
|
-
const errorBody = await response.text();
|
|
65
|
-
this.logger.error(`Token exchange failed: ${response.status} ${errorBody}`);
|
|
66
|
-
throw new Error(`GitHub token exchange failed: ${response.statusText}`);
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const tokens = (await response.json()) as {
|
|
70
|
-
access_token: string;
|
|
71
|
-
token_type: string;
|
|
72
|
-
scope: string;
|
|
73
|
-
};
|
|
74
|
-
|
|
75
|
-
return {
|
|
76
|
-
accessToken: tokens.access_token,
|
|
77
|
-
expiresIn: 315360000,
|
|
78
|
-
scope: tokens.scope,
|
|
79
|
-
};
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
refreshToken(): Promise<OAuthTokenSet> {
|
|
83
|
-
throw new Error('GitHub tokens do not expire and cannot be refreshed.');
|
|
84
|
-
}
|
|
85
|
-
}
|
package/src/github.module.ts
DELETED
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { Module } from '@nestjs/common';
|
|
2
|
-
import { OAuthModule } from '@loopstack/oauth-module';
|
|
3
|
-
import { GitHubOAuthProvider } from './github-oauth.provider';
|
|
4
|
-
import { GitHubGetWorkflowRunTool } from './tools/actions/github-get-workflow-run.tool';
|
|
5
|
-
import { GitHubListWorkflowRunsTool } from './tools/actions/github-list-workflow-runs.tool';
|
|
6
|
-
import { GitHubTriggerWorkflowTool } from './tools/actions/github-trigger-workflow.tool';
|
|
7
|
-
import { GitHubCreateOrUpdateFileTool } from './tools/content/github-create-or-update-file.tool';
|
|
8
|
-
import { GitHubGetCommitTool } from './tools/content/github-get-commit.tool';
|
|
9
|
-
import { GitHubGetFileContentTool } from './tools/content/github-get-file-content.tool';
|
|
10
|
-
import { GitHubListDirectoryTool } from './tools/content/github-list-directory.tool';
|
|
11
|
-
import { GitHubCreateIssueCommentTool } from './tools/issues/github-create-issue-comment.tool';
|
|
12
|
-
import { GitHubCreateIssueTool } from './tools/issues/github-create-issue.tool';
|
|
13
|
-
import { GitHubGetIssueTool } from './tools/issues/github-get-issue.tool';
|
|
14
|
-
import { GitHubListIssuesTool } from './tools/issues/github-list-issues.tool';
|
|
15
|
-
import { GitHubCreatePullRequestTool } from './tools/pull-requests/github-create-pull-request.tool';
|
|
16
|
-
import { GitHubGetPullRequestTool } from './tools/pull-requests/github-get-pull-request.tool';
|
|
17
|
-
import { GitHubListPrReviewsTool } from './tools/pull-requests/github-list-pr-reviews.tool';
|
|
18
|
-
import { GitHubListPullRequestsTool } from './tools/pull-requests/github-list-pull-requests.tool';
|
|
19
|
-
import { GitHubMergePullRequestTool } from './tools/pull-requests/github-merge-pull-request.tool';
|
|
20
|
-
import { GitHubCreateRepoTool } from './tools/repos/github-create-repo.tool';
|
|
21
|
-
import { GitHubGetRepoTool } from './tools/repos/github-get-repo.tool';
|
|
22
|
-
import { GitHubListBranchesTool } from './tools/repos/github-list-branches.tool';
|
|
23
|
-
import { GitHubListReposTool } from './tools/repos/github-list-repos.tool';
|
|
24
|
-
import { GitHubSearchCodeTool } from './tools/search/github-search-code.tool';
|
|
25
|
-
import { GitHubSearchIssuesTool } from './tools/search/github-search-issues.tool';
|
|
26
|
-
import { GitHubSearchReposTool } from './tools/search/github-search-repos.tool';
|
|
27
|
-
import { GitHubGetAuthenticatedUserTool } from './tools/users/github-get-authenticated-user.tool';
|
|
28
|
-
import { GitHubListUserOrgsTool } from './tools/users/github-list-user-orgs.tool';
|
|
29
|
-
|
|
30
|
-
const tools = [
|
|
31
|
-
GitHubListReposTool,
|
|
32
|
-
GitHubGetRepoTool,
|
|
33
|
-
GitHubCreateRepoTool,
|
|
34
|
-
GitHubListBranchesTool,
|
|
35
|
-
GitHubListIssuesTool,
|
|
36
|
-
GitHubGetIssueTool,
|
|
37
|
-
GitHubCreateIssueTool,
|
|
38
|
-
GitHubCreateIssueCommentTool,
|
|
39
|
-
GitHubListPullRequestsTool,
|
|
40
|
-
GitHubGetPullRequestTool,
|
|
41
|
-
GitHubCreatePullRequestTool,
|
|
42
|
-
GitHubMergePullRequestTool,
|
|
43
|
-
GitHubListPrReviewsTool,
|
|
44
|
-
GitHubGetFileContentTool,
|
|
45
|
-
GitHubCreateOrUpdateFileTool,
|
|
46
|
-
GitHubListDirectoryTool,
|
|
47
|
-
GitHubGetCommitTool,
|
|
48
|
-
GitHubListWorkflowRunsTool,
|
|
49
|
-
GitHubTriggerWorkflowTool,
|
|
50
|
-
GitHubGetWorkflowRunTool,
|
|
51
|
-
GitHubSearchCodeTool,
|
|
52
|
-
GitHubSearchReposTool,
|
|
53
|
-
GitHubSearchIssuesTool,
|
|
54
|
-
GitHubGetAuthenticatedUserTool,
|
|
55
|
-
GitHubListUserOrgsTool,
|
|
56
|
-
];
|
|
57
|
-
|
|
58
|
-
@Module({
|
|
59
|
-
imports: [OAuthModule],
|
|
60
|
-
providers: [GitHubOAuthProvider, ...tools],
|
|
61
|
-
exports: [GitHubOAuthProvider, ...tools],
|
|
62
|
-
})
|
|
63
|
-
export class GitHubModule {}
|
package/src/index.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
export * from './github.module';
|
|
2
|
-
export * from './github-oauth.provider';
|
|
3
|
-
export * from './tools/repos/github-list-repos.tool';
|
|
4
|
-
export * from './tools/repos/github-get-repo.tool';
|
|
5
|
-
export * from './tools/repos/github-create-repo.tool';
|
|
6
|
-
export * from './tools/repos/github-list-branches.tool';
|
|
7
|
-
export * from './tools/issues/github-list-issues.tool';
|
|
8
|
-
export * from './tools/issues/github-get-issue.tool';
|
|
9
|
-
export * from './tools/issues/github-create-issue.tool';
|
|
10
|
-
export * from './tools/issues/github-create-issue-comment.tool';
|
|
11
|
-
export * from './tools/pull-requests/github-list-pull-requests.tool';
|
|
12
|
-
export * from './tools/pull-requests/github-get-pull-request.tool';
|
|
13
|
-
export * from './tools/pull-requests/github-create-pull-request.tool';
|
|
14
|
-
export * from './tools/pull-requests/github-merge-pull-request.tool';
|
|
15
|
-
export * from './tools/pull-requests/github-list-pr-reviews.tool';
|
|
16
|
-
export * from './tools/content/github-get-file-content.tool';
|
|
17
|
-
export * from './tools/content/github-create-or-update-file.tool';
|
|
18
|
-
export * from './tools/content/github-list-directory.tool';
|
|
19
|
-
export * from './tools/content/github-get-commit.tool';
|
|
20
|
-
export * from './tools/actions/github-list-workflow-runs.tool';
|
|
21
|
-
export * from './tools/actions/github-trigger-workflow.tool';
|
|
22
|
-
export * from './tools/actions/github-get-workflow-run.tool';
|
|
23
|
-
export * from './tools/search/github-search-code.tool';
|
|
24
|
-
export * from './tools/search/github-search-repos.tool';
|
|
25
|
-
export * from './tools/search/github-search-issues.tool';
|
|
26
|
-
export * from './tools/users/github-get-authenticated-user.tool';
|
|
27
|
-
export * from './tools/users/github-list-user-orgs.tool';
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
import { Inject, Logger } from '@nestjs/common';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
import { BaseTool, Tool, ToolResult } from '@loopstack/common';
|
|
4
|
-
import { OAuthTokenStore } from '@loopstack/oauth-module';
|
|
5
|
-
|
|
6
|
-
const inputSchema = z
|
|
7
|
-
.object({
|
|
8
|
-
owner: z.string(),
|
|
9
|
-
repo: z.string(),
|
|
10
|
-
runId: z.number(),
|
|
11
|
-
})
|
|
12
|
-
.strict();
|
|
13
|
-
|
|
14
|
-
export type GitHubGetWorkflowRunArgs = z.infer<typeof inputSchema>;
|
|
15
|
-
|
|
16
|
-
@Tool({
|
|
17
|
-
uiConfig: {
|
|
18
|
-
description:
|
|
19
|
-
'Gets detailed information about a specific GitHub Actions workflow run. Returns { error: "unauthorized" } if no valid token is available.',
|
|
20
|
-
},
|
|
21
|
-
schema: inputSchema,
|
|
22
|
-
})
|
|
23
|
-
export class GitHubGetWorkflowRunTool extends BaseTool {
|
|
24
|
-
private readonly logger = new Logger(GitHubGetWorkflowRunTool.name);
|
|
25
|
-
|
|
26
|
-
@Inject()
|
|
27
|
-
private tokenStore: OAuthTokenStore;
|
|
28
|
-
|
|
29
|
-
async call(args: GitHubGetWorkflowRunArgs): Promise<ToolResult> {
|
|
30
|
-
const accessToken = await this.tokenStore.getValidAccessToken(this.ctx.context.userId, 'github');
|
|
31
|
-
|
|
32
|
-
if (!accessToken) {
|
|
33
|
-
return {
|
|
34
|
-
data: {
|
|
35
|
-
error: 'unauthorized',
|
|
36
|
-
message: 'No valid GitHub token found. Please authenticate first.',
|
|
37
|
-
},
|
|
38
|
-
};
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const url = `https://api.github.com/repos/${encodeURIComponent(args.owner)}/${encodeURIComponent(args.repo)}/actions/runs/${args.runId}`;
|
|
42
|
-
const response = await fetch(url, {
|
|
43
|
-
headers: {
|
|
44
|
-
Authorization: `Bearer ${accessToken}`,
|
|
45
|
-
Accept: 'application/vnd.github+json',
|
|
46
|
-
'X-GitHub-Api-Version': '2022-11-28',
|
|
47
|
-
},
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
if (response.status === 401 || response.status === 403) {
|
|
51
|
-
this.logger.warn(`GitHub API returned ${response.status} for user ${this.ctx.context.userId}`);
|
|
52
|
-
return {
|
|
53
|
-
data: {
|
|
54
|
-
error: '401',
|
|
55
|
-
message: 'GitHub token was rejected. Please re-authenticate.',
|
|
56
|
-
},
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
if (!response.ok) {
|
|
61
|
-
const body = await response.text();
|
|
62
|
-
this.logger.error(`GitHub API error: ${response.status} ${body}`);
|
|
63
|
-
return {
|
|
64
|
-
data: {
|
|
65
|
-
error: 'api_error',
|
|
66
|
-
message: `GitHub API error: ${response.statusText}`,
|
|
67
|
-
},
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const run = (await response.json()) as {
|
|
72
|
-
id: number;
|
|
73
|
-
name: string;
|
|
74
|
-
status: string;
|
|
75
|
-
conclusion: string | null;
|
|
76
|
-
head_branch: string;
|
|
77
|
-
head_sha: string;
|
|
78
|
-
event: string;
|
|
79
|
-
workflow_id: number;
|
|
80
|
-
run_number: number;
|
|
81
|
-
run_attempt: number;
|
|
82
|
-
created_at: string;
|
|
83
|
-
updated_at: string;
|
|
84
|
-
run_started_at: string;
|
|
85
|
-
html_url: string;
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
return {
|
|
89
|
-
data: {
|
|
90
|
-
run: {
|
|
91
|
-
id: run.id,
|
|
92
|
-
name: run.name,
|
|
93
|
-
status: run.status,
|
|
94
|
-
conclusion: run.conclusion,
|
|
95
|
-
headBranch: run.head_branch,
|
|
96
|
-
headSha: run.head_sha,
|
|
97
|
-
event: run.event,
|
|
98
|
-
workflowId: run.workflow_id,
|
|
99
|
-
runNumber: run.run_number,
|
|
100
|
-
runAttempt: run.run_attempt,
|
|
101
|
-
createdAt: run.created_at,
|
|
102
|
-
updatedAt: run.updated_at,
|
|
103
|
-
runStartedAt: run.run_started_at,
|
|
104
|
-
htmlUrl: run.html_url,
|
|
105
|
-
},
|
|
106
|
-
},
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
}
|
|
@@ -1,132 +0,0 @@
|
|
|
1
|
-
import { Inject, Logger } from '@nestjs/common';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
import { BaseTool, Tool, ToolResult } from '@loopstack/common';
|
|
4
|
-
import { OAuthTokenStore } from '@loopstack/oauth-module';
|
|
5
|
-
|
|
6
|
-
const inputSchema = z
|
|
7
|
-
.object({
|
|
8
|
-
owner: z.string(),
|
|
9
|
-
repo: z.string(),
|
|
10
|
-
branch: z.string().optional(),
|
|
11
|
-
status: z
|
|
12
|
-
.enum([
|
|
13
|
-
'completed',
|
|
14
|
-
'action_required',
|
|
15
|
-
'cancelled',
|
|
16
|
-
'failure',
|
|
17
|
-
'neutral',
|
|
18
|
-
'skipped',
|
|
19
|
-
'stale',
|
|
20
|
-
'success',
|
|
21
|
-
'timed_out',
|
|
22
|
-
'in_progress',
|
|
23
|
-
'queued',
|
|
24
|
-
'requested',
|
|
25
|
-
'waiting',
|
|
26
|
-
'pending',
|
|
27
|
-
])
|
|
28
|
-
.optional(),
|
|
29
|
-
perPage: z.number().default(30),
|
|
30
|
-
page: z.number().default(1),
|
|
31
|
-
})
|
|
32
|
-
.strict();
|
|
33
|
-
|
|
34
|
-
export type GitHubListWorkflowRunsArgs = z.input<typeof inputSchema>;
|
|
35
|
-
|
|
36
|
-
@Tool({
|
|
37
|
-
uiConfig: {
|
|
38
|
-
description:
|
|
39
|
-
'Lists workflow runs for a GitHub repository. Returns { error: "unauthorized" } if no valid token is available.',
|
|
40
|
-
},
|
|
41
|
-
schema: inputSchema,
|
|
42
|
-
})
|
|
43
|
-
export class GitHubListWorkflowRunsTool extends BaseTool {
|
|
44
|
-
private readonly logger = new Logger(GitHubListWorkflowRunsTool.name);
|
|
45
|
-
|
|
46
|
-
@Inject()
|
|
47
|
-
private tokenStore: OAuthTokenStore;
|
|
48
|
-
|
|
49
|
-
async call(args: GitHubListWorkflowRunsArgs): Promise<ToolResult> {
|
|
50
|
-
const accessToken = await this.tokenStore.getValidAccessToken(this.ctx.context.userId, 'github');
|
|
51
|
-
|
|
52
|
-
if (!accessToken) {
|
|
53
|
-
return {
|
|
54
|
-
data: {
|
|
55
|
-
error: 'unauthorized',
|
|
56
|
-
message: 'No valid GitHub token found. Please authenticate first.',
|
|
57
|
-
},
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const params = new URLSearchParams({
|
|
62
|
-
per_page: String(args.perPage ?? 30),
|
|
63
|
-
page: String(args.page ?? 1),
|
|
64
|
-
});
|
|
65
|
-
|
|
66
|
-
if (args.branch) params.set('branch', args.branch);
|
|
67
|
-
if (args.status) params.set('status', args.status);
|
|
68
|
-
|
|
69
|
-
const url = `https://api.github.com/repos/${encodeURIComponent(args.owner)}/${encodeURIComponent(args.repo)}/actions/runs?${params.toString()}`;
|
|
70
|
-
const response = await fetch(url, {
|
|
71
|
-
headers: {
|
|
72
|
-
Authorization: `Bearer ${accessToken}`,
|
|
73
|
-
Accept: 'application/vnd.github+json',
|
|
74
|
-
'X-GitHub-Api-Version': '2022-11-28',
|
|
75
|
-
},
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
if (response.status === 401 || response.status === 403) {
|
|
79
|
-
this.logger.warn(`GitHub API returned ${response.status} for user ${this.ctx.context.userId}`);
|
|
80
|
-
return {
|
|
81
|
-
data: {
|
|
82
|
-
error: '401',
|
|
83
|
-
message: 'GitHub token was rejected. Please re-authenticate.',
|
|
84
|
-
},
|
|
85
|
-
};
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
if (!response.ok) {
|
|
89
|
-
const body = await response.text();
|
|
90
|
-
this.logger.error(`GitHub API error: ${response.status} ${body}`);
|
|
91
|
-
return {
|
|
92
|
-
data: {
|
|
93
|
-
error: 'api_error',
|
|
94
|
-
message: `GitHub API error: ${response.statusText}`,
|
|
95
|
-
},
|
|
96
|
-
};
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
const data = (await response.json()) as {
|
|
100
|
-
total_count: number;
|
|
101
|
-
workflow_runs: Array<{
|
|
102
|
-
id: number;
|
|
103
|
-
name: string;
|
|
104
|
-
status: string;
|
|
105
|
-
conclusion: string | null;
|
|
106
|
-
head_branch: string;
|
|
107
|
-
head_sha: string;
|
|
108
|
-
event: string;
|
|
109
|
-
created_at: string;
|
|
110
|
-
updated_at: string;
|
|
111
|
-
html_url: string;
|
|
112
|
-
}>;
|
|
113
|
-
};
|
|
114
|
-
|
|
115
|
-
const runs = data.workflow_runs.map((run) => ({
|
|
116
|
-
id: run.id,
|
|
117
|
-
name: run.name,
|
|
118
|
-
status: run.status,
|
|
119
|
-
conclusion: run.conclusion,
|
|
120
|
-
headBranch: run.head_branch,
|
|
121
|
-
headSha: run.head_sha,
|
|
122
|
-
event: run.event,
|
|
123
|
-
createdAt: run.created_at,
|
|
124
|
-
updatedAt: run.updated_at,
|
|
125
|
-
htmlUrl: run.html_url,
|
|
126
|
-
}));
|
|
127
|
-
|
|
128
|
-
return {
|
|
129
|
-
data: { totalCount: data.total_count, runs },
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
}
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { Inject, Logger } from '@nestjs/common';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
import { BaseTool, Tool, ToolResult } from '@loopstack/common';
|
|
4
|
-
import { OAuthTokenStore } from '@loopstack/oauth-module';
|
|
5
|
-
|
|
6
|
-
const inputSchema = z
|
|
7
|
-
.object({
|
|
8
|
-
owner: z.string(),
|
|
9
|
-
repo: z.string(),
|
|
10
|
-
workflowId: z.string(),
|
|
11
|
-
ref: z.string(),
|
|
12
|
-
inputs: z.record(z.string(), z.string()).optional(),
|
|
13
|
-
})
|
|
14
|
-
.strict();
|
|
15
|
-
|
|
16
|
-
export type GitHubTriggerWorkflowArgs = z.infer<typeof inputSchema>;
|
|
17
|
-
|
|
18
|
-
@Tool({
|
|
19
|
-
uiConfig: {
|
|
20
|
-
description:
|
|
21
|
-
'Triggers a GitHub Actions workflow dispatch event. Returns 204 No Content on success. Returns { error: "unauthorized" } if no valid token is available.',
|
|
22
|
-
},
|
|
23
|
-
schema: inputSchema,
|
|
24
|
-
})
|
|
25
|
-
export class GitHubTriggerWorkflowTool extends BaseTool {
|
|
26
|
-
private readonly logger = new Logger(GitHubTriggerWorkflowTool.name);
|
|
27
|
-
|
|
28
|
-
@Inject()
|
|
29
|
-
private tokenStore: OAuthTokenStore;
|
|
30
|
-
|
|
31
|
-
async call(args: GitHubTriggerWorkflowArgs): Promise<ToolResult> {
|
|
32
|
-
const accessToken = await this.tokenStore.getValidAccessToken(this.ctx.context.userId, 'github');
|
|
33
|
-
|
|
34
|
-
if (!accessToken) {
|
|
35
|
-
return {
|
|
36
|
-
data: {
|
|
37
|
-
error: 'unauthorized',
|
|
38
|
-
message: 'No valid GitHub token found. Please authenticate first.',
|
|
39
|
-
},
|
|
40
|
-
};
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
const requestBody: Record<string, unknown> = {
|
|
44
|
-
ref: args.ref,
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
if (args.inputs) requestBody.inputs = args.inputs;
|
|
48
|
-
|
|
49
|
-
const url = `https://api.github.com/repos/${encodeURIComponent(args.owner)}/${encodeURIComponent(args.repo)}/actions/workflows/${encodeURIComponent(args.workflowId)}/dispatches`;
|
|
50
|
-
const response = await fetch(url, {
|
|
51
|
-
method: 'POST',
|
|
52
|
-
headers: {
|
|
53
|
-
Authorization: `Bearer ${accessToken}`,
|
|
54
|
-
Accept: 'application/vnd.github+json',
|
|
55
|
-
'X-GitHub-Api-Version': '2022-11-28',
|
|
56
|
-
'Content-Type': 'application/json',
|
|
57
|
-
},
|
|
58
|
-
body: JSON.stringify(requestBody),
|
|
59
|
-
});
|
|
60
|
-
|
|
61
|
-
if (response.status === 401 || response.status === 403) {
|
|
62
|
-
this.logger.warn(`GitHub API returned ${response.status} for user ${this.ctx.context.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 && response.status !== 204) {
|
|
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
|
-
return {
|
|
83
|
-
data: {
|
|
84
|
-
triggered: true,
|
|
85
|
-
message: 'Workflow dispatch event triggered successfully.',
|
|
86
|
-
},
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
import { Inject, Logger } from '@nestjs/common';
|
|
2
|
-
import { z } from 'zod';
|
|
3
|
-
import { BaseTool, Tool, ToolResult } from '@loopstack/common';
|
|
4
|
-
import { OAuthTokenStore } from '@loopstack/oauth-module';
|
|
5
|
-
|
|
6
|
-
const inputSchema = z
|
|
7
|
-
.object({
|
|
8
|
-
owner: z.string(),
|
|
9
|
-
repo: z.string(),
|
|
10
|
-
path: z.string(),
|
|
11
|
-
content: z.string(),
|
|
12
|
-
message: z.string(),
|
|
13
|
-
sha: z.string().optional(),
|
|
14
|
-
branch: z.string().optional(),
|
|
15
|
-
})
|
|
16
|
-
.strict();
|
|
17
|
-
|
|
18
|
-
export type GitHubCreateOrUpdateFileArgs = z.infer<typeof inputSchema>;
|
|
19
|
-
|
|
20
|
-
@Tool({
|
|
21
|
-
uiConfig: {
|
|
22
|
-
description:
|
|
23
|
-
'Creates or updates a file in a GitHub repository. Content is provided as plain text and encoded to base64 before sending. To update an existing file, provide the sha of the file being replaced. Returns { error: "unauthorized" } if no valid token is available.',
|
|
24
|
-
},
|
|
25
|
-
schema: inputSchema,
|
|
26
|
-
})
|
|
27
|
-
export class GitHubCreateOrUpdateFileTool extends BaseTool {
|
|
28
|
-
private readonly logger = new Logger(GitHubCreateOrUpdateFileTool.name);
|
|
29
|
-
|
|
30
|
-
@Inject()
|
|
31
|
-
private tokenStore: OAuthTokenStore;
|
|
32
|
-
|
|
33
|
-
async call(args: GitHubCreateOrUpdateFileArgs): Promise<ToolResult> {
|
|
34
|
-
const accessToken = await this.tokenStore.getValidAccessToken(this.ctx.context.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 requestBody: Record<string, unknown> = {
|
|
46
|
-
message: args.message,
|
|
47
|
-
content: Buffer.from(args.content).toString('base64'),
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
if (args.sha) requestBody.sha = args.sha;
|
|
51
|
-
if (args.branch) requestBody.branch = args.branch;
|
|
52
|
-
|
|
53
|
-
const url = `https://api.github.com/repos/${encodeURIComponent(args.owner)}/${encodeURIComponent(args.repo)}/contents/${args.path}`;
|
|
54
|
-
const response = await fetch(url, {
|
|
55
|
-
method: 'PUT',
|
|
56
|
-
headers: {
|
|
57
|
-
Authorization: `Bearer ${accessToken}`,
|
|
58
|
-
Accept: 'application/vnd.github+json',
|
|
59
|
-
'X-GitHub-Api-Version': '2022-11-28',
|
|
60
|
-
'Content-Type': 'application/json',
|
|
61
|
-
},
|
|
62
|
-
body: JSON.stringify(requestBody),
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
if (response.status === 401 || response.status === 403) {
|
|
66
|
-
this.logger.warn(`GitHub API returned ${response.status} for user ${this.ctx.context.userId}`);
|
|
67
|
-
return {
|
|
68
|
-
data: {
|
|
69
|
-
error: '401',
|
|
70
|
-
message: 'GitHub token was rejected. Please re-authenticate.',
|
|
71
|
-
},
|
|
72
|
-
};
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (!response.ok) {
|
|
76
|
-
const errorBody = await response.text();
|
|
77
|
-
this.logger.error(`GitHub API error: ${response.status} ${errorBody}`);
|
|
78
|
-
return {
|
|
79
|
-
data: {
|
|
80
|
-
error: 'api_error',
|
|
81
|
-
message: `GitHub API error: ${response.statusText}`,
|
|
82
|
-
},
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
const data = (await response.json()) as {
|
|
87
|
-
content: {
|
|
88
|
-
name: string;
|
|
89
|
-
path: string;
|
|
90
|
-
sha: string;
|
|
91
|
-
html_url: string;
|
|
92
|
-
};
|
|
93
|
-
commit: {
|
|
94
|
-
sha: string;
|
|
95
|
-
message: string;
|
|
96
|
-
};
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
return {
|
|
100
|
-
data: {
|
|
101
|
-
file: {
|
|
102
|
-
name: data.content.name,
|
|
103
|
-
path: data.content.path,
|
|
104
|
-
sha: data.content.sha,
|
|
105
|
-
htmlUrl: data.content.html_url,
|
|
106
|
-
},
|
|
107
|
-
commit: {
|
|
108
|
-
sha: data.commit.sha,
|
|
109
|
-
message: data.commit.message,
|
|
110
|
-
},
|
|
111
|
-
},
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
}
|