Skip to content

Commit

Permalink
Generate credentials earlier in the server
Browse files Browse the repository at this point in the history
  • Loading branch information
mburumaxwell committed Sep 19, 2023
1 parent b086585 commit f80ae2f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public void MakeExtraCredentials_Works_1()
var configuration = deserializer.Deserialize<DependabotConfiguration?>(reader);
Assert.NotNull(configuration);
var registries = UpdateRunner.MakeExtraCredentials(configuration.Registries.Values, new Dictionary<string, string>());
Assert.NotNull(registries);
Assert.Equal(11, registries.Count);

// composer-repository
Expand Down
8 changes: 2 additions & 6 deletions server/Tingle.Dependabot/Models/UpdateJobResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ namespace Tingle.Dependabot.Models;
public sealed record UpdateJobResponse(UpdateJobData Data);
public sealed record UpdateJobData(UpdateJobAttributes Attributes);

public sealed record UpdateJobAttributes()
public sealed class UpdateJobAttributes
{
public UpdateJobAttributes(UpdateJob job) : this()
{
}

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

Expand Down Expand Up @@ -65,7 +61,7 @@ public UpdateJobAttributes(UpdateJob job) : this()
public bool? Debug { get; set; }
}

public sealed record UpdateJobAttributesSource
public sealed class UpdateJobAttributesSource
{
[JsonPropertyName("provider")]
public required string Provider { get; set; }
Expand Down
61 changes: 32 additions & 29 deletions server/Tingle.Dependabot/Workflow/UpdateRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,31 +206,36 @@ internal IDictionary<string, string> CreateVariables(Repository repository, Repo
{
static string? ToJson<T>(T? entries) => entries is null ? null : JsonSerializer.Serialize(entries, serializerOptions); // null ensures we do not add to the values

var jobDirectory = Path.Join(options.WorkingDirectory, job.Id);
// Prepare extra credentials with replaced secrets
var secrets = new Dictionary<string, string>(options.Secrets) { ["DEFAULT_TOKEN"] = options.ProjectToken!, };
var registries = update.Registries?.Select(r => repository.Registries[r]).ToList();
var credentials = ToJson(MakeExtraCredentials(registries, secrets)); // add source credentials when running the in v2

// TODO: write the job definition file (find out if it is YAML/JSON)
var jobDirectory = Path.Join(options.WorkingDirectory, job.Id);

// var attr = new UpdateJobAttributes(job)
//var attr = new UpdateJobAttributes
//{
// AllowedUpdates = Array.Empty<object>(),
// CredentialsMetadata = Array.Empty<object>(),
// Dependencies = Array.Empty<object>(),
// Directory = job.Directory!,
// ExistingPullRequests = Array.Empty<object>(),
// IgnoreConditions = Array.Empty<object>(),
// PackageManager = job.PackageEcosystem!,
// RepoName = job.RepositorySlug!,
// SecurityAdvisories = Array.Empty<object>(),
// Source = new UpdateJobAttributesSource
// {
// AllowedUpdates = Array.Empty<object>(),
// CredentialsMetadata = Array.Empty<object>(),
// Dependencies = Array.Empty<object>(),
// Directory = job.Directory!,
// ExistingPullRequests = Array.Empty<object>(),
// IgnoreConditions = Array.Empty<object>(),
// PackageManager = job.PackageEcosystem,
// RepoName = job.RepositorySlug!,
// SecurityAdvisories = Array.Empty<object>(),
// Source = new UpdateJobAttributesSource
// {
// Directory = job.Directory!,
// Provider = "azure",
// Repo = job.RepositorySlug!,
// Branch = job.Branch,
// Hostname = ,
// ApiEndpoint =,
// },
// };
// Provider = "azure",
// Repo = job.RepositorySlug!,
// Branch = update.TargetBranch,
// Hostname = ,
// ApiEndpoint =,
// },
//};

// TODO: write the job definition file (find out if it is YAML/JSON)

// Add compulsory values
var values = new Dictionary<string, string>
Expand All @@ -243,6 +248,8 @@ internal IDictionary<string, string> CreateVariables(Repository repository, Repo
["DEPENDABOT_PACKAGE_MANAGER"] = job.PackageEcosystem!,
["DEPENDABOT_DIRECTORY"] = update.Directory!,
["DEPENDABOT_OPEN_PULL_REQUESTS_LIMIT"] = update.OpenPullRequestsLimit!.Value.ToString(),

["DEPENDABOT_EXTRA_CREDENTIALS"] = credentials!,
};

// Add optional values
Expand All @@ -264,8 +271,6 @@ internal IDictionary<string, string> CreateVariables(Repository repository, Repo
.AddIfNotDefault("DEPENDABOT_MILESTONE", update.Milestone?.ToString())
.AddIfNotDefault("DEPENDABOT_FAIL_ON_EXCEPTION", options.FailOnException.ToString().ToLowerInvariant());

var secrets = new Dictionary<string, string>(options.Secrets) { ["DEFAULT_TOKEN"] = options.ProjectToken!, };

// Add values for Azure DevOps
var url = options.ProjectUrl!.Value;
values.AddIfNotDefault("AZURE_HOSTNAME", url.Hostname)
Expand All @@ -278,15 +283,13 @@ internal IDictionary<string, string> CreateVariables(Repository repository, Repo
.AddIfNotDefault("AZURE_MERGE_STRATEGY", options.AutoCompleteMergeStrategy?.ToString())
.AddIfNotDefault("AZURE_AUTO_APPROVE_PR", (options.AutoApprove ?? false).ToString().ToLowerInvariant());

// Add extra credentials with replaced secrets
var registries = update.Registries?.Select(r => repository.Registries[r]).ToList();
values.AddIfNotDefault("DEPENDABOT_EXTRA_CREDENTIALS", ToJson(MakeExtraCredentials(registries, secrets)));

return values;
}
internal static IList<IDictionary<string, string>>? MakeExtraCredentials(ICollection<DependabotRegistry>? registries, IDictionary<string, string> secrets)
internal static IList<IDictionary<string, string>> MakeExtraCredentials(ICollection<DependabotRegistry>? registries, IDictionary<string, string> secrets)
{
return registries?.Select(v =>
if (registries is null) return Array.Empty<IDictionary<string, string>>();

return registries.Select(v =>
{
var type = v.Type?.Replace("-", "_") ?? throw new InvalidOperationException("Type should not be null");

Expand Down

0 comments on commit f80ae2f

Please sign in to comment.