Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c92e6f5ee9 | ||
|
|
8d56dbf2b3 | ||
|
|
1d127cadd3 | ||
|
|
97e81c3af4 | ||
|
|
071aa73015 |
@@ -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
@@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -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();
|
||||||
}
|
}
|
||||||
@@ -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)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user