Skip to content

Commit

Permalink
Add BuildMode
Browse files Browse the repository at this point in the history
  • Loading branch information
FriggaHel committed Mar 19, 2024
1 parent 88aca7f commit 25d8635
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
6 changes: 5 additions & 1 deletion visual-dotnet/SauceLabs.Visual/GraphQL/CreateBuild.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,6 +11,8 @@ internal class CreateBuild
public string Name { get; }
[JsonProperty("url")]
public string Url { get; }
[JsonProperty("mode")]
public BuildMode Mode { get; }
[JsonProperty("project")]
public string? Project { get; }
[JsonProperty("branch")]
Expand All @@ -19,11 +22,12 @@ internal class CreateBuild
[JsonProperty("defaultBranch")]
public string? DefaultBranch { get; }

public CreateBuild(string id, string name, string url, string? project, string? branch, string? customId, string? defaultBranch)
public CreateBuild(string id, string name, string url, BuildMode mode, string? project, string? branch, string? customId, string? defaultBranch)
{
Id = id;
Name = name;
Url = url;
Mode = mode;
Project = project;
Branch = branch;
CustomId = customId;
Expand Down
9 changes: 8 additions & 1 deletion visual-dotnet/SauceLabs.Visual/VisualBuild.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using SauceLabs.Visual.Models;

namespace SauceLabs.Visual
{
/// <summary>
Expand All @@ -8,10 +10,15 @@ public class VisualBuild
public string Id { get; internal set; }
public string Url { get; internal set; }

internal VisualBuild(string id, string url)
public BuildMode Mode { get; internal set; }

internal VisualBuild(string id, string url, BuildMode mode)
{
Id = id;
Url = url;
Mode = mode;
}

internal bool IsRunning() => Mode == BuildMode.Running;
}
}
34 changes: 16 additions & 18 deletions visual-dotnet/SauceLabs.Visual/VisualClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,18 @@ public VisualClient(WebDriver wd, Region region, string username, string accessK
var build = GetEffectiveBuild(EnvVars.BuildId, EnvVars.CustomId).Result;
if (build != null)
{
if (!build.IsRunning())
{
throw new VisualClientException($"build {build.Id} is not RUNNING");
}
Build = build;
_externalBuild = true;
}
else
{
buildOptions.CustomId ??= EnvVars.CustomId;
var createBuildResponse = CreateBuild(buildOptions).Result;
Build = new VisualBuild(createBuildResponse.Id, createBuildResponse.Url);
Build = new VisualBuild(createBuildResponse.Id, createBuildResponse.Url, createBuildResponse.Mode);
_externalBuild = false;
}

Expand All @@ -111,18 +115,15 @@ public VisualClient(WebDriver wd, Region region, string username, string accessK
/// <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)
try
{
throw new VisualClientException($@"build {buildId} was not found");
var build = (await _api.Build(buildId)).EnsureValidResponse().Result;
return new VisualBuild(build.Id, build.Url, build.Mode);
}

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

/// <summary>
Expand All @@ -133,18 +134,15 @@ private async Task<VisualBuild> FindBuildById(string buildId)
/// <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)
try
{
return null;
var build = (await _api.BuildByCustomId(customId)).EnsureValidResponse().Result;
return new VisualBuild(build.Id, build.Url, build.Mode);
}

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

/// <summary>
Expand Down Expand Up @@ -182,7 +180,7 @@ private async Task<VisualBuild> CreateBuild(CreateBuildOptions? options = null)
CustomId = options?.CustomId,
DefaultBranch = options?.DefaultBranch,
})).EnsureValidResponse();
return new VisualBuild(result.Result.Id, result.Result.Url);
return new VisualBuild(result.Result.Id, result.Result.Url, result.Result.Mode);
}

/// <summary>
Expand Down

0 comments on commit 25d8635

Please sign in to comment.