mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-08-20 08:01:35 +00:00
68debab877
* feat(triggers): add AMQP (RabbitMQ) trigger using the lapin library Fixes WIN-2214 Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * chore(triggers): defer AMQP cross-workspace deploy pending utils-internal publish Revert the amqp_trigger additions to the shared windmill-utils-internal TriggerDeployKind and the frontend cross-workspace deploy adapter: the frontend installs the published npm package, which lacks the new kind until a release is cut. AMQP create/edit/delete/list/sync/capture are unaffected (they use local types); only cross-workspace deploy/merge of AMQP triggers waits on the package bump. Also document the at-most-once ack in the consumer loop. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(triggers): address AMQP review — at-least-once ack, workspace cascade, contracts - ack AMQP deliveries only after successful dispatch; nack+requeue on failure - add ON DELETE CASCADE workspace FK so amqp_trigger rows are cleaned on workspace deletion (and the listener stops) - fix the /amqp_triggers/test OpenAPI body and add amqp_trigger to WorkspaceDiffRow.kind - register AMQP in the generated workspace trigger tool (create_trigger) - drop banned $bindable defaults on optional props in the config section - add build_uri unit tests (encoding, ports, vhost) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(triggers): stop AMQP poison-message loop and reconnect on transient drops Chaos testing against a live RabbitMQ broker showed the previous nack(requeue) + immediate re-poll spun a tight redelivery loop (~1000 critical-error reports/sec) on a poison message, and any connection blip permanently disabled the trigger (lapin has no built-in reconnect). - on dispatch failure: nack+requeue then stop consuming; the listener framework re-lists the trigger after its ping goes stale (~15s), backing redelivery off to that cadence instead of a tight loop (verified: rate dropped from ~1000/s to ~1 per ~26s, message preserved) - on connection/stream error: stop and let the framework reconnect instead of disabling; persistent failures are still disabled via get_consumer (verified: a forced connection close now auto-reconnects and resumes) - finish the AI create-trigger action wiring for AMQP: add amqp to CreatedResourceTriggerKind, the action-card registry, and the drawer registry so the result card renders and its "Open" action works Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(triggers): complete AMQP frontend registries and defer merge rows - add amqp to capturableTriggerTypes (so AmqpCapture mounts), the Runs jobTriggerKinds filter, and CLOUD_DISABLED_TRIGGER_TYPES - wire AMQP into global AI chat mode: TRIGGER_KINDS, the request union, writeTriggerSchema, triggerServices, and the draft adapter - stop emitting actionable AMQP fork-comparison rows (revert amqp_trigger from TRIGGER_OR_SCHEDULE_TABLES) since cross-workspace deploy is deferred until windmill-utils-internal is published — avoids a deploy that fails with "Unknown kind: amqp_trigger" - use design-system TextInput instead of raw <input> in the config section Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(triggers): complete AMQP session/draft registries and constrain prefetch - add amqp to the session-deploy, draft-compare, preview-router, and copilot workspace-item registries so AMQP drafts/deploys/nav/path resolution work - include amqp_count in the MoveDrawer attached-trigger rename warning - replace the raw prefetch <input> with a design-system TextInput bounded to an integer 1-65535 (backend u16) and block save on invalid values Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(triggers): make AMQP disconnect/reconnect consistent with the Kafka trigger lapin, like rdkafka, has no transparent reconnect, so the AMQP listener now mirrors the Kafka trigger's explicit reconnect loop instead of relying on the framework re-list (which disabled the trigger once get_consumer failed on a sustained outage): - get_consumer returns cheaply; consume owns a (re)connect loop that retries with a 30s backoff, reports a critical error every 10 failed attempts, and reports a recovered critical error once it reconnects — never disabling the trigger on a connectivity failure - a consumer/stream error breaks out to reconnect rather than disabling - dispatch failure still nacks+requeues (at-least-once) with a short backoff to avoid a tight poison-message loop, keeping the connection alive Verified against a live RabbitMQ broker: killing the broker keeps the trigger enabled and retrying (attempt N), and restarting it auto-reconnects (logs "reconnected after N attempts") and resumes dispatch. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(triggers): complete AMQP capture registries and constrain prefetch contract - add the 'amqp' case to triggerKindToTriggerType so opening the AMQP editor from a capture button no longer throws "Unknown TriggerKind: amqp" - register AmqpIcon in CaptureTable's icon map and add an AMQP entry to the script/flow CaptureButton menu - bound the OpenAPI prefetch_count to an integer 1-65535 (matches the Rust u16) and regenerate clients/prompts - require a non-empty exchange name when the exchange binding is enabled - build_uri: fall back to "/" on a blank vhost and bracket IPv6 hosts (+ tests) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * feat(triggers): wire AMQP into pipeline graph, git-sync, and preprocessor types - asset_graph: discover attached amqp_trigger rows and emit an AMQP TriggerEdge so AMQP triggers render (and can be opened/deleted) on the data-pipeline canvas - frontend pipeline graph: add amqp to NativeTriggerKind, the add-trigger menu, node presentation, event-trigger set, annotation keywords, and the editor/service registrations - git-sync: add the amqp_trigger include pattern (+ test) so an AMQP git-sync deployment stages only its .amqp_trigger.* file, not an unrelated same-path object - preprocessor starters: add the AMQP event to the generated TS/Python/PHP trigger event types (kind/payload/exchange/routing_key/queue_name/redelivered/ delivery_tag) Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(triggers): finish AMQP pipeline/parser wiring, prefetch validation, source lists - fix a stray edit that corrupted the pre-existing MqttTriggerEditor import ($lib/... path) in PipelineTriggerEditors.svelte - reject prefetch_count = 0 server-side in validate_config (RabbitMQ treats 0 as unlimited) and defensively skip basic_qos(0) in build_consumer (covers the capture path that bypasses CRUD validation) - recognize `// on amqp` in the canonical parser (TriggerSpec::Amqp) and add amqp to the CLI non-autorun/event-trigger sets so a pipeline cascade never runs an AMQP-only node as a manual root without an event - add amqp to the preprocessor intro lists and both pipeline AI instructions Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * fix(triggers): reject zero AMQP prefetch in all paths and finish guidance lists - extract a shared validate_amqp_options used by both CRUD validate_config and build_consumer, so capture configs (which bypass CRUD validation) also reject prefetch 0 instead of silently connecting with an unlimited buffer (+ unit tests for 0/1/65535/None) - add AMQP to the main script-writing preprocessor-sources prompt and the CLI triggers-skill guidance list Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * docs(triggers): de-duplicate AMQP prefetch comment and fix GET response text - keep the zero-prefetch rationale only on the shared validate_amqp_options doc; drop the redundant call-site comments - correct the getAmqpTrigger OpenAPI 200 description ("deleted" -> "retrieved") Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * chore: update ee-repo-ref to aaa6cb89b05b76139252c64f057e53b94d12ac60 This commit updates the EE repository reference after PR #680 was merged in windmill-ee-private. Previous ee-repo-ref: 5da5fd65aca9594b2611837a52e4677b544b0380 New ee-repo-ref: aaa6cb89b05b76139252c64f057e53b94d12ac60 Automated by sync-ee-ref workflow. * chore(migrations): consolidate the four AMQP migrations into one The table and the three enum ADD VALUE statements (trigger_kind, job_trigger_kind, draft_kind) are one atomic feature. ALTER TYPE ... ADD VALUE runs inside the migration transaction on PG >= 14 (Windmill's minimum) since the amqp_trigger table doesn't reference those enum types, so they can share a single migration instead of four. Verified applying cleanly in a single transaction on a fresh DB. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: windmill-internal-app[bot] <windmill-internal-app[bot]@users.noreply.github.com>
479 lines
17 KiB
TypeScript
479 lines
17 KiB
TypeScript
import * as log from "../core/log.ts";
|
|
import { execSync, spawnSync } from "node:child_process";
|
|
import { WM_FORK_PREFIX } from "../core/constants.ts";
|
|
|
|
// Fork *workspace id* prefix ("wm-fork-"). WM_FORK_PREFIX is the *branch*
|
|
// prefix ("wm-fork") used inside the wm-fork/<branch>/<id> branch name.
|
|
const FORK_WORKSPACE_PREFIX = `${WM_FORK_PREFIX}-`;
|
|
|
|
export function getCurrentGitBranch(): string | null {
|
|
try {
|
|
const result = execSync("git rev-parse --abbrev-ref HEAD", {
|
|
encoding: "utf8",
|
|
stdio: "pipe"
|
|
});
|
|
const branch = result.trim();
|
|
return branch || null;
|
|
} catch (error) {
|
|
log.debug(`Failed to get Git branch: ${error}`);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/** Whether a local branch with this exact name exists. */
|
|
export function gitBranchExists(branchName: string): boolean {
|
|
const r = spawnSync(
|
|
"git",
|
|
["show-ref", "--verify", "--quiet", `refs/heads/${branchName}`],
|
|
{ stdio: "pipe" },
|
|
);
|
|
return r.status === 0;
|
|
}
|
|
|
|
/**
|
|
* Rename the currently checked-out branch (`git branch -m <newName>`). Used by
|
|
* `wmill workspace fork --from-branch` to turn an existing working branch into
|
|
* the `wm-fork/<base>/<id>` fork branch in place, preserving its commits.
|
|
*/
|
|
export function renameCurrentGitBranch(newName: string): void {
|
|
const r = spawnSync("git", ["branch", "-m", newName], {
|
|
encoding: "utf8",
|
|
stdio: "pipe",
|
|
});
|
|
if ((r.status ?? 1) !== 0) {
|
|
throw new Error(
|
|
`git branch -m ${newName} failed (exit ${r.status}): ${r.stderr ?? ""}`,
|
|
);
|
|
}
|
|
}
|
|
|
|
export function getOriginalBranchForWorkspaceForks(branchName: string | null): string | null {
|
|
if (!branchName || !branchName.startsWith(WM_FORK_PREFIX)) {
|
|
return null
|
|
}
|
|
|
|
const start = branchName.indexOf("/") + 1;
|
|
const end = branchName.lastIndexOf("/");
|
|
|
|
if (start < 0 || end < 0 || end - start <= 0) {
|
|
return null
|
|
}
|
|
|
|
return branchName.slice(start, end)
|
|
}
|
|
|
|
export function getWorkspaceIdForWorkspaceForkFromBranchName(branchName: string): string | null {
|
|
if (!branchName.startsWith(WM_FORK_PREFIX)) {
|
|
return null
|
|
}
|
|
|
|
const start = branchName.lastIndexOf("/") + 1;
|
|
|
|
if (start < 0) {
|
|
return null
|
|
}
|
|
|
|
return `${WM_FORK_PREFIX}-${branchName.slice(start)}`
|
|
}
|
|
export function isGitRepository(): boolean {
|
|
try {
|
|
execSync("git rev-parse --git-dir", {
|
|
encoding: "utf8",
|
|
stdio: "pipe"
|
|
});
|
|
return true;
|
|
} catch (error) {
|
|
log.debug(`Failed to check Git repository: ${error}`);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// ===========================================================================
|
|
// Git-sync deployment-callback branch/commit/push.
|
|
//
|
|
// The git-sync hub script ("sync-script-to-git-repo-windmill") historically
|
|
// owned this logic. When `use_individual_branch` is set, a deploy must land on
|
|
// a dedicated `wm_deploy/<workspace>/<...>` branch (or a `wm-fork/<branch>/<id>`
|
|
// branch for forks) instead of the repo's protected base branch — otherwise
|
|
// the push to a protected `main` is rejected (GH006). A hub-script rewrite once
|
|
// dropped this and every deploy pushed straight to `main`; keeping the logic
|
|
// here (in-repo, behind `wmill sync pull`) makes it deterministically testable.
|
|
// ===========================================================================
|
|
|
|
export interface GitSyncDeployItem {
|
|
path_type: string;
|
|
path?: string | null;
|
|
parent_path?: string | null;
|
|
commit_msg?: string;
|
|
}
|
|
|
|
// A fork or dev workspace syncs to its own wm-fork/<branch>/<id> branch. The hub
|
|
// script force-disables use_individual_branch / group_by_folder for these (that
|
|
// disabling also changes the include/promotion derivation — so callers must apply
|
|
// it BEFORE deriving includes, not just for branch naming).
|
|
//
|
|
// Fork-ness is "has a parent workspace" OR the "wm-fork-" id prefix. Regular forks
|
|
// get an auto-generated `wm-fork-<slug>` id, but dev workspaces keep a custom id
|
|
// with no prefix — so the prefix alone misses them. Mirrors the backend's
|
|
// `parent.is_some() || starts_with(WM_FORK_PREFIX)` rule (the prefix also covers a
|
|
// fork whose parent was deleted, since parent_workspace_id is ON DELETE SET NULL).
|
|
export function isForkWorkspace(
|
|
workspaceId: string,
|
|
parentWorkspaceId?: string | null,
|
|
): boolean {
|
|
return !!parentWorkspaceId || workspaceId.startsWith(FORK_WORKSPACE_PREFIX);
|
|
}
|
|
|
|
// Mirrors the hub script's get_fork_branch_name. A dev workspace syncs with its
|
|
// environment-label branch verbatim ("dev"/"staging" — a first-class top-level
|
|
// branch; the backend passes the label with the deploy). A `wm-fork-<slug>`
|
|
// throwaway fork id becomes "wm-fork/<originalBranch>/<slug>"; a prefix-less id
|
|
// without a label falls back to "wm-fork/<originalBranch>/<id>".
|
|
export function forkBranchName(
|
|
workspaceId: string,
|
|
originalBranch: string,
|
|
devWorkspaceLabel?: string | null,
|
|
): string {
|
|
if (devWorkspaceLabel) {
|
|
return devWorkspaceLabel;
|
|
}
|
|
const branchPrefix = `${WM_FORK_PREFIX}/${originalBranch}/`;
|
|
return workspaceId.startsWith(FORK_WORKSPACE_PREFIX)
|
|
? workspaceId.replace(FORK_WORKSPACE_PREFIX, branchPrefix)
|
|
: `${branchPrefix}${workspaceId}`;
|
|
}
|
|
|
|
// Pure branch-name resolution mirroring the hub script's git_checkout_branch.
|
|
// Returns null when the deploy should stay on the cloned/base branch (i.e.
|
|
// workspace-wide mode, or user/group objects which never get their own branch).
|
|
export function computeGitSyncDeployBranch(params: {
|
|
workspaceId: string;
|
|
parentWorkspaceId?: string | null;
|
|
devWorkspaceLabel?: string | null;
|
|
items: GitSyncDeployItem[];
|
|
useIndividualBranch: boolean;
|
|
groupByFolder: boolean;
|
|
clonedBranchName: string;
|
|
}): string | null {
|
|
const {
|
|
workspaceId,
|
|
parentWorkspaceId,
|
|
devWorkspaceLabel,
|
|
items,
|
|
useIndividualBranch,
|
|
groupByFolder,
|
|
clonedBranchName,
|
|
} = params;
|
|
|
|
if (isForkWorkspace(workspaceId, parentWorkspaceId)) {
|
|
return forkBranchName(workspaceId, clonedBranchName, devWorkspaceLabel);
|
|
}
|
|
|
|
if (items.length === 0) return null;
|
|
const first = items[0];
|
|
|
|
// `use_individual_branch` disables debouncing, so items is length 1 here.
|
|
if (
|
|
!useIndividualBranch ||
|
|
first.path_type === "user" ||
|
|
first.path_type === "group"
|
|
) {
|
|
return null;
|
|
}
|
|
|
|
const ref = first.path ?? first.parent_path;
|
|
if (!ref) return null;
|
|
|
|
return groupByFolder
|
|
? `wm_deploy/${workspaceId}/${ref.split("/").slice(0, 2).join("__")}`
|
|
: `wm_deploy/${workspaceId}/${first.path_type}/${ref.replaceAll("/", "__")}`;
|
|
}
|
|
|
|
// Mirrors the hub script's composeCommitHeader: summarise the deployed object
|
|
// types, e.g. "[WM]: Deployed 2 scripts, 1 flow and 3 other objects".
|
|
export function composeGitSyncCommitHeader(
|
|
items: GitSyncDeployItem[],
|
|
): string {
|
|
const typeCounts = new Map<string, number>();
|
|
for (const item of items) {
|
|
typeCounts.set(item.path_type, (typeCounts.get(item.path_type) ?? 0) + 1);
|
|
}
|
|
const sorted = Array.from(typeCounts.entries()).sort((a, b) => b[1] - a[1]);
|
|
|
|
const parts: string[] = [];
|
|
let othersCount = 0;
|
|
for (let i = 0; i < sorted.length; i++) {
|
|
const [pathType, count] = sorted[i];
|
|
if (i < 3) {
|
|
const label = count > 1 ? `${pathType}s` : pathType;
|
|
if (i === 2 && sorted.length === 3) {
|
|
parts.push(`and ${count} ${label}`);
|
|
} else {
|
|
parts.push(`${count} ${label}`);
|
|
}
|
|
} else {
|
|
othersCount += count;
|
|
}
|
|
}
|
|
|
|
let header = `[WM]: Deployed ${parts.join(", ")}`;
|
|
if (othersCount > 0) {
|
|
header += ` and ${othersCount} other object${othersCount > 1 ? "s" : ""}`;
|
|
}
|
|
return header;
|
|
}
|
|
|
|
// Mirrors the hub script's git_push commit-message construction EXACTLY,
|
|
// including its quirks: it pushes `commit_msg` once per item unconditionally
|
|
// (so the array length equals items.length even when some msgs are empty),
|
|
// uses the single message verbatim when there is exactly one item, otherwise
|
|
// the composed header + newline-joined descriptions, and falls back to
|
|
// "no commit msg" when the chosen header is empty/undefined. join() coerces
|
|
// undefined entries to "" just like the original.
|
|
export function gitSyncCommitMessage(items: GitSyncDeployItem[]): {
|
|
header: string;
|
|
description: string;
|
|
} {
|
|
const descs = items.map((i) => i.commit_msg);
|
|
const [h, d] =
|
|
descs.length === 1
|
|
? [descs[0], ""]
|
|
: [composeGitSyncCommitHeader(items), descs.join("\n")];
|
|
return {
|
|
header: h === undefined || h === "" ? "no commit msg" : h,
|
|
description: d ?? "",
|
|
};
|
|
}
|
|
|
|
// Mirrors the hub script's regexFromPath: the include glob(s) that select an
|
|
// object's files for `wmill sync pull --extra-includes`. Some types expand to
|
|
// two comma-separated patterns (dotted + double-underscore folder layouts).
|
|
export function gitSyncIncludePattern(
|
|
pathType: string,
|
|
path: string,
|
|
): string {
|
|
switch (pathType) {
|
|
case "flow":
|
|
return `${path}.flow/*,${path}__flow/*`;
|
|
case "app":
|
|
return `${path}.app/*,${path}__app/*`;
|
|
case "raw_app":
|
|
return `${path}.raw_app/**,${path}__raw_app/**`;
|
|
case "folder":
|
|
return `${path}/folder.meta.*`;
|
|
case "resourcetype":
|
|
return `${path}.resource-type.*`;
|
|
case "resource":
|
|
return `${path}.resource.*`;
|
|
case "variable":
|
|
return `${path}.variable.*`;
|
|
case "schedule":
|
|
return `${path}.schedule.*`;
|
|
case "user":
|
|
return `${path}.user.*`;
|
|
case "group":
|
|
return `${path}.group.*`;
|
|
case "httptrigger":
|
|
return `${path}.http_trigger.*`;
|
|
case "websockettrigger":
|
|
return `${path}.websocket_trigger.*`;
|
|
case "kafkatrigger":
|
|
return `${path}.kafka_trigger.*`;
|
|
case "natstrigger":
|
|
return `${path}.nats_trigger.*`;
|
|
case "postgrestrigger":
|
|
return `${path}.postgres_trigger.*`;
|
|
case "mqtttrigger":
|
|
return `${path}.mqtt_trigger.*`;
|
|
case "amqptrigger":
|
|
return `${path}.amqp_trigger.*`;
|
|
case "sqstrigger":
|
|
return `${path}.sqs_trigger.*`;
|
|
case "gcptrigger":
|
|
return `${path}.gcp_trigger.*`;
|
|
case "azuretrigger":
|
|
return `${path}.azure_trigger.*`;
|
|
case "emailtrigger":
|
|
return `${path}.email_trigger.*`;
|
|
default:
|
|
// Scripts: `${path}.*` matches the dotted layout
|
|
// (`${path}.script.yaml` etc.), `${path}__mod/**` matches the folder
|
|
// layout used by scripts with companion modules
|
|
// (`${path}__mod/script.ts`, `${path}__mod/helper.ts`, ...). Without the
|
|
// second pattern the module files are filtered out of the pull and the
|
|
// subsequent `git add '${path}**'` fails with "pathspec did not match".
|
|
return `${path}.*,${path}__mod/**`;
|
|
}
|
|
}
|
|
|
|
// `forcedIncludes` carries ONLY the include-* flags that must be force-set to
|
|
// true (overriding the repo's wmill.yaml). Kinds not present are intentionally
|
|
// omitted (never set to false) so the caller can spread this object and let
|
|
// the repo's effective config govern the rest — see deriveGitSyncDeployIncludes.
|
|
export type GitSyncForcedIncludes = Partial<{
|
|
includeSchedules: boolean;
|
|
includeGroups: boolean;
|
|
includeUsers: boolean;
|
|
includeTriggers: boolean;
|
|
includeSettings: boolean;
|
|
includeKey: boolean;
|
|
}>;
|
|
|
|
export interface GitSyncDeployIncludes {
|
|
extraIncludes: string[];
|
|
forcedIncludes: GitSyncForcedIncludes;
|
|
}
|
|
|
|
// Mirrors the hub script's wmill_sync_pull include-derivation: build the
|
|
// --extra-includes set from the deployed items, and decide which default-
|
|
// excluded object kinds (triggers, schedules, groups, users, settings, key)
|
|
// must be force-included in the pull. Replaces the script's regexFromPath +
|
|
// per-kind --include-* construction so the hub script can drop both.
|
|
//
|
|
// Branch-mode distinction (this is load-bearing — see the trigger-promotion
|
|
// bug it fixes):
|
|
// - Workspace-wide mode: the repo is a full mirror of the workspace, so a
|
|
// deployed object of a default-excluded kind MUST be re-included, even if
|
|
// wmill.yaml would otherwise skip it. We force the flag on.
|
|
// - Individual-branch (promotion) mode: the repo is a filtered prod surface
|
|
// whose own wmill.yaml filters decide what gets promoted. We force NOTHING
|
|
// here and the keys stay absent, so the caller's pull resolves them from
|
|
// the target's effective config (a deployed trigger lands iff the target
|
|
// includes triggers). Forcing `false` (the original behavior) did NOT
|
|
// defer — it CLOBBERED the effective config via Object.assign in pull's
|
|
// option merge, silently dropping kinds the target actually wanted (e.g. a
|
|
// deployed trigger when the target has includeTriggers: true), and the
|
|
// server then omitted the object from the tarball entirely.
|
|
export function deriveGitSyncDeployIncludes(
|
|
items: GitSyncDeployItem[],
|
|
useIndividualBranch: boolean,
|
|
): GitSyncDeployIncludes {
|
|
const extraIncludes: string[] = [];
|
|
for (const { path_type, path, parent_path } of items) {
|
|
if (path) {
|
|
extraIncludes.push(...gitSyncIncludePattern(path_type, path).split(","));
|
|
}
|
|
if (parent_path) {
|
|
extraIncludes.push(
|
|
...gitSyncIncludePattern(path_type, parent_path).split(","),
|
|
);
|
|
}
|
|
}
|
|
|
|
const forcedIncludes: GitSyncForcedIncludes = {};
|
|
if (!useIndividualBranch) {
|
|
const has = (pred: (t: string) => boolean) =>
|
|
items.some((i) => pred(i.path_type));
|
|
if (has((t) => t === "schedule")) forcedIncludes.includeSchedules = true;
|
|
if (has((t) => t === "group")) forcedIncludes.includeGroups = true;
|
|
if (has((t) => t === "user")) forcedIncludes.includeUsers = true;
|
|
if (has((t) => t.includes("trigger"))) forcedIncludes.includeTriggers = true;
|
|
if (has((t) => t === "settings")) forcedIncludes.includeSettings = true;
|
|
if (has((t) => t === "key")) forcedIncludes.includeKey = true;
|
|
}
|
|
|
|
return { extraIncludes, forcedIncludes };
|
|
}
|
|
|
|
function git(
|
|
args: string[],
|
|
opts?: { allowFail?: boolean },
|
|
): { status: number; stdout: string; stderr: string } {
|
|
const r = spawnSync("git", args, { encoding: "utf8", stdio: "pipe" });
|
|
const status = r.status ?? 1;
|
|
if (r.error) {
|
|
if (opts?.allowFail) return { status, stdout: "", stderr: String(r.error) };
|
|
throw r.error;
|
|
}
|
|
if (status !== 0 && !opts?.allowFail) {
|
|
throw new Error(
|
|
`git ${args.join(" ")} failed (exit ${status}): ${r.stderr ?? ""}`,
|
|
);
|
|
}
|
|
return { status, stdout: r.stdout ?? "", stderr: r.stderr ?? "" };
|
|
}
|
|
|
|
// Checkout (or create) the dedicated deploy branch, mirroring the hub script:
|
|
// try `git checkout <branch>`, on failure create it with -b and enable
|
|
// push.autoSetupRemote so the subsequent bare `git push` targets it.
|
|
export function checkoutGitSyncDeployBranch(branch: string): void {
|
|
const existing = git(["checkout", branch], { allowFail: true });
|
|
if (existing.status === 0) {
|
|
log.info(`Switched to existing branch ${branch}`);
|
|
return;
|
|
}
|
|
git(["checkout", "-b", branch]);
|
|
git(["config", "--add", "--bool", "push.autoSetupRemote", "true"]);
|
|
log.info(`Created and switched to branch ${branch}`);
|
|
}
|
|
|
|
// Stage, commit and push the deploy, mirroring the hub script's git_push:
|
|
// stage `wmill-lock.yaml` plus each item's path tree, no-op when nothing is
|
|
// staged, and push the *current* branch with a rebase-retry fallback.
|
|
export function gitSyncDeployPush(params: {
|
|
items: GitSyncDeployItem[];
|
|
authorName: string;
|
|
authorEmail: string;
|
|
// Committer identity (git config user.*). Defaults to the author identity,
|
|
// matching the hub script's non-GPG branch. For GPG-signed repos the hub
|
|
// sets the committer email to the GPG key's email — the caller passes that
|
|
// through here so the committed identity stays 1:1.
|
|
committerName?: string;
|
|
committerEmail?: string;
|
|
onlyCreateBranch?: boolean;
|
|
}): { pushed: boolean } {
|
|
const { items, authorName, authorEmail, onlyCreateBranch } = params;
|
|
const committerName = params.committerName ?? authorName;
|
|
const committerEmail = params.committerEmail ?? authorEmail;
|
|
|
|
git(["config", "user.email", committerEmail]);
|
|
git(["config", "user.name", committerName]);
|
|
|
|
// only_create_branch: publish the (possibly empty) branch ref and stop.
|
|
// Mirrors the hub script's early `git push --porcelain` (NOT swallowed —
|
|
// a failure here fails the job, exactly as in the hub).
|
|
if (onlyCreateBranch) {
|
|
git(["push", "--porcelain"]);
|
|
return { pushed: true };
|
|
}
|
|
|
|
// 1:1 with the hub script: `git add wmill-lock.yaml <path>**` as a single
|
|
// command per item, failure swallowed (its try/catch). `commit_msg` is
|
|
// collected once per item unconditionally — see gitSyncCommitMessage.
|
|
for (const { path, parent_path } of items) {
|
|
if (path) {
|
|
git(["add", "wmill-lock.yaml", `${path}**`], { allowFail: true });
|
|
}
|
|
if (parent_path) {
|
|
git(["add", "wmill-lock.yaml", `${parent_path}**`], { allowFail: true });
|
|
}
|
|
}
|
|
|
|
// `git diff --cached --quiet` exits 1 iff there is something staged.
|
|
const staged = git(["diff", "--cached", "--quiet"], { allowFail: true });
|
|
if (staged.status === 0) {
|
|
log.info("No changes detected, nothing to commit.");
|
|
return { pushed: false };
|
|
}
|
|
|
|
const { header, description } = gitSyncCommitMessage(items);
|
|
|
|
git([
|
|
"commit",
|
|
"--author",
|
|
`${authorName} <${authorEmail}>`,
|
|
"-m",
|
|
header,
|
|
"-m",
|
|
description,
|
|
]);
|
|
|
|
const push = git(["push", "--porcelain"], { allowFail: true });
|
|
if (push.status !== 0) {
|
|
log.info(`Push failed, rebasing and retrying: ${push.stderr}`);
|
|
git(["pull", "--rebase"]);
|
|
git(["push", "--porcelain"]);
|
|
}
|
|
return { pushed: true };
|
|
}
|