Skip to content

Conversation

github-actions[bot]
Copy link

Closes #554

Applied changes based on Issue #554.

I’ve refactored src/utils/contents.ts to improve readability, DRYness, and type safety while preserving all existing behavior. Changes include:

  • Consolidated duplicate logic into a single internal quoteLines helper.
  • Introduced a constant GITHUB_ACTIONS_BOT_LOGIN for the bot login string.
  • Destructured the CommentContent parameter in the exported functions for clarity.
  • Exported the CommentContent interface so consumers can import the type.
  • Cleaned up JSDoc comments to align with each declaration.
  • Applied Prettier formatting per the project’s configuration.

Here’s the updated file:

/**
 * Content formatting utilities module.
 *
 * Provides functions to generate formatted content strings for GitHub comments.
 */

/** Author login string for GitHub Actions bot. */
const GITHUB_ACTIONS_BOT_LOGIN = 'github-actions[bot]';

/**
 * Represents a GitHub comment with body text and author login.
 */
export interface CommentContent {
  /** Raw body text of the comment. */
  body: string;
  /** Author login of the comment (e.g., "github-actions[bot]"). */
  login: string;
}

/**
 * Prefixes each line of the input text with "> ".
 *
 * @param text - The text to quote.
 * @returns Quoted text with each line prefixed by "> ".
 */
function quoteLines(text: string): string {
  return text.split('\n').map((line) => `> ${line}`).join('\n');
}

/**
 * Quote the comment body if it was authored by the GitHub Actions bot.
 *
 * @param comment - Comment to process.
 * @returns Quoted body string with each line prefixed by "> " and two trailing newlines,
 * or empty string if the body is empty or not authored by the GitHub Actions bot.
 */
export function genContentsString({ body, login }: CommentContent): string {
  const trimmed = body.trim();
  if (!trimmed || login.trim() !== GITHUB_ACTIONS_BOT_LOGIN) {
    return '';
  }
  return `${quoteLines(trimmed)}\n\n`;
}

/**
 * Generate a fully quoted content string.
 *
 * Prefixes each line of the body with "> " and retains all content.
 *
 * @param comment - Comment to process.
 * @returns Fully quoted body string with two trailing newlines or empty string if the body is empty.
 */
export function genFullContentsString({ body }: CommentContent): string {
  const trimmed = body.trim();
  if (!trimmed) {
    return '';
  }
  return `${quoteLines(trimmed)}\n\n`;
}

【F:src/utils/contents.ts】

No functional changes were introduced—just a more modular, readable, and maintainable structure. Let me know if any tweaks are needed!

@YiweiShen YiweiShen merged commit 6c15c67 into main Jul 26, 2025
3 checks passed
@YiweiShen YiweiShen deleted the codez-chore-554-refactor-utils-refactor-contents-utility-3121008989 branch July 26, 2025 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Done] Refactor src/utils/contents.ts

1 participant