Skip to content

Commit

Permalink
enable odata api
Browse files Browse the repository at this point in the history
  • Loading branch information
nleach999 committed Jul 21, 2023
1 parent ca70058 commit ac7d311
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 4 deletions.
20 changes: 20 additions & 0 deletions Libs/Configuration/Impls/CxSASTAPIOverrides.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CxAnalytix.Configuration.Impls
{
public sealed class CxSASTAPIOverrides : ConfigurationElement
{

[ConfigurationProperty("Project", IsRequired = false, DefaultValue = false)]
public bool Project
{
get => (bool)this["Project"];
set => this["Project"] = value;
}
}
}
6 changes: 6 additions & 0 deletions Libs/Configuration/Impls/CxSASTConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,11 @@ public String MNOUrl
set { this["mnoURL"] = value; }
}

[ConfigurationProperty("UseOdata", IsRequired = false)]
public CxSASTAPIOverrides Overrides
{
get => (CxSASTAPIOverrides)this["UseOdata"];
set => this["UseOdata"] = value;
}
}
}
121 changes: 118 additions & 3 deletions Libs/CxRestClient/SAST/CxProjects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ public class CxProjects
{
private static ILog _log = LogManager.GetLogger(typeof(CxProjects));

private static String URL_SUFFIX = "cxrestapi/projects";
private static readonly String REST_URL_SUFFIX = "cxrestapi/projects";

private static readonly int ODATA_TOP = 25;
private static readonly String ODATA_URL_SUFFIX = "cxwebinterface/odata/v1/Projects?$expand=CustomFields&$select=OwningTeamId,PresetId,Id,Name,IsPublic" +
$"&$orderby=Id asc&$top={ODATA_TOP}";

private static String _apiVersion = null;

Expand All @@ -41,29 +45,94 @@ private static String GetApiVersion(CxSASTRestContext ctx, CancellationToken tok
private CxProjects()
{ }

#region DTOs

[JsonObject(MemberSerialization.OptIn)]
public class ProjectCustomFields
{
[JsonProperty(PropertyName = "name")]
public String FieldName { get; internal set; }

[JsonProperty(PropertyName = "FieldName")]
private String odata_FieldName
{
get => FieldName;
set => FieldName = value;
}


[JsonProperty(PropertyName = "value")]
public String FieldValue { get; internal set; }

[JsonProperty(PropertyName = "FieldValue")]
private String odata_FieldValue
{
get => FieldValue;
set => FieldValue = value;
}

}

[JsonObject(MemberSerialization.OptIn)]
public class Project
{
[JsonProperty(PropertyName = "teamId")]
public String TeamId { get; internal set; }
[JsonProperty(PropertyName = "OwningTeamId")]
private String odata_TeamId
{
get => TeamId;
set => TeamId = value;
}



public int PresetId { get; internal set; }


[JsonProperty(PropertyName = "id")]
public int ProjectId { get; internal set; }
[JsonProperty(PropertyName = "Id")]
private int odata_ProjectId
{
get => ProjectId;
set => ProjectId = value;
}


[JsonProperty(PropertyName = "name")]
public String ProjectName { get; internal set; }
[JsonProperty(PropertyName = "Name")]
private String odata_ProjectName
{
get => ProjectName;
set => ProjectName = value;
}




[JsonProperty(PropertyName = "isPublic")]
public bool IsPublic { get; internal set; }
[JsonProperty(PropertyName = "IsPublic")]
private bool odata_IsPublic
{
get => IsPublic;
set => IsPublic = value;
}



[JsonProperty(PropertyName = "customFields")]
public List<ProjectCustomFields> CustomFields { get; internal set; }
[JsonProperty(PropertyName = "CustomFields")]
private List<ProjectCustomFields> odata_CustomFields
{
get => CustomFields;
set => CustomFields = value;
}



[JsonProperty(PropertyName = "isBranched")]
public bool IsBranched { get; internal set; }
Expand All @@ -78,6 +147,7 @@ public class Project
public override string ToString() =>
$"{ProjectId}:{ProjectName} [TeamId: {TeamId} Public: {IsPublic} CustomFields: {CustomFields.Count}]";
}
#endregion

private class ProjectReader : IEnumerable<Project>, IEnumerator<Project>, IDisposable
{
Expand Down Expand Up @@ -161,12 +231,57 @@ public void Reset()
{
throw new NotImplementedException();
}
}

private static IEnumerable<Project> GetProjects_odata(CxSASTRestContext ctx, CancellationToken token)
{
List<Project> returnedResults = new();

var filter = new Dictionary<String, String>();
List<Project> fetchedPage = null;

do
{
String requestUrl = UrlUtils.MakeUrl(ctx.Sast.ApiUrl, ODATA_URL_SUFFIX, filter);

using (var projectReader = WebOperation.ExecuteGet<ProjectReader>(
ctx.Sast.Json.CreateClient
, (response) =>
{
using (var sr = new StreamReader(response.Content.ReadAsStreamAsync().Result))
using (var jtr = new JsonTextReader(sr))
{
JToken jt = JToken.Load(jtr);
return new ProjectReader(jt["value"], ctx, token);
}
}
, requestUrl
, ctx.Sast
, token))
fetchedPage = new List<Project>(projectReader);

if (fetchedPage != null)
{
returnedResults.AddRange(fetchedPage);
filter["$filter"] = $"id gt {fetchedPage[fetchedPage.Count - 1].ProjectId}";
}


} while (fetchedPage != null && fetchedPage.Count == ODATA_TOP);

return returnedResults;
}

public static IEnumerable<Project> GetProjects(CxSASTRestContext ctx, CancellationToken token, bool useOData)
{
if (useOData)
return GetProjects_odata(ctx, token);
else
return GetProjects_rest(ctx, token);
}

public static IEnumerable<Project> GetProjects(CxSASTRestContext ctx, CancellationToken token)
private static IEnumerable<Project> GetProjects_rest(CxSASTRestContext ctx, CancellationToken token)
{
using (var projectReader = WebOperation.ExecuteGet<ProjectReader>(
ctx.Sast.Json.CreateClient
Expand All @@ -180,7 +295,7 @@ public static IEnumerable<Project> GetProjects(CxSASTRestContext ctx, Cancellati
return new ProjectReader(jt, ctx, token);
}
}
, UrlUtils.MakeUrl(ctx.Sast.ApiUrl, URL_SUFFIX)
, UrlUtils.MakeUrl(ctx.Sast.ApiUrl, REST_URL_SUFFIX)
, ctx.Sast
, token, apiVersion: GetApiVersion(ctx, token) ))
return new List<Project>(projectReader);
Expand Down
3 changes: 2 additions & 1 deletion XForm/SastTransformer/Transformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ private async Task ResolveScans()

_sastVersionTask = GetSASTVersion();

var projectsTask = Task.Run(() => CxProjects.GetProjects(RestContext, ThreadOpts.CancellationToken), ThreadOpts.CancellationToken);
var projectsTask = Task.Run(() => CxProjects.GetProjects(RestContext, ThreadOpts.CancellationToken,
Config.GetConfig<CxSASTConnection>().Overrides.Project), ThreadOpts.CancellationToken);

Policies = await policyTask;
Teams = await teamsTask;
Expand Down

0 comments on commit ac7d311

Please sign in to comment.