Skip to content

Commit

Permalink
Add command option for output the codeowners directly. (#2245)
Browse files Browse the repository at this point in the history
  • Loading branch information
sima-zhu authored Nov 19, 2021
1 parent dfa21b3 commit 7724333
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 25 deletions.
8 changes: 6 additions & 2 deletions eng/pipelines/templates/stages/archetype-sdk-tool-dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ parameters:
- name: NoWarn
type: boolean
default: false

- name: TestPostSteps
type: object
default: []
variables:
- template: ../variables/globals.yml
- name: Warn
Expand Down Expand Up @@ -73,7 +75,9 @@ stages:
DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1
DOTNET_CLI_TELEMETRY_OPTOUT: 1
DOTNET_MULTILEVEL_LOOKUP: 0


- ${{ parameters.TestPostSteps }}

- task: PublishTestResults@2
condition: succeededOrFailed()
inputs:
Expand Down
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Instructions for CODEOWNERS file format and automatic build failure notifications:
# https://github.com/Azure/azure-sdk/blob/main/docs/policies/opensource.md#codeowners

###########
# SDK
###########

# Catch-all for SDK changes
/sdk/ @person1 @person2

# Service teams
/sdk/azconfig/ @person3 @person4

# Example for a service that needs issues to be labeled
# ServiceLabel: %KeyVault %Service Attention
/sdk/keyvault/ @person5 @person6

# Example for a service that needs PRs to be labeled
# PRLabel: %label
/sdk/servicebus/ @person7 @person8

# Example for a service that needs both issues and PRs to be labeled
# ServiceLabel: %label
# PRLabel: %label
/sdk/eventhubs/ @person7 @person8

# Example for service that does not have the code in the repo but wants issues to be labeled
# Notice the use of the moniker /<NotInRepo>/
# ServiceLabel: %label %Service Attention
/<NotInRepo>/ @person7 @person8
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using System.IO;
using System.Text.Json;
using Azure.Sdk.Tools.CodeOwnersParser;

namespace Azure.Sdk.Tools.RetrieveCodeOwners
Expand All @@ -9,27 +9,33 @@ class Program
/// <summary>
/// Retrieves codeowners information for specific section of the repo
/// </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="rootDirectory">The root of the repo or $(Build.SourcesDirectory) on DevOps</param>
/// <param name="vsoOwningUsers">Variable for setting user aliases</param>
/// <returns></returns>
/// <returns>Exit code</returns>

public static void Main(
string targetDirectory,
string rootDirectory,
string vsoOwningUsers
public static int Main(
string codeOwnerFilePath,
string targetDirectory
)
{
var target = targetDirectory.ToLower().Trim();
var codeOwnersLocation = Path.Join(rootDirectory, ".github", "CODEOWNERS");
var owners = CodeOwnersFile.ParseAndFindOwnersForClosestMatch(codeOwnersLocation, target);
if (owners == null)
{
Console.WriteLine(String.Format("We cannot find any closest code owners from the target path {0}", targetDirectory));
try {
var codeOwnerEntry = CodeOwnersFile.ParseAndFindOwnersForClosestMatch(codeOwnerFilePath, target);
if (codeOwnerEntry == null)
{
Console.Error.WriteLine(String.Format("We cannot find any matching code owners from the target path {0}", targetDirectory));
return 1;
}
else
{
var codeOwnerJson = JsonSerializer.Serialize<CodeOwnerEntry>(codeOwnerEntry, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine(codeOwnerJson);
return 0;
}
}
else
{
Console.WriteLine(String.Format("##vso[task.setvariable variable={0};]{1}", vsoOwningUsers, String.Join(",", owners)));
catch (Exception e) {
Console.Error.WriteLine(e.Message);
return 1;
}
}
}
Expand Down
8 changes: 2 additions & 6 deletions tools/code-owners-parser/CodeOwnersParser/CodeOwnersFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ public static class CodeOwnersFile
public static List<CodeOwnerEntry> ParseFile(string filePathOrUrl)
{
string content;
Colorizer.Write("Retrieving file content from [Yellow!{0}]... ", filePathOrUrl);
content = FileHelpers.GetFileContents(filePathOrUrl);
Colorizer.WriteLine("[Green!Done]");

return ParseContent(content);
}

public static List<CodeOwnerEntry> ParseContent(string fileContent)
{
Colorizer.Write("Parsing CODEOWNERS table... ");
List<CodeOwnerEntry> entries = new List<CodeOwnerEntry>();
string line;

Expand Down Expand Up @@ -65,11 +62,10 @@ public static List<CodeOwnerEntry> ParseContent(string fileContent)

}
}
Colorizer.WriteLine("[Green!Done]");
return entries;
}

public static List<string> ParseAndFindOwnersForClosestMatch(string codeOwnersFilePathOrUrl, string targetPath)
public static CodeOwnerEntry ParseAndFindOwnersForClosestMatch(string codeOwnersFilePathOrUrl, string targetPath)
{
var codeOwnerEntries = ParseFile(codeOwnersFilePathOrUrl);
// Normalize the start and end of the paths by trimming slash
Expand All @@ -84,7 +80,7 @@ public static List<string> ParseAndFindOwnersForClosestMatch(string codeOwnersFi
// for our current scenarios but in the future might need to support globs
if (targetPath.StartsWith(codeOwnerPath, StringComparison.OrdinalIgnoreCase))
{
return codeOwnerEntries[i].Owners;
return codeOwnerEntries[i];
}
}

Expand Down
2 changes: 1 addition & 1 deletion tools/code-owners-parser/CodeOwnersParser/FileHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static string GetFileContents(string fileOrUri)
Uri uri = new Uri(fileOrUri, UriKind.Absolute);
if (uri.Scheme.ToLowerInvariant() != "https")
{
throw new ArgumentException("Cannot download off non-https uris");
throw new ArgumentException(string.Format("Cannot download off non-https uris, path: {0}", fileOrUri));
}

// try to download it.
Expand Down
7 changes: 7 additions & 0 deletions tools/code-owners-parser/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,10 @@ extends:
parameters:
ToolDirectory: tools/code-owners-parser
DotNetCoreVersion: 5.0.301
TestPostSteps:
# This is a simple test for placeholder. Will replace the test with get-codeowners.ps1 in future.
- pwsh: |
$output = dotnet run --project "tools\code-owners-parser\Azure.Sdk.Tools.RetrieveCodeOwners\Azure.Sdk.Tools.RetrieveCodeOwners.csproj" `
--code-owner-file-path "$(Build.SourcesDirectory)/tools/code-owners-parser/Azure.Sdk.Tools.RetrieveCodeOwners.Tests/CODEOWNERS" --target-directory "sdk"
$output | ConvertFrom-Json
displayName: Test on code owner tool output

0 comments on commit 7724333

Please sign in to comment.