Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions ast-visual-studio-extension/CxExtension/Toolbar/CxToolbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,61 @@ internal class CxToolbar
public Func<List<State>, Dictionary<MenuItem, State>> CreateStateMenuItems { get; set; }

private static bool initPolling = false;

public static bool IsValidSourceProject(string sourcePath)
{
if (string.IsNullOrEmpty(sourcePath))
{
return false;
}

try
{
string searchPath;
if (System.IO.File.Exists(sourcePath))
{
searchPath = System.IO.Path.GetDirectoryName(sourcePath);
}
else if (System.IO.Directory.Exists(sourcePath))
{
searchPath = sourcePath;
}
else
{
return false;
}

string[] projectExtensions = { "*.sln", "*.csproj", "*.vbproj", "*.fsproj", "*.vcxproj" };

foreach (string extension in projectExtensions)
{
var files = System.IO.Directory.GetFiles(searchPath, extension, System.IO.SearchOption.AllDirectories);
if (files.Any(file => IsValidProjectFile(file)))
return true;
}

return false;
}
catch (Exception ex)
{
UpdateStatusBar("Checkmarx: Error validating project directory" + ex.Message);
return false;
}
}


private static bool IsValidProjectFile(string filePath)
{
try
{
var fileInfo = new System.IO.FileInfo(filePath);
return fileInfo.Exists && fileInfo.Length > 0;
}
catch
{
return false;
}
}
private const string DevOrTestFilterName = "SCA Dev & Test Dependencies";


Expand Down Expand Up @@ -286,6 +341,13 @@ public async Task ScanStart_ClickAsync()
return;
}

if (!IsValidSourceProject(dte.Solution.FullName))
{
CxUtils.DisplayMessageInInfoWithLinkBar(Package, CxConstants.NOT_A_VALID_PROJECT, KnownMonikers.StatusError, "Project Error", "", false);
ScanStartButton.IsEnabled = true;
return;
}

var currentGitBranch = await GetCurrentGitBranchAsync(dte);
var checkmarxBranch = SettingsUtils.GetToolbarValue(Package, SettingsUtils.branchProperty);
var matchProject = await ASTProjectMatchesWorkspaceProjectAsync(dte);
Expand Down Expand Up @@ -322,6 +384,9 @@ private static async Task<string> GetCurrentGitBranchAsync(EnvDTE.DTE dte)
try
{
string workingDir = System.IO.Path.GetDirectoryName(dte.Solution.FullName);
if (string.IsNullOrEmpty(workingDir) || !System.IO.Directory.Exists(workingDir))
return null;

RepositoryInformation repository = RepositoryInformation.GetRepositoryInformation(workingDir);

if (repository == null)
Expand Down
4 changes: 3 additions & 1 deletion ast-visual-studio-extension/CxExtension/Utils/CxConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ internal class CxConstants
public static string PROJECT_AND_BRANCH_DO_NOT_MATCH => "The Git branch and files open in your workspace don't match the branch and project that were previously scanned in this Checkmarx project. Do you want to scan anyway?";
public static string RUN_SCAN => "Run scan";
public static string RUN_SCAN_ACTION => "RUN_SCAN_ACTION";

public static string NOT_A_VALID_PROJECT => "No.NET project files(.sln, .csproj, .vbproj, .fsproj, .vcxproj) found in directory";


/** LEARN MORE AND REMEDIATION **/
public static string CODE_SAMPLE_TITLE => "{0} using {1}";
public static string NO_INFORMATION => "No information";
Expand Down
Loading