1+ using System ;
2+ using System . Threading . Tasks ;
3+ using Interfaces ;
4+ using Octokit ;
5+ using Storage . Remote . GitHub ;
6+ using System . Text . RegularExpressions ;
7+
8+ namespace Platform . Bot . Triggers
9+ {
10+ using TContext = Issue ;
11+
12+ /// <summary>
13+ /// <para>
14+ /// Represents the create repository from template trigger.
15+ /// </para>
16+ /// <para></para>
17+ /// </summary>
18+ /// <seealso cref="ITrigger{TContext}"/>
19+ internal class CreateRepositoryFromTemplateTrigger : ITrigger < TContext >
20+ {
21+ private readonly GitHubStorage _storage ;
22+
23+ /// <summary>
24+ /// <para>
25+ /// Initializes a new <see cref="CreateRepositoryFromTemplateTrigger"/> instance.
26+ /// </para>
27+ /// <para></para>
28+ /// </summary>
29+ /// <param name="storage">
30+ /// <para>A github storage.</para>
31+ /// <para></para>
32+ /// </param>
33+ public CreateRepositoryFromTemplateTrigger ( GitHubStorage storage )
34+ {
35+ this . _storage = storage ;
36+ }
37+
38+ /// <summary>
39+ /// <para>
40+ /// Actions the context.
41+ /// </para>
42+ /// <para></para>
43+ /// </summary>
44+ /// <param name="context">
45+ /// <para>The context.</para>
46+ /// <para></para>
47+ /// </param>
48+ public async Task Action ( TContext context )
49+ {
50+ var templateType = GetTemplateType ( context . Title . ToLower ( ) ) ;
51+ var repositoryName = GetRepositoryName ( context . Body ) ;
52+
53+ if ( string . IsNullOrEmpty ( repositoryName ) )
54+ {
55+ await _storage . CreateIssueComment ( context . Repository . Id , context . Number ,
56+ "Please specify the repository name in the issue body. Example: `repository: my-new-repo`" ) ;
57+ return ;
58+ }
59+
60+ try
61+ {
62+ var templateInfo = GetTemplateInfo ( templateType ) ;
63+ var newRepository = await _storage . CreateRepositoryFromTemplate (
64+ templateInfo . Owner ,
65+ templateInfo . Name ,
66+ context . Repository . Owner . Login ,
67+ repositoryName ,
68+ $ "Generated { templateType } from template",
69+ false
70+ ) ;
71+
72+ await _storage . CreateIssueComment ( context . Repository . Id , context . Number ,
73+ $ "✅ Successfully created repository [{ repositoryName } ]({ newRepository . HtmlUrl } ) from { templateType } template!") ;
74+
75+ _storage . CloseIssue ( context ) ;
76+ }
77+ catch ( Exception ex )
78+ {
79+ await _storage . CreateIssueComment ( context . Repository . Id , context . Number ,
80+ $ "❌ Error creating repository: { ex . Message } ") ;
81+ }
82+ }
83+
84+ /// <summary>
85+ /// <para>
86+ /// Determines whether this instance condition.
87+ /// </para>
88+ /// <para></para>
89+ /// </summary>
90+ /// <param name="context">
91+ /// <para>The context.</para>
92+ /// <para></para>
93+ /// </param>
94+ /// <returns>
95+ /// <para>The bool</para>
96+ /// <para></para>
97+ /// </returns>
98+ public async Task < bool > Condition ( TContext context )
99+ {
100+ var title = context . Title . ToLower ( ) ;
101+ return title . Contains ( "hello world template" ) ||
102+ title . Contains ( "console app template" ) ||
103+ title . Contains ( "console application template" ) ||
104+ title . Contains ( "library template" ) ;
105+ }
106+
107+ private string GetTemplateType ( string title )
108+ {
109+ if ( title . Contains ( "hello world" ) )
110+ return "hello-world" ;
111+ if ( title . Contains ( "console app" ) || title . Contains ( "console application" ) )
112+ return "console-app" ;
113+ if ( title . Contains ( "library" ) )
114+ return "library" ;
115+ return "hello-world" ; // default
116+ }
117+
118+ private ( string Owner , string Name ) GetTemplateInfo ( string templateType )
119+ {
120+ return templateType switch
121+ {
122+ "hello-world" => ( "linksplatform" , "Template.HelloWorld" ) ,
123+ "console-app" => ( "linksplatform" , "Template.ConsoleApp" ) ,
124+ "library" => ( "linksplatform" , "Template.Library" ) ,
125+ _ => ( "linksplatform" , "Template.HelloWorld" )
126+ } ;
127+ }
128+
129+ private string ? GetRepositoryName ( string ? body )
130+ {
131+ if ( string . IsNullOrEmpty ( body ) )
132+ return null ;
133+
134+ var match = Regex . Match ( body , @"repository:\s*([a-zA-Z0-9._-]+)" , RegexOptions . IgnoreCase ) ;
135+ return match . Success ? match . Groups [ 1 ] . Value . Trim ( ) : null ;
136+ }
137+ }
138+ }
0 commit comments