Repository navigation
Expand file tree
/
Copy pathapi.ts
More file actions
151 lines (139 loc) · 4.48 KB
/
Copy pathapi.ts
File metadata and controls
151 lines (139 loc) · 4.48 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* @file GitHub1s APIs
* @author netcon
*/
import * as vscode from 'vscode';
// import { getBranches } from './github-api-gql';
import { hasValidToken } from './util';
import {
fetch,
RequestError,
RequestRateLimitError,
RequestInvalidTokenError,
RequestNotFoundError,
throttledReportNetworkError,
} from './util/fetch';
// TODO: GraphQL API is experimental now, we need more test.
// It maybe crash or slow when there are too many files in one directory.
// For example the repository `git/git`, the TTFB cost about 3 seconds,
// and the response body size exceed 7MB before zip
export const ENABLE_GRAPHQL: boolean = false;
export interface UriState {
owner: string;
repo: string;
path: string;
}
export const isGraphQLEnabled = () => {
return hasValidToken() && ENABLE_GRAPHQL;
};
const handleRequestError = (error: RequestError) => {
if (error instanceof RequestRateLimitError) {
if (!error.token) {
throw vscode.FileSystemError.NoPermissions(
'API Rate Limit Exceeded, Please Offer an OAuth Token.'
);
}
throw vscode.FileSystemError.NoPermissions(
'API Rate Limit Exceeded, Please Change Another OAuth Token.'
);
}
if (error instanceof RequestInvalidTokenError) {
throw vscode.FileSystemError.NoPermissions(
'Current OAuth Token Is Invalid, Please Change Another One.'
);
}
if (error instanceof RequestNotFoundError) {
throw vscode.FileSystemError.NoPermissions(
'Current OAuth Token Is Invalid, Please Change Another One.'
);
}
if (error instanceof RequestNotFoundError) {
throw vscode.FileSystemError.FileNotFound('GitHub Resource Not Found');
}
throw vscode.FileSystemError.Unavailable(
error.message || 'Unknown Error Occurred When Request To GitHub'
);
};
export const readGitHubDirectory = (
owner: string,
repo: string,
ref: string,
path: string
) => {
return fetch(
`https://api.github.com/repos/${owner}/${repo}/git/trees/${ref}${path.replace(
/^\//,
':'
)}`
).catch(handleRequestError);
};
export const readGitHubFile = (
owner: string,
repo: string,
fileSha: string
) => {
return fetch(
`https://api.github.com/repos/${owner}/${repo}/git/blobs/${fileSha}`
).catch(handleRequestError);
};
export const validateToken = (token: string) => {
const authHeaders = token ? { Authorization: `token ${token}` } : {};
return self
.fetch(`https://api.github.com`, { headers: { ...authHeaders } })
.then((response) => ({
token: !!token, // if the token is not empty
valid: response.status !== 401 ? true : false, // if the request is valid
limit: +response.headers.get('X-RateLimit-Limit') || 0, // limit count
remaining: +response.headers.get('X-RateLimit-Remaining') || 0, // remains request count
reset: +response.headers.get('X-RateLimit-Reset') || 0, // reset time
}))
.catch(() => {
throttledReportNetworkError();
throw new RequestError('Request Failed, Maybe an Network Error', token);
});
};
// the [List Branches](https://docs.github.com/en/rest/reference/repos#list-branches) API
// only returned max to 100 branches for per request
// [List matching references](https://docs.github.com/en/rest/reference/git#list-matching-references)
// can returned all branches for a request, and there is an issue for this API
// https://github.com/github/docs/issues/3863
export const getGithubBranchRefs = (owner: string, repo: string) => {
return fetch(
`https://api.github.com/repos/${owner}/${repo}/git/matching-refs/heads`
)
.then((branchRefs) => {
// the field in branchRef will looks like `refs/heads/<branch>`, we add a name field here
return branchRefs.map((item) => ({ ...item, name: item.ref.slice(11) }));
})
.catch(handleRequestError);
};
// It's similar to `getGithubBranchRefs`
export const getGithubTagRefs = (owner: string, repo: string) => {
return fetch(
`https://api.github.com/repos/${owner}/${repo}/git/matching-refs/tags`
)
.then((tagRefs) => {
// the field in tagRef will looks like `refs/tags/<tag>`, we add a name field here
return tagRefs.map((item) => ({ ...item, name: item.ref.slice(10) }));
})
.catch(handleRequestError);
};
export const getGithubAllFiles = (
owner: string,
repo: string,
ref: string,
path: string = '/'
) => {
return fetch(
`https://api.github.com/repos/${owner}/${repo}/git/trees/${ref}${path.replace(
/^\//,
':'
)}?recursive=1`
).catch(handleRequestError);
};
// export const getGitHubBranches = (owner: string, repo: string) => {
// if (isGraphQLEnabled()) {
// return getBranches(owner, repo);
// }
// return getGithubBranches(owner, repo);
// };