Skip to content

Commit

Permalink
feat: add GitHubClient logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ovsds committed Aug 1, 2024
1 parent e931a74 commit fd80d1e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 '<major>', '<minor>', '<patch>' | `v$<major>.$<minor>.$<patch>` |
| `version_template` | Version template to use for version parsing, must contain named groups `<major>`, `<minor>`, `<patch>` | `v$<major>.$<minor>.$<patch>` |
| `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?(?<major>[0-9]+)\.(?<minor>[0-9]+)\.(?<patch>[0-9]+).*$` |
| `release_filter_target_commitish` | Target commitish filter for latest release. | |
Expand Down
41 changes: 23 additions & 18 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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* () {
Expand All @@ -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;
Expand Down Expand Up @@ -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 };
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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 */
/******/ (() => {
Expand All @@ -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
Expand All @@ -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__;
/******/
/******/
/******/ })()
;
;
19 changes: 15 additions & 4 deletions src/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export interface ActionResult {
version: string;
}

interface LogFunction {
(message: string): void;
}

interface ActionOptions {
versionShift: VersionShiftLiteral;
versionTemplate: string;
Expand All @@ -16,7 +20,7 @@ interface ActionOptions {
githubOwner: string;
githubRepo: string;
githubToken: string;
logger: (message: string) => void;
logger: LogFunction;
}

interface Release {
Expand All @@ -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<Release[]> {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit fd80d1e

Please sign in to comment.