FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
codeboost/src/lib/github.ts at main · permafrost-dev/codeboost · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
permafrost-dev
/
codeboost
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
0
Star
16
Code
Issues
0
Pull requests
24
Actions
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
codeboost
/
src
/
lib
/
github.ts
Copy path
More file actions
More file actions
Latest commit
History
History
History
155 lines (122 loc) · 4.95 KB
Breadcrumbs
codeboost
/
src
/
lib
/
github.ts
Copy path
File metadata and controls
155 lines (122 loc) · 4.95 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
152
153
154
155
import
{
APP_VERSION
}
from
'@/lib/consts'
;
import
{
RepositoryInfo
}
from
'@/types/RepositoryInfo'
;
import
{
Octokit
}
from
'@octokit/core/dist-node/index'
;
import
{
throttling
}
from
'@octokit/plugin-throttling'
;
let
AppOctokitDefaults
:
typeof
Octokit
;
export
function
initOctokit
(
token
:
string
)
{
AppOctokitDefaults
=
Octokit
.
defaults
(
{
auth
:
token
,
log
:
console
,
userAgent
:
`codeboost/v
${
APP_VERSION
}
`
,
}
)
;
AppOctokitDefaults
.
plugin
(
throttling
)
;
}
export
function
createOctokit
(
)
:
Octokit
{
const
result
=
new
AppOctokitDefaults
(
{
throttle
:
{
onRateLimit
:
(
retryAfter
,
options
,
octokit
,
retryCount
)
=>
{
result
.
log
.
warn
(
`Request quota exhausted for request
${
options
.
method
}
${
options
.
url
}
`
)
;
// retries twice
if
(
retryCount
<
2
)
{
result
.
log
.
info
(
`Retrying after
${
retryAfter
}
seconds!`
)
;
return
true
;
}
}
,
onSecondaryRateLimit
:
(
retryAfter
,
options
,
octokit
)
=>
{
// does not retry, only logs a warning
result
.
log
.
warn
(
`SecondaryRateLimit detected for request
${
options
.
method
}
${
options
.
url
}
`
)
;
}
,
}
,
}
)
;
return
result
;
}
/**
* A class for interacting with the Github API
*/
export
class
Github
{
protected
static
cache
=
{
currentUser
:
null
as
{
login
:
string
}
|
null
}
;
static
async
setCache
(
cache
:
any
)
{
Github
.
cache
=
cache
;
}
static
async
currentUser
(
octokit
:
Octokit
|
null
=
null
)
{
octokit
=
octokit
??
createOctokit
(
)
;
if
(
Github
.
cache
.
currentUser
)
{
return
Github
.
cache
.
currentUser
;
}
// get currently authenticated user
const
user
=
await
octokit
.
request
(
'GET /user'
)
;
if
(
user
.
status
!==
200
)
{
throw
new
Error
(
'Failed to get currently authenticated user'
)
;
}
Github
.
cache
.
currentUser
=
{
login
:
user
.
data
.
login
}
;
return
user
.
data
;
}
/**
* Attempts to fork a repository into the currently authenticated user's account.
* Throws an error if the fork already exists.
*
@param
{
Repository
} repository
*
@param
{
Octokit|null
} octokit
*/
static
async
forkRepository
(
repository
:
RepositoryInfo
,
octokit
:
Octokit
|
null
=
null
)
{
octokit
=
octokit
??
createOctokit
(
)
;
const
currentUserLogin
=
(
await
Github
.
currentUser
(
octokit
)
)
.
login
;
if
(
currentUserLogin
===
repository
.
owner
)
{
throw
new
Error
(
`Cannot fork repository
${
repository
.
owner
}
/
${
repository
.
name
}
into itself`
)
;
}
const
result
=
await
octokit
.
request
(
`POST /repos/
${
repository
.
owner
}
/
${
repository
.
name
}
/forks`
,
{
owner
:
currentUserLogin
,
repo
:
this
.
name
,
default_branch_only
:
true
,
}
)
;
if
(
result
.
status
!==
202
)
{
throw
new
Error
(
`Failed to create fork of
${
repository
.
owner
}
/
${
repository
.
name
}
`
)
;
}
return
result
.
data
;
}
static
async
getRepository
(
repository
:
RepositoryInfo
,
octokit
:
Octokit
|
null
=
null
)
{
octokit
=
octokit
??
createOctokit
(
)
;
const
result
=
await
octokit
.
request
(
`GET /repos/
${
repository
.
owner
}
/
${
repository
.
name
}
`
)
;
if
(
result
.
status
!==
200
)
{
throw
new
Error
(
`Failed to get repository
${
repository
.
owner
}
/
${
repository
.
name
}
`
)
;
}
return
result
.
data
;
}
// create a pull request
static
async
createPullRequest
(
repository
:
RepositoryInfo
,
branchName
:
string
,
defaultBranchName
:
string
,
title
:
string
,
body
:
string
,
octokit
:
Octokit
|
null
=
null
,
isForkPr
=
true
,
)
{
octokit
=
octokit
??
createOctokit
(
)
;
const
currentUsername
=
(
await
Github
.
currentUser
(
octokit
)
)
.
login
;
const
result
=
await
octokit
.
request
(
`POST /repos/
${
repository
.
owner
}
/
${
repository
.
name
}
/pulls`
,
{
owner
:
currentUsername
,
repo
:
repository
.
name
,
title
,
body
,
head
:
`
${
isForkPr
?
currentUsername
+
':'
:
''
}
${
branchName
}
`
,
base
:
defaultBranchName
,
}
)
;
if
(
result
.
status
!==
201
)
{
throw
new
Error
(
`Failed to create pull request for
${
currentUsername
}
/
${
repository
.
name
}
`
)
;
}
return
result
.
data
;
}
static
async
mergePullRequest
(
repository
:
RepositoryInfo
,
pullRequestNumber
:
number
,
octokit
:
Octokit
|
null
=
null
)
{
octokit
=
octokit
??
createOctokit
(
)
;
const
result
=
await
octokit
.
request
(
`PUT /repos/
${
repository
.
owner
}
/
${
repository
.
name
}
/pulls/
${
pullRequestNumber
}
/merge`
,
{
owner
:
repository
.
owner
,
repo
:
repository
.
name
,
pull_number
:
pullRequestNumber
,
merge_method
:
'merge'
,
}
)
;
if
(
result
.
status
!==
200
)
{
throw
new
Error
(
`Failed to merge pull request for
${
repository
.
owner
}
/
${
repository
.
name
}
`
)
;
}
return
result
.
data
;
}
}
Back
|
FazBrowse Home
|
New Git URL