This repository has been archived by the owner on Jan 18, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStaticData.cs
51 lines (42 loc) · 2.07 KB
/
StaticData.cs
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
using System;
using System.IO;
using LibGit2Sharp;
namespace Palantir
{
internal class StaticData
{
public static void AddFile(string filePath, string repoSavePath, string commitMessage)
{
// Clone the repository
var repoPath = Program.CacheDataPath + "/repo-cache/static-data" + DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString();
// Clone the repository
var cloneOptions = new CloneOptions();
cloneOptions.CredentialsProvider = (_url, _user, _cred) =>
new UsernamePasswordCredentials { Username = Program.GithubToken, Password = string.Empty };
Repository.Clone(Program.DataRepoUrl, repoPath, cloneOptions);
// Open the repository
using (var repo = new Repository(repoPath))
{
// Create a new file in the repository
var fileName = Path.GetFileName(filePath);
var repoFilePath = Path.Combine(repoSavePath, fileName);
File.Copy(filePath, Path.Combine(repo.Info.WorkingDirectory, repoFilePath));
// Stage the file
LibGit2Sharp.Commands.Stage(repo, repoSavePath);
// Create the commit
var author = new Signature("Palantir Data Commit", "dev.tobeh@gmail.com", DateTimeOffset.Now);
var committer = author;
var commit = repo.Commit("[🤖] " + commitMessage, author, committer);
// Set the remote repository URL
var remote = repo.Network.Remotes["origin"];
var options = new PushOptions();
options.CredentialsProvider = (_url, _user, _cred) =>
new UsernamePasswordCredentials { Username = Program.GithubToken, Password = string.Empty };
// Push the commit to the remote repository
repo.Network.Push(remote, $"refs/heads/{repo.Head.FriendlyName}", options);
}
// Clean up the temporary repository
Directory.Delete(repoPath, true);
}
}
}