Skip to content

Commit 4a89cf7

Browse files
authored
@actions/github v3 using Octokit/core (#453)
* Rebuild to use @Octokit/Core
1 parent 9ba7c67 commit 4a89cf7

File tree

8 files changed

+311
-319
lines changed

8 files changed

+311
-319
lines changed

Diff for: packages/github/README.md

+27-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
55
## Usage
66

7-
Returns an authenticated Octokit client that follows the machine [proxy settings](https://help.github.com/en/actions/hosting-your-own-runners/using-a-proxy-server-with-self-hosted-runners). See https://octokit.github.io/rest.js for the API.
7+
Returns an authenticated Octokit client that follows the machine [proxy settings](https://help.github.com/en/actions/hosting-your-own-runners/using-a-proxy-server-with-self-hosted-runners) and correctly sets GHES base urls. See https://octokit.github.io/rest.js for the API.
88

99
```js
1010
const github = require('@actions/github');
@@ -17,7 +17,10 @@ async function run() {
1717
// https://help.github.com/en/actions/automating-your-workflow-with-github-actions/authenticating-with-the-github_token#about-the-github_token-secret
1818
const myToken = core.getInput('myToken');
1919

20-
const octokit = new github.GitHub(myToken);
20+
const octokit = github.getOctokit(token)
21+
22+
// You can also pass in additional options as a second parameter to getOctokit
23+
// const octokit = github.getOctokit(myToken, {userAgent: "MyActionVersion1"});
2124

2225
const { data: pullRequest } = await octokit.pulls.get({
2326
owner: 'octokit',
@@ -34,8 +37,6 @@ async function run() {
3437
run();
3538
```
3639

37-
You can pass client options, as specified by [Octokit](https://octokit.github.io/rest.js/), as a second argument to the `GitHub` constructor.
38-
3940
You can also make GraphQL requests. See https://github.com/octokit/graphql.js for the API.
4041

4142
```js
@@ -72,3 +73,25 @@ if (github.context.eventName === 'push') {
7273
core.info(`The head commit is: ${pushPayload.head}`)
7374
}
7475
```
76+
77+
## Extending the Octokit instance
78+
`@octokit/core` now supports the [plugin architecture](https://github.com/octokit/core.js#plugins). You can extend the GitHub instance using plugins.
79+
80+
For example, using the `@octokit/plugin-enterprise-server` you can now access enterprise admin apis on GHES instances.
81+
82+
```ts
83+
import { GitHub, getOctokitOptions } from '@actions/github/lib/utils'
84+
import { enterpriseServer220Admin } from '@octokit/plugin-enterprise-server'
85+
86+
const octokit = GitHub.plugin(enterpriseServer220Admin)
87+
// or override some of the default values as well
88+
// const octokit = GitHub.plugin(enterpriseServer220Admin).defaults({userAgent: "MyNewUserAgent"})
89+
90+
const myToken = core.getInput('myToken');
91+
const myOctokit = new octokit(getOctokitOptions(token))
92+
// Create a new user
93+
myOctokit.enterpriseAdmin.createUser({
94+
login: "testuser",
95+
96+
});
97+
```

Diff for: packages/github/__tests__/github.proxy.test.ts

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
import * as http from 'http'
2+
import * as https from 'https'
3+
import proxy from 'proxy'
4+
5+
// Default values are set when the module is imported, so we need to set proxy first.
6+
const proxyUrl = 'http://127.0.0.1:8081'
7+
const originalProxyUrl = process.env['https_proxy']
8+
process.env['https_proxy'] = proxyUrl
9+
// eslint-disable-next-line import/first
10+
import {getOctokit} from '../src/github'
11+
12+
describe('@actions/github', () => {
13+
let proxyConnects: string[]
14+
let proxyServer: http.Server
15+
let first = true
16+
17+
beforeAll(async () => {
18+
// Start proxy server
19+
proxyServer = proxy()
20+
await new Promise(resolve => {
21+
const port = Number(proxyUrl.split(':')[2])
22+
proxyServer.listen(port, () => resolve())
23+
})
24+
proxyServer.on('connect', req => {
25+
proxyConnects.push(req.url)
26+
})
27+
})
28+
29+
beforeEach(() => {
30+
proxyConnects = []
31+
})
32+
33+
afterAll(async () => {
34+
// Stop proxy server
35+
await new Promise(resolve => {
36+
proxyServer.once('close', () => resolve())
37+
proxyServer.close()
38+
})
39+
40+
if (originalProxyUrl) {
41+
process.env['https_proxy'] = originalProxyUrl
42+
}
43+
})
44+
45+
it('basic REST client with proxy', async () => {
46+
const token = getToken()
47+
if (!token) {
48+
return
49+
}
50+
51+
const octokit = getOctokit(token)
52+
const branch = await octokit.repos.getBranch({
53+
owner: 'actions',
54+
repo: 'toolkit',
55+
branch: 'master'
56+
})
57+
expect(branch.data.name).toBe('master')
58+
expect(proxyConnects).toEqual(['api.github.com:443'])
59+
})
60+
61+
it('basic GraphQL client with proxy', async () => {
62+
const token = getToken()
63+
if (!token) {
64+
return
65+
}
66+
process.env['https_proxy'] = proxyUrl
67+
const octokit = getOctokit(token)
68+
69+
const repository = await octokit.graphql(
70+
'{repository(owner:"actions", name:"toolkit"){name}}'
71+
)
72+
expect(repository).toEqual({repository: {name: 'toolkit'}})
73+
expect(proxyConnects).toEqual(['api.github.com:443'])
74+
})
75+
76+
it('should only use default agent if one is not provided', async () => {
77+
const token = getToken()
78+
if (!token) {
79+
return
80+
}
81+
82+
// Valid token
83+
const octokit = getOctokit(token, {
84+
request: {
85+
agent: new https.Agent()
86+
}
87+
})
88+
const branch = await octokit.repos.getBranch({
89+
owner: 'actions',
90+
repo: 'toolkit',
91+
branch: 'master'
92+
})
93+
expect(branch.data.name).toBe('master')
94+
expect(proxyConnects).toHaveLength(0)
95+
})
96+
97+
function getToken(): string {
98+
const token = process.env['GITHUB_TOKEN'] || ''
99+
if (!token && first) {
100+
/* eslint-disable-next-line no-console */
101+
console.warn(
102+
'Skipping GitHub tests. Set $GITHUB_TOKEN to run REST client and GraphQL client tests'
103+
)
104+
first = false
105+
}
106+
107+
return token
108+
}
109+
})

Diff for: packages/github/__tests__/github.test.ts

+19-36
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as http from 'http'
22
import proxy from 'proxy'
3-
import {GitHub} from '../src/github'
3+
import {getOctokit} from '../src/github'
4+
import {GitHub, getOctokitOptions} from '../src/utils'
45

56
describe('@actions/github', () => {
67
const proxyUrl = 'http://127.0.0.1:8080'
@@ -43,8 +44,22 @@ describe('@actions/github', () => {
4344
if (!token) {
4445
return
4546
}
47+
const octokit = new GitHub(getOctokitOptions(token))
48+
const branch = await octokit.repos.getBranch({
49+
owner: 'actions',
50+
repo: 'toolkit',
51+
branch: 'master'
52+
})
53+
expect(branch.data.name).toBe('master')
54+
expect(proxyConnects).toHaveLength(0)
55+
})
4656

47-
const octokit = new GitHub(token)
57+
it('basic getOctokit client', async () => {
58+
const token = getToken()
59+
if (!token) {
60+
return
61+
}
62+
const octokit = getOctokit(token)
4863
const branch = await octokit.repos.getBranch({
4964
owner: 'actions',
5065
repo: 'toolkit',
@@ -85,30 +100,13 @@ describe('@actions/github', () => {
85100
expect(failed).toBeTruthy()
86101
})
87102

88-
it('basic REST client with proxy', async () => {
89-
const token = getToken()
90-
if (!token) {
91-
return
92-
}
93-
94-
process.env['https_proxy'] = proxyUrl
95-
const octokit = new GitHub(token)
96-
const branch = await octokit.repos.getBranch({
97-
owner: 'actions',
98-
repo: 'toolkit',
99-
branch: 'master'
100-
})
101-
expect(branch.data.name).toBe('master')
102-
expect(proxyConnects).toEqual(['api.github.com:443'])
103-
})
104-
105103
it('basic GraphQL client', async () => {
106104
const token = getToken()
107105
if (!token) {
108106
return
109107
}
110108

111-
const octokit = new GitHub(token)
109+
const octokit = getOctokit(token)
112110
const repository = await octokit.graphql(
113111
'{repository(owner:"actions", name:"toolkit"){name}}'
114112
)
@@ -123,7 +121,7 @@ describe('@actions/github', () => {
123121
}
124122

125123
// Valid token
126-
let octokit = new GitHub(token)
124+
let octokit = getOctokit(token)
127125
const repository = await octokit.graphql(
128126
'{repository(owner:"actions", name:"toolkit"){name}}'
129127
)
@@ -143,21 +141,6 @@ describe('@actions/github', () => {
143141
expect(failed).toBeTruthy()
144142
})
145143

146-
it('basic GraphQL client with proxy', async () => {
147-
const token = getToken()
148-
if (!token) {
149-
return
150-
}
151-
152-
process.env['https_proxy'] = proxyUrl
153-
const octokit = new GitHub(token)
154-
const repository = await octokit.graphql(
155-
'{repository(owner:"actions", name:"toolkit"){name}}'
156-
)
157-
expect(repository).toEqual({repository: {name: 'toolkit'}})
158-
expect(proxyConnects).toEqual(['api.github.com:443'])
159-
})
160-
161144
function getToken(): string {
162145
const token = process.env['GITHUB_TOKEN'] || ''
163146
if (!token && first) {

0 commit comments

Comments
 (0)