-
Notifications
You must be signed in to change notification settings - Fork 212
Fix hardcoded github.com references in repo-memory for GitHub Enterprise support #16154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -59,6 +59,7 @@ async function main() { | |
|
|
||
| const ghToken = process.env.GH_TOKEN; | ||
| const githubRunId = process.env.GITHUB_RUN_ID || "unknown"; | ||
| const githubServerUrl = process.env.GITHUB_SERVER_URL || "https://github.com"; | ||
|
|
||
| // Log environment variable configuration for debugging | ||
| core.info("Environment configuration:"); | ||
|
|
@@ -131,7 +132,9 @@ async function main() { | |
| // Checkout or create the memory branch | ||
| core.info(`Checking out branch: ${branchName}...`); | ||
| try { | ||
| const repoUrl = `https://x-access-token:${ghToken}@github.com/${targetRepo}.git`; | ||
| // Extract host from server URL (remove https:// prefix) | ||
| const serverHost = githubServerUrl.replace(/^https?:\/\//, ""); | ||
|
||
| const repoUrl = `https://x-access-token:${ghToken}@${serverHost}/${targetRepo}.git`; | ||
|
|
||
| // Try to fetch the branch | ||
| try { | ||
|
|
@@ -342,7 +345,9 @@ async function main() { | |
| // Pull with merge strategy (ours wins on conflicts) | ||
| core.info(`Pulling latest changes from ${branchName}...`); | ||
| try { | ||
| const repoUrl = `https://x-access-token:${ghToken}@github.com/${targetRepo}.git`; | ||
| // Extract host from server URL (remove https:// prefix) | ||
| const serverHost = githubServerUrl.replace(/^https?:\/\//, ""); | ||
| const repoUrl = `https://x-access-token:${ghToken}@${serverHost}/${targetRepo}.git`; | ||
| execGitSync(["pull", "--no-rebase", "-X", "ours", repoUrl, branchName], { stdio: "inherit" }); | ||
| } catch (error) { | ||
| // Pull might fail if branch doesn't exist yet or on conflicts - this is acceptable | ||
|
|
@@ -352,7 +357,9 @@ async function main() { | |
| // Push changes | ||
| core.info(`Pushing changes to ${branchName}...`); | ||
| try { | ||
| const repoUrl = `https://x-access-token:${ghToken}@github.com/${targetRepo}.git`; | ||
| // Extract host from server URL (remove https:// prefix) | ||
| const serverHost = githubServerUrl.replace(/^https?:\/\//, ""); | ||
| const repoUrl = `https://x-access-token:${ghToken}@${serverHost}/${targetRepo}.git`; | ||
| execGitSync(["push", repoUrl, `HEAD:${branchName}`], { stdio: "inherit" }); | ||
| core.info(`Successfully pushed changes to ${branchName} branch`); | ||
| } catch (error) { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| # TARGET_REPO: Repository to clone from (e.g., owner/repo) | ||
| # MEMORY_DIR: Directory to clone into | ||
| # CREATE_ORPHAN: Whether to create orphan branch if it doesn't exist (true/false) | ||
| # GITHUB_SERVER_URL: GitHub server URL (e.g., https://github.com or https://ghe.company.com) | ||
|
|
||
| set -e | ||
|
|
||
|
|
@@ -37,9 +38,18 @@ if [ -z "$CREATE_ORPHAN" ]; then | |
| exit 1 | ||
| fi | ||
|
|
||
| # Default to github.com if not set | ||
| if [ -z "$GITHUB_SERVER_URL" ]; then | ||
| GITHUB_SERVER_URL="https://github.com" | ||
| fi | ||
|
|
||
| # Extract host from server URL (remove https:// or http:// prefix) | ||
| SERVER_HOST="${GITHUB_SERVER_URL#https://}" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good defensive programming - handling both |
||
| SERVER_HOST="${SERVER_HOST#http://}" | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot echo value |
||
|
|
||
| # Try to clone the branch (don't fail if it doesn't exist) | ||
| set +e | ||
| git clone --depth 1 --single-branch --branch "$BRANCH_NAME" "https://x-access-token:${GH_TOKEN}@github.com/${TARGET_REPO}.git" "$MEMORY_DIR" 2>/dev/null | ||
| git clone --depth 1 --single-branch --branch "$BRANCH_NAME" "https://x-access-token:${GH_TOKEN}@${SERVER_HOST}/${TARGET_REPO}.git" "$MEMORY_DIR" 2>/dev/null | ||
| CLONE_EXIT_CODE=$? | ||
| set -e | ||
|
|
||
|
|
@@ -53,7 +63,7 @@ if [ $CLONE_EXIT_CODE -ne 0 ]; then | |
| git checkout --orphan "$BRANCH_NAME" | ||
| git config user.name "github-actions[bot]" | ||
| git config user.email "github-actions[bot]@users.noreply.github.com" | ||
| git remote add origin "https://x-access-token:${GH_TOKEN}@github.com/${TARGET_REPO}.git" | ||
| git remote add origin "https://x-access-token:${GH_TOKEN}@${SERVER_HOST}/${TARGET_REPO}.git" | ||
| else | ||
| echo "Branch $BRANCH_NAME does not exist and create-orphan is false, skipping" | ||
| mkdir -p "$MEMORY_DIR" | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -306,3 +306,61 @@ This workflow has repo-memory disabled. | |||||||||||||||||
| t.Error("Should not have repo memory prompt when disabled") | ||||||||||||||||||
| } | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // TestRepoMemoryGitHubEnterpriseSupport tests that GITHUB_SERVER_URL is used for GHE compatibility | ||||||||||||||||||
| func TestRepoMemoryGitHubEnterpriseSupport(t *testing.T) { | ||||||||||||||||||
| tmpDir := testutil.TempDir(t, "test-*") | ||||||||||||||||||
| workflowPath := filepath.Join(tmpDir, "test-workflow.md") | ||||||||||||||||||
|
|
||||||||||||||||||
| content := `--- | ||||||||||||||||||
| name: Test Repo Memory GHE | ||||||||||||||||||
| on: workflow_dispatch | ||||||||||||||||||
| engine: copilot | ||||||||||||||||||
| tools: | ||||||||||||||||||
| repo-memory: true | ||||||||||||||||||
| --- | ||||||||||||||||||
|
|
||||||||||||||||||
| # Test Workflow | ||||||||||||||||||
|
|
||||||||||||||||||
| This workflow tests GitHub Enterprise support. | ||||||||||||||||||
| ` | ||||||||||||||||||
|
|
||||||||||||||||||
| if err := os.WriteFile(workflowPath, []byte(content), 0644); err != nil { | ||||||||||||||||||
| t.Fatalf("Failed to write workflow file: %v", err) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| compiler := NewCompiler() | ||||||||||||||||||
| if err := compiler.CompileWorkflow(workflowPath); err != nil { | ||||||||||||||||||
| t.Fatalf("Failed to compile workflow: %v", err) | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Read the generated lock file | ||||||||||||||||||
| lockPath := stringutil.MarkdownToLockFile(workflowPath) | ||||||||||||||||||
| lockContent, err := os.ReadFile(lockPath) | ||||||||||||||||||
| if err != nil { | ||||||||||||||||||
| t.Fatalf("Failed to read lock file: %v", err) | ||||||||||||||||||
| } | ||||||||||||||||||
| lockFile := string(lockContent) | ||||||||||||||||||
|
|
||||||||||||||||||
| // Check that GITHUB_SERVER_URL is passed to clone step | ||||||||||||||||||
| if !strings.Contains(lockFile, "GITHUB_SERVER_URL: ${{ github.server_url }}") { | ||||||||||||||||||
| t.Error("Expected GITHUB_SERVER_URL environment variable in clone step") | ||||||||||||||||||
| } | ||||||||||||||||||
|
|
||||||||||||||||||
| // Check that GITHUB_SERVER_URL is passed to push step (in push_repo_memory job) | ||||||||||||||||||
| // The push step should include GITHUB_SERVER_URL in the env section | ||||||||||||||||||
| if !strings.Contains(lockFile, "GITHUB_SERVER_URL: ${{ github.server_url }}") { | ||||||||||||||||||
| t.Error("Expected GITHUB_SERVER_URL environment variable in push step") | ||||||||||||||||||
|
Comment on lines
+350
to
+353
|
||||||||||||||||||
| // Check that GITHUB_SERVER_URL is passed to push step (in push_repo_memory job) | |
| // The push step should include GITHUB_SERVER_URL in the env section | |
| if !strings.Contains(lockFile, "GITHUB_SERVER_URL: ${{ github.server_url }}") { | |
| t.Error("Expected GITHUB_SERVER_URL environment variable in push step") | |
| // Check that GITHUB_SERVER_URL is passed to both clone and push steps | |
| // It should appear at least twice (once for the clone step and once for the push step) | |
| if strings.Count(lockFile, "GITHUB_SERVER_URL: ${{ github.server_url }}") < 2 { | |
| t.Error("Expected GITHUB_SERVER_URL environment variable in both clone and push steps") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice GitHub Enterprise support! Dynamically extracting the server host ensures compatibility with both github.com and GHE instances. 👍