1- using Discord ;
1+ using System . Collections . Generic ;
2+ using System . Linq ;
3+ using Discord ;
24using Octokit ;
35
46namespace TabletBot . Discord . Embeds
57{
68 public static class GitHubEmbeds
79 {
8- public static Embed GetIssueEmbed ( Issue issue )
10+ private const uint OPEN_COLOR = 0x238636 ;
11+ private const uint RESOLVED_COLOR = 0x8957e5 ;
12+ private const uint CLOSED_COLOR = 0xda3633 ;
13+
14+ public static EmbedBuilder GetEmbed ( Issue ? issue )
915 {
10- var embed = new EmbedBuilder
16+ if ( issue == null )
17+ {
18+ return new EmbedBuilder ( )
19+ {
20+ Color = CLOSED_COLOR ,
21+ Description = "No issue or pull request was found."
22+ } ;
23+ }
24+
25+ return new EmbedBuilder
1126 {
12- Title = string . Format ( "{0} #{1}" , issue . Title , issue . Number ) ,
13- Timestamp = issue . CreatedAt ,
27+ Title = $ "{ issue . Title } #{ issue . Number } ",
1428 Url = issue . HtmlUrl ,
15- Footer = new EmbedFooterBuilder
29+ Description = issue . Body ?? string . Empty ,
30+ Color = GetColor ( issue ) ,
31+ Fields = GetFields ( issue ) . ToList ( ) ,
32+ Author = new EmbedAuthorBuilder
1633 {
17- Text = string . Format ( "{0} opened this issue on {1}" , issue . User . Login , issue . CreatedAt ) ,
34+ Name = issue . User . Login ,
35+ Url = issue . User . HtmlUrl ,
1836 IconUrl = issue . User . AvatarUrl
19- } ,
20- Description = issue . Body
37+ }
2138 } ;
22- return embed . Build ( ) ;
2339 }
2440
25- public static Embed GetPullRequestEmbed ( PullRequest pr )
41+ private static uint GetColor ( Issue issue )
2642 {
27- var embed = new EmbedBuilder
43+ if ( issue . ClosedAt == null )
44+ return OPEN_COLOR ;
45+
46+ if ( issue . PullRequest != null )
47+ return issue . PullRequest . Merged ? RESOLVED_COLOR : CLOSED_COLOR ;
48+
49+ return RESOLVED_COLOR ;
50+ }
51+
52+ private static IEnumerable < EmbedFieldBuilder > GetFields ( Issue issue )
53+ {
54+ if ( issue . Milestone != null )
2855 {
29- Title = $ "{ pr . Title } #{ pr . Number } ",
30- Timestamp = pr . UpdatedAt ,
31- Url = pr . HtmlUrl ,
32- Footer = new EmbedFooterBuilder
56+ yield return new EmbedFieldBuilder
3357 {
34- Text = $ "{ pr . User ? . Login } opened this pull request on { pr . CreatedAt } ",
35- IconUrl = pr . User ? . AvatarUrl
36- } ,
37- Description = pr . Body ?? string . Empty
38- } ;
39- return embed . Build ( ) ;
58+ Name = "Milestone" ,
59+ Value = Formatting . UrlString ( issue . Milestone . Title , issue . Milestone . HtmlUrl ) ,
60+ IsInline = true
61+ } ;
62+ }
63+
64+ if ( issue . Labels . Any ( ) )
65+ {
66+ yield return new EmbedFieldBuilder
67+ {
68+ Name = "Labels" ,
69+ Value = string . Join ( ", " , issue . Labels . Select ( l => Formatting . CodeString ( l . Name ) ) ) ,
70+ IsInline = true
71+ } ;
72+ }
4073 }
4174 }
4275}
0 commit comments