Skip to content

Commit

Permalink
Add CacheStore and use wrap hook for Octokit
Browse files Browse the repository at this point in the history
  • Loading branch information
yykamei committed Mar 30, 2024
1 parent f7f2dc1 commit 2578599
Show file tree
Hide file tree
Showing 6 changed files with 261 additions and 113 deletions.
292 changes: 189 additions & 103 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5448,104 +5448,6 @@ legacyRestEndpointMethods.VERSION = VERSION;
0 && (0);


/***/ }),

/***/ 537:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

"use strict";

var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// pkg/dist-src/index.js
var dist_src_exports = {};
__export(dist_src_exports, {
RequestError: () => RequestError
});
module.exports = __toCommonJS(dist_src_exports);
var import_deprecation = __nccwpck_require__(8932);
var import_once = __toESM(__nccwpck_require__(1223));
var logOnceCode = (0, import_once.default)((deprecation) => console.warn(deprecation));
var logOnceHeaders = (0, import_once.default)((deprecation) => console.warn(deprecation));
var RequestError = class extends Error {
constructor(message, statusCode, options) {
super(message);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
this.name = "HttpError";
this.status = statusCode;
let headers;
if ("headers" in options && typeof options.headers !== "undefined") {
headers = options.headers;
}
if ("response" in options) {
this.response = options.response;
headers = options.response.headers;
}
const requestCopy = Object.assign({}, options.request);
if (options.request.headers.authorization) {
requestCopy.headers = Object.assign({}, options.request.headers, {
authorization: options.request.headers.authorization.replace(
/ .*$/,
" [REDACTED]"
)
});
}
requestCopy.url = requestCopy.url.replace(/\bclient_secret=\w+/g, "client_secret=[REDACTED]").replace(/\baccess_token=\w+/g, "access_token=[REDACTED]");
this.request = requestCopy;
Object.defineProperty(this, "code", {
get() {
logOnceCode(
new import_deprecation.Deprecation(
"[@octokit/request-error] `error.code` is deprecated, use `error.status`."
)
);
return statusCode;
}
});
Object.defineProperty(this, "headers", {
get() {
logOnceHeaders(
new import_deprecation.Deprecation(
"[@octokit/request-error] `error.headers` is deprecated, use `error.response.headers`."
)
);
return headers || {};
}
});
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (0);


/***/ }),

/***/ 6234:
Expand Down Expand Up @@ -5597,7 +5499,7 @@ function isPlainObject(value) {
}

// pkg/dist-src/fetch-wrapper.js
var import_request_error = __nccwpck_require__(537);
var import_request_error = __nccwpck_require__(13);

// pkg/dist-src/get-buffer-response.js
function getBufferResponse(response) {
Expand Down Expand Up @@ -5775,6 +5677,104 @@ var request = withDefaults(import_endpoint.endpoint, {
0 && (0);


/***/ }),

/***/ 13:
/***/ ((module, __unused_webpack_exports, __nccwpck_require__) => {

"use strict";

var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);

// pkg/dist-src/index.js
var dist_src_exports = {};
__export(dist_src_exports, {
RequestError: () => RequestError
});
module.exports = __toCommonJS(dist_src_exports);
var import_deprecation = __nccwpck_require__(8932);
var import_once = __toESM(__nccwpck_require__(1223));
var logOnceCode = (0, import_once.default)((deprecation) => console.warn(deprecation));
var logOnceHeaders = (0, import_once.default)((deprecation) => console.warn(deprecation));
var RequestError = class extends Error {
constructor(message, statusCode, options) {
super(message);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
this.name = "HttpError";
this.status = statusCode;
let headers;
if ("headers" in options && typeof options.headers !== "undefined") {
headers = options.headers;
}
if ("response" in options) {
this.response = options.response;
headers = options.response.headers;
}
const requestCopy = Object.assign({}, options.request);
if (options.request.headers.authorization) {
requestCopy.headers = Object.assign({}, options.request.headers, {
authorization: options.request.headers.authorization.replace(
/ .*$/,
" [REDACTED]"
)
});
}
requestCopy.url = requestCopy.url.replace(/\bclient_secret=\w+/g, "client_secret=[REDACTED]").replace(/\baccess_token=\w+/g, "access_token=[REDACTED]");
this.request = requestCopy;
Object.defineProperty(this, "code", {
get() {
logOnceCode(
new import_deprecation.Deprecation(
"[@octokit/request-error] `error.code` is deprecated, use `error.status`."
)
);
return statusCode;
}
});
Object.defineProperty(this, "headers", {
get() {
logOnceHeaders(
new import_deprecation.Deprecation(
"[@octokit/request-error] `error.headers` is deprecated, use `error.response.headers`."
)
);
return headers || {};
}
});
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (0);


/***/ }),

/***/ 3682:
Expand Down Expand Up @@ -28980,7 +28980,7 @@ __nccwpck_require__.a(module, async (__webpack_handle_async_dependencies__, __we
__nccwpck_require__.r(__webpack_exports__);
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0__ = __nccwpck_require__(2186);
/* harmony import */ var _actions_core__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__nccwpck_require__.n(_actions_core__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var _main__WEBPACK_IMPORTED_MODULE_1__ = __nccwpck_require__(4377);
/* harmony import */ var _main__WEBPACK_IMPORTED_MODULE_1__ = __nccwpck_require__(211);


try {
Expand All @@ -28996,7 +28996,7 @@ __webpack_async_result__();

/***/ }),

/***/ 4377:
/***/ 211:
/***/ ((__unused_webpack_module, __webpack_exports__, __nccwpck_require__) => {

"use strict";
Expand All @@ -29010,6 +29010,46 @@ __nccwpck_require__.d(__webpack_exports__, {
var core = __nccwpck_require__(2186);
// EXTERNAL MODULE: ./node_modules/@actions/github/lib/github.js
var github = __nccwpck_require__(5438);
;// CONCATENATED MODULE: ./node_modules/@octokit/request-error/dist-src/index.js
class RequestError extends Error {
name;
/**
* http status code
*/
status;
/**
* Request options that lead to the error.
*/
request;
/**
* Response object if a response was received
*/
response;
constructor(message, statusCode, options) {
super(message);
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor);
}
this.name = "HttpError";
this.status = statusCode;
if ("response" in options) {
this.response = options.response;
}
const requestCopy = Object.assign({}, options.request);
if (options.request.headers.authorization) {
requestCopy.headers = Object.assign({}, options.request.headers, {
authorization: options.request.headers.authorization.replace(
/ .*$/,
" [REDACTED]"
)
});
}
requestCopy.url = requestCopy.url.replace(/\bclient_secret=\w+/g, "client_secret=[REDACTED]").replace(/\baccess_token=\w+/g, "access_token=[REDACTED]");
this.request = requestCopy;
}
}


;// CONCATENATED MODULE: ./src/Duration.ts
class Duration {
milliseconds;
Expand Down Expand Up @@ -29100,11 +29140,44 @@ class Usage {




const GITHUB_LINK_REL_REXT = 'rel="next"';
class GitHubAPIClient {
cacheStore;
client;
constructor(token) {
constructor(token, cacheStore) {
this.cacheStore = cacheStore;
this.client = (0,github.getOctokit)(token);
this.client.hook.wrap("request", async (request, options) => {
let cache = null;
// @ts-ignore
const cacheKey = options.cacheKey;
// @ts-ignore
options.cacheKey = undefined;
if (cacheKey) {
cache = await this.cacheStore.read(cacheKey);
if (cache) {
options.headers["If-None-Match"] = cache.etag;
}
}
try {
const response = await request(options);
response.data;
if (cacheKey) {
await this.cacheStore.write(cacheKey, response);
}
return response;
}
catch (e) {
if (cache &&
e instanceof RequestError &&
e.status === 304 &&
e.response) {
return { ...e.response, data: cache.data };
}
throw e;
}
});
this.client.hook.after("request", async (response, options) => {
const rateLimit = response.headers["x-ratelimit-limit"];
const rateLimitRemaining = response.headers["x-ratelimit-remaining"];
Expand Down Expand Up @@ -29191,6 +29264,7 @@ class GitHubAPIClient {
headers: {
"X-GitHub-Api-Version": "2022-11-28",
},
cacheKey: `/repos/${owner}/${repo}/actions/runs/${runId}/timing`,
});
const durationMs = response.data.run_duration_ms;
return new Usage(runId, durationMs ? new Duration(durationMs) : null);
Expand Down Expand Up @@ -29368,6 +29442,17 @@ class Input {
}
}

;// CONCATENATED MODULE: ./src/MemoryCacheStore.ts
class MemoryCacheStore {
data = {};
async read(cacheKey) {
return this.data[cacheKey] || null;
}
async write(cacheKey, data) {
this.data[cacheKey] = data;
}
}

;// CONCATENATED MODULE: ./src/MermaidXYChart.ts
class MermaidXYChart {
workflow;
Expand Down Expand Up @@ -29428,10 +29513,11 @@ xychart-beta




const main = async () => {
const now = new Date();
const input = new Input();
const apiClient = new GitHubAPIClient(input.token);
const apiClient = new GitHubAPIClient(input.token, new MemoryCacheStore());
const repository = new GitHubRepository(input.owner, input.repo, apiClient);
const workflows = await repository.getWorkflows(input.only);
const charts = await Promise.all(workflows.map(async (w) => {
Expand Down
9 changes: 9 additions & 0 deletions src/CacheStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type OctokitCachedData = {
readonly etag?: string;
readonly data: unknown;
};

export interface CacheStore {
read(cacheKey: string): Promise<OctokitCachedData | null>;
write(cacheKey: string, response: OctokitCachedData): Promise<void>;
}
Loading

0 comments on commit 2578599

Please sign in to comment.