Skip to content

Commit

Permalink
Move @actions/http-client into toolkit
Browse files Browse the repository at this point in the history
fix failing http-client tests

npm run lint-fix

Simple manual fixes for lint rules

Silence some linter errors

Silence some linter warnings, but not "explicit any"

Silence @typescript-eslint/no-explicit-any

fix @typescript-eslint/no-non-null-assertion

fix npm audit for http-client

don't use I- prefix on interface names

Get rid of `any` type in `Headers`

Add http-client to the main README

Update release notes
  • Loading branch information
brcrista committed Apr 28, 2022
1 parent bcb0e62 commit f300e2c
Show file tree
Hide file tree
Showing 18 changed files with 825 additions and 10,945 deletions.
30 changes: 15 additions & 15 deletions .github/workflows/releases.yml
Expand Up @@ -5,8 +5,8 @@ on:
inputs:
package:
required: true
description: 'core, artifact, cache, exec, github, glob, io, tool-cache'
description: 'core, artifact, cache, exec, github, glob, http-client, io, tool-cache'

jobs:
test:
runs-on: macos-latest
Expand All @@ -17,48 +17,48 @@ jobs:

- name: verify package exists
run: ls packages/${{ github.event.inputs.package }}

- name: Set Node.js 12.x
uses: actions/setup-node@v1
with:
node-version: 12.x

- name: npm install
run: npm install

- name: bootstrap
run: npm run bootstrap

- name: build
run: npm run build

- name: test
run: npm run test

- name: pack
run: npm pack
working-directory: packages/${{ github.event.inputs.package }}

- name: upload artifact
uses: actions/upload-artifact@v2
with:
name: ${{ github.event.inputs.package }}
path: packages/${{ github.event.inputs.package }}/*.tgz

publish:
runs-on: macos-latest
needs: test
environment: npm-publish
steps:

- name: download artifact
uses: actions/download-artifact@v2
with:
name: ${{ github.event.inputs.package }}

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

- name: publish
Expand All @@ -68,13 +68,13 @@ jobs:
if: failure()
run: |
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
env:
env:
SLACK_WEBHOOK: ${{ secrets.SLACK }}

- name: notify slack on success
if: success()
run: |
curl -X POST -H 'Content-type: application/json' --data '{"text":":dance: Successfully published a new version of ${{ github.event.inputs.package }}"}' $SLACK_WEBHOOK
env:
env:
SLACK_WEBHOOK: ${{ secrets.SLACK }}

9 changes: 9 additions & 0 deletions README.md
Expand Up @@ -46,6 +46,15 @@ $ npm install @actions/glob
```
<br/>

:phone: [@actions/http-client](packages/http-client)

A lightweight HTTP client optimized for building actions. Read more [here](packages/http-client)

```bash
$ npm install @actions/http-client
```
<br/>

:pencil2: [@actions/io](packages/io)

Provides disk i/o functions like cp, mv, rmRF, which etc. Read more [here](packages/io)
Expand Down
3 changes: 0 additions & 3 deletions packages/http-client/.gitignore
@@ -1,5 +1,2 @@
_out
node_modules
.DS_Store
testoutput.txt
npm-debug.log
32 changes: 13 additions & 19 deletions packages/http-client/README.md
@@ -1,18 +1,11 @@
# `@actions/http-client`

<p align="center">
<img src="actions.png">
</p>

# Actions Http-Client

[![Http Status](https://github.com/actions/http-client/workflows/http-tests/badge.svg)](https://github.com/actions/http-client/actions)

A lightweight HTTP client optimized for use with actions, TypeScript with generics and async await.
A lightweight HTTP client optimized for building actions.

## Features

- HTTP client with TypeScript generics and async/await/Promises
- Typings included so no need to acquire separately (great for intellisense and no versioning drift)
- Typings included!
- [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
- Targets ES2019 (runner runs actions with node 12+). Only supported on node 12+.
- Basic, Bearer and PAT Support out of the box. Extensible handlers for others.
Expand All @@ -28,7 +21,7 @@ npm install @actions/http-client --save

## Samples

See the [HTTP](./__tests__) tests for detailed examples.
See the [tests](./__tests__) for detailed examples.

## Errors

Expand All @@ -39,13 +32,13 @@ The HTTP client does not throw unless truly exceptional.
* A request that successfully executes resulting in a 404, 500 etc... will return a response object with a status code and a body.
* Redirects (3xx) will be followed by default.

See [HTTP tests](./__tests__) for detailed examples.
See the [tests](./__tests__) for detailed examples.

## Debugging

To enable detailed console logging of all HTTP requests and responses, set the NODE_DEBUG environment varible:

```
```shell
export NODE_DEBUG=http
```

Expand All @@ -63,17 +56,18 @@ We welcome PRs. Please create an issue and if applicable, a design before proce

once:

```bash
$ npm install
```
npm install
```

To build:

```bash
$ npm run build
```
npm run build
```

To run all tests:
```bash
$ npm test

```
npm test
```
8 changes: 8 additions & 0 deletions packages/http-client/RELEASES.md
@@ -1,5 +1,13 @@
## Releases

## 2.0.0
- The package is now compiled with TypeScript's [`strict` compiler setting](https://www.typescriptlang.org/tsconfig#strict). To comply with stricter rules:
- Some exported types now include `| null` or `| undefined`, matching their actual behavior.
- 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.
- Types using `any` have been scoped to more specific types.
- Following TypeScript's naming conventions, exported interfaces no longer begin with the prefix `I-`.
- The source code of the package was moved to build with [actions/toolkit](https://github.com/actions/toolkit).

## 1.0.11

Contains a bug fix where proxy is defined without a user and password. see [PR here](https://github.com/actions/http-client/pull/42)
Expand Down
64 changes: 38 additions & 26 deletions packages/http-client/__tests__/auth.test.ts
@@ -1,23 +1,27 @@
import * as httpm from '../_out'
import * as am from '../_out/auth'
import * as httpm from '../lib'
import * as am from '../lib/auth'

describe('auth', () => {
beforeEach(() => {})

afterEach(() => {})

it('does basic http get request with basic auth', async () => {
let bh: am.BasicCredentialHandler = new am.BasicCredentialHandler(
const bh: am.BasicCredentialHandler = new am.BasicCredentialHandler(
'johndoe',
'password'
)
let http: httpm.HttpClient = new httpm.HttpClient('http-client-tests', [bh])
let res: httpm.HttpClientResponse = await http.get('http://httpbin.org/get')
const http: httpm.HttpClient = new httpm.HttpClient('http-client-tests', [
bh
])
const res: httpm.HttpClientResponse = await http.get(
'http://httpbin.org/get'
)
expect(res.message.statusCode).toBe(200)
let body: string = await res.readBody()
let obj: any = JSON.parse(body)
let auth: string = obj.headers.Authorization
let creds: string = Buffer.from(
const body: string = await res.readBody()
const obj = JSON.parse(body)
const auth: string = obj.headers.Authorization
const creds: string = Buffer.from(
auth.substring('Basic '.length),
'base64'
).toString()
Expand All @@ -26,36 +30,44 @@ describe('auth', () => {
})

it('does basic http get request with pat token auth', async () => {
let token: string = 'scbfb44vxzku5l4xgc3qfazn3lpk4awflfryc76esaiq7aypcbhs'
let ph: am.PersonalAccessTokenCredentialHandler = new am.PersonalAccessTokenCredentialHandler(
const token = 'scbfb44vxzku5l4xgc3qfazn3lpk4awflfryc76esaiq7aypcbhs'
const ph: am.PersonalAccessTokenCredentialHandler = new am.PersonalAccessTokenCredentialHandler(
token
)

let http: httpm.HttpClient = new httpm.HttpClient('http-client-tests', [ph])
let res: httpm.HttpClientResponse = await http.get('http://httpbin.org/get')
const http: httpm.HttpClient = new httpm.HttpClient('http-client-tests', [
ph
])
const res: httpm.HttpClientResponse = await http.get(
'http://httpbin.org/get'
)
expect(res.message.statusCode).toBe(200)
let body: string = await res.readBody()
let obj: any = JSON.parse(body)
let auth: string = obj.headers.Authorization
let creds: string = Buffer.from(
const body: string = await res.readBody()
const obj = JSON.parse(body)
const auth: string = obj.headers.Authorization
const creds: string = Buffer.from(
auth.substring('Basic '.length),
'base64'
).toString()
expect(creds).toBe('PAT:' + token)
expect(creds).toBe(`PAT:${token}`)
expect(obj.url).toBe('http://httpbin.org/get')
})

it('does basic http get request with pat token auth', async () => {
let token: string = 'scbfb44vxzku5l4xgc3qfazn3lpk4awflfryc76esaiq7aypcbhs'
let ph: am.BearerCredentialHandler = new am.BearerCredentialHandler(token)
const token = 'scbfb44vxzku5l4xgc3qfazn3lpk4awflfryc76esaiq7aypcbhs'
const ph: am.BearerCredentialHandler = new am.BearerCredentialHandler(token)

let http: httpm.HttpClient = new httpm.HttpClient('http-client-tests', [ph])
let res: httpm.HttpClientResponse = await http.get('http://httpbin.org/get')
const http: httpm.HttpClient = new httpm.HttpClient('http-client-tests', [
ph
])
const res: httpm.HttpClientResponse = await http.get(
'http://httpbin.org/get'
)
expect(res.message.statusCode).toBe(200)
let body: string = await res.readBody()
let obj: any = JSON.parse(body)
let auth: string = obj.headers.Authorization
expect(auth).toBe('Bearer ' + token)
const body: string = await res.readBody()
const obj = JSON.parse(body)
const auth: string = obj.headers.Authorization
expect(auth).toBe(`Bearer ${token}`)
expect(obj.url).toBe('http://httpbin.org/get')
})
})

0 comments on commit f300e2c

Please sign in to comment.