Skip to content

Commit

Permalink
feat: switch to using actions/http-client (#94)
Browse files Browse the repository at this point in the history
* feat: switch to using actions/http-client

* use actions/http-client retry, address comments

* inject more context when rejected

* better error message

* run docs

* better error

* switch from getJson to get
  • Loading branch information
bharathkkb committed Feb 14, 2022
1 parent ca75faf commit f15cbfb
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 67 deletions.
2 changes: 1 addition & 1 deletion docs/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ The latest stable version of the gcloud SDK.

#### Defined in

[version-util.ts:36](https://github.com/google-github-actions/setup-cloud-sdk/blob/main/src/version-util.ts#L36)
[version-util.ts:35](https://github.com/google-github-actions/setup-cloud-sdk/blob/main/src/version-util.ts#L35)

___

Expand Down
42 changes: 31 additions & 11 deletions package-lock.json

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

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@
"dependencies": {
"@actions/core": "^1.6.0",
"@actions/exec": "^1.1.0",
"@actions/http-client": "^1.0.11",
"@actions/tool-cache": "^1.7.1",
"@lifeomic/attempt": "^3.0.2"
"@google-github-actions/actions-utils": "^0.1.2"
},
"devDependencies": {
"@types/chai": "^4.3.x",
Expand All @@ -40,15 +41,15 @@
"@typescript-eslint/parser": "^5.10.2",
"@vercel/ncc": "^0.33.1",
"chai": "^4.3.x",
"eslint": "^8.8.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint": "^8.8.0",
"mocha": "^9.2.0",
"prettier": "^2.5.1",
"sinon": "^13.0.1",
"ts-node": "^10.4.0",
"typedoc-plugin-markdown": "^3.11.12",
"typedoc": "^0.22.11",
"typedoc-plugin-markdown": "^3.11.12",
"typescript": "^4.5.5"
}
}
71 changes: 19 additions & 52 deletions src/version-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
* Contains version utility functions.
*/

import https from 'https';
import { URL } from 'url';
import { retry } from '@lifeomic/attempt';
import { HttpClient } from '@actions/http-client';
import { errorMessage } from '@google-github-actions/actions-utils';

import { userAgentString } from './user-agent-util';

Expand All @@ -34,15 +33,7 @@ const cloudSDKComponentsURL = 'https://dl.google.com/dl/cloudsdk/channels/rapid/
* @returns The latest stable version of the gcloud SDK.
*/
export async function getLatestGcloudSDKVersion(): Promise<string> {
const retryOpts = {
delay: 200,
factor: 2,
maxAttempts: 3,
};

return await retry(async () => {
return await getGcloudVersion(cloudSDKComponentsURL);
}, retryOpts);
return await getGcloudVersion(cloudSDKComponentsURL);
}

/**
Expand All @@ -54,47 +45,23 @@ export async function getLatestGcloudSDKVersion(): Promise<string> {
*
*/
async function getGcloudVersion(url: string): Promise<string> {
const u = new URL(url);

const opts = {
hostname: u.hostname,
port: u.port,
path: u.pathname + u.search,
method: 'GET',
headers: {
'User-Agent': userAgentString,
},
};

const resp: string = await new Promise((resolve, reject) => {
const req = https.request(opts, (res) => {
res.setEncoding('utf8');

let body = '';
res.on('data', (data) => {
body += data;
});

res.on('end', () => {
if (res.statusCode && res.statusCode >= 400) {
reject(body);
} else {
resolve(body);
}
});
});

req.on('error', (err) => {
reject(err);
});
try {
const http = new HttpClient(userAgentString, undefined, { allowRetries: true, maxRetries: 3 });
const res = await http.get(url);

req.end();
});
const body = await res.readBody();
const statusCode = res.message.statusCode || 500;
if (statusCode >= 400) {
throw new Error(`(${statusCode}) ${body}`);
}

const body = JSON.parse(resp);
const version = body.version;
if (!version) {
throw new Error(`Failed to retrieve gcloud SDK version, invalid response body: ${body}`);
const parsed = JSON.parse(body) as { version: string };
if (!parsed.version) {
throw new Error(`invalid response - ${body}`);
}
return parsed.version;
} catch (err) {
const msg = errorMessage(err);
throw new Error(`failed to retrieve gcloud SDK version from ${url}: ${msg}`);
}
return version;
}

0 comments on commit f15cbfb

Please sign in to comment.