using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
namespace WindowsFormsApp1
{
public partial class BlackDuckTool : Form
{
public BlackDuckTool()
{
InitializeComponent();
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
// Use SecurityProtocolType.Ssl3 if needed for compatibility reasons
}
string baseProjectUrl = "https://xxx.app.blackduck.com/api/projects/17d8b108-4e2d-4321-95ff-a967da407efd/versions/1c04aecf-ba41-41c4-8048-2b4fcf2d98a1/components?offset={0}&limit=100&sort=projectName%20ASC";
string linksPath = "C:\BlackDuck_MatckLinks.txt";
private void btnDone_Click(object sender, EventArgs e)
{
getLinks();
ParseAllSourcePaths();
}
void ParseAllSourcePaths()
{
string allPath = "";
List<string> listPath = new List<string>();
string links = File.ReadAllText(linksPath);
var oklinks = links.Split("
".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
foreach (string l in oklinks)
{
if (string.IsNullOrEmpty(l)) { continue; };
var json = Httpget(l);
Matck.MatchObject mc = JsonConvert.DeserializeObject<Matck.MatchObject>(json);
if (mc != null && mc.items.Length > 0) {
mc.items.ToList().ForEach(
n => {
allPath+= n.filePath.path+"
";
listPath.Add(n.filePath.path);
}
);
}
}
listPath.Sort();
allPath = "";
foreach (var s in listPath) {
allPath += s+"
";
}
File.WriteAllText("C:\BlackDuckAllFilePath.txt",allPath);
}
private void getLinks()
{
string err = "";
int pageIndex = 0;
List<string> urlList = new List<string>();
for (int i = 0; i < 5; i++)
{
string url = string.Format(baseProjectUrl, pageIndex * 100);
string json = Httpget(url);
if (json != "")
{
ResponseData data = JsonConvert.DeserializeObject<ResponseData>(json);
foreach (var d in data.items)
{
foreach (var o in d.origins)
{
o._meta.links.ToList().ForEach(n =>
{
if (n.rel == "matched-files")
{
if (!urlList.Contains(n.href))
{
urlList.Add(n.href);
}
};
});
}
}
}
else
{
err += (pageIndex + 1) + " failed
";
}
pageIndex++;
}
string links = "";
foreach (var s in urlList)
{
links += s + "
";
}
File.WriteAllText("C:\BlackDuck_MatckLinks.txt", links);
if (err != "")
{
MessageBox.Show(err);
}
else
{
MessageBox.Show("get URLS OK");
}
}
string Httpget(string url)
{
try
{
WebClient wc = new WebClient();
wc.Headers.Add("cookie", Properties.Resources.cookie);
string s = wc.DownloadString(url);
wc.Dispose();
wc = null;
return s;
}
catch (Exception ex)
{
return "";
}
}
}
public class ResponseData
{
public int totalCount { get; set; }
public Item[] items { get; set; }
public object[] appliedFilters { get; set; }
public _Meta _meta { get; set; }
}
public class _Meta
{
public string[] allow { get; set; }
public string href { get; set; }
public Link[] links { get; set; }
}
public class Link
{
public string rel { get; set; }
public string href { get; set; }
public string name { get; set; }
public string label { get; set; }
}
public class Item
{
public string componentName { get; set; }
public string componentVersionName { get; set; }
public string component { get; set; }
public string componentVersion { get; set; }
public int totalFileMatchCount { get; set; }
public License[] licenses { get; set; }
public Origin[] origins { get; set; }
public string[] usages { get; set; }
public string[] matchTypes { get; set; }
public DateTime releasedOn { get; set; }
public Licenseriskprofile licenseRiskProfile { get; set; }
public Securityriskprofile securityRiskProfile { get; set; }
public Versionriskprofile versionRiskProfile { get; set; }
public Activityriskprofile activityRiskProfile { get; set; }
public Operationalriskprofile operationalRiskProfile { get; set; }
public Activitydata activityData { get; set; }
public string reviewStatus { get; set; }
public string approvalStatus { get; set; }
public string policyStatus { get; set; }
public bool componentModified { get; set; }
public _Meta1 _meta { get; set; }
}
public class Licenseriskprofile
{
public Count[] counts { get; set; }
}
public class Count
{
public string countType { get; set; }
public int count { get; set; }
}
public class Securityriskprofile
{
public Count1[] counts { get; set; }
}
public class Count1
{
public string countType { get; set; }
public int count { get; set; }
}
public class Versionriskprofile
{
public Count2[] counts { get; set; }
}
public class Count2
{
public string countType { get; set; }
public int count { get; set; }
}
public class Activityriskprofile
{
public Count3[] counts { get; set; }
}
public class Count3
{
public string countType { get; set; }
public int count { get; set; }
}
public class Operationalriskprofile
{
public Count4[] counts { get; set; }
}
public class Count4
{
public string countType { get; set; }
public int count { get; set; }
}
public class Activitydata
{
public DateTime lastCommitDate { get; set; }
public int newerReleases { get; set; }
public int contributorCount12Month { get; set; }
public int commitCount12Month { get; set; }
public string trending { get; set; }
}
public class _Meta1
{
public string[] allow { get; set; }
public string href { get; set; }
public Link1[] links { get; set; }
}
public class Link1
{
public string rel { get; set; }
public string href { get; set; }
}
public class License
{
public string licenseDisplay { get; set; }
public string license { get; set; }
public string spdxId { get; set; }
public License1[] licenses { get; set; }
public string licenseType { get; set; }
}
public class License1
{
public string licenseDisplay { get; set; }
public string license { get; set; }
public string spdxId { get; set; }
public object[] licenses { get; set; }
}
public class Origin
{
public string name { get; set; }
public string origin { get; set; }
public string externalNamespace { get; set; }
public string externalId { get; set; }
public bool externalNamespaceDistribution { get; set; }
public _Meta2 _meta { get; set; }
}
public class _Meta2
{
public object[] allow { get; set; }
public Link2[] links { get; set; }
}
public class Link2
{
public string rel { get; set; }
public string href { get; set; }
}
//---------------------
}
namespace WindowsFormsApp1.Matck
{
public class MatchObject
{
public int totalCount { get; set; }
public Item[] items { get; set; }
public object[] appliedFilters { get; set; }
public _Meta _meta { get; set; }
}
public class _Meta
{
public string[] allow { get; set; }
public string href { get; set; }
public object[] links { get; set; }
}
public class Item
{
public Filepath filePath { get; set; }
public string[] usages { get; set; }
public _Meta1 _meta { get; set; }
}
public class Filepath
{
public string path { get; set; }
public string archiveContext { get; set; }
public string compositePathContext { get; set; }
public string fileName { get; set; }
}
public class _Meta1
{
public Link[] links { get; set; }
}
public class Link
{
public string rel { get; set; }
public string href { get; set; }
}
}