Skip to content

Commit af870cb

Browse files
committed
update github gql to use octokit
what could go wrong right?
1 parent e42ae1a commit af870cb

File tree

8 files changed

+333
-54
lines changed

8 files changed

+333
-54
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ resources/game-server-status/style.css
1010

1111
# Our files
1212
.env
13+
*.pem
1314
*.json
1415
!*.example.json
1516
deploy.sh

app/services/Github.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Container, Service } from "../Container.js";
2+
import { Octokit } from "octokit";
3+
import { createAppAuth } from "@octokit/auth-app";
4+
import config from "@/config/github.json" with { type: "json" };
5+
6+
export class Github extends Service {
7+
name = "Github";
8+
9+
octokit = new Octokit({
10+
authStrategy: createAppAuth,
11+
auth: {
12+
appId: config.appId,
13+
privateKey: config.privateKey,
14+
clientId: config.clientId,
15+
clientSecret: config.clientSecret,
16+
},
17+
});
18+
}
19+
20+
export default (container: Container): Service => {
21+
return new Github(container);
22+
};

app/services/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import DataProvider, { Data } from "./Data.js";
44
import DiscordBotProvider, { DiscordBot } from "./discord/index.js";
55
import DiscordMetadataProvider, { DiscordMetadata } from "./DiscordMetadata.js";
66
import GameBridgeProvider, { GameBridge } from "./gamebridge/index.js";
7+
import GithubProvider, { Github } from "./Github.js";
78
import IRCProvider, { IRC } from "./IRC.js";
89
import MarkovProvider, { Markov } from "./Markov.js";
910
import MotdProvider, { Motd } from "./Motd.js";
@@ -20,6 +21,7 @@ export default [
2021
DiscordBotProvider,
2122
DiscordMetadataProvider,
2223
GameBridgeProvider,
24+
GithubProvider,
2325
IRCProvider,
2426
MarkovProvider,
2527
MotdProvider,
@@ -37,6 +39,7 @@ export {
3739
DiscordBot,
3840
DiscordMetadata,
3941
GameBridge,
42+
Github,
4043
IRC,
4144
Markov,
4245
Motd,
@@ -54,6 +57,7 @@ export type ServiceMap = {
5457
DiscordBot: DiscordBot;
5558
DiscordMetadata: DiscordMetadata;
5659
GameBridge: GameBridge;
60+
Github: Github;
5761
IRC: IRC;
5862
Markov: Markov;
5963
Motd: Motd;

config/apikeys.example.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
{
2-
"github": "",
32
"gitlab": "",
43
"steam": ""
54
}

config/github.example.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"appId": "",
3+
"clientId": "",
4+
"clientSecret": "",
5+
"privateKey": ""
6+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
"@ffmpeg.wasm/main": "^0.13.1",
2525
"@microsoft/signalr": "^8.0.7",
2626
"@napi-rs/canvas": "^0.1.37",
27-
"@octokit/webhooks": "^13.8.2",
27+
"@octokit/auth-app": "^8.1.1",
2828
"ajv": "^8.17.1",
2929
"audio-buffer": "^5.0.0",
3030
"axios": "^1.9.0",
@@ -44,6 +44,7 @@
4444
"node-schedule": "^2.1.1",
4545
"node-ssh": "^13.0.0",
4646
"node-wav": "^0.0.2",
47+
"octokit": "^5.0.3",
4748
"pg": "^8.8.0",
4849
"pug": "^3.0.2",
4950
"qs": "^6.11.0",

utils.ts

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ export const getAsBase64 = async (url: string): Promise<string | null> => {
8080

8181
interface GithubResponse {
8282
repository: {
83-
content: {
83+
object: {
8484
text: string;
85-
};
85+
} | null;
8686
};
8787
}
8888
interface GitlabResponse {
@@ -115,26 +115,13 @@ export const getOrFetchGmodFile = async (path: PathLike) => {
115115
const provider = url.match(/([^\.\/]+)\.com/);
116116
if (!provider) return;
117117
const isGithub = provider[1] === "github";
118-
const endpoint = isGithub
119-
? "https://api.github.com/graphql"
120-
: "https://gitlab.com/api/graphql";
118+
const gitlabEndpoint = "https://gitlab.com/api/graphql";
121119
const repo = addon;
122120
const owner = url.match(/\.com\/(.+?)\//);
123121
const branch = url.split("/").at(-2);
124122

125123
if (!owner) return;
126-
const query = isGithub
127-
? gql`{
128-
repository(owner:"${owner[1]}", name:"${repo}") {
129-
content: object(expression:"${branch}:${path}") {
130-
... on Blob {
131-
text
132-
}
133-
}
134-
}
135-
}
136-
`
137-
: gql`{
124+
const query = gql`{
138125
project(fullPath:"${url.match(/\.com\/(.+?)\/\-/)?.[1]}") {
139126
repository {
140127
blobs(paths:"${path}"){
@@ -145,23 +132,40 @@ export const getOrFetchGmodFile = async (path: PathLike) => {
145132
}
146133
`;
147134
try {
148-
const data = await request<GithubResponse | GitlabResponse>(
149-
endpoint,
150-
query,
151-
{},
152-
{
153-
authorization: `Bearer ${isGithub ? apikeys.github : apikeys.gitlab}`,
135+
if (isGithub) {
136+
const github = await globalThis.MetaConcord.container.getService("Github");
137+
const request: { data: GithubResponse } = await github.octokit.graphql(
138+
`query text($owner: String!, $repo: String!) {
139+
repository(owner: $owner, name: $repo) {
140+
object(expression: "${branch ?? "HEAD"}:${path}") {
141+
... on Blob {
142+
text
143+
}
144+
}
145+
}`,
146+
{ owner, repo }
147+
);
148+
if (request.data.repository.object?.text)
149+
return request.data.repository.object.text;
150+
return;
151+
} else {
152+
const data = await request<GithubResponse | GitlabResponse>(
153+
gitlabEndpoint,
154+
query,
155+
{},
156+
{
157+
authorization: `Bearer ${apikeys.gitlab}`,
158+
}
159+
);
160+
if (data) {
161+
const filecontent = (data as GitlabResponse).project.repository.blobs
162+
.nodes[0].rawTextBlob;
163+
return linenos
164+
? getStackLines(filecontent, Number(linenos), Number(linenoe))
165+
: filecontent;
154166
}
155-
);
156-
if (data) {
157-
const filecontent = isGithub
158-
? (data as GithubResponse).repository.content.text
159-
: (data as GitlabResponse).project.repository.blobs.nodes[0].rawTextBlob;
160-
return linenos
161-
? getStackLines(filecontent, Number(linenos), Number(linenoe))
162-
: filecontent;
167+
return;
163168
}
164-
return;
165169
} catch (err) {
166170
console.error(JSON.stringify(err, undefined, 2));
167171
return;

0 commit comments

Comments
 (0)