Skip to content

Commit dc1d992

Browse files
committed
Fix for URL rewriting in install command
Fixes an error where cloning the `github/codeql` repo would fail when the local environment had a git config for automatically rewriting the URL scheme (to SSH) for any `git clone` task.
1 parent 9ada5bb commit dc1d992

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

src/CodeQLToolkit.Features/CodeQL/Commands/Targets/InstallCommand.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,33 @@ public class InstallCommand : CommandTarget
1414
{
1515
public bool CustomBundles { get; set; }
1616
public bool QuickBundles { get; set; }
17-
public string[] Packs { get; set; }
17+
public string[] Packs { get; set; } = Array.Empty<string>();
1818

1919
void SetEnvironmentVariableMultiTarget(string name, string value)
2020
{
2121
Log<InstallCommand>.G().LogInformation($"Setting {name} to {value}...");
2222

2323
Environment.SetEnvironmentVariable(name, value);
2424

25+
// Only attempt to write to GITHUB_ENV if we're running in GitHub Actions
2526
if (AutomationTypeHelper.AutomationTypeFromString(AutomationTarget) == AutomationType.ACTIONS)
2627
{
2728
string? githubEnvPath = Environment.GetEnvironmentVariable("GITHUB_ENV");
2829
try
2930
{
30-
if (File.Exists(githubEnvPath))
31+
if (!string.IsNullOrEmpty(githubEnvPath) && File.Exists(githubEnvPath))
3132
{
3233
File.AppendAllText(githubEnvPath, $"{name}={value}\n");
34+
Log<InstallCommand>.G().LogInformation($"Successfully wrote {name} to GITHUB_ENV file.");
3335
}
3436
else
3537
{
36-
throw new Exception("Could not find GITHUB_ENV file.");
38+
Log<InstallCommand>.G().LogWarning($"GITHUB_ENV file not found. Environment variable {name} set locally only.");
3739
}
3840
}
39-
catch (Exception)
41+
catch (Exception ex)
4042
{
41-
Log<InstallCommand>.G().LogError($"Could not write to GITHUB_ENV file.");
42-
throw;
43+
Log<InstallCommand>.G().LogWarning($"Could not write to GITHUB_ENV file: {ex.Message}. Environment variable {name} set locally only.");
4344
}
4445
}
4546
}

src/CodeQLToolkit.Shared/CodeQL/CodeQLInstallation.cs

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,12 +170,35 @@ private void PackageInstall()
170170

171171
Log<CodeQLInstallation>.G().LogInformation($"Checkout standard library into.. {StdLibDirectory}");
172172

173-
var repoPath = Repository.Clone("https://github.com/github/codeql.git", StdLibDirectory);
174-
173+
// Use direct git command to clone the repository to avoid LibGit2Sharp
174+
// issues with SSH URL rewriting in Git configuration
175+
using (Process gitProcess = new Process())
176+
{
177+
gitProcess.StartInfo.FileName = "git";
178+
gitProcess.StartInfo.WorkingDirectory = InstallationDirectory;
179+
gitProcess.StartInfo.UseShellExecute = false;
180+
gitProcess.StartInfo.RedirectStandardOutput = true;
181+
gitProcess.StartInfo.RedirectStandardError = true;
182+
gitProcess.StartInfo.Arguments = $"clone https://github.com/github/codeql.git {Path.GetFileName(StdLibDirectory)}";
183+
184+
gitProcess.Start();
185+
string output = gitProcess.StandardOutput.ReadToEnd();
186+
string error = gitProcess.StandardError.ReadToEnd();
187+
gitProcess.WaitForExit();
188+
189+
if (gitProcess.ExitCode != 0)
190+
{
191+
Log<CodeQLInstallation>.G().LogError($"Git clone failed with exit code {gitProcess.ExitCode}");
192+
Log<CodeQLInstallation>.G().LogError($"Output: {output}");
193+
Log<CodeQLInstallation>.G().LogError($"Error: {error}");
194+
throw new Exception($"Failed to clone CodeQL standard library repository: {error}");
195+
}
196+
}
175197

176198
Log<CodeQLInstallation>.G().LogInformation($"Getting standard library version.. {StandardLibraryVersion}");
177199

178-
using (var repo = new Repository(repoPath))
200+
// Now use LibGit2Sharp to checkout the specific version since the repo is already cloned
201+
using (var repo = new Repository(StdLibDirectory))
179202
{
180203
var tag = repo.Tags[$"refs/tags/{StandardLibraryVersion}"];
181204

0 commit comments

Comments
 (0)