1212using System . Threading . Tasks ;
1313using Azure . Sdk . Tools . NotificationConfiguration . Helpers ;
1414using System ;
15- using Azure . Sdk . Tools . CodeOwnersParser ;
16- using System . Text . RegularExpressions ;
1715
1816namespace Azure . Sdk . Tools . NotificationConfiguration
1917{
@@ -24,11 +22,10 @@ class NotificationConfigurator
2422 private readonly ILogger < NotificationConfigurator > logger ;
2523
2624 private const int MaxTeamNameLength = 64 ;
27- // Type 2 maps to a pipeline YAML file in the repository
28- private const int PipelineYamlProcessType = 2 ;
25+
2926 // A cache on the code owners github identity to owner descriptor.
30- private readonly Dictionary < string , string > codeOwnerCache = new Dictionary < string , string > ( ) ;
31- // A cache on the team member to member discriptor .
27+ private readonly Dictionary < string , string > contactsCache = new Dictionary < string , string > ( ) ;
28+ // A cache on the team member to member descriptor .
3229 private readonly Dictionary < string , string > teamMemberCache = new Dictionary < string , string > ( ) ;
3330
3431 public NotificationConfigurator ( AzureDevOpsService service , GitHubService gitHubService , ILogger < NotificationConfigurator > logger )
@@ -171,72 +168,51 @@ private async Task<WebApiTeam> EnsureTeamExists(
171168
172169 if ( purpose == TeamPurpose . SynchronizedNotificationTeam )
173170 {
174- await SyncTeamWithCodeOwnerFile ( pipeline , result , gitHubToAADConverter , gitHubService , persistChanges ) ;
171+ await SyncTeamWithCodeownersFile ( pipeline , result , gitHubToAADConverter , persistChanges ) ;
175172 }
176173 return result ;
177174 }
178175
179- private async Task SyncTeamWithCodeOwnerFile ( BuildDefinition pipeline , WebApiTeam team , GitHubToAADConverter gitHubToAADConverter , GitHubService gitHubService , bool persistChanges )
176+ private async Task SyncTeamWithCodeownersFile (
177+ BuildDefinition buildDefinition ,
178+ WebApiTeam team ,
179+ GitHubToAADConverter gitHubToAADConverter ,
180+ bool persistChanges )
180181 {
181182 using ( logger . BeginScope ( "Team Name = {0}" , team . Name ) )
182183 {
183- if ( pipeline . Process . Type != PipelineYamlProcessType )
184- {
185- return ;
186- }
187-
188- // Get contents of CODEOWNERS
189- Uri repoUrl = pipeline . Repository . Url ;
190- logger . LogInformation ( "Fetching CODEOWNERS file from repo url '{repoUrl}'" , repoUrl ) ;
191-
192- if ( repoUrl != null )
193- {
194- repoUrl = new Uri ( Regex . Replace ( repoUrl . ToString ( ) , @"\.git$" , String . Empty ) ) ;
195- }
196- else
197- {
198- logger . LogError ( "No repository url returned from pipeline. Repo id: {0}" , pipeline . Repository . Id ) ;
199- return ;
200- }
201- var codeOwnerEntries = await gitHubService . GetCodeownersFile ( repoUrl ) ;
202-
203- if ( codeOwnerEntries == default )
184+ List < string > contacts =
185+ await new Contacts ( gitHubService , logger ) . GetFromBuildDefinitionRepoCodeowners ( buildDefinition ) ;
186+ if ( contacts == null )
204187 {
205- logger . LogInformation ( "CODEOWNERS file not found, skipping sync" ) ;
188+ // assert: the reason for why contacts is null has been already logged.
206189 return ;
207190 }
208- var process = pipeline . Process as YamlProcess ;
209-
210- logger . LogInformation ( "Searching CODEOWNERS for matching path for {0}" , process . YamlFilename ) ;
211-
212- var codeOwnerEntry = CodeownersFile . GetMatchingCodeownersEntry ( process . YamlFilename , codeOwnerEntries ) ;
213- codeOwnerEntry . ExcludeNonUserAliases ( ) ;
214-
215- logger . LogInformation ( "Matching Contacts Path = {0}, NumContacts = {1}" , process . YamlFilename , codeOwnerEntry . Owners . Count ) ;
216191
217192 // Get set of team members in the CODEOWNERS file
218- var codeownersDescriptors = new List < String > ( ) ;
219- foreach ( var contact in codeOwnerEntry . Owners )
193+ var contactsDescriptors = new List < string > ( ) ;
194+ foreach ( string contact in contacts )
220195 {
221- if ( ! codeOwnerCache . ContainsKey ( contact ) )
196+ if ( ! contactsCache . ContainsKey ( contact ) )
222197 {
223198 // TODO: Better to have retry if no success on this call.
224199 var userPrincipal = gitHubToAADConverter . GetUserPrincipalNameFromGithub ( contact ) ;
225200 if ( ! string . IsNullOrEmpty ( userPrincipal ) )
226201 {
227- codeOwnerCache [ contact ] = await service . GetDescriptorForPrincipal ( userPrincipal ) ;
202+ contactsCache [ contact ] = await service . GetDescriptorForPrincipal ( userPrincipal ) ;
228203 }
229204 else
230205 {
231- logger . LogInformation ( "Cannot find the user principal for github {0}" , contact ) ;
232- codeOwnerCache [ contact ] = null ;
206+ logger . LogInformation (
207+ "Cannot find the user principal for GitHub contact '{contact}'" ,
208+ contact ) ;
209+ contactsCache [ contact ] = null ;
233210 }
234211 }
235- codeownersDescriptors . Add ( codeOwnerCache [ contact ] ) ;
212+ contactsDescriptors . Add ( contactsCache [ contact ] ) ;
236213 }
237214
238-
239- var codeownersSet = new HashSet < string > ( codeownersDescriptors ) ;
215+ var contactsSet = new HashSet < string > ( contactsDescriptors ) ;
240216 // Get set of team members in the DevOps teams
241217 var teamMembers = await service . GetMembersAsync ( team ) ;
242218 var teamDescriptors = new List < String > ( ) ;
@@ -250,24 +226,24 @@ private async Task SyncTeamWithCodeOwnerFile(BuildDefinition pipeline, WebApiTea
250226 teamDescriptors . Add ( teamMemberCache [ member . Identity . Id ] ) ;
251227 }
252228 var teamSet = new HashSet < string > ( teamDescriptors ) ;
253- var contactsToRemove = teamSet . Except ( codeownersSet ) ;
254- var contactsToAdd = codeownersSet . Except ( teamSet ) ;
229+ var contactsToRemove = teamSet . Except ( contactsSet ) ;
230+ var contactsToAdd = contactsSet . Except ( teamSet ) ;
255231
256- foreach ( var descriptor in contactsToRemove )
232+ foreach ( string descriptor in contactsToRemove )
257233 {
258234 if ( persistChanges && descriptor != null )
259235 {
260- var teamDescriptor = await service . GetDescriptorAsync ( team . Id ) ;
236+ string teamDescriptor = await service . GetDescriptorAsync ( team . Id ) ;
261237 logger . LogInformation ( "Delete Contact TeamDescriptor = {0}, ContactDescriptor = {1}" , teamDescriptor , descriptor ) ;
262238 await service . RemoveMember ( teamDescriptor , descriptor ) ;
263239 }
264240 }
265241
266- foreach ( var descriptor in contactsToAdd )
242+ foreach ( string descriptor in contactsToAdd )
267243 {
268244 if ( persistChanges && descriptor != null )
269245 {
270- var teamDescriptor = await service . GetDescriptorAsync ( team . Id ) ;
246+ string teamDescriptor = await service . GetDescriptorAsync ( team . Id ) ;
271247 logger . LogInformation ( "Add Contact TeamDescriptor = {0}, ContactDescriptor = {1}" , teamDescriptor , descriptor ) ;
272248 await service . AddToTeamAsync ( teamDescriptor , descriptor ) ;
273249 }
0 commit comments