Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 50 additions & 4 deletions src/apireview.net/Services/SummaryPublishingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public async Task<ApiReviewPublicationResult> PublishAsync(ApiReviewSummary summ
await UpdateCommentsAsync(summary);
}

string url = await CommitAsync(group, summary);
string url = await CommitOrCreatePullRequestAsync(group, summary);
await SendEmailAsync(group, summary);
return ApiReviewPublicationResult.Suceess(url);
}
Expand Down Expand Up @@ -163,7 +163,7 @@ private async Task UpdateCommentsDevAsync(ApiReviewSummary summary)
}
}

private async Task<string> CommitAsync(RepositoryGroup group, ApiReviewSummary summary)
private async Task<string> CommitOrCreatePullRequestAsync(RepositoryGroup group, ApiReviewSummary summary)
{
(string owner, string repo) = group.NotesRepo;
string branch = ApiReviewConstants.ApiReviewsBranch;
Expand Down Expand Up @@ -199,14 +199,60 @@ private async Task<string> CommitAsync(RepositoryGroup group, ApiReviewSummary s
NewCommit newCommit = new NewCommit(commitMessage, newTreeResponse.Sha, latestCommit.Sha);
Commit? newCommitResponse = await github.Git.Commit.Create(owner, repo, newCommit);

ReferenceUpdate newReference = new ReferenceUpdate(newCommitResponse.Sha);
await github.Git.Reference.Update(owner, repo, head, newReference);
try
{
ReferenceUpdate newReference = new ReferenceUpdate(newCommitResponse.Sha);
await github.Git.Reference.Update(owner, repo, head, newReference);
}
catch (Exception ex)
{
_logger.LogWarning(ex, "Direct commit to {Owner}/{Repo} on {Branch} failed; creating pull request instead.", owner, repo, branch);
return await CreatePullRequestAsync(
github,
owner,
repo,
branch,
group,
date,
commitMessage,
latestCommit.Sha,
newCommitResponse.Sha);
}
}

string url = $"https://github.com/{owner}/{repo}/blob/{branch}/{path}";
return url;
}

private static async Task<string> CreatePullRequestAsync(
GitHubClient github,
string owner,
string repo,
string targetBranch,
RepositoryGroup group,
DateTime date,
string commitMessage,
string baseCommitSha,
string newCommitSha)
{
string prBranch = $"apireview/{date:yyyy-MM-dd}-{group.NotesSuffix}-{newCommitSha[..7]}";
string prHead = $"refs/heads/{prBranch}";
string prBody = $"This PR was created automatically because updating `{targetBranch}` directly failed.\n\nIt adds the generated API review notes for {date:d}.";

await github.Git.Reference.Create(owner, repo, new NewReference(prHead, baseCommitSha));

ReferenceUpdate prReferenceUpdate = new ReferenceUpdate(newCommitSha);
await github.Git.Reference.Update(owner, repo, prHead, prReferenceUpdate);

NewPullRequest pullRequest = new NewPullRequest(commitMessage, prBranch, targetBranch)
{
Body = prBody
};

PullRequest pr = await github.PullRequest.Create(owner, repo, pullRequest);
return pr.HtmlUrl;
}

private static string GetMarkdown(ApiReviewSummary summary)
{
StringWriter noteWriter = new StringWriter();
Expand Down
Loading