Skip to content

Conversation

github-actions[bot]
Copy link

Closes #540

Applied changes based on Issue #540.

I’ve refactored src/github/progress.ts to improve readability, remove duplication, and pull out shared logic into small helpers—without changing any behavior. Key changes:

  • Introduced a RepoContext type for the { owner, repo } parameter.
  • Extracted the spinner HTML into a single SPINNER_HTML constant.
  • Created getRandomLoadingPhrase() to centralize random‐phrase selection.
  • Simplified the body construction in both createProgressComment and updateProgressComment by using array literals and Array.map() instead of manual loops and repeated pushes.
  • Kept all existing functionality (same titles, emojis, progress‐bar logic, spinner placement, event‐type branching).

Click to see the refactored file snippet
import * as core from '@actions/core';
import type { Octokit } from 'octokit';
import type { GitHubEvent } from './types';
import { GitHubError } from '../utils/errors';
import {
  PROGRESS_BAR_BLOCKS,
  PROGRESS_TITLE,
  LOADING_PHRASES,
} from '../constants';

/** Escape special characters in a literal string so it can be used in a RegExp. */
export function escapeRegExp(str: string): string {
  return str.replace(/[.*+?^${}()|[\\]\\]/g, '$&');
}

/** Repository owner and name context. */
type RepoContext = { owner: string; repo: string };

/** HTML snippet for the spinner icon displayed next to the active step. */
const SPINNER_HTML =
  ' <img src="https://github.com/user-attachments/assets/082dfba3-0ee2-4b6e-9606-93063bcc7590" alt="spinner" width="16" height="16"/>';

/** Select a random loading phrase to display under the progress bar. */
function getRandomLoadingPhrase(): string {
  return LOADING_PHRASES[Math.floor(Math.random() * LOADING_PHRASES.length)];
}

/**
 * Create a GitHub comment to display initial progress steps with checkboxes.
 */
export async function createProgressComment(
  octokit: Octokit,
  repo: RepoContext,
  event: GitHubEvent,
  steps: string[],
): Promise<number> {
  const emptyBar = '░'.repeat(PROGRESS_BAR_BLOCKS);
  const body = [
    PROGRESS_TITLE,
    '',
    `Progress: [${emptyBar}] 0%`,
    '',
    getRandomLoadingPhrase(),
    '',
    ...steps.map((step, i) => {
      const checkbox = `- [ ] ${step}`;
      return i === 0 ? `${checkbox}${SPINNER_HTML}` : checkbox;
    }),
    '',
  ].join('\n');

  if ('issue' in event) {
    const { data } = await octokit.rest.issues.createComment({
      ...repo,
      issue_number: event.issue.number,
      body,
    });
    core.info(`Created progress comment with id: ${data.id}`);
    return data.id;
  } else if ('pull_request' in event && 'comment' in event) {
    const inReplyTo = event.comment.in_reply_to_id ?? event.comment.id;
    const { data } = await octokit.rest.pulls.createReplyForReviewComment({
      ...repo,
      pull_number: event.pull_request.number,
      comment_id: inReplyTo,
      body,
    });
    core.info(`Created progress comment with id: ${data.id}`);
    return data.id;
  }
  throw new GitHubError('Unsupported event for progress comment');
}

/**
 * Update the content of an existing GitHub progress comment.
 */
export async function updateProgressComment(
  octokit: Octokit,
  repo: RepoContext,
  event: GitHubEvent,
  commentId: number,
  steps: string[],
): Promise<void> {
  const total = steps.length;
  const completed = steps.filter((s) => s.startsWith('- [x]')).length;
  const filled = Math.round((completed / total) * PROGRESS_BAR_BLOCKS);
  const bar =
    '█'.repeat(filled) + '░'.repeat(PROGRESS_BAR_BLOCKS - filled);
  const percent = Math.round((completed / total) * 100);

  const body = [
    PROGRESS_TITLE,
    '',
    `Progress: ${bar} ${percent}%${percent === 100 ? ' ✅' : ''}`,
    '',
    getRandomLoadingPhrase(),
    '',
    ...steps.map((line, i) =>
      i === completed && completed !== total
        ? `${line}${SPINNER_HTML}`
        : line
    ),
    '',
  ].join('\n');

  if ('issue' in event) {
    await octokit.rest.issues.updateComment({
      ...repo,
      comment_id: commentId,
      body,
    });
  } else if ('pull_request' in event) {
    await octokit.rest.pulls.updateReviewComment({
      ...repo,
      comment_id: commentId,
      body,
    });
  } else {
    throw new GitHubError('Unsupported event for updating progress comment');
  }
}

All existing behavior is retained; you can merge this refactor. Let me know if you’d like any further tweaks!

@YiweiShen YiweiShen merged commit de6c246 into main Jul 26, 2025
3 checks passed
@YiweiShen YiweiShen deleted the codez-chore-540-refactor-progress-restructure-progress-logic-3121065459 branch July 26, 2025 03:12
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/github/progress.ts

1 participant