1- using Azure . Storage . Blobs ;
2- using Microsoft . AspNetCore . Http ;
3- using Microsoft . AspNetCore . Mvc ;
4- using Octokit ;
1+ using Octokit ;
52using System ;
63using System . Collections . Generic ;
7- using System . IO ;
84using System . Linq ;
9- using System . Net . Http ;
10- using System . Text ;
115using System . Threading . Tasks ;
126
137namespace GithubWebpagesWebhook
148{
159 public static class GithubClientWrapper
1610 {
1711 private static readonly GitHubClient _client ;
18- private static string _clientLogin ;
19- public static string LocalDirectory = Path . Combine ( Path . GetTempPath ( ) , "my-cloned-repo" ) ;
12+ private static readonly string _clientLogin ;
13+ private static readonly string _webPageRepositoryName = Environment . GetEnvironmentVariable ( "WebPagesRepositorieName" ) ;
14+
2015 static GithubClientWrapper ( )
2116 {
2217 var accessToken = Environment . GetEnvironmentVariable ( "GithubAccessToken" ) ;
@@ -25,8 +20,6 @@ static GithubClientWrapper()
2520 {
2621 Credentials = credentials
2722 } ;
28- Directory . CreateDirectory ( LocalDirectory ) ;
29-
3023 var user = _client . User . Current ( ) . Result ;
3124 _clientLogin = user . Login ;
3225 }
@@ -59,93 +52,31 @@ public static async Task<GitHubCommit> GetGitHubCommitAsync(string repositoryNam
5952 return commitMessage ;
6053 }
6154
62- public static async Task < IActionResult > CloneTest ( )
55+ public static async Task < IReadOnlyList < RepositoryContent > > GetAllRepositoryContentAsync ( string branch = "main" )
6356 {
64- try
65- {
66- var client = new HttpClient ( ) ;
67- var repo = Environment . GetEnvironmentVariable ( "WebPagesRepositorieName" ) ;
68- var branch = "main" ;
69- // Get the repository contents
70- var contents = await _client . Repository . Content . GetAllContentsByRef ( _clientLogin , repo , "/" , branch ) ;
71-
72- string indexPath = string . Empty ;
73-
74- foreach ( var content in contents )
75- {
76- if ( content . Type == ContentType . File )
77- {
78- var fileBytes = await client . GetStringAsync ( content . DownloadUrl ) ;
79-
80- var path = Path . Combine ( LocalDirectory , content . Name ) ;
81-
82- File . WriteAllText ( path , fileBytes ) ;
83-
84- if ( content . Name . Contains ( "index.html" ) )
85- {
86- indexPath = path ;
87- }
88- }
89- }
90-
91- var blobClient = new BlobClient ( Environment . GetEnvironmentVariable ( "AzureWebJobsStorage" ) , "html-templates" , "index.html" ) ;
92-
93- var blobContent = await blobClient . DownloadContentAsync ( ) ;
94-
95- var projects = await ProjectDivGenerator . GenerateProjectDivsAsync ( ) ;
96-
97- var htmlTemplate = blobContent . Value . Content . ToString ( )
98- . Replace ( "[user-name]" , ClientLogin )
99- . Replace ( "[page-content]" , projects )
100- . Replace ( "[last-update]" , DateTime . Now . ToLongDateString ( ) ) ;
101-
102- File . WriteAllText ( indexPath , htmlTemplate ) ;
103-
104- // Step 3a: Get the latest commit on the main branch
105- var baseRef = await _client . Git . Reference . Get ( _clientLogin , repo , $ "heads/{ branch } ") ;
106-
107- // Step 3b: Prepare the new tree
108- var treeBuilder = new NewTree ( ) ;
109- foreach ( var file in Directory . GetFiles ( LocalDirectory ) )
110- {
111- var fileName = Path . GetFileName ( file ) ;
112- var content = File . ReadAllText ( file ) ;
113-
114- // Create a new tree entry for each file
115- treeBuilder . Tree . Add ( new NewTreeItem
116- {
117- Path = fileName ,
118- Mode = "100644" ,
119- Type = TreeType . Blob ,
120- Content = content
121- } ) ;
122- }
57+ return await _client . Repository . Content . GetAllContentsByRef ( _clientLogin , _webPageRepositoryName , "/" , branch ) ;
58+ }
12359
124- // Create a new tree from the tree builder
125- var newTree = await _client . Git . Tree . Create ( _clientLogin , repo , treeBuilder ) ;
60+ public static async Task < Reference > GetReferenceAsync ( string branch = "main" )
61+ {
62+ return await _client . Git . Reference . Get ( _clientLogin , _webPageRepositoryName , $ "heads/{ branch } ") ;
63+ }
12664
127- // Step 3c: Create a new commit
128- // Step 3c: Create a new commit
129- var changes = new NewCommit ( "Updated the repository with new changes" , newTree . Sha , new [ ] { baseRef . Object . Sha } )
130- {
131- } ;
65+ public static async Task < TreeResponse > CreateTreeResponseAsync ( NewTree treeBuilder )
66+ {
67+ return await _client . Git . Tree . Create ( _clientLogin , _webPageRepositoryName , treeBuilder ) ;
68+ }
13269
133- var commit = await _client . Git . Commit . Create ( _clientLogin , repo , changes ) ;
70+ public static async Task < Commit > CreateCommitAsync ( TreeResponse newTree , Reference baseRef )
71+ {
72+ var changes = new NewCommit ( "Updated the repository with new changes via GitHubWebpageWebHook" , newTree . Sha , new [ ] { baseRef . Object . Sha } ) ;
13473
135- // Step 3d: Update the main branch to point to the new commit
136- await _client . Git . Reference . Update ( _clientLogin , repo , $ "heads/ { branch } " , new ReferenceUpdate ( commit . Sha ) ) ;
74+ return await _client . Git . Commit . Create ( _clientLogin , _webPageRepositoryName , changes ) ;
75+ }
13776
138- return new ContentResult ( )
139- {
140- Content = "Okay" ,
141- ContentType = "document" ,
142- StatusCode = 200 ,
143- } ;
144- }
145- catch ( Exception ex )
146- {
147- return new OkObjectResult ( ex ) ;
148- }
77+ public static async Task UpdateRepositoryAsync ( Commit commit , string branch = "main" )
78+ {
79+ await _client . Git . Reference . Update ( _clientLogin , _webPageRepositoryName , $ "heads/{ branch } ", new ReferenceUpdate ( commit . Sha ) ) ;
14980 }
15081 }
15182}
0 commit comments