Skip to content

Commit

Permalink
feat: Support github_artifact:// protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
ovr committed Jul 8, 2024
1 parent 10846b1 commit 7baab90
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 4 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
},
"dependencies": {
"@cubejs-backend/shared": "0.33.20",
"@octokit/core": "^5",
"@octokit/plugin-rest-endpoint-methods": "^10",
"source-map-support": "^0.5.21"
},
"publishConfig": {
Expand Down
72 changes: 68 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function resolveVars(variables: Record<string, any>): UrlVariable[] {
const res = [];

for (const [variableName, variable] of Object.entries(variables)) {
let value = variable["default"];
let value: string | null = null;

let constraintPass = resolveConstraints(variable);
if (constraintPass) {
Expand All @@ -100,9 +100,17 @@ function resolveVars(variables: Record<string, any>): UrlVariable[] {
}
}

if (!value) {
if ("default" in variable) {
value = variable["default"];
} else {
throw new Error(`Unable to resolve variable ${variableName}`);
}
}

res.push({
resolve(url: string): string {
url = url.replace("${" + variableName + "}", value);
url = url.replace("${" + variableName + "}", value as string);

return url;
},
Expand All @@ -120,7 +128,10 @@ function resolveLibc(): string {
return "unknown";
}

function resolvePath(path: string, variables: UrlVariable[]): string {
import { Octokit } from "@octokit/core";
import { restEndpointMethods } from "@octokit/plugin-rest-endpoint-methods";

function resolveSimplePath(path: string, variables: UrlVariable[]): string {
path = path.replace("${version}", pkg.version);
path = path.replace("${platform}", process.platform);
path = path.replace("${arch}", process.arch);
Expand All @@ -133,6 +144,59 @@ function resolvePath(path: string, variables: UrlVariable[]): string {
return path;
}

async function resolveGithubArtifactPath(
url: string,
name: string,
variables: UrlVariable[]
): Promise<string> {
const extractParamsRegexp =
/github_artifact:\/\/(?<owner>[a-z-]+)\/(?<repo>[a-z-]+)\/actions\/(?<workflow>[a-zA-Z${}]+)/;

const params = url.match(extractParamsRegexp);

const MyOctokit = Octokit.plugin(restEndpointMethods);
const ghClient = new MyOctokit({
auth: process.env.GH_TOKEN,
});

if (!params || !("groups" in params)) {
throw new Error("Unable to decode url from github_artifact protocol");
}

const listWorkflowRunArtifacts =
await ghClient.rest.actions.listWorkflowRunArtifacts({
owner: params.groups?.owner as any,
repo: params.groups?.repo as any,
run_id: process.env.GITHUB_RUN_ID as any,
});

const resolvedName = resolveSimplePath(name, variables);
const artifactToDownload = listWorkflowRunArtifacts.data.artifacts.find(
(artifact) => artifact.name === resolvedName
);
if (!artifactToDownload) {
throw new Error(`Artifact '${resolvedName}' doesn't exist`);
}

return artifactToDownload.archive_download_url;
}

async function resolvePath(
file: any,
variables: UrlVariable[]
): Promise<string> {
if (file.host.startsWith("github_artifact://")) {
return resolveGithubArtifactPath(file.host, file.name, variables);
} else if (
file.host.startsWith("http://") ||
file.host.startsWith("https://")
) {
return resolveSimplePath(file.host + file.path, variables);
} else {
throw new Error(`Unsupported protocol in path: ${path}`);
}
}

(async () => {
try {
if (!pkg.resources) {
Expand All @@ -144,7 +208,7 @@ function resolvePath(path: string, variables: UrlVariable[]): string {
const variables = resolveVars(pkg.resources.vars || []);

for (const file of pkg.resources.files) {
const url = resolvePath(file.host + file.path, variables);
const url = await resolvePath(file, variables);

let constraintPass = resolveConstraints(file);
if (constraintPass) {
Expand Down
100 changes: 100 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,91 @@
supports-color "^5.4.0"
tslib "^1"

"@octokit/auth-token@^4.0.0":
version "4.0.0"
resolved "https://registry.yarnpkg.com/@octokit/auth-token/-/auth-token-4.0.0.tgz#40d203ea827b9f17f42a29c6afb93b7745ef80c7"
integrity sha512-tY/msAuJo6ARbK6SPIxZrPBms3xPbfwBrulZe0Wtr/DIY9lje2HeV1uoebShn6mx7SjCHif6EjMvoREj+gZ+SA==

"@octokit/core@^5":
version "5.2.0"
resolved "https://registry.yarnpkg.com/@octokit/core/-/core-5.2.0.tgz#ddbeaefc6b44a39834e1bb2e58a49a117672a7ea"
integrity sha512-1LFfa/qnMQvEOAdzlQymH0ulepxbxnCYAKJZfMci/5XJyIHWgEYnDmgnKakbTh7CH2tFQ5O60oYDvns4i9RAIg==
dependencies:
"@octokit/auth-token" "^4.0.0"
"@octokit/graphql" "^7.1.0"
"@octokit/request" "^8.3.1"
"@octokit/request-error" "^5.1.0"
"@octokit/types" "^13.0.0"
before-after-hook "^2.2.0"
universal-user-agent "^6.0.0"

"@octokit/endpoint@^9.0.1":
version "9.0.5"
resolved "https://registry.yarnpkg.com/@octokit/endpoint/-/endpoint-9.0.5.tgz#e6c0ee684e307614c02fc6ac12274c50da465c44"
integrity sha512-ekqR4/+PCLkEBF6qgj8WqJfvDq65RH85OAgrtnVp1mSxaXF03u2xW/hUdweGS5654IlC0wkNYC18Z50tSYTAFw==
dependencies:
"@octokit/types" "^13.1.0"
universal-user-agent "^6.0.0"

"@octokit/graphql@^7.1.0":
version "7.1.0"
resolved "https://registry.yarnpkg.com/@octokit/graphql/-/graphql-7.1.0.tgz#9bc1c5de92f026648131f04101cab949eeffe4e0"
integrity sha512-r+oZUH7aMFui1ypZnAvZmn0KSqAUgE1/tUXIWaqUCa1758ts/Jio84GZuzsvUkme98kv0WFY8//n0J1Z+vsIsQ==
dependencies:
"@octokit/request" "^8.3.0"
"@octokit/types" "^13.0.0"
universal-user-agent "^6.0.0"

"@octokit/openapi-types@^20.0.0":
version "20.0.0"
resolved "https://registry.yarnpkg.com/@octokit/openapi-types/-/openapi-types-20.0.0.tgz#9ec2daa0090eeb865ee147636e0c00f73790c6e5"
integrity sha512-EtqRBEjp1dL/15V7WiX5LJMIxxkdiGJnabzYx5Apx4FkQIFgAfKumXeYAqqJCj1s+BMX4cPFIFC4OLCR6stlnA==

"@octokit/openapi-types@^22.2.0":
version "22.2.0"
resolved "https://registry.yarnpkg.com/@octokit/openapi-types/-/openapi-types-22.2.0.tgz#75aa7dcd440821d99def6a60b5f014207ae4968e"
integrity sha512-QBhVjcUa9W7Wwhm6DBFu6ZZ+1/t/oYxqc2tp81Pi41YNuJinbFRx8B133qVOrAaBbF7D/m0Et6f9/pZt9Rc+tg==

"@octokit/plugin-rest-endpoint-methods@^10":
version "10.4.1"
resolved "https://registry.yarnpkg.com/@octokit/plugin-rest-endpoint-methods/-/plugin-rest-endpoint-methods-10.4.1.tgz#41ba478a558b9f554793075b2e20cd2ef973be17"
integrity sha512-xV1b+ceKV9KytQe3zCVqjg+8GTGfDYwaT1ATU5isiUyVtlVAO3HNdzpS4sr4GBx4hxQ46s7ITtZrAsxG22+rVg==
dependencies:
"@octokit/types" "^12.6.0"

"@octokit/request-error@^5.1.0":
version "5.1.0"
resolved "https://registry.yarnpkg.com/@octokit/request-error/-/request-error-5.1.0.tgz#ee4138538d08c81a60be3f320cd71063064a3b30"
integrity sha512-GETXfE05J0+7H2STzekpKObFe765O5dlAKUTLNGeH+x47z7JjXHfsHKo5z21D/o/IOZTUEI6nyWyR+bZVP/n5Q==
dependencies:
"@octokit/types" "^13.1.0"
deprecation "^2.0.0"
once "^1.4.0"

"@octokit/request@^8.3.0", "@octokit/request@^8.3.1":
version "8.4.0"
resolved "https://registry.yarnpkg.com/@octokit/request/-/request-8.4.0.tgz#7f4b7b1daa3d1f48c0977ad8fffa2c18adef8974"
integrity sha512-9Bb014e+m2TgBeEJGEbdplMVWwPmL1FPtggHQRkV+WVsMggPtEkLKPlcVYm/o8xKLkpJ7B+6N8WfQMtDLX2Dpw==
dependencies:
"@octokit/endpoint" "^9.0.1"
"@octokit/request-error" "^5.1.0"
"@octokit/types" "^13.1.0"
universal-user-agent "^6.0.0"

"@octokit/types@^12.6.0":
version "12.6.0"
resolved "https://registry.yarnpkg.com/@octokit/types/-/types-12.6.0.tgz#8100fb9eeedfe083aae66473bd97b15b62aedcb2"
integrity sha512-1rhSOfRa6H9w4YwK0yrf5faDaDTb+yLyBUKOCV4xtCDB5VmIPqd/v9yr9o6SAzOAlRxMiRiCic6JVM1/kunVkw==
dependencies:
"@octokit/openapi-types" "^20.0.0"

"@octokit/types@^13.0.0", "@octokit/types@^13.1.0":
version "13.5.0"
resolved "https://registry.yarnpkg.com/@octokit/types/-/types-13.5.0.tgz#4796e56b7b267ebc7c921dcec262b3d5bfb18883"
integrity sha512-HdqWTf5Z3qwDVlzCrP8UJquMwunpDiMPt5er+QjGzL4hqr/vBVY/MauQgS1xWxCDT1oMx1EULyqxncdCY/NVSQ==
dependencies:
"@octokit/openapi-types" "^22.2.0"

"@tootallnate/once@1":
version "1.1.2"
resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-1.1.2.tgz#ccb91445360179a04e7fe6aff78c00ffc1eeaf82"
Expand Down Expand Up @@ -94,6 +179,11 @@ base64-js@^1.3.1:
resolved "https://registry.yarnpkg.com/base64-js/-/base64-js-1.5.1.tgz#1b1b440160a5bf7ad40b650f095963481903930a"
integrity sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==

before-after-hook@^2.2.0:
version "2.2.3"
resolved "https://registry.yarnpkg.com/before-after-hook/-/before-after-hook-2.2.3.tgz#c51e809c81a4e354084422b9b26bad88249c517c"
integrity sha512-NzUnlZexiaH/46WDhANlyR2bXRopNg4F/zuSA3OpZnllCUgRaOF2znDioDWrmbNVsuZk6l9pMquQB38cfBZwkQ==

bl@^1.0.0:
version "1.2.3"
resolved "https://registry.yarnpkg.com/bl/-/bl-1.2.3.tgz#1e8dd80142eac80d7158c9dccc047fb620e035e7"
Expand Down Expand Up @@ -282,6 +372,11 @@ decompress@^4.2.1:
pify "^2.3.0"
strip-dirs "^2.0.0"

deprecation@^2.0.0:
version "2.3.1"
resolved "https://registry.yarnpkg.com/deprecation/-/deprecation-2.3.1.tgz#6368cbdb40abf3373b525ac87e4a260c3a700919"
integrity sha512-xmHIy4F3scKVwMsQ4WnVaS8bHOx0DmVwRywosKhaILI0ywMDWPtBSku2HNxRvF7jtwDRsoEwYQSfbxj8b7RlJQ==

emoji-regex@^8.0.0:
version "8.0.0"
resolved "https://registry.yarnpkg.com/emoji-regex/-/emoji-regex-8.0.0.tgz#e818fd69ce5ccfcb404594f842963bf53164cc37"
Expand Down Expand Up @@ -800,6 +895,11 @@ unbzip2-stream@^1.0.9:
buffer "^5.2.1"
through "^2.3.8"

universal-user-agent@^6.0.0:
version "6.0.1"
resolved "https://registry.yarnpkg.com/universal-user-agent/-/universal-user-agent-6.0.1.tgz#15f20f55da3c930c57bddbf1734c6654d5fd35aa"
integrity sha512-yCzhz6FN2wU1NiiQRogkTQszlQSlpWaw8SvVegAc+bDxbzHgh1vX8uIe8OYyMH6DwH+sdTJsgMl36+mSMdRJIQ==

universalify@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/universalify/-/universalify-2.0.0.tgz#75a4984efedc4b08975c5aeb73f530d02df25717"
Expand Down

0 comments on commit 7baab90

Please sign in to comment.