Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
179 changes: 177 additions & 2 deletions server/routes/xcode-cloud.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,40 @@ function normalizeBuildRun(item) {
};
}

function normalizeWorkflow(item) {
function normalizeWorkflow(item, included) {
const a = item.attributes || {};
return {
const workflow = {
id: item.id,
name: a.name,
description: a.description || "",
branchStartCondition: a.branchStartCondition || null,
tagStartCondition: a.tagStartCondition || null,
pullRequestStartCondition: a.pullRequestStartCondition || null,
scheduledStartCondition: a.scheduledStartCondition || null,
manualBranchStartCondition: a.manualBranchStartCondition || null,
manualTagStartCondition: a.manualTagStartCondition || null,
manualPullRequestStartCondition: a.manualPullRequestStartCondition || null,
actions: a.actions || [],
isEnabled: a.isEnabled ?? true,
clean: a.clean ?? false,
containerFilePath: a.containerFilePath || "",
isLockedForEditing: a.isLockedForEditing ?? false,
lastModifiedDate: a.lastModifiedDate,
};

if (included) {
const rels = item.relationships || {};
if (rels.xcodeVersion?.data?.id) {
const xv = included.find((i) => i.type === "ciXcodeVersions" && i.id === rels.xcodeVersion.data.id);
if (xv) workflow.xcodeVersion = { id: xv.id, name: xv.attributes?.name, version: xv.attributes?.version };
}
if (rels.macOsVersion?.data?.id) {
const mv = included.find((i) => i.type === "ciMacOsVersions" && i.id === rels.macOsVersion.data.id);
if (mv) workflow.macOsVersion = { id: mv.id, name: mv.attributes?.name, version: mv.attributes?.version };
}
}

return workflow;
}

// ── Build Runs ──────────────────────────────────────────────────────────────
Expand Down Expand Up @@ -139,6 +160,160 @@ router.get("/:appId/xcode-cloud/workflows", async (req, res) => {
}
});

// ── Single Workflow ──────────────────────────────────────────────────────────

router.get("/:appId/xcode-cloud/workflows/:workflowId", async (req, res) => {
const { workflowId } = req.params;
const account = resolveAccount(req, res);
if (!account) return;

const cacheKey = `ci:workflow:${workflowId}:${account.id}`;
const cached = apiCache.get(cacheKey);
if (cached) return res.json(cached);

try {
const data = await ascFetch(
account,
`/v1/ciWorkflows/${workflowId}?include=xcodeVersion,macOsVersion`
);
const result = normalizeWorkflow(data.data, data.included || []);
apiCache.set(cacheKey, result);
res.json(result);
} catch (err) {
console.error(`Failed to fetch workflow ${workflowId}:`, err.message);
res.status(502).json({ error: err.message });
}
});

// ── Update Workflow ─────────────────────────────────────────────────────────

router.patch("/:appId/xcode-cloud/workflows/:workflowId", async (req, res) => {
const { appId, workflowId } = req.params;
const account = resolveAccount(req, res);
if (!account) return;

try {
const {
accountId: _a, name, description, isEnabled, clean,
isLockedForEditing, containerFilePath, actions,
branchStartCondition, tagStartCondition,
pullRequestStartCondition, scheduledStartCondition,
manualBranchStartCondition, manualTagStartCondition,
manualPullRequestStartCondition,
xcodeVersionId, macOsVersionId,
} = req.body;

const attributes = {};
if (name !== undefined) attributes.name = name;
if (description !== undefined) attributes.description = description;
if (isEnabled !== undefined) attributes.isEnabled = isEnabled;
if (clean !== undefined) attributes.clean = clean;
if (isLockedForEditing !== undefined) attributes.isLockedForEditing = isLockedForEditing;
if (containerFilePath !== undefined) attributes.containerFilePath = containerFilePath;
if (actions !== undefined) attributes.actions = actions;
if (branchStartCondition !== undefined) attributes.branchStartCondition = branchStartCondition;
if (tagStartCondition !== undefined) attributes.tagStartCondition = tagStartCondition;
if (pullRequestStartCondition !== undefined) attributes.pullRequestStartCondition = pullRequestStartCondition;
if (scheduledStartCondition !== undefined) attributes.scheduledStartCondition = scheduledStartCondition;
if (manualBranchStartCondition !== undefined) attributes.manualBranchStartCondition = manualBranchStartCondition;
if (manualTagStartCondition !== undefined) attributes.manualTagStartCondition = manualTagStartCondition;
if (manualPullRequestStartCondition !== undefined) attributes.manualPullRequestStartCondition = manualPullRequestStartCondition;

const relationships = {};
if (xcodeVersionId) {
relationships.xcodeVersion = { data: { type: "ciXcodeVersions", id: xcodeVersionId } };
}
if (macOsVersionId) {
relationships.macOsVersion = { data: { type: "ciMacOsVersions", id: macOsVersionId } };
}

const body = {
data: {
type: "ciWorkflows",
id: workflowId,
attributes,
...(Object.keys(relationships).length > 0 && { relationships }),
},
};

const data = await ascFetch(account, `/v1/ciWorkflows/${workflowId}`, {
method: "PATCH",
body,
});

// Invalidate caches
apiCache.delete(`ci:workflows:${appId}:${account.id}`);
apiCache.delete(`ci:workflow:${workflowId}:${account.id}`);

const result = normalizeWorkflow(data.data, data.included || []);
res.json(result);
} catch (err) {
console.error(`Failed to update workflow ${workflowId}:`, err.message);
res.status(502).json({ error: err.message });
}
});

// ── Xcode & macOS Versions ──────────────────────────────────────────────────

const VERSION_TTL = 60 * 60 * 1000; // 1 hour

router.get("/:appId/xcode-cloud/xcode-versions", async (req, res) => {
const account = resolveAccount(req, res);
if (!account) return;

const cacheKey = `ci:xcode-versions:${account.id}`;
const cached = apiCache.get(cacheKey);
if (cached) return res.json(cached);

try {
const data = await ascFetch(account, `/v1/ciXcodeVersions?include=macOsVersions&limit=50`);
const result = (data.data || []).map((item) => {
const a = item.attributes || {};
const macOsVersionIds = (item.relationships?.macOsVersions?.data || []).map((r) => r.id);
const macOsVersions = macOsVersionIds
.map((id) => {
const mv = (data.included || []).find((i) => i.type === "ciMacOsVersions" && i.id === id);
return mv ? { id: mv.id, name: mv.attributes?.name, version: mv.attributes?.version } : null;
})
.filter(Boolean);
return {
id: item.id,
name: a.name,
version: a.version,
testDestinations: a.testDestinations || [],
macOsVersions,
};
});
apiCache.set(cacheKey, result, VERSION_TTL);
res.json(result);
} catch (err) {
console.error("Failed to fetch Xcode versions:", err.message);
res.status(502).json({ error: err.message });
}
});

router.get("/:appId/xcode-cloud/macos-versions", async (req, res) => {
const account = resolveAccount(req, res);
if (!account) return;

const cacheKey = `ci:macos-versions:${account.id}`;
const cached = apiCache.get(cacheKey);
if (cached) return res.json(cached);

try {
const data = await ascFetch(account, `/v1/ciMacOsVersions?limit=50`);
const result = (data.data || []).map((item) => {
const a = item.attributes || {};
return { id: item.id, name: a.name, version: a.version };
});
apiCache.set(cacheKey, result, VERSION_TTL);
res.json(result);
} catch (err) {
console.error("Failed to fetch macOS versions:", err.message);
res.status(502).json({ error: err.message });
}
});

// ── Build Actions ────────────────────────────────────────────────────────────

router.get("/:appId/xcode-cloud/builds/:buildId/actions", async (req, res) => {
Expand Down
28 changes: 28 additions & 0 deletions src/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,3 +473,31 @@ export async function fetchCiWorkflows(appId, accountId) {
if (!res.ok) throw new Error(`Failed to fetch CI workflows: ${res.status}`);
return res.json();
}

export async function fetchCiWorkflowDetail(appId, workflowId, accountId) {
const params = new URLSearchParams({ accountId });
const res = await fetch(`/api/apps/${appId}/xcode-cloud/workflows/${workflowId}?${params}`);
if (!res.ok) throw new Error(`Failed to fetch workflow detail: ${res.status}`);
return res.json();
}

export async function updateCiWorkflow(appId, workflowId, payload) {
const res = await fetch(`/api/apps/${appId}/xcode-cloud/workflows/${workflowId}`, {
method: "PATCH",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
if (!res.ok) {
const err = await res.json().catch(() => ({}));
throw new Error(err.error || `Failed to update workflow: ${res.status}`);
}
return res.json();
}

export async function fetchCiXcodeVersions(appId, accountId) {
const params = new URLSearchParams({ accountId });
const res = await fetch(`/api/apps/${appId}/xcode-cloud/xcode-versions?${params}`);
if (!res.ok) throw new Error(`Failed to fetch Xcode versions: ${res.status}`);
return res.json();
}

52 changes: 52 additions & 0 deletions src/components/AppStoreManager.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import VersionDetailPage from "./VersionDetailPage.jsx";
import ProductsPage from "./ProductsPage.jsx";
import XcodeCloudPage from "./XcodeCloudPage.jsx";
import BuildDetailPage from "./BuildDetailPage.jsx";
import WorkflowEditPage from "./WorkflowEditPage.jsx";

function buildGroups(apps, groupBy, accounts) {
if (groupBy === "none") return [{ key: "all", label: null, apps }];
Expand Down Expand Up @@ -42,6 +43,8 @@ function getRouteFromPath() {
if (versionMatch) return { appId: versionMatch[1], versionId: versionMatch[2], subPage: null };
const productsMatch = path.match(/^\/app\/([^/]+)\/products$/);
if (productsMatch) return { appId: productsMatch[1], versionId: null, subPage: "products" };
const xcodeCloudWorkflowMatch = path.match(/^\/app\/([^/]+)\/xcode-cloud\/workflow\/([^/]+)$/);
if (xcodeCloudWorkflowMatch) return { appId: xcodeCloudWorkflowMatch[1], versionId: null, subPage: "xcode-cloud-workflow", workflowId: xcodeCloudWorkflowMatch[2] };
const xcodeCloudBuildMatch = path.match(/^\/app\/([^/]+)\/xcode-cloud\/build\/([^/]+)$/);
if (xcodeCloudBuildMatch) return { appId: xcodeCloudBuildMatch[1], versionId: null, subPage: "xcode-cloud-build", buildId: xcodeCloudBuildMatch[2] };
const xcodeCloudMatch = path.match(/^\/app\/([^/]+)\/xcode-cloud$/);
Expand Down Expand Up @@ -110,6 +113,20 @@ export default function AppStoreManager() {
}, []);

const [selectedBuildRun, setSelectedBuildRun] = useState(null);
const [selectedWorkflowId, setSelectedWorkflowId] = useState(null);

const navigateToWorkflowEdit = useCallback((workflow, app) => {
setSelectedApp(app);
setSelectedVersion(null);
setSelectedBuildRun(null);
setSelectedWorkflowId(workflow.id);
setCurrentView("xcode-cloud-workflow");
window.history.pushState(
{ appId: app.id, workflowId: workflow.id, subPage: "xcode-cloud-workflow" },
"",
`/app/${app.id}/xcode-cloud/workflow/${workflow.id}`
);
}, []);

const navigateToXcodeCloudBuild = useCallback((buildRun, app) => {
setSelectedApp(app);
Expand Down Expand Up @@ -150,6 +167,13 @@ export default function AppStoreManager() {
setSelectedApp(appMatch);
setCurrentView("products");
}
} else if (route.subPage === "xcode-cloud-workflow") {
const appMatch = appsList.find((a) => a.id === route.appId);
if (appMatch) {
setSelectedApp(appMatch);
setSelectedWorkflowId(route.workflowId);
setCurrentView("xcode-cloud-workflow");
}
} else if (route.subPage === "xcode-cloud-build") {
const appMatch = appsList.find((a) => a.id === route.appId);
if (appMatch) {
Expand Down Expand Up @@ -221,6 +245,21 @@ export default function AppStoreManager() {
return;
}

if (route.subPage === "xcode-cloud-workflow") {
const appMatch = apps.find((a) => a.id === route.appId);
if (appMatch) {
setSelectedApp(appMatch);
setSelectedWorkflowId(route.workflowId);
setSelectedBuildRun(null);
setCurrentView("xcode-cloud-workflow");
} else {
setSelectedApp(null);
setSelectedWorkflowId(null);
setCurrentView(null);
}
return;
}

if (route.subPage === "xcode-cloud-build") {
const appMatch = apps.find((a) => a.id === route.appId);
if (appMatch) {
Expand Down Expand Up @@ -298,6 +337,18 @@ export default function AppStoreManager() {
);
}

if (currentView === "xcode-cloud-workflow" && selectedApp && selectedWorkflowId) {
return (
<div className="font-sans bg-dark-bg text-dark-text min-h-screen antialiased">
<WorkflowEditPage
app={selectedApp}
workflowId={selectedWorkflowId}
isMobile={isMobile}
/>
</div>
);
}

if (currentView === "xcode-cloud-build" && selectedApp && selectedBuildRun) {
return (
<div className="font-sans bg-dark-bg text-dark-text min-h-screen antialiased">
Expand All @@ -319,6 +370,7 @@ export default function AppStoreManager() {
accounts={accounts}
isMobile={isMobile}
onSelectBuild={(buildRun) => navigateToXcodeCloudBuild(buildRun, selectedApp)}
onSelectWorkflow={(workflow) => navigateToWorkflowEdit(workflow, selectedApp)}
/>
</div>
);
Expand Down
Loading
Loading