Skip to content
This repository was archived by the owner on Jul 21, 2025. It is now read-only.

Commit f5802c5

Browse files
Merge pull request #61 from microsoft/artifact_project_creation_bug
Improve error handling and refactor code structure
2 parents 8cec044 + 5a5a445 commit f5802c5

3 files changed

Lines changed: 50 additions & 29 deletions

File tree

src/ADOGenerator/Program.cs

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
.Replace("bin\\Debug\\net8.0", "")
2424
.Replace("bin\\Release\\net8.0", "")
2525
.Replace("bin\\Debug", "")
26-
.Replace("bin\\Release", "");
26+
.Replace("bin\\Release", "")
27+
.Replace("ADOGenerator\\", "ADOGenerator\\");
2728

2829
do
2930
{
31+
if (currentPath.EndsWith("ADOGenerator"))
32+
currentPath = currentPath + "\\";
3033
string id = Guid.NewGuid().ToString().Split('-')[0];
3134
Console.ForegroundColor = ConsoleColor.Cyan;
3235
Console.WriteLine("Do you want to create a new template or create a new project using the demo generator project template?");
@@ -103,32 +106,35 @@
103106

104107
void HandleTemplateAndArtifactsUpdate(string template, string id, Project model, string currentPath)
105108
{
109+
var templatePathBin = Path.Combine(Directory.GetCurrentDirectory(), "Templates", "TemplateSetting.json");
110+
var templatePathOriginal = Path.Combine(currentPath, "Templates", "TemplateSetting.json");
111+
if (UpdateTemplateSettings(template, id, templatePathOriginal))
112+
{
113+
id.AddMessage(Environment.NewLine + "Template settings updated successfully at " + templatePathOriginal);
114+
}
115+
else
116+
{
117+
id.ErrorId().AddMessage(Environment.NewLine + "Template settings update failed at " + templatePathOriginal);
118+
}
119+
if (templatePathBin.Equals(templatePathOriginal))
120+
{
121+
id.AddMessage(Environment.NewLine + "Template path is same as original.");
122+
return;
123+
}
106124
id.AddMessage(Environment.NewLine+"Do you want to update the template settings and move the artifacts to the executable directory? (yes/no): press enter to confirm");
107125
var updateTemplateSettings = Console.ReadLine();
108-
109126
if (string.IsNullOrWhiteSpace(updateTemplateSettings) || updateTemplateSettings.Equals("yes", StringComparison.OrdinalIgnoreCase) || updateTemplateSettings.Equals("y", StringComparison.OrdinalIgnoreCase))
110127
{
111128
id.AddMessage(Environment.NewLine + "Updating template settings...");
112-
var templatePathBin = Path.Combine(Directory.GetCurrentDirectory(), "Templates", "TemplateSetting.json");
113-
var templatePathOriginal = Path.Combine(currentPath, "Templates", "TemplateSetting.json");
114-
115-
if (UpdateTemplateSettings(template, id, templatePathOriginal))
116-
{
117-
id.AddMessage(Environment.NewLine+"Template settings updated successfully at " + templatePathOriginal);
118-
}
119-
else
120-
{
121-
id.ErrorId().AddMessage(Environment.NewLine+"Template settings update failed at " + templatePathOriginal);
122-
}
123-
129+
124130
CopyFileIfExists(id,templatePathOriginal, templatePathBin);
125131

126132
id.AddMessage(Environment.NewLine+"Template settings copied to the current directory and updated successfully.");
127133
id.AddMessage(Environment.NewLine + "Moving artifacts to the current directory...");
128134

129135
var artifactsPathOriginal = Path.Combine(currentPath, "Templates", $"CT-{model.ProjectName.Replace(" ", "-")}");
130136
var artifactsPath = Path.Combine(Directory.GetCurrentDirectory(), "Templates", $"CT-{model.ProjectName.Replace(" ", "-")}");
131-
137+
132138
MoveArtifacts(artifactsPathOriginal, artifactsPath, id);
133139
}
134140
else
@@ -139,18 +145,26 @@ void HandleTemplateAndArtifactsUpdate(string template, string id, Project model,
139145

140146
void CopyFileIfExists(string id,string sourcePath, string destinationPath)
141147
{
142-
if (File.Exists(sourcePath))
148+
try
143149
{
144-
File.Copy(sourcePath, destinationPath, true);
150+
if (File.Exists(sourcePath))
151+
{
152+
File.Copy(sourcePath, destinationPath, true);
153+
}
154+
else
155+
{
156+
Console.ForegroundColor = ConsoleColor.Green;
157+
id.AddMessage(Environment.NewLine + $"Source file '{sourcePath}' does not exist. Creating a new file at the destination.");
158+
string fileContents = File.ReadAllText(sourcePath);
159+
File.WriteAllText(destinationPath, fileContents);
160+
id.AddMessage(Environment.NewLine + $"New file created at '{destinationPath}'.");
161+
Console.ResetColor();
162+
}
145163
}
146-
else
164+
catch(Exception ex)
147165
{
148-
Console.ForegroundColor = ConsoleColor.Green;
149-
id.AddMessage(Environment.NewLine+$"Source file '{sourcePath}' does not exist. Creating a new file at the destination.");
150-
string fileContents = File.ReadAllText(sourcePath);
151-
File.WriteAllText(destinationPath, fileContents);
152-
id.AddMessage(Environment.NewLine + $"New file created at '{destinationPath}'.");
153-
Console.ResetColor();
166+
id.ErrorId().AddMessage(Environment.NewLine + "Error copying file: " + ex.Message);
167+
return;
154168
}
155169
}
156170

src/ADOGenerator/Services/ExtractorService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ public class ExtractorService : IExtractorService
4343
public ExtractorService(IConfiguration config)
4444
{
4545
_config = config;
46+
if (currentPath.EndsWith("ADOGenerator"))
47+
currentPath = currentPath + "\\";
4648
}
4749
#endregion STATIC DECLARATIONS
4850

src/API/ProjectsAndTeams/Teams.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ public GetTeamResponse.Team CreateNewTeam(string json, string project)
3333
if (response.IsSuccessStatusCode)
3434
{
3535
GetTeamResponse.Team viewModel = new GetTeamResponse.Team();
36-
viewModel = response.Content.ReadFromJsonAsync<GetTeamResponse.Team>().Result;
36+
string responseString = response.Content.ReadAsStringAsync().Result;
37+
viewModel = JsonConvert.DeserializeObject<GetTeamResponse.Team>(responseString);
3738
return viewModel;
3839
}
3940
else
@@ -68,7 +69,8 @@ public TeamMemberResponse.TeamMembers GetTeamMembers(string projectName, string
6869
if (response.IsSuccessStatusCode)
6970
{
7071
TeamMemberResponse.TeamMembers viewModel = new TeamMemberResponse.TeamMembers();
71-
viewModel = response.Content.ReadFromJsonAsync<TeamMemberResponse.TeamMembers>().Result;
72+
string responseString = response.Content.ReadAsStringAsync().Result;
73+
viewModel = JsonConvert.DeserializeObject<TeamMemberResponse.TeamMembers>(responseString);
7274
return viewModel;
7375
}
7476
else
@@ -186,7 +188,8 @@ public string GetTeamSetting(string projectName)
186188
if (response.IsSuccessStatusCode)
187189
{
188190
TeamSettingResponse.TeamSetting viewModel = new TeamSettingResponse.TeamSetting();
189-
viewModel = response.Content.ReadFromJsonAsync<TeamSettingResponse.TeamSetting>().Result;
191+
string responseString = response.Content.ReadAsStringAsync().Result;
192+
viewModel = JsonConvert.DeserializeObject<TeamSettingResponse.TeamSetting>(responseString);
190193
return viewModel.backlogIteration.id;
191194
}
192195
else
@@ -257,7 +260,8 @@ public TeamIterationsResponse.Iterations GetAllIterations(string projectName)
257260
HttpResponseMessage response = client.GetAsync(projectName + "/_apis/wit/classificationnodes?$depth=1&api-version=5.0-preview.2").Result;
258261
if (response.IsSuccessStatusCode)
259262
{
260-
viewModel = response.Content.ReadFromJsonAsync<TeamIterationsResponse.Iterations>().Result;
263+
string responseString = response.Content.ReadAsStringAsync().Result;
264+
viewModel = JsonConvert.DeserializeObject<TeamIterationsResponse.Iterations>(responseString);
261265
return viewModel;
262266
}
263267
else
@@ -329,7 +333,8 @@ public TeamResponse GetTeamByName(string projectName, string teamaName)
329333
HttpResponseMessage response = client.GetAsync("_apis/projects/" + projectName + "/teams/" + teamaName + "?api-version=" + _configuration.VersionNumber).Result;
330334
if (response.IsSuccessStatusCode)
331335
{
332-
viewModel = response.Content.ReadFromJsonAsync<TeamResponse>().Result;
336+
string responseString = response.Content.ReadAsStringAsync().Result;
337+
viewModel = JsonConvert.DeserializeObject<TeamResponse>(responseString);
333338
return viewModel;
334339
}
335340
else

0 commit comments

Comments
 (0)