Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

octokit client should follow proxy settings #314

Merged
merged 13 commits into from
Jan 18, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## Usage

Returns an Octokit client. See https://octokit.github.io/rest.js for the API.
Returns an authenticated Octokit client that follows the machine [proxy settings](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/about-self-hosted-runners#using-a-proxy-server-with-self-hosted-runners). See https://octokit.github.io/rest.js for the API.

```js
const github = require('@actions/github');
Expand Down Expand Up @@ -34,7 +34,7 @@ async function run() {
run();
```

You can pass client options (except `auth`, which is handled by the token argument), as specified by [Octokit](https://octokit.github.io/rest.js/), as a second argument to the `GitHub` constructor.
You can pass client options, as specified by [Octokit](https://octokit.github.io/rest.js/), as a second argument to the `GitHub` constructor.

You can also make GraphQL requests. See https://github.com/octokit/graphql.js for the API.

Expand Down
15 changes: 14 additions & 1 deletion packages/github/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions packages/github/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@actions/github",
"version": "2.0.1",
"version": "2.0.2",
"description": "Actions github lib",
"keywords": [
"github",
Expand Down Expand Up @@ -37,8 +37,10 @@
"url": "https://github.com/actions/toolkit/issues"
},
"dependencies": {
"@actions/http-client": "^1.0.2",
"@octokit/graphql": "^4.3.1",
"@octokit/rest": "^16.15.0"
"@octokit/rest": "^16.15.0",
"tunnel": "0.0.6"
},
"devDependencies": {
"jest": "^24.7.1"
Expand Down
120 changes: 114 additions & 6 deletions packages/github/src/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ 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} from '@octokit/graphql/dist-types/types'
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 {HttpClient} from '@actions/http-client'

// We need this in order to extend Octokit
Octokit.prototype = new Octokit()
Expand All @@ -17,11 +22,114 @@ export const context = new Context.Context()
export class GitHub extends Octokit {
graphql: GraphQL

constructor(token: string, opts: Omit<Octokit.Options, 'auth'> = {}) {
super({...opts, auth: `token ${token}`})
/* 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

this.graphql = graphql.defaults({
headers: {authorization: `token ${token}`}
})
/**
Copy link
Contributor Author

@ericsciple ericsciple Jan 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made a change to allow users to specify their own auth settings (in opts) instead of providing a token.

Customers who are using other authentication methods should still be able to use this class for the proxy benefits.

The one quirky detail is graphql auth only supports a string. So if token is provided or opts.auth is a string, then i setup the graphql authorization header. But if opts.auth is an object, then i dont set the graphql authorization header

* 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?: Octokit.Options)
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 as Octokit.Options) : opts || {}
]
}

/* eslint-disable @typescript-eslint/promise-function-async */
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 proxyUrl = process.env['https_proxy']
if (proxyUrl) {
const httpClient = new HttpClient()
return httpClient.getAgent('https://api.github.com')
}
}

return undefined
}
}
30 changes: 10 additions & 20 deletions packages/tool-cache/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/tool-cache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"dependencies": {
"@actions/core": "^1.2.0",
"@actions/exec": "^1.0.0",
"@actions/http-client": "^1.0.1",
"@actions/http-client": "^1.0.2",
"@actions/io": "^1.0.1",
"semver": "^6.1.0",
"uuid": "^3.3.2"
Expand Down