Skip to content

Commit

Permalink
Add functions for finding build
Browse files Browse the repository at this point in the history
  • Loading branch information
FriggaHel committed Mar 18, 2024
1 parent 49c86c1 commit c35d772
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 8 deletions.
9 changes: 5 additions & 4 deletions visual-dotnet/SauceLabs.Visual/GraphQL/Build.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using SauceLabs.Visual.Models;

namespace SauceLabs.Visual.GraphQL
{
Expand All @@ -10,15 +11,15 @@ internal class Build
public string Name { get; }
[JsonProperty("url")]
public string Url { get; }
[JsonProperty("status")]
public string Status { get; }
[JsonProperty("mode")]
public BuildMode Mode { get; }

public Build(string id, string name, string url, string status)
public Build(string id, string name, string url, BuildMode mode)
{
Id = id;
Name = name;
Url = url;
Status = status;
Mode = mode;
}
}
}
18 changes: 18 additions & 0 deletions visual-dotnet/SauceLabs.Visual/GraphQL/BuildByCustomIdQuery.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace SauceLabs.Visual.GraphQL
{
internal static class BuildByCustomIdQuery
{
public const string OperationName = "buildByCustomId";

public const string OperationDocument = @"
query buildByCustomId($input: UUID!) {
result: buildByCustomId(customId: $input) {
id,
url,
name,
mode
}
}
";
}
}
2 changes: 1 addition & 1 deletion visual-dotnet/SauceLabs.Visual/GraphQL/BuildQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ query build($input: UUID!) {
id,
url,
name,
status
mode
}
}
";
Expand Down
8 changes: 8 additions & 0 deletions visual-dotnet/SauceLabs.Visual/Models/BuildMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace SauceLabs.Visual.Models
{
public enum BuildMode
{
Running,
Completed
}
}
9 changes: 9 additions & 0 deletions visual-dotnet/SauceLabs.Visual/VisualApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ public async Task<GraphQLResponse<ServerResponse<Build>>> Build(string buildId)
return await _graphQlClient.SendQueryAsync<ServerResponse<Build>>(request);
}

public async Task<GraphQLResponse<ServerResponse<Build>>> BuildByCustomId(string customId)
{
var request = CreateAuthenticatedRequest(BuildByCustomIdQuery.OperationDocument, BuildByCustomIdQuery.OperationName, new
{
input = customId
});
return await _graphQlClient.SendQueryAsync<ServerResponse<Build>>(request);
}

public async Task<GraphQLResponse<ServerResponse<WebDriverSessionInfo>>> WebDriverSessionInfo(string jobId, string sessionId)
{
var request = CreateAuthenticatedRequest(WebDriverSessionInfoQuery.OperationDocument, WebDriverSessionInfoQuery.OperationName, new { jobId, sessionId });
Expand Down
83 changes: 80 additions & 3 deletions visual-dotnet/SauceLabs.Visual/VisualClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GraphQL;
using GraphQL.Client.Http;
using OpenQA.Selenium;
using Polly;
using Polly.Retry;
Expand Down Expand Up @@ -78,9 +80,20 @@ public VisualClient(WebDriver wd, Region region, string username, string accessK
var metadata = response.EnsureValidResponse();
_sessionMetadataBlob = metadata.Result.Blob;

var createBuildResponse = CreateBuild(buildOptions).Result;
Build = new VisualBuild(createBuildResponse.Id, createBuildResponse.Url);
_externalBuild = false;


var build = GetEffectiveBuild(Environment.GetEnvironmentVariable("SAUCE_VISUAL_BUILD_ID"), Environment.GetEnvironmentVariable("SAUCE_VISUAL_CUSTOM_ID")).Result;
if (build != null)
{
Build = build;
_externalBuild = true;
}
else
{
var createBuildResponse = CreateBuild(buildOptions).Result;
Build = new VisualBuild(createBuildResponse.Id, createBuildResponse.Url);
_externalBuild = false;
}

_retryPipeline = new ResiliencePipelineBuilder()
.AddRetry(new RetryStrategyOptions()
Expand All @@ -93,6 +106,70 @@ public VisualClient(WebDriver wd, Region region, string username, string accessK
.Build();
}

/// <summary>
/// <c>FindBuildById</c> returns the build identified by <c>buildId</c>
/// </summary>
/// <param name="buildId"></param>
/// <returns>the matching build</returns>
/// <exception cref="VisualClientException">when build is not existing or has an invalid state</exception>
private async Task<VisualBuild> FindBuildById(string buildId)
{
var response = await _api.Build(buildId);
if (response.Data?.Result == null)
{
throw new VisualClientException($@"build {buildId} was not found");
}

var build = response.Data.Result;
if (build.Mode != BuildMode.Running)
{
throw new VisualClientException($"build {build.Id} is not RUNNING");
}
return new VisualBuild(build.Id, build.Url);
}

/// <summary>
/// <c>TryFindBuildByCustomId</c> returns the build identified by <c>customId</c> or null if not found
/// </summary>
/// <param name="customId"></param>
/// <returns>the matching build or null</returns>
/// <exception cref="VisualClientException">when build has an invalid state</exception>
private async Task<VisualBuild?> TryFindBuildByCustomId(string customId)
{
var response = await _api.BuildByCustomId(customId);
if (response.Data?.Result == null)
{
return null;
}

var build = response.Data.Result;
if (build.Mode != BuildMode.Running)
{
throw new VisualClientException($"build {build.Id} is not RUNNING");
}
return new VisualBuild(build.Id, build.Url);
}

/// <summary>
/// <c>GetEffectiveBuild</c> tries to find the build matching the criterion provided by the user.
/// </summary>
/// <param name="buildId"></param>
/// <param name="customId"></param>
/// <returns></returns>
private async Task<VisualBuild?> GetEffectiveBuild(string? buildId, string? customId)
{
if (!string.IsNullOrEmpty(buildId))
{
return await FindBuildById(buildId);

Check warning on line 163 in visual-dotnet/SauceLabs.Visual/VisualClient.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'buildId' in 'Task<VisualBuild> VisualClient.FindBuildById(string buildId)'.

Check warning on line 163 in visual-dotnet/SauceLabs.Visual/VisualClient.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'buildId' in 'Task<VisualBuild> VisualClient.FindBuildById(string buildId)'.

Check warning on line 163 in visual-dotnet/SauceLabs.Visual/VisualClient.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'buildId' in 'Task<VisualBuild> VisualClient.FindBuildById(string buildId)'.

Check warning on line 163 in visual-dotnet/SauceLabs.Visual/VisualClient.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'buildId' in 'Task<VisualBuild> VisualClient.FindBuildById(string buildId)'.
}

if (!string.IsNullOrEmpty(customId))
{
return await TryFindBuildByCustomId(customId);

Check warning on line 168 in visual-dotnet/SauceLabs.Visual/VisualClient.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'customId' in 'Task<VisualBuild?> VisualClient.TryFindBuildByCustomId(string customId)'.

Check warning on line 168 in visual-dotnet/SauceLabs.Visual/VisualClient.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'customId' in 'Task<VisualBuild?> VisualClient.TryFindBuildByCustomId(string customId)'.

Check warning on line 168 in visual-dotnet/SauceLabs.Visual/VisualClient.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'customId' in 'Task<VisualBuild?> VisualClient.TryFindBuildByCustomId(string customId)'.

Check warning on line 168 in visual-dotnet/SauceLabs.Visual/VisualClient.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'customId' in 'Task<VisualBuild?> VisualClient.TryFindBuildByCustomId(string customId)'.
}
return null;
}

/// <summary>
/// <c>CreateBuild</c> creates a new Visual build.
/// </summary>
Expand Down

0 comments on commit c35d772

Please sign in to comment.