md-linear-sync
Version:
Sync Linear tickets to local markdown files with status-based folder organization
83 lines ⢠3.77 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.commentCommand = commentCommand;
const fs_1 = __importDefault(require("fs"));
const path_1 = __importDefault(require("path"));
const client_1 = require("../client");
const config_1 = require("../config");
const parsers_1 = require("../parsers");
async function commentCommand(ticketId, commentText) {
console.log(`š¬ Adding comment to ticket ${ticketId}...\n`);
try {
// Load configuration
const config = await config_1.ConfigManager.loadConfig();
const envConfig = config_1.ConfigManager.loadEnvironmentConfig();
if (!envConfig.linear?.apiKey) {
console.error('ā LINEAR_API_KEY not found in environment');
process.exit(1);
}
// Initialize Linear client
const client = new client_1.LinearSyncClient(envConfig.linear.apiKey);
// Find the ticket file
const filePath = findTicketFile(ticketId);
if (!filePath) {
throw new Error(`Ticket file for ${ticketId} not found`);
}
console.log(`š Found ticket file: ${path_1.default.basename(filePath)}`);
// Parse the file to get Linear issue ID
const content = fs_1.default.readFileSync(filePath, 'utf-8');
const ticket = parsers_1.TicketFileParser.parseFile(content);
if (!ticket.frontmatter.linear_id) {
throw new Error(`No Linear ID found in ticket file. File may not be synced with Linear yet.`);
}
// Get the Linear issue to get the internal ID
const linearIssue = await client.getIssue(ticket.frontmatter.linear_id);
// Create the comment
console.log(`š Adding comment to Linear issue ${ticket.frontmatter.linear_id}...`);
const comment = await client.createComment(linearIssue.id, commentText);
console.log(`ā
Comment added successfully!`);
console.log(`š View ticket: ${ticket.frontmatter.url}`);
console.log(`ā¹ļø Comment will be synced to local file via webhook`);
}
catch (error) {
console.error('\nā Failed to add comment:', error instanceof Error ? error.message : 'Unknown error');
process.exit(1);
}
}
function findTicketFile(ticketId) {
const linearTicketsDir = 'linear-tickets';
if (!fs_1.default.existsSync(linearTicketsDir)) {
return null;
}
// Search all status folders for the ticket file
const statusFolders = fs_1.default.readdirSync(linearTicketsDir, { withFileTypes: true })
.filter(dirent => dirent.isDirectory())
.map(dirent => dirent.name);
for (const folder of statusFolders) {
const folderPath = path_1.default.join(linearTicketsDir, folder);
const files = fs_1.default.readdirSync(folderPath);
// Look for files by parsing frontmatter to check linear_id
for (const file of files) {
if (!file.endsWith('.md'))
continue;
try {
const filePath = path_1.default.join(folderPath, file);
const content = fs_1.default.readFileSync(filePath, 'utf-8');
const ticket = parsers_1.TicketFileParser.parseFile(content);
// Check if this file has the matching linear_id in frontmatter
if (ticket.frontmatter.linear_id === ticketId) {
return filePath;
}
}
catch (error) {
// Skip files that can't be parsed
continue;
}
}
}
return null;
}
//# sourceMappingURL=comment.js.map