Skip to content

Commit

Permalink
refactor: unify main and mainRegex functions to remove duplicate code (
Browse files Browse the repository at this point in the history
…#44)

* refactor: unify main and mainRegex functions to remove duplicate code

* chore: run build

* fix: correctly set input variable

* fix: reorder variables

* fix: correctly sets outputPath
  • Loading branch information
dsaltares authored May 8, 2022
1 parent 78a7243 commit aaca6e0
Show file tree
Hide file tree
Showing 2 changed files with 230 additions and 71 deletions.
255 changes: 216 additions & 39 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,171 @@ var require_oidc_utils = __commonJS({
}
});

// node_modules/@actions/core/lib/summary.js
var require_summary = __commonJS({
"node_modules/@actions/core/lib/summary.js"(exports) {
"use strict";
var __awaiter = exports && exports.__awaiter || function(thisArg, _arguments, P, generator) {
function adopt(value) {
return value instanceof P ? value : new P(function(resolve) {
resolve(value);
});
}
return new (P || (P = Promise))(function(resolve, reject) {
function fulfilled(value) {
try {
step(generator.next(value));
} catch (e2) {
reject(e2);
}
}
function rejected(value) {
try {
step(generator["throw"](value));
} catch (e2) {
reject(e2);
}
}
function step(result) {
result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected);
}
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.summary = exports.markdownSummary = exports.SUMMARY_DOCS_URL = exports.SUMMARY_ENV_VAR = void 0;
var os_1 = require("os");
var fs_1 = require("fs");
var { access, appendFile, writeFile: writeFile2 } = fs_1.promises;
exports.SUMMARY_ENV_VAR = "GITHUB_STEP_SUMMARY";
exports.SUMMARY_DOCS_URL = "https://docs.github.com/actions/using-workflows/workflow-commands-for-github-actions#adding-a-job-summary";
var Summary = class {
constructor() {
this._buffer = "";
}
filePath() {
return __awaiter(this, void 0, void 0, function* () {
if (this._filePath) {
return this._filePath;
}
const pathFromEnv = process.env[exports.SUMMARY_ENV_VAR];
if (!pathFromEnv) {
throw new Error(`Unable to find environment variable for $${exports.SUMMARY_ENV_VAR}. Check if your runtime environment supports job summaries.`);
}
try {
yield access(pathFromEnv, fs_1.constants.R_OK | fs_1.constants.W_OK);
} catch (_a) {
throw new Error(`Unable to access summary file: '${pathFromEnv}'. Check if the file has correct read/write permissions.`);
}
this._filePath = pathFromEnv;
return this._filePath;
});
}
wrap(tag, content, attrs = {}) {
const htmlAttrs = Object.entries(attrs).map(([key, value]) => ` ${key}="${value}"`).join("");
if (!content) {
return `<${tag}${htmlAttrs}>`;
}
return `<${tag}${htmlAttrs}>${content}</${tag}>`;
}
write(options) {
return __awaiter(this, void 0, void 0, function* () {
const overwrite = !!(options === null || options === void 0 ? void 0 : options.overwrite);
const filePath = yield this.filePath();
const writeFunc = overwrite ? writeFile2 : appendFile;
yield writeFunc(filePath, this._buffer, { encoding: "utf8" });
return this.emptyBuffer();
});
}
clear() {
return __awaiter(this, void 0, void 0, function* () {
return this.emptyBuffer().write({ overwrite: true });
});
}
stringify() {
return this._buffer;
}
isEmptyBuffer() {
return this._buffer.length === 0;
}
emptyBuffer() {
this._buffer = "";
return this;
}
addRaw(text, addEOL = false) {
this._buffer += text;
return addEOL ? this.addEOL() : this;
}
addEOL() {
return this.addRaw(os_1.EOL);
}
addCodeBlock(code, lang) {
const attrs = Object.assign({}, lang && { lang });
const element = this.wrap("pre", this.wrap("code", code), attrs);
return this.addRaw(element).addEOL();
}
addList(items, ordered = false) {
const tag = ordered ? "ol" : "ul";
const listItems = items.map((item) => this.wrap("li", item)).join("");
const element = this.wrap(tag, listItems);
return this.addRaw(element).addEOL();
}
addTable(rows) {
const tableBody = rows.map((row) => {
const cells = row.map((cell) => {
if (typeof cell === "string") {
return this.wrap("td", cell);
}
const { header, data, colspan, rowspan } = cell;
const tag = header ? "th" : "td";
const attrs = Object.assign(Object.assign({}, colspan && { colspan }), rowspan && { rowspan });
return this.wrap(tag, data, attrs);
}).join("");
return this.wrap("tr", cells);
}).join("");
const element = this.wrap("table", tableBody);
return this.addRaw(element).addEOL();
}
addDetails(label, content) {
const element = this.wrap("details", this.wrap("summary", label) + content);
return this.addRaw(element).addEOL();
}
addImage(src, alt, options) {
const { width, height } = options || {};
const attrs = Object.assign(Object.assign({}, width && { width }), height && { height });
const element = this.wrap("img", null, Object.assign({ src, alt }, attrs));
return this.addRaw(element).addEOL();
}
addHeading(text, level) {
const tag = `h${level}`;
const allowedTag = ["h1", "h2", "h3", "h4", "h5", "h6"].includes(tag) ? tag : "h1";
const element = this.wrap(allowedTag, text);
return this.addRaw(element).addEOL();
}
addSeparator() {
const element = this.wrap("hr", null);
return this.addRaw(element).addEOL();
}
addBreak() {
const element = this.wrap("br", null);
return this.addRaw(element).addEOL();
}
addQuote(text, cite) {
const attrs = Object.assign({}, cite && { cite });
const element = this.wrap("blockquote", text, attrs);
return this.addRaw(element).addEOL();
}
addLink(text, href) {
const element = this.wrap("a", text, { href });
return this.addRaw(element).addEOL();
}
};
var _summary = new Summary();
exports.markdownSummary = _summary;
exports.summary = _summary;
}
});

// node_modules/@actions/core/lib/core.js
var require_core = __commonJS({
"node_modules/@actions/core/lib/core.js"(exports) {
Expand Down Expand Up @@ -1296,6 +1461,14 @@ Support boolean input list: \`true | True | TRUE | false | False | FALSE\``);
});
}
exports.getIDToken = getIDToken;
var summary_1 = require_summary();
Object.defineProperty(exports, "summary", { enumerable: true, get: function() {
return summary_1.summary;
} });
var summary_2 = require_summary();
Object.defineProperty(exports, "markdownSummary", { enumerable: true, get: function() {
return summary_2.markdownSummary;
} });
}
});

Expand Down Expand Up @@ -9755,10 +9928,13 @@ var require_ponyfill_es2018 = __commonJS({
const byteLengthSizeFunction = (chunk) => {
return chunk.byteLength;
};
Object.defineProperty(byteLengthSizeFunction, "name", {
value: "size",
configurable: true
});
try {
Object.defineProperty(byteLengthSizeFunction, "name", {
value: "size",
configurable: true
});
} catch (_a) {
}
class ByteLengthQueuingStrategy {
constructor(options) {
assertRequiredArgument(options, 1, "ByteLengthQueuingStrategy");
Expand Down Expand Up @@ -9803,10 +9979,13 @@ var require_ponyfill_es2018 = __commonJS({
const countSizeFunction = () => {
return 1;
};
Object.defineProperty(countSizeFunction, "name", {
value: "size",
configurable: true
});
try {
Object.defineProperty(countSizeFunction, "name", {
value: "size",
configurable: true
});
} catch (_a) {
}
class CountQueuingStrategy {
constructor(options) {
assertRequiredArgument(options, 1, "CountQueuingStrategy");
Expand Down Expand Up @@ -10925,10 +11104,10 @@ var init_multipart_parser = __esm({
});

// index.ts
var import_path = require("path");
var import_promises = require("fs/promises");
var core = __toESM(require_core());
var github = __toESM(require_github());
var import_promises = require("fs/promises");
var import_path = require("path");

// node_modules/node-fetch/src/index.js
var import_node_http2 = __toESM(require("node:http"), 1);
Expand Down Expand Up @@ -11666,7 +11845,9 @@ var Request = class extends Body {
throw new TypeError(`${parsedURL} is an url with embedded credentials.`);
}
let method = init.method || input.method || "GET";
method = method.toUpperCase();
if (/^(delete|get|head|options|post|put)$/i.test(method)) {
method = method.toUpperCase();
}
if ("data" in init) {
doBadDataWarn();
}
Expand Down Expand Up @@ -12108,20 +12289,25 @@ var getRelease = (octokit, { owner, repo, version }) => {
if (version === "latest") {
return octokit.rest.repos.getLatestRelease({ owner, repo });
} else if (tagsMatch !== null && tagsMatch[1]) {
return octokit.rest.repos.getReleaseByTag({ owner, repo, tag: tagsMatch[1] });
return octokit.rest.repos.getReleaseByTag({
owner,
repo,
tag: tagsMatch[1]
});
} else {
return octokit.rest.repos.getRelease({ owner, repo, release_id: Math.trunc(Number(version)) });
return octokit.rest.repos.getRelease({
owner,
repo,
release_id: Math.trunc(Number(version))
});
}
};
var MAX_RETRY = 5;
var RETRY_INTERVAL = 1e3;
var fetchAssetFile = async (octokit, { id, outputPath, owner, repo, token }) => {
const {
body,
headers: {
accept,
"user-agent": userAgent
},
headers: { accept, "user-agent": userAgent },
method,
url
} = octokit.request.endpoint("GET /repos/:owner/:repo/releases/assets/:asset_id", {
Expand Down Expand Up @@ -12164,43 +12350,34 @@ var printOutput = (release) => {
core.setOutput("name", release.data.name);
core.setOutput("body", release.data.body);
};
var filterByFileName = (file) => (asset) => file === asset.name;
var filterByRegex = (file) => (asset) => new RegExp(file).test(asset.name);
var main = async () => {
const { owner, repo } = getRepo(core.getInput("repo", { required: false }), github.context);
const token = core.getInput("token", { required: false });
const version = core.getInput("version", { required: false });
const inputTarget = core.getInput("target", { required: false });
const file = core.getInput("file", { required: true });
const usesRegex = core.getBooleanInput("regex", { required: false });
const target = inputTarget === "" ? file : inputTarget;
const octokit = github.getOctokit(token);
const release = await getRelease(octokit, { owner, repo, version });
const asset = release.data.assets.find((e2) => e2.name === file);
if (typeof asset === "undefined")
throw new Error("Could not find asset id");
await fetchAssetFile(octokit, { id: asset.id, outputPath: target, owner, repo, token });
printOutput(release);
};
var mainRegex = async () => {
const { owner, repo } = getRepo(core.getInput("repo", { required: false }), github.context);
const token = core.getInput("token", { required: false });
const version = core.getInput("version", { required: false });
const file = core.getInput("file", { required: true });
const target = core.getInput("target", { required: true });
const octokit = github.getOctokit(token);
const release = await getRelease(octokit, { owner, repo, version });
const regexp = new RegExp(file);
const assets = release.data.assets.filter((e2) => regexp.test(e2.name));
const assetFilterFn = usesRegex ? filterByRegex(file) : filterByFileName(file);
const assets = release.data.assets.filter(assetFilterFn);
if (assets.length === 0)
throw new Error("Could not find asset id");
for (const asset of assets) {
await fetchAssetFile(octokit, { id: asset.id, outputPath: `${target}${asset.name}`, owner, repo, token });
await fetchAssetFile(octokit, {
id: asset.id,
outputPath: usesRegex ? `${target}${asset.name}` : target,
owner,
repo,
token
});
}
printOutput(release);
};
if (core.getBooleanInput("regex", { required: false })) {
mainRegex();
} else {
main();
}
void main();
/*!
* is-plain-object <https://github.com/jonschlinkert/is-plain-object>
*
Expand Down
Loading

0 comments on commit aaca6e0

Please sign in to comment.