in src/VstsDemoBuilder/Services/ProjectService.cs [1732:1845]
private void CreateServiceEndPoint(Project model, List<string> jsonPaths, Configuration _endpointConfig)
{
try
{
string serviceEndPointId = string.Empty;
foreach (string jsonPath in jsonPaths)
{
string fileName = Path.GetFileName(jsonPath);
string jsonCreateService = jsonPath;
if (File.Exists(jsonCreateService))
{
string username = System.Configuration.ConfigurationManager.AppSettings["UserID"];
string password = System.Configuration.ConfigurationManager.AppSettings["Password"];
//string extractPath = HostingEnvironment.MapPath("~/Templates/" + model.SelectedTemplate);
string projectFileData = File.ReadAllText(GetJsonFilePath(model.IsPrivatePath, model.PrivateTemplatePath, model.SelectedTemplate, "ProjectTemplate.json"));
ProjectSetting settings = JsonConvert.DeserializeObject<ProjectSetting>(projectFileData);
ServiceEndPoint objService = new ServiceEndPoint(_endpointConfig);
string gitUserName = System.Configuration.ConfigurationManager.AppSettings["GitUserName"];
string gitUserPassword = System.Configuration.ConfigurationManager.AppSettings["GitUserPassword"];
jsonCreateService = model.ReadJsonFile(jsonCreateService);
if (!string.IsNullOrEmpty(settings.IsPrivate))
{
jsonCreateService = jsonCreateService.Replace("$ProjectName$", model.ProjectName);
jsonCreateService = jsonCreateService.Replace("$username$", model.Email).Replace("$password$", model.accessToken);
}
// File contains "GitHub_" means - it contains GitHub URL, user wanted to fork repo to his github
if (fileName.Contains("GitHub_") && model.GitHubFork && model.GitHubToken != null)
{
JObject jsonToCreate = JObject.Parse(jsonCreateService);
string type = jsonToCreate["type"].ToString();
string url = jsonToCreate["url"].ToString();
string repoNameInUrl = Path.GetFileName(url);
// Endpoint type is Git(External Git), so we should point Build def to his repo by creating endpoint of Type GitHub(Public)
foreach (var repo in model.Environment.GitHubRepos.Keys)
{
if (repoNameInUrl.Contains(repo))
{
if (type.ToLower() == "git")
{
jsonToCreate["type"] = "GitHub"; //Changing endpoint type
jsonToCreate["url"] = model.Environment.GitHubRepos[repo].ToString(); // updating endpoint URL with User forked repo URL
}
// Endpoint type is GitHub(Public), so we should point the build def to his repo by updating the URL
else if (type.ToLower() == "github")
{
jsonToCreate["url"] = model.Environment.GitHubRepos[repo].ToString(); // Updating repo URL to user repo
}
else
{
}
}
}
jsonCreateService = jsonToCreate.ToString();
jsonCreateService = jsonCreateService.Replace("$GitUserName$", model.GitHubUserName).Replace("$GitUserPassword$", model.GitHubToken);
}
// user doesn't want to fork repo
else
{
jsonCreateService = jsonCreateService.Replace("$ProjectName$", model.ProjectName); // Replaces the Place holder with project name if exists
jsonCreateService = jsonCreateService.Replace("$username$", username).Replace("$password$", password) // Replaces user name and password with app setting username and password if require[to import soure code to Azure Repos]
.Replace("$GitUserName$", gitUserName).Replace("$GitUserPassword$", gitUserPassword); // Replaces GitUser name and passwords with Demo gen username and password [Just to point build def to respective repo]
}
if (model.SelectedTemplate.ToLower() == "bikesharing360")
{
string bikeSharing360username = System.Configuration.ConfigurationManager.AppSettings["UserID"];
string bikeSharing360password = System.Configuration.ConfigurationManager.AppSettings["BikeSharing360Password"];
jsonCreateService = jsonCreateService.Replace("$BikeSharing360username$", bikeSharing360username).Replace("$BikeSharing360password$", bikeSharing360password);
}
else if (model.SelectedTemplate.ToLower() == "contososhuttle" || model.SelectedTemplate.ToLower() == "contososhuttle2")
{
string contosousername = System.Configuration.ConfigurationManager.AppSettings["ContosoUserID"];
string contosopassword = System.Configuration.ConfigurationManager.AppSettings["ContosoPassword"];
jsonCreateService = jsonCreateService.Replace("$ContosoUserID$", contosousername).Replace("$ContosoPassword$", contosopassword);
}
else if (model.SelectedTemplate.ToLower() == "sonarqube")
{
if (!string.IsNullOrEmpty(model.SonarQubeDNS))
{
jsonCreateService = jsonCreateService.Replace("$URL$", model.SonarQubeDNS);
}
}
else if (model.SelectedTemplate.ToLower() == "octopus")
{
var url = model.Parameters["OctopusURL"];
var apiKey = model.Parameters["APIkey"];
if (!string.IsNullOrEmpty(url.ToString()) && !string.IsNullOrEmpty(apiKey.ToString()))
{
jsonCreateService = jsonCreateService.Replace("$URL$", url).Replace("$Apikey$", apiKey);
}
}
var endpoint = objService.CreateServiceEndPoint(jsonCreateService, model.ProjectName);
if (!(string.IsNullOrEmpty(objService.LastFailureMessage)))
{
AddMessage(model.id.ErrorId(), "Error while creating service endpoint: " + objService.LastFailureMessage + Environment.NewLine);
}
else
{
model.Environment.serviceEndpoints[endpoint.name] = endpoint.id;
}
}
}
}
catch (Exception ex)
{
logger.Info(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss") + "\t" + "\t" + ex.Message + "\t" + "\n" + ex.StackTrace + "\n");
AddMessage(model.id.ErrorId(), "Error while creating service endpoint: " + ex.Message);
}
}