-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make improvements to retrieve-codeowners
tool: change accepted params (targetPath
etc.) + refactor
#5104
Make improvements to retrieve-codeowners
tool: change accepted params (targetPath
etc.) + refactor
#5104
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.Collections.Generic; | ||
using System.Text.Json; | ||
using Azure.Sdk.Tools.CodeOwnersParser; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.Sdk.Tools.RetrieveCodeOwners.Tests | ||
{ | ||
/// <summary> | ||
/// Test class for Azure.Sdk.Tools.RetrieveCodeOwners.Program. | ||
/// </summary> | ||
[TestFixture] | ||
public class ProgramTests | ||
{ | ||
private const string CodeownersFilePath = "CODEOWNERS"; | ||
|
||
private static readonly object[] sourceLists = | ||
{ | ||
new object[] {"sdk", false, new List<string> { "person1", "person2" } }, | ||
new object[] { "/sdk", false, new List<string> { "person1", "person2" } }, | ||
new object[] { "sdk/noPath", false, new List<string> { "person1", "person2" } }, | ||
new object[] { "/sdk/azconfig", false, new List<string> { "person3", "person4" } }, | ||
new object[] { "/sdk/azconfig/package", false, new List<string> { "person3", "person4" } }, | ||
new object[] { "/sdk/testUser/", true, new List<string> { "azure-sdk" } }, | ||
new object[] { "/sd", true, new List<string>() } | ||
}; | ||
|
||
[TestCaseSource(nameof(sourceLists))] | ||
public void TestOnNormalOutput(string targetPath, bool excludeNonUserAliases, List<string> expectedOwners) | ||
{ | ||
using var consoleOutput = new ConsoleOutput(); | ||
|
||
// Act | ||
Program.Main(targetPath, CodeownersFilePath, excludeNonUserAliases); | ||
|
||
string actualOutput = consoleOutput.GetOutput(); | ||
AssertOwners(actualOutput, expectedOwners); | ||
} | ||
|
||
[TestCase("PathNotExist")] | ||
[TestCase("http://testLink")] | ||
[TestCase("https://testLink")] | ||
public void TestOnError(string codeownersPath) | ||
{ | ||
Assert.That(Program.Main("sdk", codeownersPath), Is.EqualTo(1)); | ||
} | ||
|
||
private static void AssertOwners(string actualOutput, List<string> expectedOwners) | ||
{ | ||
CodeownersEntry? actualEntry = JsonSerializer.Deserialize<CodeownersEntry>(actualOutput); | ||
List<string> actualOwners = actualEntry!.Owners; | ||
Assert.That(actualOwners, Has.Count.EqualTo(expectedOwners.Count)); | ||
for (int i = 0; i < actualOwners.Count; i++) | ||
{ | ||
Assert.That(actualOwners[i], Is.EqualTo(expectedOwners[i])); | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,36 +5,47 @@ | |
namespace Azure.Sdk.Tools.RetrieveCodeOwners | ||
{ | ||
/// <summary> | ||
/// The tool command to retrieve code owners. | ||
/// See Program.Main comment. | ||
/// </summary> | ||
public static class Program | ||
{ | ||
/// <summary> | ||
/// Retrieves CODEOWNERS information for specific section of the repo | ||
/// Given targetPath and CODEOWNERS file path or https url codeownersFilePathOrUrl, | ||
/// prints out to stdout owners of the targetPath as determined by the CODEOWNERS data. | ||
/// </summary> | ||
/// <param name="codeOwnerFilePath">The path of CODEOWNERS file in repo</param> | ||
/// <param name="targetDirectory">The directory whose information is to be retrieved</param> | ||
/// <param name="filterOutNonUserAliases">The option to filter out code owner team alias.</param> | ||
/// <returns>Exit code</returns> | ||
|
||
/// <param name="targetPath">The path whose owners are to be determined.</param> | ||
/// <param name="codeownersFilePathOrUrl">The https url or path to the CODEOWNERS file.</param> | ||
/// <param name="excludeNonUserAliases">Whether owners that aren't users should be excluded from the | ||
/// returned owners.</param> | ||
/// <returns> | ||
/// On STDOUT: The JSON representation of the matched CodeownersEntry. | ||
/// "new CodeownersEntry()" if no path in the CODEOWNERS data matches. | ||
/// <br/><br/> | ||
/// From the Main method: exit code. 0 if successful, 1 if error. | ||
/// </returns> | ||
public static int Main( | ||
string codeOwnerFilePath, | ||
string targetDirectory, | ||
bool filterOutNonUserAliases = false | ||
) | ||
string targetPath, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just keep in mind that you will need to update places like https://github.com/Azure/azure-sdk-tools/blob/main/eng/common/scripts/get-codeowners.ps1 when you update to the latest package version. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @weshaggard yeah I already have a PR for that (in draft state as of now). Thanks for the reminder! :) |
||
string codeownersFilePathOrUrl, | ||
bool excludeNonUserAliases = false) | ||
{ | ||
var target = targetDirectory.Trim(); | ||
try { | ||
var codeOwnerEntry = CodeOwnersFile.ParseAndFindOwnersForClosestMatch(codeOwnerFilePath, target); | ||
if (filterOutNonUserAliases) | ||
targetPath = targetPath.Trim(); | ||
try | ||
{ | ||
var codeownersEntry = CodeownersFile.GetMatchingCodeownersEntry(targetPath, codeownersFilePathOrUrl); | ||
if (excludeNonUserAliases) | ||
{ | ||
codeOwnerEntry.FilterOutNonUserAliases(); | ||
codeownersEntry.ExcludeNonUserAliases(); | ||
} | ||
var codeOwnerJson = JsonSerializer.Serialize<CodeOwnerEntry>(codeOwnerEntry, new JsonSerializerOptions { WriteIndented = true }); | ||
Console.WriteLine(codeOwnerJson); | ||
|
||
var codeownersJson = JsonSerializer.Serialize<CodeownersEntry>( | ||
codeownersEntry, | ||
new JsonSerializerOptions { WriteIndented = true }); | ||
|
||
Console.WriteLine(codeownersJson); | ||
return 0; | ||
} | ||
catch (Exception e) { | ||
catch (Exception e) | ||
{ | ||
Console.Error.WriteLine(e.Message); | ||
return 1; | ||
} | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI this file is renamed from
MainTests
.