Skip to content

Commit

Permalink
Add detailed logging and fix successful imports
Browse files Browse the repository at this point in the history
  • Loading branch information
nverges committed Nov 22, 2024
1 parent 2993d1f commit 4b12646
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 17 deletions.
60 changes: 48 additions & 12 deletions src/import.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { MAX_REQUESTS_PER_SECOND } from "../config/config.js";
import {
ENABLE_DETAILED_LOGGING,
MAX_REQUESTS_PER_SECOND,
} from "../config/config.js";
import parseCSV from "./csv/parse.mjs";
import chalk from "chalk";

Expand All @@ -25,12 +28,9 @@ import { setupLogger } from "./logger/init.mjs";
import { RELEASE_LABEL_NAME } from "./init.mjs";
import init from "./init.mjs";
import readSuccessfulImports from "./logger/read_successful_imports.mjs";

import logSuccessfulImport from "./logger/log_successful_import.mjs";

const CREATE_ISSUES = true;

// const DELAY = 0
const DELAY = Math.ceil(500 / MAX_REQUESTS_PER_SECOND);

const { teamId, teamName } = await selectTeam();
Expand All @@ -51,22 +51,63 @@ const {
csvFilename,
pivotalUsers,
} = await parseCSV();

// Optional Import params
const estimationScale = await importPivotalEstimates({ teamId });
const { importFiles } = await importFileAttachments();
const { importLabels } = await importLabelsFromCSV();
const { selectedStatusTypes } = await selectStatusTypes(statusTypes);
const successfulImports = await readSuccessfulImports(teamName);
const uniquePivotalStories = [
...new Map(pivotalStories.map((story) => [story.id, story])).values(),
];

// Logs
if (ENABLE_DETAILED_LOGGING) {
console.log("\nImport Status:");
console.log("Successful imports from CSV:", successfulImports.size);
console.log(
"Sample of successful imports:",
Array.from(successfulImports).slice(0, 5),
);
console.log("\nPivotal Stories:");
console.log("Total stories from Pivotal (raw):", pivotalStories.length);
console.log(
"Total unique stories from Pivotal:",
uniquePivotalStories.length,
);
console.log(
"Sample of unique Pivotal story IDs:",
uniquePivotalStories.slice(0, 5).map((story) => story.id),
);
}

const newReleaseStories = releaseStories.filter(
(story) => !successfulImports.has(story.id),
);

// Filter pivotal stories based on selectedStatusTypes
const newPivotalStories = pivotalStories.filter(
const newPivotalStories = uniquePivotalStories.filter(
(story) =>
selectedStatusTypes.includes(story.type.toLowerCase()) &&
!successfulImports.has(story.id),
);

if (ENABLE_DETAILED_LOGGING) {
console.log("\nFiltering results:");
console.log("- Total stories before filtering:", uniquePivotalStories.length);
console.log(
"- Stories matching selected type(s):",
uniquePivotalStories.filter((story) =>
selectedStatusTypes.includes(story.type.toLowerCase()),
).length,
);
console.log(
"- Stories remaining after excluding imports:",
newPivotalStories.length,
);
}

const { userConfirmedProceed } = await proceedWithImport({
releaseStories: newReleaseStories,
pivotalStories: newPivotalStories,
Expand All @@ -82,9 +123,6 @@ if (userConfirmedProceed) {
process.exit(0);
}

// Delete existing labels
// await deleteLabels({ teamId })

// Creates Team Labels and Workflow Statuses
await init({ teamId, teamName, pivotalUsers });

Expand All @@ -97,9 +135,7 @@ if (userConfirmedProceed) {
const processReleaseStories = async () => {
if (newReleaseStories?.length === 0) {
console.log(
chalk.cyan(
`Converting ${newReleaseStories.length} Release Stories into Linear Cycles for Team ${teamName}`,
),
chalk.cyan(`No release stories to convert for Team ${teamName}`),
);
} else {
console.log(
Expand Down Expand Up @@ -168,7 +204,7 @@ if (userConfirmedProceed) {
// Process Pivotal Stories
const processPivotalStories = async () => {
if (newPivotalStories?.length === 0) {
console.log("No Pivotal Stories found in the CSV file.");
console.log("No Pivotal Stories found to import.");
} else {
console.log(
chalk.cyan(
Expand Down
24 changes: 19 additions & 5 deletions src/logger/read_successful_imports.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,40 @@ import fs from 'fs/promises';
import Logger from './logger.mjs';

const readSuccessfulImports = async (teamName) => {
if (!teamName) {
console.warn('No team name provided to readSuccessfulImports');
return new Set();
}

try {
const filePath = Logger.getTeamLogPath(teamName, 'successful_imports.csv');

// Check if file exists
try {
await fs.access(filePath);
} catch {
// If file doesn't exist, return empty set
} catch (error) {
console.log(`No existing import log found for team "${teamName}"`);
return new Set();
}

const content = await fs.readFile(filePath, 'utf-8');
const lines = content.split('\n').slice(1); // Skip header row

return new Set(
const successfulImports = new Set(
lines
.filter(line => line.trim()) // Remove empty lines
.map(line => line.split(',')[1]) // Get ID from CSV
.map(line => {
const [, id] = line.split(',');
return id?.trim(); // Ensure ID is trimmed
})
.filter(Boolean) // Remove any undefined/null/empty values
);

// console.log(`Found ${successfulImports.size} previously imported stories for team "${teamName}"`);
return successfulImports;

} catch (error) {
console.error('Error reading successful imports:', error);
console.error(`Error reading successful imports for team "${teamName}":`, error);
return new Set();
}
};
Expand Down

0 comments on commit 4b12646

Please sign in to comment.