Skip to content

[code-simplifier] Simplify serverHost extraction in push_repo_memory.cjs#16182

Merged
pelikhan merged 1 commit intomainfrom
code-simplifier/simplify-serverhost-extraction-20260216-1d6f1fdf38181fa0
Feb 16, 2026
Merged

[code-simplifier] Simplify serverHost extraction in push_repo_memory.cjs#16182
pelikhan merged 1 commit intomainfrom
code-simplifier/simplify-serverhost-extraction-20260216-1d6f1fdf38181fa0

Conversation

@github-actions
Copy link
Contributor

Code Simplification - 2026-02-16

This PR simplifies recently modified code to improve clarity and eliminate duplication while preserving all functionality.

Files Simplified

  • actions/setup/js/push_repo_memory.cjs - Eliminated duplicate serverHost extraction pattern

Improvements Made

1. Reduced Complexity

Before: The serverHost extraction pattern was duplicated 3 times:

  • Line 136: In checkout branch section
  • Line 349: In pull section
  • Line 361: In push section

After: Extract once at the top of main() function (line 63) and reuse throughout.

Code reduction: 9 lines removed (3 duplicate extractions + 3 duplicate comments)

2. Enhanced Clarity

  • Single source of truth - serverHost is now defined in one place
  • Consistent pattern - All three git operations use the same serverHost variable
  • Easier to modify - Any changes to host extraction logic only need to happen once
  • Clearer intent - Reader immediately sees how serverHost is derived

3. Applied Project Standards

  • Follows DRY (Don't Repeat Yourself) principle
  • Improves code maintainability
  • Reduces chance of inconsistencies if logic needs to change

Changes Based On

Recent changes from:

  • #16154 - Fix hardcoded github.com references in repo-memory for GitHub Enterprise support
  • Commit 425869f - Merged today (2026-02-16)

The original PR added GitHub Enterprise support by extracting serverHost from GITHUB_SERVER_URL. This simplification consolidates the three duplicate extraction patterns into a single declaration.

Testing

  • ✅ JavaScript syntax validated (node -c)
  • ✅ Code formatting passes (make fmt-cjs)
  • ✅ Linting passes (make lint-cjs)
  • ✅ No functional changes - behavior is identical
  • ✅ All three git operations (fetch, pull, push) use the same serverHost variable

Review Focus

Please verify:

  • Functionality is preserved (serverHost used in all three places)
  • Simplification improves code quality
  • Changes align with DRY principle
  • No unintended side effects

Diff Summary

+ const serverHost = githubServerUrl.replace(/^https?:\/\//, "");  // Added once at top

- // Extract host from server URL (remove https:// prefix)          // Removed from 3 places
- const serverHost = githubServerUrl.replace(/^https?:\/\//, "");

Net change: +1 line, -6 lines (3 extractions + 3 comments) = 5 lines saved


Automated by Code Simplifier Agent - analyzing code from the last 24 hours

AI generated by Code Simplifier

  • expires on Feb 17, 2026, 7:01 PM UTC

Extract serverHost from githubServerUrl once at the top of main() function
instead of duplicating the extraction pattern 3 times throughout the code.

Changes:
- Add serverHost extraction after githubServerUrl initialization (line 63)
- Remove duplicate extraction in checkout branch section (line 135-136)
- Remove duplicate extraction in pull section (line 347-348)
- Remove duplicate extraction in push section (line 359-360)

Benefits:
- Eliminates code duplication (DRY principle)
- Single source of truth for server host extraction
- Easier to maintain and modify
- Clearer code structure

All functionality preserved - no behavioral changes.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@pelikhan pelikhan marked this pull request as ready for review February 16, 2026 19:03
Copilot AI review requested due to automatic review settings February 16, 2026 19:03
@pelikhan pelikhan merged commit cf0ad8c into main Feb 16, 2026
4 checks passed
@pelikhan pelikhan deleted the code-simplifier/simplify-serverhost-extraction-20260216-1d6f1fdf38181fa0 branch February 16, 2026 19:04
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors push_repo_memory.cjs to remove duplicated GitHub Enterprise host extraction logic by computing serverHost once and reusing it across repo-memory git operations.

Changes:

  • Extract serverHost once near the top of main() instead of repeating it in checkout/pull/push sections.
  • Reuse the shared serverHost when constructing repo-memory git remote URLs.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

const ghToken = process.env.GH_TOKEN;
const githubRunId = process.env.GITHUB_RUN_ID || "unknown";
const githubServerUrl = process.env.GITHUB_SERVER_URL || "https://github.com";
const serverHost = githubServerUrl.replace(/^https?:\/\//, "");
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

serverHost derivation doesn’t trim a trailing slash from GITHUB_SERVER_URL. If GITHUB_SERVER_URL is set to something like https://ghe.example.com/, this produces ...@ghe.example.com//owner/repo.git remote URLs. Consider normalizing first (e.g., remove a trailing /) before building git URLs for consistency with other host-resolution code in the repo.

Suggested change
const serverHost = githubServerUrl.replace(/^https?:\/\//, "");
const serverHost = githubServerUrl.replace(/^https?:\/\//, "").replace(/\/+$/, "");

Copilot uses AI. Check for mistakes.
Comment on lines 136 to 138
const repoUrl = `https://x-access-token:${ghToken}@${serverHost}/${targetRepo}.git`;

// Try to fetch the branch
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

repoUrl is still constructed three times with the same template (checkout/pull/push). Since ghToken, serverHost, and targetRepo are constant within main(), consider defining repoUrl once and reusing it to further reduce duplication and lower the chance of future inconsistencies.

See below for a potential fix:

  const repoUrl = `https://x-access-token:${ghToken}@${serverHost}/${targetRepo}.git`;
  try {

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant