Skip to content

Commit

Permalink
Merge pull request #19 from fluentci-io/feat/flu-30-allow-specific-pi…
Browse files Browse the repository at this point in the history
…peline-version

feat: allow user to use a specific pipeline version: `fluentci run pipeline@version`
  • Loading branch information
tsirysndr authored Jan 12, 2024
2 parents d8f5ffa + b10041d commit 435bd8a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 18 deletions.
21 changes: 13 additions & 8 deletions src/cmd/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
SpinnerTypes,
} from "https://deno.land/x/[email protected]/mod.ts";
import { green } from "https://deno.land/[email protected]/fmt/colors.ts";
import { extractVersion } from "../utils.ts";

const BASE_URL = "https://api.fluentci.io/v1";

Expand All @@ -27,15 +28,16 @@ async function init(
_name?: string
) {
const infos = await promptPackageDetails(standalone);
template = template || "base_pipeline";
let version = extractVersion(template || "base_pipeline");
template = template?.split("@")[0] || "base_pipeline";

let result = await fetch(`${BASE_URL}/pipeline/${template}`);
let data = await result.json();

if (data.version) {
if (
await downloadTemplateFromRegistry(template, data.version, standalone)
) {
version =
version === "latest" ? data.version || data.default_branch : version;
if (await downloadTemplateFromRegistry(template, version, standalone)) {
if (standalone === true) {
await overrideDaggerJson(infos, ".");
return;
Expand All @@ -53,7 +55,9 @@ async function init(
data = await result.json();

if (data.github_url) {
await downloadTemplateFromGithub(data, template, standalone);
version =
version === "latest" ? data.version || data.default_branch : version;
await downloadTemplateFromGithub(data, template, version, standalone);
if (standalone === true) {
await overrideDaggerJson(infos, ".");
return;
Expand Down Expand Up @@ -148,24 +152,25 @@ async function downloadTemplateFromGithub(
owner: string;
},
template: string,
version: string,
standalone?: boolean
) {
const archiveUrl =
data.version && data.version.startsWith("v")
? `${data.github_url.replace(
"https://github.com",
"https://codeload.github.com"
)}/zip/refs/tags/${data.version}`
)}/zip/refs/tags/${version}`
: `${data.github_url.replace(
"https://github.com",
"https://api.github.com/repos"
)}/zipball/${data.version || data.default_branch}`;
)}/zipball/${version}`;

await download(archiveUrl, template);

const repoName = data.github_url.split("/").pop();

let outputDir = `${repoName}-${data.version.replace("v", "")}`;
let outputDir = `${repoName}-${version.replace("v", "")}`;

if (data.directory) {
outputDir += `/${data.directory}`;
Expand Down
21 changes: 11 additions & 10 deletions src/cmd/run.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BlobWriter, green, load, walk, ZipWriter, dayjs } from "../../deps.ts";
import { BASE_URL, FLUENTCI_WS_URL, RUNNER_URL } from "../consts.ts";
import { getCommitInfos } from "../git.ts";
import { getAccessToken, isLogged } from "../utils.ts";
import { getAccessToken, isLogged, extractVersion } from "../utils.ts";

/**
* Runs a Fluent CI pipeline.
Expand Down Expand Up @@ -132,24 +132,25 @@ async function run(
return;
}

const result = await fetch(`${BASE_URL}/pipeline/${pipeline}`);
const name = pipeline.split("@")[0];
let version = extractVersion(pipeline);

const result = await fetch(`${BASE_URL}/pipeline/${name}`);
const data = await result.json();

if (!data.github_url && !data.version) {
console.log(
`Pipeline template ${green('"')}${green(pipeline)}${green(
`Pipeline template ${green('"')}${green(name)}${green(
'"'
)} not found in Fluent CI registry`
);
Deno.exit(1);
}

version =
version === "latest" ? data.version || data.default_branch : version;
let denoModule = [
`--import-map=https://pkg.fluentci.io/${pipeline}@${
data.version || data.default_branch
}/import_map.json`,
`https://pkg.fluentci.io/${pipeline}@${
data.version || data.default_branch
}/src/dagger/runner.ts`,
`--import-map=https://pkg.fluentci.io/${name}@${version}/import_map.json`,
`https://pkg.fluentci.io/${name}@${version}/src/dagger/runner.ts`,
...jobs,
...Object.keys(options)
.filter((key) => key !== "reload")
Expand Down
16 changes: 16 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,19 @@ export function formatBytes(bytes: number, decimals = 2) {
parseFloat((bytes / Math.pow(k, i)).toFixed(decimals)) + " " + sizes[i]
);
}

export function extractVersion(name: string): string {
const version = name.split("@").pop();
if (version && name.split("@").length === 2) {
if (
/^v?(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-(alpha|beta)\.([1-9]\d*|0))?$/g.test(
version
)
) {
return version.startsWith("v") ? version : `v${version}`;
}
console.log('Invalid version format. Please use "vX.X.X" format.');
Deno.exit(1);
}
return "latest";
}

0 comments on commit 435bd8a

Please sign in to comment.