7
7
using ImageMagick ;
8
8
using ImgBot . Common ;
9
9
using LibGit2Sharp ;
10
+ using LibGit2Sharp . Handlers ;
10
11
using Octokit ;
11
12
using Octokit . Internal ;
12
13
@@ -15,11 +16,22 @@ namespace ImgBot.Function
15
16
public static class CompressImages
16
17
{
17
18
private const string BranchName = "imgbot" ;
19
+ private const string Username = "x-access-token" ;
18
20
19
21
public static async Task RunAsync ( CompressimagesParameters parameters )
20
22
{
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
+
21
29
// 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
+ } ) ;
23
35
var repo = new LibGit2Sharp . Repository ( parameters . LocalPath ) ;
24
36
var remote = repo . Network . Remotes [ "origin" ] ;
25
37
@@ -43,18 +55,13 @@ public static async Task RunAsync(CompressimagesParameters parameters)
43
55
repo . Commit ( commitMessage , signature , signature ) ;
44
56
45
57
// push to GitHub
46
- var username = "x-access-token" ;
47
- var options = new PushOptions
58
+ repo . Network . Push ( remote , $ "refs/heads/{ BranchName } ", new PushOptions
48
59
{
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
+ } ) ;
54
62
55
63
// 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 ) ;
58
65
59
66
var pr = new NewPullRequest ( "[ImgBot] Optimizes Images" , BranchName , "master" ) ;
60
67
pr . Body = "Beep boop. Optimizing your images is my life" ;
0 commit comments