From a0458aebfe8a0f78c861b0d4fa48e86548495c04 Mon Sep 17 00:00:00 2001 From: Konrad Pabjan Date: Mon, 14 Mar 2022 11:20:11 -0400 Subject: [PATCH] Save record order for annotation links when creating issues (#1744) * Save record order for annotation links when creating issues * PR feedback * Add tests for step and line numbers --- src/Runner.Worker/ExecutionContext.cs | 14 +++---- src/Test/L0/Worker/ExecutionContextL0.cs | 49 ++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 7 deletions(-) diff --git a/src/Runner.Worker/ExecutionContext.cs b/src/Runner.Worker/ExecutionContext.cs index 48366b310..ace20cc77 100644 --- a/src/Runner.Worker/ExecutionContext.cs +++ b/src/Runner.Worker/ExecutionContext.cs @@ -550,10 +550,15 @@ namespace GitHub.Runner.Worker issue.Message = issue.Message[.._maxIssueMessageLength]; } + // Tracking the line number (logFileLineNumber) and step number (stepNumber) for each issue that gets created + // Actions UI from the run summary page use both values to easily link to an exact locations in logs where annotations originate from + if (_record.Order != null) + { + issue.Data["stepNumber"] = _record.Order.ToString(); + } + if (issue.Type == IssueType.Error) { - // tracking line number for each issue in log file - // log UI use this to navigate from issue to log if (!string.IsNullOrEmpty(logMessage)) { long logLineNumber = Write(WellKnownTags.Error, logMessage); @@ -569,8 +574,6 @@ namespace GitHub.Runner.Worker } else if (issue.Type == IssueType.Warning) { - // tracking line number for each issue in log file - // log UI use this to navigate from issue to log if (!string.IsNullOrEmpty(logMessage)) { long logLineNumber = Write(WellKnownTags.Warning, logMessage); @@ -586,9 +589,6 @@ namespace GitHub.Runner.Worker } else if (issue.Type == IssueType.Notice) { - - // tracking line number for each issue in log file - // log UI use this to navigate from issue to log if (!string.IsNullOrEmpty(logMessage)) { long logLineNumber = Write(WellKnownTags.Notice, logMessage); diff --git a/src/Test/L0/Worker/ExecutionContextL0.cs b/src/Test/L0/Worker/ExecutionContextL0.cs index e6351edb0..aa50c71fa 100644 --- a/src/Test/L0/Worker/ExecutionContextL0.cs +++ b/src/Test/L0/Worker/ExecutionContextL0.cs @@ -144,6 +144,55 @@ namespace GitHub.Runner.Common.Tests.Worker } } + [Fact] + [Trait("Level", "L0")] + [Trait("Category", "Worker")] + public void AddIssue_AddStepAndLineNumberInformation() + { + using (TestHostContext hc = CreateTestContext()) + { + + TaskOrchestrationPlanReference plan = new TaskOrchestrationPlanReference(); + TimelineReference timeline = new TimelineReference(); + Guid jobId = Guid.NewGuid(); + string jobName = "some job name"; + var jobRequest = new Pipelines.AgentJobRequestMessage(plan, timeline, jobId, jobName, jobName, null, null, null, new Dictionary(), new List(), new Pipelines.JobResources(), new Pipelines.ContextData.DictionaryContextData(), new Pipelines.WorkspaceOptions(), new List(), null, null, null, null); + jobRequest.Resources.Repositories.Add(new Pipelines.RepositoryResource() + { + Alias = Pipelines.PipelineConstants.SelfAlias, + Id = "github", + Version = "sha1" + }); + jobRequest.ContextData["github"] = new Pipelines.ContextData.DictionaryContextData(); + + // Arrange: Setup the paging logger. + var pagingLogger = new Mock(); + var pagingLogger2 = new Mock(); + var jobServerQueue = new Mock(); + jobServerQueue.Setup(x => x.QueueTimelineRecordUpdate(It.IsAny(), It.IsAny())); + + hc.EnqueueInstance(pagingLogger.Object); + hc.EnqueueInstance(pagingLogger2.Object); + hc.SetSingleton(jobServerQueue.Object); + + var ec = new Runner.Worker.ExecutionContext(); + ec.Initialize(hc); + ec.InitializeJob(jobRequest, CancellationToken.None); + ec.Start(); + + var embeddedStep = ec.CreateChild(Guid.NewGuid(), "action_1_pre", "action_1_pre", null, null, ActionRunStage.Main, isEmbedded: true); + embeddedStep.Start(); + + embeddedStep.AddIssue(new Issue() { Type = IssueType.Error, Message = "error annotation that should have step and line number information" }); + embeddedStep.AddIssue(new Issue() { Type = IssueType.Warning, Message = "warning annotation that should have step and line number information" }); + embeddedStep.AddIssue(new Issue() { Type = IssueType.Notice, Message = "notice annotation that should have step and line number information" }); + + jobServerQueue.Verify(x => x.QueueTimelineRecordUpdate(It.IsAny(), It.Is(t => t.Issues.Where(i => i.Data.ContainsKey("stepNumber") && i.Data.ContainsKey("logFileLineNumber") && i.Type == IssueType.Error).Count() == 1)), Times.AtLeastOnce); + jobServerQueue.Verify(x => x.QueueTimelineRecordUpdate(It.IsAny(), It.Is(t => t.Issues.Where(i => i.Data.ContainsKey("stepNumber") && i.Data.ContainsKey("logFileLineNumber") && i.Type == IssueType.Warning).Count() == 1)), Times.AtLeastOnce); + jobServerQueue.Verify(x => x.QueueTimelineRecordUpdate(It.IsAny(), It.Is(t => t.Issues.Where(i => i.Data.ContainsKey("stepNumber") && i.Data.ContainsKey("logFileLineNumber") && i.Type == IssueType.Notice).Count() == 1)), Times.AtLeastOnce); + } + } + [Fact] [Trait("Level", "L0")] [Trait("Category", "Worker")]