in src/VstsDemoBuilder/Controllers/EnvironmentController.cs [641:809]
public JsonResult CheckForInstalledExtensions(string selectedTemplate, string token, string account, string PrivatePath = "")
{
try
{
bool isTemplateBelongToPrivateFolder = projectService.WhereDoseTemplateBelongTo(selectedTemplate);
if (!string.IsNullOrEmpty(selectedTemplate) && !string.IsNullOrEmpty(account) && !string.IsNullOrEmpty(token))
{
string accountName = string.Empty;
string pat = string.Empty;
accountName = account;
pat = token;
string templatesFolder = string.Empty;
string extensionJsonFile = string.Empty;
if (isTemplateBelongToPrivateFolder)
{
templatesFolder = PrivatePath;//Session["PrivateTemplateURL"].ToString();
extensionJsonFile = string.Format("{0}\\{1}", templatesFolder, "Extensions.json");
}
else if (string.IsNullOrEmpty(PrivatePath))
{
templatesFolder = Server.MapPath("~") + @"\Templates\";
extensionJsonFile = string.Format(templatesFolder + @"\{0}\Extensions.json", selectedTemplate);
}
else
{
templatesFolder = PrivatePath;
extensionJsonFile = string.Format(templatesFolder + @"\Extensions.json");
}
if (!(System.IO.File.Exists(extensionJsonFile)))
{
return Json(new { message = "Template not found", status = "false" }, JsonRequestBehavior.AllowGet);
}
RequiredExtensions.Extension template = new RequiredExtensions.Extension();
string listedExtension = System.IO.File.ReadAllText(extensionJsonFile);
if (!string.IsNullOrEmpty(listedExtension))
{
template = JsonConvert.DeserializeObject<RequiredExtensions.Extension>(listedExtension);
}
if (template == null || template.Extensions == null || template.Extensions.Count == 0)
{
return Json(new { message = "no extensions required", status = "false" }, JsonRequestBehavior.AllowGet);
}
template.Extensions.RemoveAll(x => x.extensionName.ToLower() == "analytics");
template.Extensions = template.Extensions.OrderBy(y => y.extensionName).ToList();
string requiresExtensionNames = string.Empty;
string requiredMicrosoftExt = string.Empty;
string requiredThirdPartyExt = string.Empty;
string finalExtensionString = string.Empty;
//Check for existing extensions
if (template.Extensions.Count > 0)
{
Dictionary<string, bool> dict = new Dictionary<string, bool>();
foreach (RequiredExtensions.ExtensionWithLink ext in template.Extensions)
{
if (!dict.ContainsKey(ext.extensionName))
{
dict.Add(ext.extensionName, false);
}
}
var connection = new VssConnection(new Uri(string.Format("https://{0}.visualstudio.com", accountName)), new Microsoft.VisualStudio.Services.OAuth.VssOAuthAccessTokenCredential(pat));// VssOAuthCredential(PAT));
var client = connection.GetClient<ExtensionManagementHttpClient>();
var installed = client.GetInstalledExtensionsAsync().Result;
var extensions = installed.Where(x => x.Flags == 0 && x.ExtensionDisplayName.ToLower() != "analytics").ToList();
var trustedFlagExtensions = installed.Where(x => x.Flags == ExtensionFlags.Trusted && x.ExtensionDisplayName.ToLower() != "analytics").ToList();
var builtInExtensions = installed.Where(x => x.Flags.ToString() == "BuiltIn, Trusted" && x.ExtensionDisplayName.ToLower() != "analytics").ToList();
extensions.AddRange(trustedFlagExtensions);
extensions.AddRange(builtInExtensions);
string askld = JsonConvert.SerializeObject(extensions);
foreach (var ext in extensions)
{
foreach (var extension in template.Extensions)
{
if (extension.extensionName.ToLower() == ext.ExtensionDisplayName.ToLower() && extension.extensionId.ToLower() == ext.ExtensionName.ToLower())
{
dict[extension.extensionName] = true;
}
}
}
var required = dict.Where(x => x.Value == false).ToList();
if (required.Count > 0)
{
requiresExtensionNames = "<p style='color:red'>One or more extension(s) is not installed/enabled in your Azure DevOps Organization.</p><p> You will need to install and enable them in order to proceed. If you agree with the terms below, the required extensions will be installed automatically for the selected organization when the project is provisioned, otherwise install them manually and try refreshing the page </p>";
var installedExtensions = dict.Where(x => x.Value == true).ToList();
if (installedExtensions.Count > 0)
{
foreach (var ins in installedExtensions)
{
string link = "<i class='fas fa-check-circle'></i> " + template.Extensions.Where(x => x.extensionName == ins.Key).FirstOrDefault().link;
string lincense = "";
requiresExtensionNames = requiresExtensionNames + link + lincense + "<br/><br/>";
}
}
foreach (var req in required)
{
string publisher = template.Extensions.Where(x => x.extensionName == req.Key).FirstOrDefault().publisherName;
if (publisher == "Microsoft")
{
string link = "<i class='fa fa-times-circle'></i> " + template.Extensions.Where(x => x.extensionName == req.Key).FirstOrDefault().link;
string lincense = "";
if (!string.IsNullOrEmpty(template.Extensions.Where(x => x.extensionName == req.Key).FirstOrDefault().License))
{
lincense = " - " + template.Extensions.Where(x => x.extensionName == req.Key).FirstOrDefault().License;
}
requiredMicrosoftExt = requiredMicrosoftExt + link + lincense + "<br/>";
}
else
{
string link = "<i class='fa fa-times-circle'></i> " + template.Extensions.Where(x => x.extensionName == req.Key).FirstOrDefault().link;
string lincense = "";
if (!string.IsNullOrEmpty(template.Extensions.Where(x => x.extensionName == req.Key).FirstOrDefault().License))
{
lincense = " - " + template.Extensions.Where(x => x.extensionName == req.Key).FirstOrDefault().License;
}
requiredThirdPartyExt = requiredThirdPartyExt + link + lincense + "<br/>";
}
}
if (!string.IsNullOrEmpty(requiredMicrosoftExt))
{
requiredMicrosoftExt = requiredMicrosoftExt + "<br/><div id='agreeTerms'><label style='font-weight: 400; text-align: justify; padding-left: 5px;'><input type = 'checkbox' class='terms' id = 'agreeTermsConditions' placeholder='microsoft' /> By checking the box I agree, and also on behalf of all users in the organization, that our use of the extension(s) is governed by the <a href = 'https://go.microsoft.com/fwlink/?LinkID=266231' target = '_blank'> Microsoft Online Services Terms </a> and <a href = 'https://go.microsoft.com/fwlink/?LinkId=131004&clcid=0x409' target = '_blank'> Microsoft Online Services Privacy Statement</a></label></div>";
}
if (!string.IsNullOrEmpty(requiredThirdPartyExt))
{
requiredThirdPartyExt = requiredThirdPartyExt + "<br/><div id='ThirdPartyAgreeTerms'><label style = 'font-weight: 400; text-align: justify; padding-left: 5px;'><input type = 'checkbox' class='terms' id = 'ThirdPartyagreeTermsConditions' placeholder='thirdparty' /> The extension(s) are offered to you for your use by a third party, not Microsoft. The extension(s) is licensed separately according to its corresponding License Terms. By continuing and installing those extensions, you also agree to those License Terms.</label></div>";
}
finalExtensionString = requiresExtensionNames + requiredMicrosoftExt + requiredThirdPartyExt;
return Json(new { message = finalExtensionString, status = "false" }, JsonRequestBehavior.AllowGet);
}
else
{
var installedExtensions = dict.Where(x => x.Value == true).ToList();
if (installedExtensions.Count > 0)
{
requiresExtensionNames = "All required extensions are installed/enabled in your Azure DevOps Organization.<br/><br/><b>";
foreach (var ins in installedExtensions)
{
string link = "<i class='fas fa-check-circle'></i> " + template.Extensions.Where(x => x.extensionName == ins.Key).FirstOrDefault().link;
string lincense = "";
requiresExtensionNames = requiresExtensionNames + link + lincense + "<br/>";
}
return Json(new { message = requiresExtensionNames, status = "true" }, JsonRequestBehavior.AllowGet);
}
}
}
else { requiresExtensionNames = "no extensions required"; return Json(new { message = "no extensions required", status = "false" }, JsonRequestBehavior.AllowGet); }
return Json(new { message = requiresExtensionNames, status = "false" }, JsonRequestBehavior.AllowGet);
}
else
{
return Json(new { message = "Error", status = "false" }, JsonRequestBehavior.AllowGet);
}
}
catch (Exception ex)
{
ProjectService.logger.Info(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + "\t" + ex.Message + "\t" + "\n" + ex.StackTrace + "\n");
return Json(new { message = "Error", status = "false" }, JsonRequestBehavior.AllowGet);
}
}