Skip to content

Commit 5d3ffb2

Browse files
calin-lupas_dsamcapscalin-lupas_dsamcaps
authored andcommitted
#39 Implement team update feature
Enhance GitHub client extensions and refactor models - Modified `GitHubClientExtensions` to support file moving with a new optional `newPath` parameter. - Updated `AddAppServices` method to simplify its signature. - Renamed and added error constants in `Constants.cs` for clarity. - Replaced `CreateTeamRequestModel` with `TeamRequestModel` for improved team management. - Updated `RequestType` enum to differentiate between create and update operations. - Refactored `RepositoryRequestModel` and adjusted parsing/validation in `RepositoryService`. - Enhanced `TeamService` to utilize the new `TeamRequestModel` and support team updates. - Updated localization files for consistency in error messages. - Revised `IRepositoryService` and `ITeamService` interfaces to remove `ValidateCreate` methods. - Streamlined service registration in `Program.cs` for better configuration management.
1 parent f6c6493 commit 5d3ffb2

16 files changed

Lines changed: 423 additions & 178 deletions

src/DevExcelerateApi/Core/Extensions/GitHubClientExtensions.cs

Lines changed: 59 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public static async Task<string> GetFileContentAsync(this IGitHubClient gitHubCl
4747
string defaultBranchName,
4848
string branch,
4949
string path,
50-
string content)
50+
string? newPath,
51+
string content)
5152
{
5253
if (gitHubClient == null)
5354
{
@@ -71,25 +72,63 @@ public static async Task<string> GetFileContentAsync(this IGitHubClient gitHubCl
7172
}
7273
}
7374

75+
string pathForCreationOrNonExistent = newPath ?? path;
76+
7477
try
7578
{
76-
// Check if the file already exists
79+
// Check if the file already exists at the original 'path'
7780
var existingFile = await gitHubClient.Repository.Content.GetAllContentsByRef(owner, repo, path, branch);
7881

7982
if (existingFile != null && existingFile.Count > 0)
8083
{
81-
var updateFileRequest = new UpdateFileRequest($"Updating file {path}", content, existingFile[0].Sha, branch);
82-
return await gitHubClient.Repository.Content.UpdateFile(owner, repo, path, updateFileRequest);
84+
// File exists at 'path'
85+
if (newPath != null && newPath != path)
86+
{
87+
// Path change requested: delete old file, then create new file
88+
var deleteFileRequest = new DeleteFileRequest(
89+
$"Deleting old file at {path} to move to {newPath}",
90+
existingFile[0].Sha,
91+
branch);
92+
await gitHubClient.Repository.Content.DeleteFile(owner, repo, path, deleteFileRequest);
93+
94+
var createFileRequest = new CreateFileRequest(
95+
$"Adding file {newPath} (moved from {path})",
96+
content,
97+
branch);
98+
return await gitHubClient.Repository.Content.CreateFile(owner, repo, newPath, createFileRequest);
99+
}
100+
else
101+
{
102+
// Update in place (newPath is null or same as path)
103+
var updateFileRequest = new UpdateFileRequest(
104+
$"Updating file {path}",
105+
content,
106+
existingFile[0].Sha,
107+
branch);
108+
return await gitHubClient.Repository.Content.UpdateFile(owner, repo, path, updateFileRequest);
109+
}
110+
}
111+
else
112+
{
113+
// File does not exist at 'path' (e.g., GetAllContentsByRef returned empty list)
114+
// Create the file at 'pathForCreationOrNonExistent'
115+
var createFileRequest = new CreateFileRequest(
116+
$"Adding file {pathForCreationOrNonExistent}",
117+
content,
118+
branch);
119+
return await gitHubClient.Repository.Content.CreateFile(owner, repo, pathForCreationOrNonExistent, createFileRequest);
83120
}
84121
}
85122
catch (NotFoundException)
86123
{
87-
var createFileRequest = new CreateFileRequest($"Adding file {path}", content, branch);
88-
89-
return await gitHubClient.Repository.Content.CreateFile(owner, repo, path, createFileRequest);
124+
// File does not exist at 'path' (GetAllContentsByRef threw NotFoundException)
125+
// Create the file at 'pathForCreationOrNonExistent'
126+
var createFileRequest = new CreateFileRequest(
127+
$"Adding file {pathForCreationOrNonExistent}",
128+
content,
129+
branch);
130+
return await gitHubClient.Repository.Content.CreateFile(owner, repo, pathForCreationOrNonExistent, createFileRequest);
90131
}
91-
92-
return null;
93132
}
94133

95134
public static async Task<PullRequest> CreatePullRequestAsync(this IGitHubClient gitHubClient, string owner, string repo, string headBranch, string baseBranch, string title, string body)
@@ -149,22 +188,19 @@ public static async Task<Dictionary<string, string>> GetCommitFilesWithContentAs
149188

150189
foreach (var file in files)
151190
{
152-
if (file.Status == "added" || file.Status == "modified") // Only fetch content for added/modified files
191+
try
153192
{
154-
try
155-
{
156-
// Fetch the raw content of the file
157-
var rawContent = await gitHubClient.Repository.Content.GetRawContent(owner, repo, file.Filename);
158-
var contentString = System.Text.Encoding.UTF8.GetString(rawContent);
193+
// Fetch the raw content of the file
194+
var rawContent = await gitHubClient.Repository.Content.GetRawContent(owner, repo, file.Filename);
195+
var contentString = System.Text.Encoding.UTF8.GetString(rawContent);
159196

160-
// Add the file path and content to the dictionary
161-
fileContents.Add(file.Filename, contentString);
162-
}
163-
catch (NotFoundException)
164-
{
165-
// Handle cases where the file content is not accessible
166-
fileContents.Add(file.Filename, "Content not accessible");
167-
}
197+
// Add the file path and content to the dictionary
198+
fileContents.Add(file.Filename, contentString);
199+
}
200+
catch (NotFoundException)
201+
{
202+
// Handle cases where the file content is not accessible
203+
fileContents.Add(file.Filename, "Content not accessible");
168204
}
169205
}
170206

src/DevExcelerateApi/Core/Extensions/ServiceExtensions.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,12 @@ internal static IServiceCollection AddAppLocalization(this IServiceCollection se
5858
return services;
5959
}
6060

61-
internal static IServiceCollection AddAppServices(this IServiceCollection services, IWebHostEnvironment environment, IConfiguration configuration)
61+
internal static IServiceCollection AddAppServices(this IServiceCollection services, IWebHostEnvironment environment)
6262
{
6363
TokenCredential azureCredentials = environment.IsDevelopment()
6464
// Only allow Developer style credentials in development
6565
? new ChainedTokenCredential(
6666
new VisualStudioCredential(),
67-
new VisualStudioCodeCredential(),
6867
new AzureCliCredential(),
6968
new AzureDeveloperCliCredential(),
7069
new AzurePowerShellCredential())

src/DevExcelerateApi/Models/Constants.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,16 @@ public static class Constants
2626
public const string ERROR_PR_NO_CHANGES = "ERROR_REPO_PR_NO_CHANGES";
2727

2828
// Team related constants
29-
public const string ERROR_TEAM_CREATION = "ERROR_TEAM_CREATION";
29+
public const string ERROR_TEAM_ISSUE = "ERROR_TEAM_ISSUE";
3030
public const string ERROR_TEAM_EXISTS = "ERROR_TEAM_EXISTS";
3131
public const string ERROR_PARENT_TEAM_DOES_NOT_EXISTS = "ERROR_PARENT_TEAM_DOES_NOT_EXISTS";
3232
public const string ERROR_TEAM_MEMBER_DOES_NOT_EXISTS = "ERROR_TEAM_MEMBER_DOES_NOT_EXISTS";
3333
public const string ERROR_INVAILD_TEAM_NAME = "ERROR_INVAILD_TEAM_NAME";
34+
public const string ERROR_USER_NOT_TEAM_MAINTAINER = "ERROR_USER_NOT_TEAM_MAINTAINER";
35+
public const string ERROR_TEAM_DOES_NOT_EXIST = "ERROR_TEAM_DOES_NOT_EXIST";
36+
37+
public const string ERROR_UPDATE_NO_CHANGES = "ERROR_UPDATE_NO_CHANGES";
38+
public const string NONE = "NONE";
3439

3540
public const string VALIDATION_PASSED = "VALIDATION_PASSED";
3641
public const string ISSUE_APPROVED = "ISSUE_APPROVED";

src/DevExcelerateApi/Models/CreateTeamRequestModel.cs

Lines changed: 0 additions & 90 deletions
This file was deleted.

src/DevExcelerateApi/Models/DevExIssueRequestModel.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public static DevExIssueRequestModel Parse(IDictionary<string, object?> values)
4040

4141
public enum RequestType
4242
{
43-
CREATE_REPOSITORY = 1,
44-
CREATE_TEAM = 2
43+
CREATE_REPOSITORY = 10,
44+
UPDATE_REPOSITORY = 11,
45+
CREATE_TEAM = 20,
46+
UPDATE_TEAM = 21
4547
}
4648
}

src/DevExcelerateApi/Models/CreateRepositoryRequestModel.cs renamed to src/DevExcelerateApi/Models/RepositoryRequestModel.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace DevExcelerateApi.Models
66
{
7-
public class CreateRepositoryRequestModel : DevExIssueRequestModel
7+
public class RepositoryRequestModel : DevExIssueRequestModel
88
{
99
[YamlMember(Alias = "owner", Order = 10)]
1010
public string? RepositoryOwner { get; set; } // will be inherited from issue
@@ -48,7 +48,7 @@ public class CreateRepositoryRequestModel : DevExIssueRequestModel
4848
[YamlMember(Alias = "rulesets", Order = 19)]
4949
public List<string>? RulesetList { get; set; }
5050

51-
public static new CreateRepositoryRequestModel Parse(IDictionary<string, object?> values)
51+
public static new RepositoryRequestModel Parse(IDictionary<string, object?> values)
5252
{
5353
ArgumentNullException.ThrowIfNull(values);
5454

@@ -62,7 +62,7 @@ public class CreateRepositoryRequestModel : DevExIssueRequestModel
6262
values.TryGetValue("repository_contributors", out var repositoryContributors);
6363
values.TryGetValue("repository_rulesets", out var repositoryRulesets);
6464

65-
return new CreateRepositoryRequestModel
65+
return new RepositoryRequestModel
6666
{
6767
RequestType = DevExIssueRequestModel.Parse(values).RequestType,
6868
RepositoryOwner = repositoryOwner?.ToString(),
@@ -96,10 +96,10 @@ public string SerializeToYaml()
9696
return YamlHelpers.Serialize(this);
9797
}
9898

99-
public static CreateRepositoryRequestModel DeserializeFromYaml(string yamlContent)
99+
public static RepositoryRequestModel DeserializeFromYaml(string yamlContent)
100100
{
101101
ArgumentNullException.ThrowIfNull(yamlContent);
102-
return YamlHelpers.Deserialize<CreateRepositoryRequestModel>(yamlContent);
102+
return YamlHelpers.Deserialize<RepositoryRequestModel>(yamlContent);
103103
}
104104
}
105105
}

0 commit comments

Comments
 (0)