From 2a6b73e004cffda4abf85e79656bcf1ab2d3504e Mon Sep 17 00:00:00 2001 From: Dmitrii Ovsyannikov Date: Thu, 1 Aug 2024 15:16:43 +0200 Subject: [PATCH] feat: add GitHubClient logging (#2) --- README.md | 4 ++-- dist/index.js | 41 +++++++++++++++++++++++------------------ src/action.ts | 19 +++++++++++++++---- 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index b78e4a8..053a404 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Get Next Release Version Action -[![CI](https://github.com/ovsds/get-next-release-version-action/workflows/Check%20PR/badge.svg)](https://github.com/ovsds/get-next-release-version-action/actions?query=workflow%3A%22%22Check+PR%22%22) +[![CI](https://github.com/datalens-tech/get-next-release-version-action/workflows/Check%20PR/badge.svg)](https://github.com/ovsds/get-next-release-version-action/actions?query=workflow%3A%22%22Check+PR%22%22) [![GitHub Marketplace](https://img.shields.io/badge/Marketplace-Get%20Next%20Release%20Version-blue.svg)](https://github.com/marketplace/actions/get-next-release-version) Get Next Release Version Action @@ -18,7 +18,7 @@ placeholder # TODO: Add example | Name | Description | Default | | --------------------------------- | ------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------- | | `version_shift` | Version shift, used to calculate next version `[major, minor, patch]`. | `minor` | -| `version_template` | Version template to use for version parsing, must contain named groups '', '', '' | `v$.$.$` | +| `version_template` | Version template to use for version parsing, must contain named groups ``, ``, `` | `v$.$.$` | | `version_override` | Version override, if used then will be output directly. | | | `release_version_regexp` | Version regexp to use for version parsing, must contain named groups 'major', 'minor', 'patch'. | `v?(?[0-9]+)\.(?[0-9]+)\.(?[0-9]+).*$` | | `release_filter_target_commitish` | Target commitish filter for latest release. | | diff --git a/dist/index.js b/dist/index.js index a5f731c..3510694 100644 --- a/dist/index.js +++ b/dist/index.js @@ -19,13 +19,14 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); exports.Action = void 0; const rest_1 = __nccwpck_require__(1273); class GitHubClient { - static fromGithubToken(githubToken) { + static fromGithubToken(githubToken, logger) { return new GitHubClient(new rest_1.Octokit({ auth: githubToken, - })); + }), logger); } - constructor(octokitClient) { + constructor(octokitClient, logger) { this.octokitClient = octokitClient; + this.logger = logger; } getReleases(owner, repo, perPage, page) { return __awaiter(this, void 0, void 0, function* () { @@ -48,14 +49,18 @@ class GitHubClient { const perPage = 100; while (true) { const releases = yield this.getReleases(owner, repo, perPage, page); + this.logger(`Fetched ${releases.length} releases from page ${page}`); for (const release of releases) { if (targetCommitish !== "" && release.targetCommitish !== targetCommitish) { + this.logger(`Skipping release ${release.tagName} because target commitish does not match`); continue; } if (prerelease !== null && release.prerelease !== prerelease) { + this.logger(`Skipping release ${release.tagName} because prerelease status does not match`); continue; } if (!releaseVersionRegexp.test(release.tagName)) { + this.logger(`Skipping release ${release.tagName} because it does not match the version regexp`); continue; } return release; @@ -106,7 +111,7 @@ class Action { if (this.options.versionOverride !== "") { return { version: this.options.versionOverride }; } - const githubClient = GitHubClient.fromGithubToken(this.options.githubToken); + const githubClient = GitHubClient.fromGithubToken(this.options.githubToken, this.options.logger); const latestRelease = yield githubClient.getLatestRelease(this.options.githubOwner, this.options.githubRepo, this.options.releaseFilterTargetCommitish, this.options.releaseFilterPrerelease, this.options.releaseVersionRegexp); const newVersion = shiftVersion(latestRelease.tagName, this.options.releaseVersionRegexp, this.options.versionShift, this.options.versionTemplate); return { version: newVersion }; @@ -824,8 +829,8 @@ class OidcClient { const res = yield httpclient .getJson(id_token_url) .catch(error => { - throw new Error(`Failed to get ID Token. \n - Error Code : ${error.statusCode}\n + throw new Error(`Failed to get ID Token. \n + Error Code : ${error.statusCode}\n Error Message: ${error.message}`); }); const id_token = (_a = res.result) === null || _a === void 0 ? void 0 : _a.value; @@ -30607,7 +30612,7 @@ const dist_src_Octokit = Octokit.plugin(requestLog, legacyRestEndpointMethods, p /************************************************************************/ /******/ // The module cache /******/ var __webpack_module_cache__ = {}; -/******/ +/******/ /******/ // The require function /******/ function __nccwpck_require__(moduleId) { /******/ // Check if module is in cache @@ -30621,7 +30626,7 @@ const dist_src_Octokit = Octokit.plugin(requestLog, legacyRestEndpointMethods, p /******/ // no module.loaded needed /******/ exports: {} /******/ }; -/******/ +/******/ /******/ // Execute the module function /******/ var threw = true; /******/ try { @@ -30630,11 +30635,11 @@ const dist_src_Octokit = Octokit.plugin(requestLog, legacyRestEndpointMethods, p /******/ } finally { /******/ if(threw) delete __webpack_module_cache__[moduleId]; /******/ } -/******/ +/******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } -/******/ +/******/ /************************************************************************/ /******/ /* webpack/runtime/define property getters */ /******/ (() => { @@ -30647,12 +30652,12 @@ const dist_src_Octokit = Octokit.plugin(requestLog, legacyRestEndpointMethods, p /******/ } /******/ }; /******/ })(); -/******/ +/******/ /******/ /* webpack/runtime/hasOwnProperty shorthand */ /******/ (() => { /******/ __nccwpck_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop)) /******/ })(); -/******/ +/******/ /******/ /* webpack/runtime/make namespace object */ /******/ (() => { /******/ // define __esModule on exports @@ -30663,18 +30668,18 @@ const dist_src_Octokit = Octokit.plugin(requestLog, legacyRestEndpointMethods, p /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ })(); -/******/ +/******/ /******/ /* webpack/runtime/compat */ -/******/ +/******/ /******/ if (typeof __nccwpck_require__ !== 'undefined') __nccwpck_require__.ab = __dirname + "/"; -/******/ +/******/ /************************************************************************/ -/******/ +/******/ /******/ // startup /******/ // Load entry module and return exports /******/ // This entry module is referenced by other modules so it can't be inlined /******/ var __webpack_exports__ = __nccwpck_require__(3109); /******/ module.exports = __webpack_exports__; -/******/ +/******/ /******/ })() -; +; \ No newline at end of file diff --git a/src/action.ts b/src/action.ts index e5acedd..54c1a69 100644 --- a/src/action.ts +++ b/src/action.ts @@ -6,6 +6,10 @@ export interface ActionResult { version: string; } +interface LogFunction { + (message: string): void; +} + interface ActionOptions { versionShift: VersionShiftLiteral; versionTemplate: string; @@ -16,7 +20,7 @@ interface ActionOptions { githubOwner: string; githubRepo: string; githubToken: string; - logger: (message: string) => void; + logger: LogFunction; } interface Release { @@ -26,18 +30,21 @@ interface Release { } class GitHubClient { - static fromGithubToken(githubToken: string): GitHubClient { + static fromGithubToken(githubToken: string, logger: LogFunction): GitHubClient { return new GitHubClient( new Octokit({ auth: githubToken, }), + logger, ); } private readonly octokitClient: Octokit; + private readonly logger: LogFunction; - constructor(octokitClient: Octokit) { + constructor(octokitClient: Octokit, logger: LogFunction) { this.octokitClient = octokitClient; + this.logger = logger; } async getReleases(owner: string, repo: string, perPage: number, page: number): Promise { @@ -67,17 +74,21 @@ class GitHubClient { while (true) { const releases = await this.getReleases(owner, repo, perPage, page); + this.logger(`Fetched ${releases.length} releases from page ${page}`); for (const release of releases) { if (targetCommitish !== "" && release.targetCommitish !== targetCommitish) { + this.logger(`Skipping release ${release.tagName} because target commitish does not match`); continue; } if (prerelease !== null && release.prerelease !== prerelease) { + this.logger(`Skipping release ${release.tagName} because prerelease status does not match`); continue; } if (!releaseVersionRegexp.test(release.tagName)) { + this.logger(`Skipping release ${release.tagName} because it does not match the version regexp`); continue; } @@ -141,7 +152,7 @@ export class Action { return { version: this.options.versionOverride }; } - const githubClient = GitHubClient.fromGithubToken(this.options.githubToken); + const githubClient = GitHubClient.fromGithubToken(this.options.githubToken, this.options.logger); const latestRelease = await githubClient.getLatestRelease( this.options.githubOwner, this.options.githubRepo,