Skip to content

Commit

Permalink
Update other packages to use http-client v2 (#1082)
Browse files Browse the repository at this point in the history
We moved `@actions/http-client` to be part of the toolkit in #1062.  We also made some breaking changes to exported types and released v2.

The biggest change in terms of lines of code affected was to get rid of the `I-` prefix for interfaces since TypeScript doesn't follow this convention.

I bumped the patch version of all packages except for `tool-cache`, where I bumped the major version.  The rationale is explained in the release notes for that package.
  • Loading branch information
brcrista committed May 11, 2022
1 parent 925ae69 commit aa676f3
Show file tree
Hide file tree
Showing 30 changed files with 249 additions and 223 deletions.
6 changes: 5 additions & 1 deletion packages/artifact/RELEASES.md
Expand Up @@ -77,4 +77,8 @@

### 1.0.0

- Update `lockfileVersion` to `v2` in `package-lock.json [#1009](https://github.com/actions/toolkit/pull/1009)
- Update `lockfileVersion` to `v2` in `package-lock.json` [#1009](https://github.com/actions/toolkit/pull/1009)

### 1.0.1

- Update to v2.0.0 of `@actions/http-client`
5 changes: 2 additions & 3 deletions packages/artifact/__tests__/retry.test.ts
Expand Up @@ -3,7 +3,6 @@ import * as net from 'net'
import * as core from '@actions/core'
import * as configVariables from '../src/internal/config-variables'
import {retry} from '../src/internal/requestUtils'
import {IHttpClientResponse} from '@actions/http-client/interfaces'
import {HttpClientResponse} from '@actions/http-client'

jest.mock('../src/internal/config-variables')
Expand Down Expand Up @@ -42,7 +41,7 @@ async function testRetry(

async function handleResponse(
testResponseCode: number | undefined
): Promise<IHttpClientResponse> {
): Promise<HttpClientResponse> {
if (!testResponseCode) {
throw new Error(
'Test incorrectly set up. reverse.pop() was called too many times so not enough test response codes were supplied'
Expand Down Expand Up @@ -72,7 +71,7 @@ async function emptyMockReadBody(): Promise<string> {

async function setupSingleMockResponse(
statusCode: number
): Promise<IHttpClientResponse> {
): Promise<HttpClientResponse> {
const mockMessage = new http.IncomingMessage(new net.Socket())
const mockReadBody = emptyMockReadBody
mockMessage.statusCode = statusCode
Expand Down
44 changes: 28 additions & 16 deletions packages/artifact/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions packages/artifact/package.json
@@ -1,6 +1,6 @@
{
"name": "@actions/artifact",
"version": "1.0.0",
"version": "1.0.1",
"preview": true,
"description": "Actions artifact lib",
"keywords": [
Expand Down Expand Up @@ -38,7 +38,7 @@
},
"dependencies": {
"@actions/core": "^1.2.6",
"@actions/http-client": "^1.0.11",
"@actions/http-client": "^2.0.0",
"tmp": "^0.2.1",
"tmp-promise": "^3.0.2"
},
Expand Down
8 changes: 4 additions & 4 deletions packages/artifact/src/internal/download-http-client.ts
Expand Up @@ -18,7 +18,7 @@ import {URL} from 'url'
import {StatusReporter} from './status-reporter'
import {performance} from 'perf_hooks'
import {ListArtifactsResponse, QueryArtifactResponse} from './contracts'
import {IHttpClientResponse} from '@actions/http-client/interfaces'
import {HttpClientResponse} from '@actions/http-client'
import {HttpManager} from './http-manager'
import {DownloadItem} from './download-specification'
import {getDownloadFileConcurrency, getRetryLimit} from './config-variables'
Expand Down Expand Up @@ -152,7 +152,7 @@ export class DownloadHttpClient {
const headers = getDownloadHeaders('application/json', true, true)

// a single GET request is used to download a file
const makeDownloadRequest = async (): Promise<IHttpClientResponse> => {
const makeDownloadRequest = async (): Promise<HttpClientResponse> => {
const client = this.downloadHttpManager.getClient(httpClientIndex)
return await client.get(artifactLocation, headers)
}
Expand Down Expand Up @@ -225,7 +225,7 @@ export class DownloadHttpClient {

// keep trying to download a file until a retry limit has been reached
while (retryCount <= retryLimit) {
let response: IHttpClientResponse
let response: HttpClientResponse
try {
response = await makeDownloadRequest()
} catch (error) {
Expand Down Expand Up @@ -295,7 +295,7 @@ export class DownloadHttpClient {
* @param isGzip a boolean denoting if the content is compressed using gzip and if we need to decode it
*/
async pipeResponseToFile(
response: IHttpClientResponse,
response: HttpClientResponse,
destinationStream: fs.WriteStream,
isGzip: boolean
): Promise<void> {
Expand Down
2 changes: 1 addition & 1 deletion packages/artifact/src/internal/http-manager.ts
@@ -1,4 +1,4 @@
import {HttpClient} from '@actions/http-client/index'
import {HttpClient} from '@actions/http-client'
import {createHttpClient} from './utils'

/**
Expand Down
12 changes: 6 additions & 6 deletions packages/artifact/src/internal/requestUtils.ts
@@ -1,4 +1,4 @@
import {IHttpClientResponse} from '@actions/http-client/interfaces'
import {HttpClientResponse} from '@actions/http-client'
import {
isRetryableStatusCode,
isSuccessStatusCode,
Expand All @@ -11,11 +11,11 @@ import {getRetryLimit} from './config-variables'

export async function retry(
name: string,
operation: () => Promise<IHttpClientResponse>,
operation: () => Promise<HttpClientResponse>,
customErrorMessages: Map<number, string>,
maxAttempts: number
): Promise<IHttpClientResponse> {
let response: IHttpClientResponse | undefined = undefined
): Promise<HttpClientResponse> {
let response: HttpClientResponse | undefined = undefined
let statusCode: number | undefined = undefined
let isRetryable = false
let errorMessage = ''
Expand Down Expand Up @@ -71,9 +71,9 @@ export async function retry(

export async function retryHttpClientRequest(
name: string,
method: () => Promise<IHttpClientResponse>,
method: () => Promise<HttpClientResponse>,
customErrorMessages: Map<number, string> = new Map(),
maxAttempts = getRetryLimit()
): Promise<IHttpClientResponse> {
): Promise<HttpClientResponse> {
return await retry(name, method, customErrorMessages, maxAttempts)
}
9 changes: 4 additions & 5 deletions packages/artifact/src/internal/upload-http-client.ts
Expand Up @@ -31,8 +31,7 @@ import {promisify} from 'util'
import {URL} from 'url'
import {performance} from 'perf_hooks'
import {StatusReporter} from './status-reporter'
import {HttpCodes} from '@actions/http-client'
import {IHttpClientResponse} from '@actions/http-client/interfaces'
import {HttpCodes, HttpClientResponse} from '@actions/http-client'
import {HttpManager} from './http-manager'
import {UploadSpecification} from './upload-specification'
import {UploadOptions} from './upload-options'
Expand Down Expand Up @@ -416,7 +415,7 @@ export class UploadHttpClient {
getContentRange(start, end, uploadFileSize)
)

const uploadChunkRequest = async (): Promise<IHttpClientResponse> => {
const uploadChunkRequest = async (): Promise<HttpClientResponse> => {
const client = this.uploadHttpManager.getClient(httpClientIndex)
return await client.sendStream('PUT', resourceUrl, openStream(), headers)
}
Expand All @@ -427,7 +426,7 @@ export class UploadHttpClient {
// Increments the current retry count and then checks if the retry limit has been reached
// If there have been too many retries, fail so the download stops
const incrementAndCheckRetryLimit = (
response?: IHttpClientResponse
response?: HttpClientResponse
): boolean => {
retryCount++
if (retryCount > retryLimit) {
Expand Down Expand Up @@ -464,7 +463,7 @@ export class UploadHttpClient {

// allow for failed chunks to be retried multiple times
while (retryCount <= retryLimit) {
let response: IHttpClientResponse
let response: HttpClientResponse

try {
response = await uploadChunkRequest()
Expand Down
19 changes: 9 additions & 10 deletions packages/artifact/src/internal/utils.ts
@@ -1,9 +1,8 @@
import {debug, info, warning} from '@actions/core'
import {promises as fs} from 'fs'
import {HttpCodes, HttpClient} from '@actions/http-client'
import {BearerCredentialHandler} from '@actions/http-client/auth'
import {IHeaders, IHttpClientResponse} from '@actions/http-client/interfaces'
import {IncomingHttpHeaders} from 'http'
import {IncomingHttpHeaders, OutgoingHttpHeaders} from 'http'
import {debug, info, warning} from '@actions/core'
import {HttpCodes, HttpClient, HttpClientResponse} from '@actions/http-client'
import {BearerCredentialHandler} from '@actions/http-client/lib/auth'
import {
getRuntimeToken,
getRuntimeUrl,
Expand Down Expand Up @@ -139,8 +138,8 @@ export function getDownloadHeaders(
contentType: string,
isKeepAlive?: boolean,
acceptGzip?: boolean
): IHeaders {
const requestOptions: IHeaders = {}
): OutgoingHttpHeaders {
const requestOptions: OutgoingHttpHeaders = {}

if (contentType) {
requestOptions['Content-Type'] = contentType
Expand Down Expand Up @@ -181,8 +180,8 @@ export function getUploadHeaders(
uncompressedLength?: number,
contentLength?: number,
contentRange?: string
): IHeaders {
const requestOptions: IHeaders = {}
): OutgoingHttpHeaders {
const requestOptions: OutgoingHttpHeaders = {}
requestOptions['Accept'] = `application/json;api-version=${getApiVersion()}`
if (contentType) {
requestOptions['Content-Type'] = contentType
Expand Down Expand Up @@ -227,7 +226,7 @@ export function getArtifactUrl(): string {
* Certain information such as the TLSSocket and the Readable state are not really useful for diagnostic purposes so they can be avoided.
* Other information such as the headers, the response code and message might be useful, so this is displayed.
*/
export function displayHttpDiagnostics(response: IHttpClientResponse): void {
export function displayHttpDiagnostics(response: HttpClientResponse): void {
info(
`##### Begin Diagnostic HTTP information #####
Status Code: ${response.message.statusCode}
Expand Down
3 changes: 3 additions & 0 deletions packages/cache/RELEASES.md
Expand Up @@ -56,3 +56,6 @@

### 2.0.0
- Added support to check if Actions cache service feature is available or not [#1028](https://github.com/actions/toolkit/pull/1028)

### 2.0.3
- Update to v2.0.0 of `@actions/http-client`
8 changes: 4 additions & 4 deletions packages/cache/__tests__/saveCache.test.ts
Expand Up @@ -5,7 +5,7 @@ import * as cacheHttpClient from '../src/internal/cacheHttpClient'
import * as cacheUtils from '../src/internal/cacheUtils'
import {CacheFilename, CompressionMethod} from '../src/internal/constants'
import * as tar from '../src/internal/tar'
import {ITypedResponse} from '@actions/http-client/interfaces'
import {TypedResponse} from '@actions/http-client/lib/interfaces'
import {
ReserveCacheResponse,
ITypedResponseWithError
Expand Down Expand Up @@ -172,7 +172,7 @@ test('save with reserve cache failure should fail', async () => {
const reserveCacheMock = jest
.spyOn(cacheHttpClient, 'reserveCache')
.mockImplementation(async () => {
const response: ITypedResponse<ReserveCacheResponse> = {
const response: TypedResponse<ReserveCacheResponse> = {
statusCode: 500,
result: null,
headers: {}
Expand Down Expand Up @@ -208,7 +208,7 @@ test('save with server error should fail', async () => {
const reserveCacheMock = jest
.spyOn(cacheHttpClient, 'reserveCache')
.mockImplementation(async () => {
const response: ITypedResponse<ReserveCacheResponse> = {
const response: TypedResponse<ReserveCacheResponse> = {
statusCode: 500,
result: {cacheId},
headers: {}
Expand Down Expand Up @@ -257,7 +257,7 @@ test('save with valid inputs uploads a cache', async () => {
const reserveCacheMock = jest
.spyOn(cacheHttpClient, 'reserveCache')
.mockImplementation(async () => {
const response: ITypedResponse<ReserveCacheResponse> = {
const response: TypedResponse<ReserveCacheResponse> = {
statusCode: 500,
result: {cacheId},
headers: {}
Expand Down

0 comments on commit aa676f3

Please sign in to comment.