Skip to content

Commit

Permalink
Write job definition file before creating ContainerJob (#806)
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell authored Sep 19, 2023
1 parent f27a97c commit c354692
Show file tree
Hide file tree
Showing 6 changed files with 448 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ public void Creation_WithParsing_Works(string projectUrl, string hostname, strin
{
var url = (AzureDevOpsProjectUrl)projectUrl;
Assert.Equal(hostname, url.Hostname);
Assert.Null(url.Port);
Assert.Equal(organizationName, url.OrganizationName);
Assert.Equal(organizationUrl, url.OrganizationUrl);
Assert.Equal(projectIdOrName, url.ProjectIdOrName);
Expand Down
417 changes: 260 additions & 157 deletions server/Tingle.Dependabot.Tests/Workflow/UpdateRunnerTests.cs

Large diffs are not rendered by default.

35 changes: 31 additions & 4 deletions server/Tingle.Dependabot/Models/DependabotConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,16 @@ public record DependabotUpdate
[JsonPropertyName("schedule")]
public DependabotUpdateSchedule? Schedule { get; set; }

[Required]
[JsonPropertyName("open-pull-requests-limit")]
public int? OpenPullRequestsLimit { get; set; } = 5;
public int OpenPullRequestsLimit { get; set; } = 5;

[JsonPropertyName("registries")]
public List<string>? Registries { get; set; }

[JsonPropertyName("allow")]
public List<DependabotAllowDependency>? Allow { get; set; }
[JsonPropertyName("ignore")]
public List<DependabotIgnoreDependency>? Ignore { get; set; }
[JsonPropertyName("labels")]
public List<string>? Labels { get; set; }
[JsonPropertyName("milestone")]
Expand Down Expand Up @@ -116,14 +117,40 @@ public string GenerateCron()
}
}

public class DependabotAllowDependency
public class DependabotAllowDependency : IValidatableObject
{
[JsonPropertyName("dependency-name")]
public string? DependencyName { get; set; }
[JsonPropertyName("dependency-type")]
public string? DependencyType { get; set; }

public bool IsValid() => DependencyName is not null || DependencyType is not null;
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (DependencyName is null && DependencyType is null)
{
yield return new ValidationResult("Each entry under 'allow' must have 'dependency-name', 'dependency-type' or both set");
}
}
}

public class DependabotIgnoreDependency : IValidatableObject
{
[JsonPropertyName("dependency-name")]
public string? DependencyName { get; set; }

[JsonPropertyName("versions")]
public IList<string>? Versions { get; set; }

[JsonPropertyName("update-types")]
public IList<string>? UpdateTypes { get; set; }

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (DependencyName is null && Versions is null && UpdateTypes is null)
{
yield return new ValidationResult("Each entry under 'ignore' must have one of 'dependency-name', 'versions', or 'update-types' set");
}
}
}

public class DependabotPullRequestBranchName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,82 +4,13 @@

namespace Tingle.Dependabot.Models;

public sealed record UpdateJobResponse(UpdateJobData Data);
public sealed record UpdateJobData(UpdateJobAttributes Attributes);

public sealed class UpdateJobAttributes
public sealed class DependabotUpdateJobDefinition
{
[JsonPropertyName("allowed-updates")]
public required IEnumerable<object> AllowedUpdates { get; set; }

[JsonPropertyName("credentials-metadata")]
public required IEnumerable<object> CredentialsMetadata { get; set; }

[JsonPropertyName("dependencies")]
public required IEnumerable<object> Dependencies { get; set; }

[JsonPropertyName("directory")]
public required string Directory { get; set; }

[JsonPropertyName("existing-pull-requests")]
public required IEnumerable<object> ExistingPullRequests { get; set; }

[JsonPropertyName("ignore-conditions")]
public required IEnumerable<object> IgnoreConditions { get; set; }

[JsonPropertyName("security-advisories")]
public required IEnumerable<object> SecurityAdvisories { get; set; }

[JsonPropertyName("package_manager")]
public required string PackageManager { get; set; }

[JsonPropertyName("repo-name")]
public required string RepoName { get; set; }

[JsonPropertyName("source")]
public required UpdateJobAttributesSource Source { get; set; }

[JsonPropertyName("lockfile-only")]
public bool? LockfileOnly { get; set; }

[JsonPropertyName("requirements-update-strategy")]
public string? RequirementsUpdateStrategy { get; set; }

[JsonPropertyName("update-subdependencies")]
public bool? UpdateSubdependencies { get; set; }

[JsonPropertyName("updating-a-pull-request")]
public bool? UpdatingAPullRequest { get; set; }

[JsonPropertyName("vendor-dependencies")]
public bool? VendorDependencies { get; set; }

[JsonPropertyName("security-updates-only")]
public bool? SecurityUpdatesOnly { get; set; }

[JsonPropertyName("debug")]
public bool? Debug { get; set; }
}

public sealed class UpdateJobAttributesSource
{
[JsonPropertyName("provider")]
public required string Provider { get; set; }

[JsonPropertyName("repo")]
public required string Repo { get; set; }

[JsonPropertyName("directory")]
public required string Directory { get; set; }

[JsonPropertyName("branch")]
public string? Branch { get; set; }

[JsonPropertyName("hostname")]
public string? Hostname { get; set; }
[JsonPropertyName("job")]
public required JsonObject? Job { get; set; }

[JsonPropertyName("api-endpoint")]
public string? ApiEndpoint { get; set; }
[JsonPropertyName("credentials")]
public required JsonArray Credentials { get; set; }
}

public sealed class CreatePullRequestModel
Expand Down
8 changes: 8 additions & 0 deletions server/Tingle.Dependabot/Workflow/AzureDevOpsProjectUrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public AzureDevOpsProjectUrl(Uri uri)
{
this.uri = uri ?? throw new ArgumentNullException(nameof(uri));
var host = Hostname = uri.Host;
Port = uri switch
{
{ Scheme: "http", Port: 80 } => null,
{ Scheme: "https", Port: 443 } => null,
_ => uri.Port,
};

var builder = new UriBuilder(uri) { UserName = null, Password = null };
if (string.Equals(host, "dev.azure.com", StringComparison.OrdinalIgnoreCase))
Expand All @@ -29,6 +35,7 @@ public AzureDevOpsProjectUrl(Uri uri)
builder.Path = string.Empty;
ProjectIdOrName = uri.AbsolutePath.Replace("_apis/projects/", "").Split("/")[1];
}
// TODO: add support for Azure DevOps Server here
else throw new ArgumentException($"Error parsing: '{uri}' into components");

OrganizationUrl = builder.Uri.ToString();
Expand All @@ -52,6 +59,7 @@ public static AzureDevOpsProjectUrl Create(string hostname, string organizationN
}

public string Hostname { get; }
public int? Port { get; }
public string OrganizationName { get; }
public string OrganizationUrl { get; }
public string ProjectIdOrName { get; }
Expand Down
Loading

0 comments on commit c354692

Please sign in to comment.