Skip to content

Commit

Permalink
Record pipelineReference in spawned TestRuns (#7209)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChadNedzlek authored Apr 9, 2021
1 parent 814754c commit e7ede87
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Microsoft.DotNet.Helix/Sdk/StartAzurePipelinesTestRun.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ public class StartAzurePipelinesTestRun : AzureDevOpsTask
[Required]
public string TestRunName { get; set; }

public string JobName { get; set; }
public int JobAttempt { get; set; }
public string StageName { get; set; }
public int StageAttempt { get; set; }
public string PhaseName { get; set; }
public int PhaseAttempt { get; set; }

[Output]
public int TestRunId { get; set; }

Expand All @@ -33,6 +40,7 @@ protected override Task ExecuteCoreAsync(HttpClient client)
["build"] = new JObject {["id"] = BuildId,},
["name"] = TestRunName,
["state"] = "InProgress",
["pipelineReference"] = BuildPipelineReference(),
}),
Encoding.UTF8,
"application/json"),
Expand All @@ -50,5 +58,41 @@ protected override Task ExecuteCoreAsync(HttpClient client)
}
});
}

private JObject BuildPipelineReference()
{
var obj = new JObject
{
{"jobReference", BuildReference("job", JobName, JobAttempt)},
{"phaseReference", BuildReference("phase", PhaseName, PhaseAttempt)},
{"stageReference", BuildReference("stage", StageName, StageAttempt)},
};

if (int.TryParse(BuildId, out var buildId))
{
obj["pipelineId"] = buildId;
}

return obj;
}

private JObject BuildReference(string part, string name, int attempt)
{
var reference = new JObject
{
[$"{part.ToLowerInvariant()}Name"] = name ?? GetEnvironmentVariable($"SYSTEM_{part.ToUpperInvariant()}NAME"),
};

if (attempt != 0)
{
reference["attempt"] = attempt;
}
else if (int.TryParse(GetEnvironmentVariable($"SYSTEM_{part.ToUpperInvariant()}ATTEMPT"), out int attemptFromEnv))
{
reference["attempt"] = attemptFromEnv;
}

return reference;
}
}
}

0 comments on commit e7ede87

Please sign in to comment.