Skip to content

Commit 51a72c2

Browse files
authored
Merge pull request #28 from dabutvin/private-repos
use credentials to clone (fixes #19)
2 parents a1aa3ea + 67faca0 commit 51a72c2

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

ImgBot.Function/CompressImages.cs

+17-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using ImageMagick;
88
using ImgBot.Common;
99
using LibGit2Sharp;
10+
using LibGit2Sharp.Handlers;
1011
using Octokit;
1112
using Octokit.Internal;
1213

@@ -15,11 +16,22 @@ namespace ImgBot.Function
1516
public static class CompressImages
1617
{
1718
private const string BranchName = "imgbot";
19+
private const string Username = "x-access-token";
1820

1921
public static async Task RunAsync(CompressimagesParameters parameters)
2022
{
23+
CredentialsHandler credentialsProvider =
24+
(_url, _user, _cred) =>
25+
new UsernamePasswordCredentials { Username = Username, Password = parameters.Password };
26+
27+
InMemoryCredentialStore inMemoryCredentialStore = new InMemoryCredentialStore(new Octokit.Credentials(Username, parameters.Password));
28+
2129
// clone
22-
LibGit2Sharp.Repository.Clone(parameters.CloneUrl, parameters.LocalPath);
30+
var cloneOptions =
31+
LibGit2Sharp.Repository.Clone(parameters.CloneUrl, parameters.LocalPath, new CloneOptions
32+
{
33+
CredentialsProvider = credentialsProvider,
34+
});
2335
var repo = new LibGit2Sharp.Repository(parameters.LocalPath);
2436
var remote = repo.Network.Remotes["origin"];
2537

@@ -43,18 +55,13 @@ public static async Task RunAsync(CompressimagesParameters parameters)
4355
repo.Commit(commitMessage, signature, signature);
4456

4557
// push to GitHub
46-
var username = "x-access-token";
47-
var options = new PushOptions
58+
repo.Network.Push(remote, $"refs/heads/{BranchName}", new PushOptions
4859
{
49-
CredentialsProvider = (_url, _user, _cred) =>
50-
new UsernamePasswordCredentials { Username = username, Password = parameters.Password }
51-
};
52-
53-
repo.Network.Push(remote, $"refs/heads/{BranchName}", options);
60+
CredentialsProvider = credentialsProvider,
61+
});
5462

5563
// open PR
56-
var credentials = new InMemoryCredentialStore(new Octokit.Credentials(username, parameters.Password));
57-
var githubClient = new GitHubClient(new ProductHeaderValue("ImgBot"), credentials);
64+
var githubClient = new GitHubClient(new ProductHeaderValue("ImgBot"), inMemoryCredentialStore);
5865

5966
var pr = new NewPullRequest("[ImgBot] Optimizes Images", BranchName, "master");
6067
pr.Body = "Beep boop. Optimizing your images is my life";

0 commit comments

Comments
 (0)