-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathLastCommitActivityTrigger.cs
More file actions
116 lines (103 loc) · 4.67 KB
/
Copy pathLastCommitActivityTrigger.cs
File metadata and controls
116 lines (103 loc) · 4.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using Interfaces;
using Octokit;
using Platform.Communication.Protocol.Lino;
using Storage.Remote.GitHub;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace Platform.Bot.Triggers
{
using TContext = Issue;
internal class LastCommitActivityTrigger : IPrivateMessageTrigger<TContext>
{
private readonly GitHubStorage _githubStorage;
public LastCommitActivityTrigger(GitHubStorage storage) => _githubStorage = storage;
public async Task<bool> Condition(TContext issue)
{
return "last 3 months commit activity" == issue.Title.ToLower();
}
public async Task Action(TContext issue)
{
Console.WriteLine($"Issue {issue.Title} is processed: {issue.HtmlUrl}");
await _githubStorage.Client.Issue.Update(issue.Repository.Owner.Login, issue.Repository.Name, issue.Number, new IssueUpdate() { State = ItemState.Closed });
}
public string GetTargetUserLogin(TContext issue)
{
return issue.User.Login;
}
public string GetMessageSubject(TContext issue)
{
return "Last 3 Months Commit Activity Report";
}
public async Task<string> GetPrivateMessageContent(TContext issue)
{
var organizationName = issue.Repository.Owner.Login;
var allMembers = await _githubStorage.GetAllOrganizationMembers(organizationName);
var allRepositories = await _githubStorage.GetAllRepositories(organizationName);
if (!allRepositories.Any())
{
return "No repositories found in the organization.";
}
var commitsPerUserInLast3Months = await allRepositories
.Where(repository => _githubStorage.Client.Repository.Branch.GetAll(repository.Id).Result.Any())
.Select(repository => _githubStorage.GetCommits(repository.Id, new CommitRequest { Since = DateTime.Now.AddMonths(-3) }).Result)
.SelectMany(x => x)
.Where(commit => allMembers.Find(user => user.Id == commit.Author.Id) != null)
.Aggregate(Task.FromResult(new Dictionary<User, List<GitHubCommit>>()), async (dictionaryTask, commit) =>
{
var dictionary = await dictionaryTask;
var member = allMembers.Find(user => user.Id == commit.Author.Id)!;
if (dictionary.ContainsKey(member))
{
dictionary[member].Add(commit);
}
else
{
dictionary.Add(member, new List<GitHubCommit> { commit });
}
return dictionary;
});
StringBuilder messageSb = new();
var ShortSummaryMessage = GetShortSummaryMessage(commitsPerUserInLast3Months.Select(pair => pair.Key).ToList());
messageSb.Append(ShortSummaryMessage);
messageSb.AppendLine("---");
var detailedMessage = await GetDetailedMessage(commitsPerUserInLast3Months);
messageSb.Append(detailedMessage);
return messageSb.ToString();
}
private string GetShortSummaryMessage(List<User> users)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine("# Short Summary:");
users.All(user =>
{
stringBuilder.AppendLine($"- [{user.Login}]({user.Url})");
return true;
});
return stringBuilder.ToString();
}
private async Task<string> GetDetailedMessage(IDictionary<User, List<GitHubCommit>> commitsPerUser)
{
StringBuilder stringBuilder = new StringBuilder();
foreach (var userAndCommitedRepositories in commitsPerUser)
{
var user = userAndCommitedRepositories.Key;
var commits = userAndCommitedRepositories.Value;
stringBuilder.AppendLine($"## [{user.Login}]({user.Url})");
commits
.ForEach(commit =>
{
Regex regex = new Regex(@"\n+");
var commitMessage = commit.Commit.Message;
var commitMesasgeWithoutNewLines = regex.Replace(commitMessage, " ");
stringBuilder.AppendLine($"- [{commitMesasgeWithoutNewLines}]({commit.Url})");
});
}
return stringBuilder.ToString();
}
}
}