Skip to content

Commit 91b7bf9

Browse files
authored
Move @actions/http-client into the toolkit (actions#1062)
💡 See actions#1064 for a better diff! https://github.com/actions/toolkit contains a variety of packages used for building actions. https://github.com/actions/http-client is one such package, but lives outside of the toolkit. Moving it inside of the toolkit will improve discoverability and reduce the number of repos we have to keep track of for maintenance tasks (such as github/c2c-actions-service#2937). I checked with @bryanmacfarlane on the historical decision here. Apparently it was just inertia from before we released the toolkit as multiple packages. The benefits here are: - Have one fewer repo to keep track of - Signal that this is an HTTP client meant for building actions, not for general use. ## Notes - `@actions/http-client` will continue to be released as its own package. - Bumping the package version to **2.0.0**. Since we're compiling in strict mode now, there are some breaking changes to the exported types. This is an improvement because the null-unsafe version of`http-client` is currently breaking the safety of null-safe consumers. - I'm not updating the other packages to use the new version in this PR. I plan to do that in a follow-up. We'll hold off on publishing `http-client` v2 to NPM until that's done just in case other changes shake out of it.
1 parent 3e2837d commit 91b7bf9

File tree

19 files changed

+2425
-17
lines changed

19 files changed

+2425
-17
lines changed

.github/workflows/releases.yml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ on:
55
inputs:
66
package:
77
required: true
8-
description: 'core, artifact, cache, exec, github, glob, io, tool-cache'
9-
8+
description: 'core, artifact, cache, exec, github, glob, http-client, io, tool-cache'
9+
1010
jobs:
1111
test:
1212
runs-on: macos-latest
@@ -17,48 +17,48 @@ jobs:
1717

1818
- name: verify package exists
1919
run: ls packages/${{ github.event.inputs.package }}
20-
20+
2121
- name: Set Node.js 12.x
2222
uses: actions/setup-node@v1
2323
with:
2424
node-version: 12.x
25-
25+
2626
- name: npm install
2727
run: npm install
28-
28+
2929
- name: bootstrap
3030
run: npm run bootstrap
31-
31+
3232
- name: build
3333
run: npm run build
34-
34+
3535
- name: test
3636
run: npm run test
3737

3838
- name: pack
3939
run: npm pack
4040
working-directory: packages/${{ github.event.inputs.package }}
41-
41+
4242
- name: upload artifact
4343
uses: actions/upload-artifact@v2
4444
with:
4545
name: ${{ github.event.inputs.package }}
4646
path: packages/${{ github.event.inputs.package }}/*.tgz
47-
47+
4848
publish:
4949
runs-on: macos-latest
5050
needs: test
5151
environment: npm-publish
5252
steps:
53-
53+
5454
- name: download artifact
5555
uses: actions/download-artifact@v2
5656
with:
5757
name: ${{ github.event.inputs.package }}
5858

5959
- name: setup authentication
6060
run: echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" >> .npmrc
61-
env:
61+
env:
6262
NPM_TOKEN: ${{ secrets.TOKEN }}
6363

6464
- name: publish
@@ -68,13 +68,13 @@ jobs:
6868
if: failure()
6969
run: |
7070
curl -X POST -H 'Content-type: application/json' --data '{"text":":pb__failed: Failed to publish a new version of ${{ github.event.inputs.package }}"}' $SLACK_WEBHOOK
71-
env:
71+
env:
7272
SLACK_WEBHOOK: ${{ secrets.SLACK }}
73-
73+
7474
- name: notify slack on success
7575
if: success()
7676
run: |
7777
curl -X POST -H 'Content-type: application/json' --data '{"text":":dance: Successfully published a new version of ${{ github.event.inputs.package }}"}' $SLACK_WEBHOOK
78-
env:
78+
env:
7979
SLACK_WEBHOOK: ${{ secrets.SLACK }}
80-
80+

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ $ npm install @actions/glob
4646
```
4747
<br/>
4848

49+
:phone: [@actions/http-client](packages/http-client)
50+
51+
A lightweight HTTP client optimized for building actions. Read more [here](packages/http-client)
52+
53+
```bash
54+
$ npm install @actions/http-client
55+
```
56+
<br/>
57+
4958
:pencil2: [@actions/io](packages/io)
5059

5160
Provides disk i/o functions like cp, mv, rmRF, which etc. Read more [here](packages/io)

packages/http-client/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
testoutput.txt
2+
npm-debug.log

packages/http-client/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
Actions Http Client for Node.js
2+
3+
Copyright (c) GitHub, Inc.
4+
5+
All rights reserved.
6+
7+
MIT License
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
10+
associated documentation files (the "Software"), to deal in the Software without restriction,
11+
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
12+
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
13+
subject to the following conditions:
14+
15+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
18+
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
19+
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

packages/http-client/README.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# `@actions/http-client`
2+
3+
A lightweight HTTP client optimized for building actions.
4+
5+
## Features
6+
7+
- HTTP client with TypeScript generics and async/await/Promises
8+
- Typings included!
9+
- [Proxy support](https://help.github.com/en/actions/automating-your-workflow-with-github-actions/about-self-hosted-runners#using-a-proxy-server-with-self-hosted-runners) just works with actions and the runner
10+
- Targets ES2019 (runner runs actions with node 12+). Only supported on node 12+.
11+
- Basic, Bearer and PAT Support out of the box. Extensible handlers for others.
12+
- Redirects supported
13+
14+
Features and releases [here](./RELEASES.md)
15+
16+
## Install
17+
18+
```
19+
npm install @actions/http-client --save
20+
```
21+
22+
## Samples
23+
24+
See the [tests](./__tests__) for detailed examples.
25+
26+
## Errors
27+
28+
### HTTP
29+
30+
The HTTP client does not throw unless truly exceptional.
31+
32+
* A request that successfully executes resulting in a 404, 500 etc... will return a response object with a status code and a body.
33+
* Redirects (3xx) will be followed by default.
34+
35+
See the [tests](./__tests__) for detailed examples.
36+
37+
## Debugging
38+
39+
To enable detailed console logging of all HTTP requests and responses, set the NODE_DEBUG environment varible:
40+
41+
```shell
42+
export NODE_DEBUG=http
43+
```
44+
45+
## Node support
46+
47+
The http-client is built using the latest LTS version of Node 12. It may work on previous node LTS versions but it's tested and officially supported on Node12+.
48+
49+
## Support and Versioning
50+
51+
We follow semver and will hold compatibility between major versions and increment the minor version with new features and capabilities (while holding compat).
52+
53+
## Contributing
54+
55+
We welcome PRs. Please create an issue and if applicable, a design before proceeding with code.
56+
57+
once:
58+
59+
```
60+
npm install
61+
```
62+
63+
To build:
64+
65+
```
66+
npm run build
67+
```
68+
69+
To run all tests:
70+
71+
```
72+
npm test
73+
```

packages/http-client/RELEASES.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
## Releases
2+
3+
## 2.0.0
4+
- The package is now compiled with TypeScript's [`strict` compiler setting](https://www.typescriptlang.org/tsconfig#strict). To comply with stricter rules:
5+
- Some exported types now include `| null` or `| undefined`, matching their actual behavior.
6+
- Types implementing the method `RequestHandler.handleAuthentication()` now throw an `Error` rather than returning `null` if they do not support handling an HTTP 401 response. Callers can still use `canHandleAuthentication()` to determine if this handling is supported or not.
7+
- Types using `any` have been scoped to more specific types.
8+
- Following TypeScript's naming conventions, exported interfaces no longer begin with the prefix `I-`.
9+
- Delete the `IHttpClientResponse` interface in favor of the `HttpClientResponse` class.
10+
- Delete the `IHeaders` interface in favor of `http.OutgoingHttpHeaders`.
11+
- The source code of the package was moved to build with [actions/toolkit](https://github.com/actions/toolkit).
12+
13+
## 1.0.11
14+
15+
Contains a bug fix where proxy is defined without a user and password. see [PR here](https://github.com/actions/http-client/pull/42)
16+
17+
## 1.0.9
18+
Throw HttpClientError instead of a generic Error from the \<verb>Json() helper methods when the server responds with a non-successful status code.
19+
20+
## 1.0.8
21+
Fixed security issue where a redirect (e.g. 302) to another domain would pass headers. The fix was to strip the authorization header if the hostname was different. More [details in PR #27](https://github.com/actions/http-client/pull/27)
22+
23+
## 1.0.7
24+
Update NPM dependencies and add 429 to the list of HttpCodes
25+
26+
## 1.0.6
27+
Automatically sends Content-Type and Accept application/json headers for \<verb>Json() helper methods if not set in the client or parameters.
28+
29+
## 1.0.5
30+
Adds \<verb>Json() helper methods for json over http scenarios.
31+
32+
## 1.0.4
33+
Started to add \<verb>Json() helper methods. Do not use this release for that. Use >= 1.0.5 since there was an issue with types.
34+
35+
## 1.0.1 to 1.0.3
36+
Adds proxy support.
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import * as httpm from '../lib'
2+
import * as am from '../lib/auth'
3+
4+
describe('auth', () => {
5+
beforeEach(() => {})
6+
7+
afterEach(() => {})
8+
9+
it('does basic http get request with basic auth', async () => {
10+
const bh: am.BasicCredentialHandler = new am.BasicCredentialHandler(
11+
'johndoe',
12+
'password'
13+
)
14+
const http: httpm.HttpClient = new httpm.HttpClient('http-client-tests', [
15+
bh
16+
])
17+
const res: httpm.HttpClientResponse = await http.get(
18+
'http://httpbin.org/get'
19+
)
20+
expect(res.message.statusCode).toBe(200)
21+
const body: string = await res.readBody()
22+
const obj = JSON.parse(body)
23+
const auth: string = obj.headers.Authorization
24+
const creds: string = Buffer.from(
25+
auth.substring('Basic '.length),
26+
'base64'
27+
).toString()
28+
expect(creds).toBe('johndoe:password')
29+
expect(obj.url).toBe('http://httpbin.org/get')
30+
})
31+
32+
it('does basic http get request with pat token auth', async () => {
33+
const token = 'scbfb44vxzku5l4xgc3qfazn3lpk4awflfryc76esaiq7aypcbhs'
34+
const ph: am.PersonalAccessTokenCredentialHandler = new am.PersonalAccessTokenCredentialHandler(
35+
token
36+
)
37+
38+
const http: httpm.HttpClient = new httpm.HttpClient('http-client-tests', [
39+
ph
40+
])
41+
const res: httpm.HttpClientResponse = await http.get(
42+
'http://httpbin.org/get'
43+
)
44+
expect(res.message.statusCode).toBe(200)
45+
const body: string = await res.readBody()
46+
const obj = JSON.parse(body)
47+
const auth: string = obj.headers.Authorization
48+
const creds: string = Buffer.from(
49+
auth.substring('Basic '.length),
50+
'base64'
51+
).toString()
52+
expect(creds).toBe(`PAT:${token}`)
53+
expect(obj.url).toBe('http://httpbin.org/get')
54+
})
55+
56+
it('does basic http get request with pat token auth', async () => {
57+
const token = 'scbfb44vxzku5l4xgc3qfazn3lpk4awflfryc76esaiq7aypcbhs'
58+
const ph: am.BearerCredentialHandler = new am.BearerCredentialHandler(token)
59+
60+
const http: httpm.HttpClient = new httpm.HttpClient('http-client-tests', [
61+
ph
62+
])
63+
const res: httpm.HttpClientResponse = await http.get(
64+
'http://httpbin.org/get'
65+
)
66+
expect(res.message.statusCode).toBe(200)
67+
const body: string = await res.readBody()
68+
const obj = JSON.parse(body)
69+
const auth: string = obj.headers.Authorization
70+
expect(auth).toBe(`Bearer ${token}`)
71+
expect(obj.url).toBe('http://httpbin.org/get')
72+
})
73+
})

0 commit comments

Comments
 (0)