5 Commits
Author SHA1 Message Date
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
kislovdm 8d56dbf2b3 new code
Create and publish a Docker image / build-and-push-image (push) Failing after 15s
2024-02-07 14:29:13 +03:00
kislovdm 1d127cadd3 ok
Create and publish a Docker image / build-and-push-image (push) Failing after 14s
2024-02-07 13:56:26 +03:00
monster 97e81c3af4 Update blank.yml
Create and publish a Docker image / build-and-push-image (push) Failing after 14s
2024-02-07 13:31:39 +03:00
kislovdm 071aa73015 build fix 2024-02-07 13:04:29 +03:00
4 changed files with 49 additions and 22 deletions
+2
View File
@@ -4,6 +4,8 @@ name: Create and publish a Docker image
on: on:
push: push:
branches: [ "master" ] branches: [ "master" ]
tags:
- '*'
# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. # Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds.
env: env:
+41 -12
View File
@@ -23,11 +23,6 @@ public class App : IApp
ServerCertificateCustomValidationCallback = (_, _, _, _) => true ServerCertificateCustomValidationCallback = (_, _, _, _) => true
}; };
_settings = new Settings(); _settings = new Settings();
if (string.IsNullOrEmpty(_settings.GitlabUrl))
{
Console.WriteLine("CI_SERVER_URL is not defined.");
return;
}
_client = new GitLabClient(_settings.GitlabUrl, _settings.GitlabToken, httpHandler); _client = new GitLabClient(_settings.GitlabUrl, _settings.GitlabToken, httpHandler);
_customClient = new GitlabCustomClient(_settings.GitlabUrl, _settings.GitlabToken, httpHandler); _customClient = new GitlabCustomClient(_settings.GitlabUrl, _settings.GitlabToken, httpHandler);
} }
@@ -44,11 +39,37 @@ public class App : IApp
Console.WriteLine("Not in MergeRequest. Exiting."); Console.WriteLine("Not in MergeRequest. Exiting.");
return 0; return 0;
} }
var approversDict = ParseApprovers();
if (approversDict is null)
{
return -1;
}
var mrId = int.Parse(_settings.MrPath.Split('!')[1]); var mrId = int.Parse(_settings.MrPath.Split('!')[1]);
var mergeRequest = await _client.MergeRequests.GetAsync(_settings.ProjectId, mrId); var mergeRequest = await _client.MergeRequests.GetAsync(_settings.ProjectId, mrId);
var commit = await _client.Commits.GetAsync(_settings.ProjectId, mergeRequest.Sha); var commit = await _client.Commits.GetAsync(_settings.ProjectId, mergeRequest.Sha);
if (!await IsApproved(mergeRequest, commit))
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))
{ {
return -1; return -1;
} }
@@ -56,13 +77,8 @@ public class App : IApp
return 0; return 0;
} }
private async Task<bool> IsApproved(MergeRequest mergeRequest, Commit commit) private async Task<bool> IsApproved(MergeRequest mergeRequest, Commit commit, Dictionary<string, ApproversDto> approversDict)
{ {
var approversDict = ParseApprovers();
if (approversDict is null)
{
return false;
}
var emojis = await _client.MergeRequests.GetAwardEmojisAsync(_settings.ProjectId, mergeRequest.Iid); var emojis = await _client.MergeRequests.GetAwardEmojisAsync(_settings.ProjectId, mergeRequest.Iid);
var sb = new StringBuilder(); var sb = new StringBuilder();
@@ -130,4 +146,17 @@ public class App : IApp
return null; return null;
} }
} }
private Dictionary<string, ApproversDto> FilterApprovers(Dictionary<string, ApproversDto> approvers, bool isCodeFreezePeriod)
{
var result = new Dictionary<string, ApproversDto>();
foreach (var approversDto in approvers)
{
if (approversDto.Value.ForCodeFreeze == isCodeFreezePeriod)
{
result.Add(approversDto.Key, approversDto.Value);
}
}
return result;
}
} }
+2 -1
View File
@@ -8,6 +8,7 @@ namespace Approve.Checker.Gitlab;
public class ApproversDto public class ApproversDto
{ {
public bool ForCodeFreeze { get; set; }
public int CountApprovers { get; set; } = 1; public int CountApprovers { get; set; } = 1;
public List<string> Users { get; set; } public List<string> Users { get; set; } = new();
} }
+3 -8
View File
@@ -1,10 +1,4 @@
using System; namespace Approve.Checker.Gitlab
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Approve.Checker.Gitlab
{ {
public class Settings public class Settings
{ {
@@ -12,7 +6,8 @@ namespace Approve.Checker.Gitlab
public string GitlabToken = Environment.GetEnvironmentVariable("GITLAB_TOKEN") ?? CheckAndThrow("GITLAB_TOKEN"); public string GitlabToken = Environment.GetEnvironmentVariable("GITLAB_TOKEN") ?? CheckAndThrow("GITLAB_TOKEN");
public string MrPath = Environment.GetEnvironmentVariable("CI_OPEN_MERGE_REQUESTS")!; public string MrPath = Environment.GetEnvironmentVariable("CI_OPEN_MERGE_REQUESTS")!;
public string ProjectId = Environment.GetEnvironmentVariable("CI_PROJECT_ID") ?? CheckAndThrow("CI_PROJECT_ID"); public string ProjectId = Environment.GetEnvironmentVariable("CI_PROJECT_ID") ?? CheckAndThrow("CI_PROJECT_ID");
public string Approvers = Environment.GetEnvironmentVariable("APPROVERS_FILEPATH ") ?? CheckAndThrow("APPROVERS_FILEPATH"); public string Approvers = Environment.GetEnvironmentVariable("APPROVERS_FILEPATH") ?? CheckAndThrow("APPROVERS_FILEPATH");
public string ReleaseCodeFreeze = Environment.GetEnvironmentVariable("RELEASE_CODEFREEZE") ?? "false";
private static string CheckAndThrow(string name) private static string CheckAndThrow(string name)
{ {