mirror of
https://github.com/actions/toolkit.git
synced 2025-06-10 18:10:47 +09:00
@actions/github v3 using Octokit/core (#453)
* Rebuild to use @Octokit/Core
This commit is contained in:
parent
9ba7c679ad
commit
4a89cf72de
8 changed files with 311 additions and 319 deletions
109
packages/github/__tests__/github.proxy.test.ts
Normal file
109
packages/github/__tests__/github.proxy.test.ts
Normal file
|
@ -0,0 +1,109 @@
|
|||
import * as http from 'http'
|
||||
import * as https from 'https'
|
||||
import proxy from 'proxy'
|
||||
|
||||
// Default values are set when the module is imported, so we need to set proxy first.
|
||||
const proxyUrl = 'http://127.0.0.1:8081'
|
||||
const originalProxyUrl = process.env['https_proxy']
|
||||
process.env['https_proxy'] = proxyUrl
|
||||
// eslint-disable-next-line import/first
|
||||
import {getOctokit} from '../src/github'
|
||||
|
||||
describe('@actions/github', () => {
|
||||
let proxyConnects: string[]
|
||||
let proxyServer: http.Server
|
||||
let first = true
|
||||
|
||||
beforeAll(async () => {
|
||||
// Start proxy server
|
||||
proxyServer = proxy()
|
||||
await new Promise(resolve => {
|
||||
const port = Number(proxyUrl.split(':')[2])
|
||||
proxyServer.listen(port, () => resolve())
|
||||
})
|
||||
proxyServer.on('connect', req => {
|
||||
proxyConnects.push(req.url)
|
||||
})
|
||||
})
|
||||
|
||||
beforeEach(() => {
|
||||
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 with proxy', async () => {
|
||||
const token = getToken()
|
||||
if (!token) {
|
||||
return
|
||||
}
|
||||
|
||||
const octokit = getOctokit(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 with proxy', async () => {
|
||||
const token = getToken()
|
||||
if (!token) {
|
||||
return
|
||||
}
|
||||
process.env['https_proxy'] = proxyUrl
|
||||
const octokit = getOctokit(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'])
|
||||
})
|
||||
|
||||
it('should only use default agent if one is not provided', async () => {
|
||||
const token = getToken()
|
||||
if (!token) {
|
||||
return
|
||||
}
|
||||
|
||||
// Valid token
|
||||
const octokit = getOctokit(token, {
|
||||
request: {
|
||||
agent: new https.Agent()
|
||||
}
|
||||
})
|
||||
const branch = await octokit.repos.getBranch({
|
||||
owner: 'actions',
|
||||
repo: 'toolkit',
|
||||
branch: 'master'
|
||||
})
|
||||
expect(branch.data.name).toBe('master')
|
||||
expect(proxyConnects).toHaveLength(0)
|
||||
})
|
||||
|
||||
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
|
||||
}
|
||||
})
|
|
@ -1,6 +1,7 @@
|
|||
import * as http from 'http'
|
||||
import proxy from 'proxy'
|
||||
import {GitHub} from '../src/github'
|
||||
import {getOctokit} from '../src/github'
|
||||
import {GitHub, getOctokitOptions} from '../src/utils'
|
||||
|
||||
describe('@actions/github', () => {
|
||||
const proxyUrl = 'http://127.0.0.1:8080'
|
||||
|
@ -43,8 +44,22 @@ describe('@actions/github', () => {
|
|||
if (!token) {
|
||||
return
|
||||
}
|
||||
const octokit = new GitHub(getOctokitOptions(token))
|
||||
const branch = await octokit.repos.getBranch({
|
||||
owner: 'actions',
|
||||
repo: 'toolkit',
|
||||
branch: 'master'
|
||||
})
|
||||
expect(branch.data.name).toBe('master')
|
||||
expect(proxyConnects).toHaveLength(0)
|
||||
})
|
||||
|
||||
const octokit = new GitHub(token)
|
||||
it('basic getOctokit client', async () => {
|
||||
const token = getToken()
|
||||
if (!token) {
|
||||
return
|
||||
}
|
||||
const octokit = getOctokit(token)
|
||||
const branch = await octokit.repos.getBranch({
|
||||
owner: 'actions',
|
||||
repo: 'toolkit',
|
||||
|
@ -85,30 +100,13 @@ describe('@actions/github', () => {
|
|||
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 octokit = getOctokit(token)
|
||||
const repository = await octokit.graphql(
|
||||
'{repository(owner:"actions", name:"toolkit"){name}}'
|
||||
)
|
||||
|
@ -123,7 +121,7 @@ describe('@actions/github', () => {
|
|||
}
|
||||
|
||||
// Valid token
|
||||
let octokit = new GitHub(token)
|
||||
let octokit = getOctokit(token)
|
||||
const repository = await octokit.graphql(
|
||||
'{repository(owner:"actions", name:"toolkit"){name}}'
|
||||
)
|
||||
|
@ -143,21 +141,6 @@ describe('@actions/github', () => {
|
|||
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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue