5 Commits
Author SHA1 Message Date
kislovdm 26807458dc ok
Create and publish a Docker image / build-and-push-image (push) Failing after 12s
2024-02-07 18:59:43 +03:00
kislovdm 703ee0fb87 ok
Create and publish a Docker image / build-and-push-image (push) Failing after 15s
2024-02-07 18:45:00 +03:00
kislovdm ee39e29269 ok
Create and publish a Docker image / build-and-push-image (push) Failing after 16s
2024-02-07 16:19:15 +03:00
kislovdm d6c7792a6c new code
Create and publish a Docker image / build-and-push-image (push) Failing after 13s
2024-02-07 14:52:20 +03:00
kislovdm c92e6f5ee9 new code
Create and publish a Docker image / build-and-push-image (push) Failing after 14s
2024-02-07 14:42:02 +03:00
5 changed files with 151 additions and 23 deletions
+21 -3
View File
@@ -50,12 +50,30 @@ public class App : IApp
var mergeRequest = await _client.MergeRequests.GetAsync(_settings.ProjectId, mrId);
var commit = await _client.Commits.GetAsync(_settings.ProjectId, mergeRequest.Sha);
var isCodeFreezePeriod = _settings.ReleaseCodeFreeze != "true";
var mrsToSameRelease = await _customClient.GetMergeRequestsByTargetBranch(mergeRequest.TargetBranch);
var sameMrs = mrsToSameRelease.Where(f => f.SourceBranch == mergeRequest.SourceBranch &&
f.TargetBranch == mergeRequest.TargetBranch &&
f.State == MergeRequestState.Merged).ToArray();
if (sameMrs.Any())
{
var mrUrls = string.Join("\r\n", sameMrs.Select(f=>f.WebUrl));
var message = $"Аналогичный MR ({sameMrs.First().WebUrl}) уже был одобрен и влит, автоматически одобряю текущий:\r\n{mrUrls}";
await _client.MergeRequests.CreateNoteAsync(mergeRequest.ProjectId, mergeRequest.Iid,
new CreateMergeRequestNoteRequest(message));
return 0;
}
var isCodeFreezePeriod = _settings.ReleaseCodeFreeze == "true";
var periodDescription = isCodeFreezePeriod ? "Code-Freeze. Restrictions may apply" : "Normal";
Console.WriteLine($"Current period is {periodDescription}.");
approversDict = FilterApprovers(approversDict, isCodeFreezePeriod);
if (!await IsApproved(mergeRequest, commit, approversDict))
var filtered = FilterApprovers(approversDict, isCodeFreezePeriod);
if (!filtered.Any() && isCodeFreezePeriod)
{
filtered = FilterApprovers(approversDict, false);
}
if (!await IsApproved(mergeRequest, commit, filtered))
{
return -1;
}
@@ -0,0 +1,109 @@
using GitLabApiClient.Models.Issues.Responses;
using GitLabApiClient.Models.MergeRequests.Responses;
using GitLabApiClient.Models.Milestones.Responses;
using GitLabApiClient.Models.Pipelines.Responses;
using GitLabApiClient.Models;
using Newtonsoft.Json;
#pragma warning disable CS8618
namespace Approve.Checker.Gitlab.Dto;
public sealed class CustomMergeRequest
{
[JsonProperty("iid")]
public int Iid { get; set; }
[JsonProperty("id")]
public int Id { get; set; }
[JsonProperty("created_at")]
public DateTime CreatedAt { get; set; }
[JsonProperty("updated_at")]
public DateTime UpdatedAt { get; set; }
[JsonProperty("labels")]
public List<string> Labels { get; } = new List<string>();
[JsonProperty("source_branch")]
public string SourceBranch { get; set; }
[JsonProperty("downvotes")]
public int Downvotes { get; set; }
[JsonProperty("author")]
public Assignee Author { get; set; }
[JsonProperty("assignee")]
public Assignee Assignee { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
[JsonProperty("force_remove_source_branch")]
public bool? ForceRemoveSourceBranch { get; set; }
[JsonProperty("milestone")]
public Milestone Milestone { get; set; }
[JsonProperty("merge_status")]
public MergeStatus Status { get; set; }
[JsonProperty("merge_commit_sha")]
public string MergeCommitSha { get; set; }
[JsonProperty("merge_when_pipeline_succeeds")]
public bool MergeWhenPipelineSucceeds { get; set; }
[JsonProperty("sha")]
public string Sha { get; set; }
[JsonProperty("project_id")]
public string ProjectId { get; set; }
[JsonProperty("should_remove_source_branch")]
public bool? ShouldRemoveSourceBranch { get; set; }
[JsonProperty("target_project_id")]
public int TargetProjectId { get; set; }
[JsonProperty("state")]
public MergeRequestState State { get; set; }
[JsonProperty("source_project_id")]
public int SourceProjectId { get; set; }
[JsonProperty("closed_at")]
public DateTime? ClosedAt { get; set; }
[JsonProperty("closed_by")]
public ClosedBy ClosedBy { get; set; }
[JsonProperty("target_branch")]
public string TargetBranch { get; set; }
[JsonProperty("upvotes")]
public int Upvotes { get; set; }
[JsonProperty("web_url")]
public string WebUrl { get; set; }
[JsonProperty("title")]
public string Title { get; set; }
[JsonProperty("user_notes_count")]
public int UserNotesCount { get; set; }
[JsonProperty("work_in_progress")]
public bool WorkInProgress { get; set; }
[JsonProperty("time_stats")]
public MergeRequestTimeStatistic TimeStats { get; set; }
[JsonProperty("pipeline")]
public Pipeline Pipeline { get; set; }
[JsonProperty("merged_by")]
public Assignee MergedBy { get; set; }
}
@@ -1,5 +1,6 @@
using System.Net.Http.Json;
using System.Text;
using Approve.Checker.Gitlab.Dto;
using GitLabApiClient.Models.Job.Responses;
using GitLabApiClient.Models.MergeRequests.Responses;
using Newtonsoft.Json;
@@ -54,7 +55,7 @@ public class GitlabCustomClient : IGitlabCustomClient
return responseObj!;
}
public async Task<MergeRequest[]> GetMergeRequestsByTargetBranch(string targetBranch)
public async Task<CustomMergeRequest[]> GetMergeRequestsByTargetBranch(string targetBranch)
{
var request = new HttpRequestMessage(HttpMethod.Get, $"{_url}/api/v4/merge_requests?target_branch={targetBranch}&scope=all&per_page=1000");
request.Headers.Add("PRIVATE-TOKEN", _token);
@@ -62,7 +63,7 @@ public class GitlabCustomClient : IGitlabCustomClient
response.EnsureSuccessStatusCode();
var responseStr = await response.Content.ReadAsStringAsync();
var responseObj = JsonConvert.DeserializeObject<MergeRequest[]>(responseStr)!;
var responseObj = JsonConvert.DeserializeObject<CustomMergeRequest[]>(responseStr)!;
return responseObj;
}
@@ -1,4 +1,5 @@
using GitLabApiClient.Models.Job.Responses;
using Approve.Checker.Gitlab.Dto;
using GitLabApiClient.Models.Job.Responses;
using GitLabApiClient.Models.MergeRequests.Responses;
using ReleaseScripts.ExternalSystems.Gitlab.CustomClient.Dto;
using ReleaseScripts.ExternalSystems.Gitlab.CustomClient.Dto.ApprovalState;
@@ -10,7 +11,7 @@ public interface IGitlabCustomClient
Task<Reviewers[]> GetReviewers(string projectId, int Iid);
Task<ApprovalStateResponse> GetApprovals(string projectId, int Iid);
Task<ApprovalStateResponse> GetDiscussions(string projectId, int Iid);
Task<MergeRequest[]> GetMergeRequestsByTargetBranch(string targetBranch);
Task<CustomMergeRequest[]> GetMergeRequestsByTargetBranch(string targetBranch);
Task<Job[]> GetPipelineTriggers(string projectId, int pipelineId);
Task RestartJob(string projectId, int failedJobId);
Task<MergeRequest[]> GetMergeRequestsBySourceBranch(string targetBranch);
+3 -4
View File
@@ -1,7 +1,7 @@
namespace Approve.Checker.Gitlab
namespace Approve.Checker.Gitlab;
public class Settings
{
public class Settings
{
public string GitlabUrl = Environment.GetEnvironmentVariable("CI_SERVER_URL") ?? CheckAndThrow("CI_SERVER_URL");
public string GitlabToken = Environment.GetEnvironmentVariable("GITLAB_TOKEN") ?? CheckAndThrow("GITLAB_TOKEN");
public string MrPath = Environment.GetEnvironmentVariable("CI_OPEN_MERGE_REQUESTS")!;
@@ -14,5 +14,4 @@
Console.WriteLine($"Please define {name}");
throw new ArgumentException($"Please define {name}");
}
}
}