-
Notifications
You must be signed in to change notification settings - Fork 183
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CodeOwnersParser Team/User lookup (#6366)
* Codeowners Team/User lookup * Add a TeamUserHolder and pass that to Owners parsing. Remove the manual tester project * Add command line option to RetrieveCodeOwners to output json to a file * Add team/storage URI to the deserialize error message in the TeamUserHolder
- Loading branch information
1 parent
2964ccf
commit 5ad8b9e
Showing
5 changed files
with
177 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
tools/code-owners-parser/CodeOwnersParser/DefaultStorageConstants.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Azure.Sdk.Tools.CodeOwnersParser | ||
{ | ||
public class DefaultStorageConstants | ||
{ | ||
public const string DefaultStorageURI = "https://azuresdkartifacts.blob.core.windows.net/azure-sdk-write-teams/azure-sdk-write-teams-blob"; | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
tools/code-owners-parser/CodeOwnersParser/TeamUserHolder.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Threading.Tasks; | ||
|
||
namespace Azure.Sdk.Tools.CodeOwnersParser | ||
{ | ||
public class TeamUserHolder | ||
{ | ||
private string TeamUserStorageURI { get; set; } = DefaultStorageConstants.DefaultStorageURI; | ||
private Dictionary<string, List<string>>? _teamUserDict = null; | ||
|
||
public Dictionary<string, List<string>> TeamUserDict | ||
{ | ||
get | ||
{ | ||
if (_teamUserDict == null) | ||
{ | ||
_teamUserDict = GetTeamUserData(); | ||
} | ||
return _teamUserDict; | ||
} | ||
set | ||
{ | ||
_teamUserDict = value; | ||
} | ||
} | ||
|
||
public TeamUserHolder(string? teamUserStorageURI) | ||
{ | ||
if (!string.IsNullOrWhiteSpace(teamUserStorageURI)) | ||
{ | ||
TeamUserStorageURI = teamUserStorageURI; | ||
} | ||
} | ||
|
||
private Dictionary<string, List<string>> GetTeamUserData() | ||
{ | ||
if (null == _teamUserDict) | ||
{ | ||
string rawJson = FileHelpers.GetFileOrUrlContents(TeamUserStorageURI); | ||
var list = JsonSerializer.Deserialize<List<KeyValuePair<string, List<string>>>>(rawJson); | ||
if (null != list) | ||
{ | ||
return list.ToDictionary((keyItem) => keyItem.Key, (valueItem) => valueItem.Value); | ||
} | ||
Console.WriteLine($"Error! Unable to deserialize json team/user data from {TeamUserStorageURI}. rawJson={rawJson}"); | ||
return new Dictionary<string, List<string>>(); | ||
} | ||
return _teamUserDict; | ||
} | ||
|
||
public List<string> GetUsersForTeam(string teamName) | ||
{ | ||
// The teamName in the codeowners file should be in the form <org>/<team>. | ||
// The dictionary's team names do not contain the org so the org needs to | ||
// be stripped off. Handle the case where the teamName passed in does and | ||
// does not being with @org/ | ||
string teamWithoutOrg = teamName.Trim(); | ||
if (teamWithoutOrg.Contains('/')) | ||
{ | ||
teamWithoutOrg = teamWithoutOrg.Split("/")[1]; | ||
} | ||
if (TeamUserDict != null) | ||
{ | ||
if (TeamUserDict.ContainsKey(teamWithoutOrg)) | ||
{ | ||
Console.WriteLine($"Found team entry for {teamWithoutOrg}"); | ||
return TeamUserDict[teamWithoutOrg]; | ||
} | ||
Console.WriteLine($"Warning: TeamUserDictionary did not contain a team entry for {teamWithoutOrg}"); | ||
} | ||
return new List<string>(); | ||
} | ||
} | ||
} |