-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathget-github-repo.ts
More file actions
60 lines (52 loc) · 1.27 KB
/
get-github-repo.ts
File metadata and controls
60 lines (52 loc) · 1.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import type { GithubRepo } from '../types'
function getGitHubToken(): string {
return process.env.GITHUB_TOKEN || import.meta.env.GITHUB_TOKEN || ''
}
export async function getGitHubRepo(name: string): Promise<GithubRepo> {
const token = getGitHubToken()
// name comes in as user/repo
const user = name.split('/')[0]
const repo = name.split('/')[1]
const response = await fetch('https://api.github.com/graphql', {
method: 'POST',
headers: {
'content-type': 'application/json',
authorization: `Bearer ${token}`
},
body: JSON.stringify({
query: CHANGELOG_QUERY,
variables: { user, repo }
})
})
if (!response.ok) {
console.error(response)
}
const json = await response.json()
if (json.errors) {
console.error(json.errors)
}
const repoInfo = json?.data?.user?.repository
return repoInfo
}
export const CHANGELOG_QUERY = /* GraphQL */ `
query GitHubRepo($user: String!, $repo: String!) {
user(login: $user) {
repository(name: $repo) {
name
description
forkCount
stargazerCount
url
owner {
login
}
object(expression: "main:CHANGELOG.md") {
id
... on Blob {
text
}
}
}
}
}
`