-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
github.ts
134 lines (112 loc) · 3.75 KB
/
github.ts
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
// Originally pulled from https://github.com/JasonEtco/actions-toolkit/blob/master/src/github.ts
import {graphql} from '@octokit/graphql'
// we need this type to set up a property on the GitHub object
// that has token authorization
// (it is not exported from octokit by default)
import {
graphql as GraphQL,
RequestParameters as GraphQLRequestParameters
} from '@octokit/graphql/dist-types/types'
import Octokit from '@octokit/rest'
import * as Context from './context'
import * as http from 'http'
import * as httpClient from '@actions/http-client'
// We need this in order to extend Octokit
Octokit.prototype = new Octokit()
export const context = new Context.Context()
export class GitHub extends Octokit {
graphql: GraphQL
/* eslint-disable no-dupe-class-members */
// Disable no-dupe-class-members due to false positive for method overload
// https://github.com/typescript-eslint/typescript-eslint/issues/291
/**
* Sets up the REST client and GraphQL client with auth and proxy support.
* The parameter `token` or `opts.auth` must be supplied. The GraphQL client
* authorization is not setup when `opts.auth` is a function or object.
*
* @param token Auth token
* @param opts Octokit options
*/
constructor(token: string, opts?: Omit<Octokit.Options, 'auth'>)
constructor(opts: Octokit.Options)
constructor(token: string | Octokit.Options, opts?: Octokit.Options) {
super(GitHub.getOctokitOptions(GitHub.disambiguate(token, opts)))
this.graphql = GitHub.getGraphQL(GitHub.disambiguate(token, opts))
}
/**
* Disambiguates the constructor overload parameters
*/
private static disambiguate(
token: string | Octokit.Options,
opts?: Octokit.Options
): [string, Octokit.Options] {
return [
typeof token === 'string' ? token : '',
typeof token === 'object' ? token : opts || {}
]
}
private static getOctokitOptions(
args: [string, Octokit.Options]
): Octokit.Options {
const token = args[0]
const options = {...args[1]} // Shallow clone - don't mutate the object provided by the caller
// Auth
const auth = GitHub.getAuthString(token, options)
if (auth) {
options.auth = auth
}
// Proxy
const agent = GitHub.getProxyAgent(options)
if (agent) {
// Shallow clone - don't mutate the object provided by the caller
options.request = options.request ? {...options.request} : {}
// Set the agent
options.request.agent = agent
}
return options
}
private static getGraphQL(args: [string, Octokit.Options]): GraphQL {
const defaults: GraphQLRequestParameters = {}
const token = args[0]
const options = args[1]
// Authorization
const auth = this.getAuthString(token, options)
if (auth) {
defaults.headers = {
authorization: auth
}
}
// Proxy
const agent = GitHub.getProxyAgent(options)
if (agent) {
defaults.request = {agent}
}
return graphql.defaults(defaults)
}
private static getAuthString(
token: string,
options: Octokit.Options
): string | undefined {
// Validate args
if (!token && !options.auth) {
throw new Error('Parameter token or opts.auth is required')
} else if (token && options.auth) {
throw new Error(
'Parameters token and opts.auth may not both be specified'
)
}
return typeof options.auth === 'string' ? options.auth : `token ${token}`
}
private static getProxyAgent(
options: Octokit.Options
): http.Agent | undefined {
if (!options.request?.agent) {
const serverUrl = 'https://api.github.com'
if (httpClient.getProxyUrl(serverUrl)) {
const hc = new httpClient.HttpClient()
return hc.getAgent(serverUrl)
}
}
return undefined
}
}