/**
* Developed by eBrook Group.
* Copyright © 2026 eBrook Group (https://www.ebrook.com.tw)
*/
/**
* Update GitHub Branch field tool handler
* Updates the GitHub Branch custom field of a ClickUp task
*/
import { ClickUpService } from "../services/clickup.js";
import { debug, error } from "../utils/logger.js";
import { sanitizeErrorResponse } from "../utils/error-sanitizer.js";
import type { AppConfig } from "../config/env.js";
import type { ClickUpTaskResponse } from "../types/index.js";
/**
* Update GitHub Branch field tool handler
* @param task_id - ClickUp task ID
* @param branch_name - GitHub branch name
* @param _config - Application configuration (unused)
* @param click_up_service - ClickUp service instance
* @returns Tool execution result
*/
export async function handleUpdateGithubBranch(
task_id: string,
branch_name: string,
_config: AppConfig,
click_up_service: ClickUpService
): Promise<{ content: Array<{ type: "text"; text: string }>; isError?: boolean }> {
debug(`Updating GitHub Branch field for task: ${task_id} to: ${branch_name}`);
// First, get the task to find the custom field ID
const task_res = await click_up_service.getTask(task_id);
debug(`ClickUp API response status: ${task_res.status}`);
if (task_res.status !== 200) {
// Sanitized error message for client
const error_msg = `Failed to fetch task ${task_id}. ${sanitizeErrorResponse(task_res.status, task_res.body)}`;
// Full error details only in logs
debug(`Full API error response: ${JSON.stringify(task_res.body)}`);
error(error_msg);
return {
content: [
{
type: "text",
text: error_msg,
},
],
isError: true,
};
}
const task = task_res.body as ClickUpTaskResponse;
// Find the GitHub Branch custom field ID
const field_id = click_up_service.findCustomFieldId(task, "Github Branch");
if (!field_id) {
const error_msg = `Custom field 'Github Branch' not found in task ${task_id}. Available custom fields: ${
task.custom_fields?.map((f) => f.name).join(", ") || "none"
}`;
error(error_msg);
return {
content: [
{
type: "text",
text: error_msg,
},
],
isError: true,
};
}
debug(`Found GitHub Branch field ID: ${field_id}`);
// Update the custom field
const update_res = await click_up_service.updateCustomField(task_id, field_id, branch_name);
debug(`ClickUp custom field update response status: ${update_res.status}`);
if (update_res.status !== 200) {
// Sanitized error message for client
const error_msg = `Failed to update GitHub Branch field for task ${task_id}. ${sanitizeErrorResponse(update_res.status, update_res.body)}`;
// Full error details only in logs
debug(`Full API error response: ${JSON.stringify(update_res.body)}`);
error(error_msg);
return {
content: [
{
type: "text",
text: error_msg,
},
],
isError: true,
};
}
debug(`Successfully updated GitHub Branch field`);
// Build formatted output
const output_lines: string[] = [];
output_lines.push("=".repeat(80));
output_lines.push(`✅ GITHUB BRANCH FIELD UPDATED`);
output_lines.push("=".repeat(80));
output_lines.push("");
output_lines.push(`📋 Task: ${task.name || "Unknown"}`);
output_lines.push(`🆔 Task ID: ${task.custom_id || task.id || task_id}`);
output_lines.push(`🌿 GitHub Branch: ${branch_name}`);
output_lines.push(`🔗 URL: ${task.url || "N/A"}`);
output_lines.push("");
output_lines.push("=".repeat(80));
// Return formatted result
return {
content: [
{
type: "text",
text: output_lines.join("\n"),
},
],
isError: false,
};
}