Skip to content

Commit aebe9df

Browse files
authored
Make direct API call to sync fork (#581)
1 parent 60cc6f2 commit aebe9df

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/WingetCreateCore/Common/GitHub.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ namespace Microsoft.WingetCreateCore.Common
77
using System.Collections.Generic;
88
using System.IO;
99
using System.Linq;
10+
using System.Net.Http;
1011
using System.Security.Cryptography;
12+
using System.Text;
13+
using System.Text.Json.Nodes;
1114
using System.Threading.Tasks;
1215
using Jose;
1316
using Microsoft.WingetCreateCore.Common.Exceptions;
@@ -483,8 +486,24 @@ private async Task UpdateForkedRepoWithUpstreamCommits(Repository forkedRepo)
483486
throw new NonFastForwardException(commitsAheadBy);
484487
}
485488

486-
var upstreamBranchReference = await this.github.Git.Reference.Get(upstream.Id, $"heads/{upstream.DefaultBranch}");
487-
await this.github.Git.Reference.Update(forkedRepo.Id, $"heads/{forkedRepo.DefaultBranch}", new ReferenceUpdate(upstreamBranchReference.Object.Sha));
489+
// Octokit .NET doesn't support sync fork endpoint, so we make a direct call to the GitHub API.
490+
// Tracking issue for the request: https://github.com/octokit/octokit.net/issues/2989
491+
// API reference: https://docs.github.com/en/rest/branches/branches?apiVersion=2022-11-28#sync-a-fork-branch-with-the-upstream-repository
492+
HttpClient httpClient = new HttpClient();
493+
494+
// Headers
495+
httpClient.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", this.github.Credentials.Password);
496+
httpClient.DefaultRequestHeaders.Add("Accept", "application/vnd.github+json");
497+
httpClient.DefaultRequestHeaders.Add("X-GitHub-Api-Version", "2022-11-28");
498+
httpClient.DefaultRequestHeaders.UserAgent.TryParseAdd(Constants.ProgramName);
499+
500+
// Payload
501+
JsonObject jsonObject = new JsonObject { { "branch", forkedRepo.DefaultBranch } };
502+
var content = new StringContent(jsonObject.ToString(), Encoding.UTF8, "application/json");
503+
504+
var url = $"https://api.github.com/repos/{forkedRepo.Owner.Login}/{forkedRepo.Name}/merge-upstream";
505+
var response = await httpClient.PostAsync(url, content);
506+
response.EnsureSuccessStatusCode();
488507
}
489508
}
490509

0 commit comments

Comments
 (0)