-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathproject.service.ts
More file actions
106 lines (91 loc) · 2.63 KB
/
Copy pathproject.service.ts
File metadata and controls
106 lines (91 loc) · 2.63 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { graphql } from "@octokit/graphql";
import dotenv from "dotenv";
import type {
FilterProps,
RepositoryProps,
GraphQLResponseProps,
OptionsTypesProps,
} from "@opensox/shared";
dotenv.config();
const getGithubPersonalAccessToken = () => {
const token = process.env.GITHUB_PERSONAL_ACCESS_TOKEN?.trim();
if (!token) {
throw new Error(
"GITHUB_PERSONAL_ACCESS_TOKEN is required to fetch GitHub projects. Please configure it in apps/api/.env."
);
}
return token;
};
const createGithubClient = () =>
graphql.defaults({
headers: {
authorization: `token ${getGithubPersonalAccessToken()}`,
},
});
export const projectService = {
/**
* Fetch GitHub repositories based on filters and options
*/
async fetchGithubProjects(
filters: Partial<FilterProps> = {},
options: Partial<OptionsTypesProps> = {}
): Promise<RepositoryProps[]> {
const queryParts: string[] = [];
if (filters.language) {
queryParts.push(`language:${filters.language}`);
}
if (filters.stars) {
queryParts.push(`stars:${filters.stars.min}..${filters.stars.max}`);
}
if (filters.forks) {
queryParts.push(`forks:${filters.forks.min}..${filters.forks.max}`);
}
if (filters.pushed) {
queryParts.push(`pushed:${filters.pushed}`);
}
if (filters.created) {
queryParts.push(`created:${filters.created}`);
}
// Default fields to filter contributor friendly repos
queryParts.push(`is:organization`);
queryParts.push(`is:public`);
queryParts.push(`fork:true`);
const searchQueryString = queryParts.join(" ");
const graphqlWithAuth = createGithubClient();
const response: GraphQLResponseProps = await graphqlWithAuth(
`
query($searchQuery: String!, $first: Int!) {
search(
query: $searchQuery,
type: REPOSITORY,
first: $first
) {
nodes {
... on Repository {
id
name
description
url
owner {
avatarUrl
}
issues(states: OPEN) {
totalCount
}
primaryLanguage {
name
}
}
}
repositoryCount
}
}
`,
{
searchQuery: searchQueryString,
first: options.per_page || 100,
}
);
return response.search.nodes;
},
};