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 11 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
2 changes: 2 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ jobs:

- name: npm test
run: npm test
env:
GITHUB_TOKEN: ${{ github.token }}

- name: Lint
run: npm run lint
Expand Down
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
173 changes: 173 additions & 0 deletions packages/github/__tests__/github.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import * as http from 'http'
import proxy from 'proxy'
import {GitHub} from '../src/github'

describe('@actions/github', () => {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

New tests to exercise GitHub REST client and GraphQL client with and without proxy

Only runs when env var GITHUB_TOKEN is defined, otherwise warns for discoverability

const proxyUrl = 'http://127.0.0.1:8080'
const originalProxyUrl = process.env['https_proxy']
let proxyConnects: string[]
let proxyServer: http.Server
let first = true

beforeAll(async () => {
// Start proxy server
proxyServer = proxy() as http.Server
await new Promise(resolve => {
const port = Number(proxyUrl.split(':')[2])
proxyServer.listen(port, () => resolve())
})
proxyServer.on('connect', req => {
proxyConnects.push(req.url)
})
})

beforeEach(() => {
delete process.env['https_proxy']
proxyConnects = []
})

afterAll(async () => {
// Stop proxy server
await new Promise(resolve => {
proxyServer.once('close', () => resolve())
proxyServer.close()
})

if (originalProxyUrl) {
process.env['https_proxy'] = originalProxyUrl
}
})

it('basic REST client', async () => {
const token = getToken()
if (!token) {
return
}

const octokit = new GitHub(token)
const branch = await octokit.repos.getBranch({
owner: 'actions',
repo: 'toolkit',
branch: 'master'
})
expect(branch.data.name).toBe('master')
expect(proxyConnects).toHaveLength(0)
})

it('basic REST client with custom auth', async () => {
const token = getToken()
if (!token) {
return
}

// Valid token
let octokit = new GitHub({auth: `token ${token}`})
const branch = await octokit.repos.getBranch({
owner: 'actions',
repo: 'toolkit',
branch: 'master'
})
expect(branch.data.name).toBe('master')
expect(proxyConnects).toHaveLength(0)

// Invalid token
octokit = new GitHub({auth: `token asdf`})
let failed = false
try {
await octokit.repos.getBranch({
owner: 'actions',
repo: 'toolkit',
branch: 'master'
})
} catch (err) {
failed = true
}
expect(failed).toBeTruthy()
})

it('basic REST client with proxy', async () => {
const token = getToken()
if (!token) {
return
}

process.env['https_proxy'] = proxyUrl
const octokit = new GitHub(token)
const branch = await octokit.repos.getBranch({
owner: 'actions',
repo: 'toolkit',
branch: 'master'
})
expect(branch.data.name).toBe('master')
expect(proxyConnects).toEqual(['api.github.com:443'])
})

it('basic GraphQL client', async () => {
const token = getToken()
if (!token) {
return
}

const octokit = new GitHub(token)
const repository = await octokit.graphql(
'{repository(owner:"actions", name:"toolkit"){name}}'
)
expect(repository).toEqual({repository: {name: 'toolkit'}})
expect(proxyConnects).toHaveLength(0)
})

it('basic GraphQL client with custom auth', async () => {
const token = getToken()
if (!token) {
return
}

// Valid token
let octokit = new GitHub(token)
const repository = await octokit.graphql(
'{repository(owner:"actions", name:"toolkit"){name}}'
)
expect(repository).toEqual({repository: {name: 'toolkit'}})
expect(proxyConnects).toHaveLength(0)

// Invalid token
octokit = new GitHub({auth: `token asdf`})
let failed = false
try {
await octokit.graphql(
'{repository(owner:"actions", name:"toolkit"){name}}'
)
} catch (err) {
failed = true
}
expect(failed).toBeTruthy()
})

it('basic GraphQL client with proxy', async () => {
const token = getToken()
if (!token) {
return
}

process.env['https_proxy'] = proxyUrl
const octokit = new GitHub(token)
const repository = await octokit.graphql(
'{repository(owner:"actions", name:"toolkit"){name}}'
)
expect(repository).toEqual({repository: {name: 'toolkit'}})
expect(proxyConnects).toEqual(['api.github.com:443'])
})

function getToken(): string {
const token = process.env['GITHUB_TOKEN'] || ''
if (!token && first) {
/* eslint-disable-next-line no-console */
console.warn(
'Skipping GitHub tests. Set $GITHUB_TOKEN to run REST client and GraphQL client tests'
)
first = false
}

return token
}
})
5 changes: 5 additions & 0 deletions packages/github/__tests__/proxy.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
declare module 'proxy' {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

typescript made me do this :(

import * as http from 'http'
function internal(): http.Server
export = internal
}
Loading