Skip to content
Open
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,13 @@ repoSync.AddTargetRepository(new(
branch: "master"));

// Run the sync
await repoSync.Sync(syncOutput: SyncOutput.MergePullRequest);
await repoSync.Sync(
pullRequestTitle: "Pull request title",
branchName: "GitBranchName",
commitMessage: "Commit message",
syncOutput: SyncOutput.MergePullRequest);
```
<sup><a href='/src/Tests/Snippets.cs#L5-L42' title='Snippet source file'>snippet source</a> | <a href='#snippet-usage' title='Start of snippet'>anchor</a></sup>
<sup><a href='/src/Tests/Snippets.cs#L5-L46' title='Snippet source file'>snippet source</a> | <a href='#snippet-usage' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


Expand Down
6 changes: 5 additions & 1 deletion src/GitHubSync.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ static Task SyncRepository(Context context, Repository targetRepository, ICreden
syncOutput = SyncOutput.MergePullRequest;
}

return sync.Sync(syncOutput);
return sync.Sync(
$"GitHubSync update - {targetRepository.Branch}",
$"GitHubSync-{DateTimeOffset.UtcNow:yyyyMMdd-HHmmss}",
$"GitHubSync update - {targetRepository.Branch}",
syncOutput);
}

static RepositoryInfo BuildInfo(string url, string branch, ICredentials credentials)
Expand Down
7 changes: 4 additions & 3 deletions src/GitHubSync/GitHubGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,9 @@ IDictionary<string, IList<string>> CacheFor<T>()
return dic;
}

public async Task<string> CreateCommit(string treeSha, string owner, string repo, string parentCommitSha, string branch)
public async Task<string> CreateCommit(string treeSha, string owner, string repo, string parentCommitSha, string branch, string commitMessage)
{
var newCommit = new NewCommit($"GitHubSync update - {branch}", treeSha, [parentCommitSha]);
var newCommit = new NewCommit(commitMessage, treeSha, [parentCommitSha]);

var createdCommit = await client.Git.Commit.Create(owner, repo, newCommit);

Expand Down Expand Up @@ -383,10 +383,11 @@ public async Task<int> CreatePullRequest(
string repository,
string branch,
string targetBranch,
string title,
bool merge,
string? description)
{
var newPullRequest = new NewPullRequest($"GitHubSync update - {targetBranch}", branch, targetBranch);
var newPullRequest = new NewPullRequest(title, branch, targetBranch);

if (!string.IsNullOrWhiteSpace(description))
{
Expand Down
4 changes: 2 additions & 2 deletions src/GitSync/GitProvider/IGitProviderGateway.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
Task<Tuple<Parts, ITreeResponse>?> TreeFrom(Parts source, bool throwsIfNotFound);
Task<Tuple<Parts, ITreeItem>?> BlobFrom(Parts source, bool throwsIfNotFound);
bool IsKnownBy<T>(string sha, string owner, string repository);
Task<string> CreateCommit(string treeSha, string owner, string repo, string parentCommitSha, string branch);
Task<string> CreateCommit(string treeSha, string owner, string repo, string parentCommitSha, string branch, string commitMessage);
Task<string> CreateTree(INewTree newTree, string owner, string repo);
Task CreateBlob(string owner, string repository, string sha);
Task FetchBlob(string owner, string repository, string sha);
Task<string> CreateBranch(string owner, string repository, string branchName, string commitSha);
Task<int> CreatePullRequest(string owner, string repository, string branch, string targetBranch, bool merge, string? description);
Task<int> CreatePullRequest(string owner, string repository, string branch, string targetBranch, string title, bool merge, string? description);
Task<IReadOnlyList<ILabel>> ApplyLabels(string owner, string repository, int issueNumber, string[] labels);
INewTree CreateNewTree(string? path);
}
6 changes: 3 additions & 3 deletions src/GitSync/RepoSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ void ProcessItem(string item, List<SyncItem> itemsToSync, RepositoryInfo source)
itemsToSync.Add(new(parts, syncMode == SyncMode.IncludeAllByDefault, null));
}

public async Task<IReadOnlyList<UpdateResult>> Sync(SyncOutput syncOutput = SyncOutput.CreatePullRequest)
public async Task<IReadOnlyList<UpdateResult>> Sync(string pullRequestTitle, string branchName, string commitMessage, SyncOutput syncOutput = SyncOutput.CreatePullRequest)
{
var list = new List<UpdateResult>();
foreach (var targetRepository in targets)
Expand All @@ -165,7 +165,7 @@ public async Task<IReadOnlyList<UpdateResult>> Sync(SyncOutput syncOutput = Sync
var targetRepositoryDisplayName = $"{targetRepository.Owner}/{targetRepository.Repository}";

using var syncer = new Syncer(targetRepository.Credentials, null, log);
if (!await syncer.CanSynchronize(targetRepository, syncOutput, targetRepository.Branch))
if (!await syncer.CanSynchronize(targetRepository, syncOutput, pullRequestTitle))
{
continue;
}
Expand All @@ -178,7 +178,7 @@ public async Task<IReadOnlyList<UpdateResult>> Sync(SyncOutput syncOutput = Sync
continue;
}

var sync = await syncer.Sync(syncContext.Diff, syncOutput, labelsToApplyOnPullRequests, syncContext.Description, skipCollaboratorCheck);
var sync = await syncer.Sync(syncContext.Diff, syncOutput, branchName, commitMessage, pullRequestTitle, labelsToApplyOnPullRequests, syncContext.Description, skipCollaboratorCheck);
if (sync.Count == 0)
{
log($"Repo {targetRepositoryDisplayName} is in sync");
Expand Down
31 changes: 17 additions & 14 deletions src/GitSync/Syncer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
class Syncer :
IDisposable
{
const string pullRequestTitle = "GitHubSync update";

IGitProviderGateway gateway;
Action<string> log;
ICredentials credentials;
Expand Down Expand Up @@ -64,11 +62,11 @@ internal async Task<Mapper> Diff(Mapper input)
return outMapper;
}

internal async Task<bool> CanSynchronize(RepositoryInfo targetRepository, SyncOutput expectedOutput, string branch)
internal async Task<bool> CanSynchronize(RepositoryInfo targetRepository, SyncOutput expectedOutput, string pullRequestTitle)
{
if (expectedOutput is SyncOutput.CreatePullRequest or SyncOutput.MergePullRequest)
{
var hasOpenPullRequests = await gateway.HasOpenPullRequests(targetRepository.Owner, targetRepository.Repository, $"{pullRequestTitle} - {branch}");
var hasOpenPullRequests = await gateway.HasOpenPullRequests(targetRepository.Owner, targetRepository.Repository, pullRequestTitle);
if (hasOpenPullRequests)
{
log("Cannot create pull request, there is an existing open pull request, close or merge that first");
Expand All @@ -82,6 +80,9 @@ internal async Task<bool> CanSynchronize(RepositoryInfo targetRepository, SyncOu
internal async Task<IReadOnlyList<UpdateResult>> Sync(
Mapper diff,
SyncOutput expectedOutput,
string branchName,
string commitMessage,
string pullRequestTitle,
IEnumerable<string>? labelsToApplyOnPullRequests = null,
string? description = null,
bool skipCollaboratorCheck = false)
Expand All @@ -102,7 +103,7 @@ internal async Task<IReadOnlyList<UpdateResult>> Sync(

foreach (var updatesPerOwnerRepositoryBranch in t.Values)
{
var updates = await ProcessUpdates(expectedOutput, updatesPerOwnerRepositoryBranch, labels, description, skipCollaboratorCheck);
var updates = await ProcessUpdates(expectedOutput, updatesPerOwnerRepositoryBranch, labels, description, skipCollaboratorCheck, branchName, pullRequestTitle, commitMessage);
results.Add(updates);
}

Expand All @@ -114,9 +115,11 @@ async Task<UpdateResult> ProcessUpdates(
IList<Tuple<Parts, IParts>> updatesPerOwnerRepositoryBranch,
string[] labels,
string? description,
bool skipCollaboratorCheck)
bool skipCollaboratorCheck,
string branchName,
string pullRequestTitle,
string commitMessage)
{
var branchName = $"GitHubSync-{DateTimeOffset.UtcNow:yyyyMMdd-HHmmss}";
var root = updatesPerOwnerRepositoryBranch.First().Item1.Root();

string commitSha;
Expand All @@ -125,7 +128,7 @@ async Task<UpdateResult> ProcessUpdates(
await gateway.IsCollaborator(root.Owner, root.Repository);
if (isCollaborator)
{
commitSha = await ProcessUpdatesInTargetRepository(root, updatesPerOwnerRepositoryBranch);
commitSha = await ProcessUpdatesInTargetRepository(root, updatesPerOwnerRepositoryBranch, commitMessage);
}
else
{
Expand All @@ -136,7 +139,7 @@ async Task<UpdateResult> ProcessUpdates(
throw new NotSupportedException($"User is not a collaborator, sync output '{expectedOutput}' is not supported, only creating PRs is supported");
}

commitSha = await ProcessUpdatesInFork(root, branchName, updatesPerOwnerRepositoryBranch);
commitSha = await ProcessUpdatesInFork(root, branchName, updatesPerOwnerRepositoryBranch, commitMessage);
}

if (expectedOutput == SyncOutput.CreateCommit)
Expand Down Expand Up @@ -168,7 +171,7 @@ async Task<UpdateResult> ProcessUpdates(
prSourceBranch = $"{forkedRepository.Owner.Login}:{prSourceBranch}";
}

var prNumber = await gateway.CreatePullRequest(root.Owner, root.Repository, prSourceBranch, root.Branch, merge, description);
var prNumber = await gateway.CreatePullRequest(root.Owner, root.Repository, prSourceBranch, root.Branch, pullRequestTitle, merge, description);

if (isCollaborator)
{
Expand All @@ -181,7 +184,7 @@ async Task<UpdateResult> ProcessUpdates(
throw new NotSupportedException();
}

async Task<string> ProcessUpdatesInTargetRepository(Parts root, IList<Tuple<Parts, IParts>> updatesPerOwnerRepositoryBranch)
async Task<string> ProcessUpdatesInTargetRepository(Parts root, IList<Tuple<Parts, IParts>> updatesPerOwnerRepositoryBranch, string commitMessage)
{
var tt = new TargetTree(root);

Expand Down Expand Up @@ -209,11 +212,11 @@ async Task<string> ProcessUpdatesInTargetRepository(Parts root, IList<Tuple<Part

var parentCommit = await gateway.RootCommitFrom(root);

var commitSha = await gateway.CreateCommit(btt, root.Owner, root.Repository, parentCommit.Sha, root.Branch);
var commitSha = await gateway.CreateCommit(btt, root.Owner, root.Repository, parentCommit.Sha, root.Branch, commitMessage);
return commitSha;
}

async Task<string> ProcessUpdatesInFork(Parts root, string temporaryBranchName, IList<Tuple<Parts, IParts>> updatesPerOwnerRepositoryBranch)
async Task<string> ProcessUpdatesInFork(Parts root, string temporaryBranchName, IList<Tuple<Parts, IParts>> updatesPerOwnerRepositoryBranch, string commitMessage)
{
var forkedRepository = await gateway.Fork(root.Owner, root.Repository);

Expand Down Expand Up @@ -290,7 +293,7 @@ async Task<string> ProcessUpdatesInFork(Parts root, string temporaryBranchName,
LibGit2Sharp.Commands.Stage(repository, "*");

// Step 7: create & push commit
var commit = repository.Commit("Apply GitHubSync changes", commitSignature, commitSignature,
var commit = repository.Commit(commitMessage, commitSignature, commitSignature,
new());

repository.Network.Push(tempBranch, new()
Expand Down
22 changes: 18 additions & 4 deletions src/Tests/RepoSyncTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ public async Task SyncPrIncludeAllByDefault()
repoSync.RemoveBlob("README.md");
repoSync.AddTargetRepository(new(credentials, Client.RepositoryOwner, "GitHubSync.TestRepository", repoContext.TempBranchName));

var sync = await repoSync.Sync();
var sync = await repoSync.Sync(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldnt these be in new tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you mean to make the GitHubSync* messages the defaults for prTitle, branchName, & commitMessage? or create new tests which don't use the previous defaults?

$"GitHubSync update - {repoContext.TempBranchName}",
$"GitHubSync-{DateTimeOffset.UtcNow:yyyyMMdd-HHmmss}",
$"GitHubSync update - {repoContext.TempBranchName}");
await repoContext.VerifyPullRequest(sync.Single());
}

Expand All @@ -28,7 +31,10 @@ public async Task SyncPrExcludeAllByDefault()
repoSync.AddSourceItem(TreeEntryTargetType.Blob,"sourceFile.txt", "nested/sourceFile.txt");
repoSync.AddTargetRepository(new(credentials, Client.RepositoryOwner, "GitHubSync.TestRepository", repoContext.TempBranchName));

var sync = await repoSync.Sync();
var sync = await repoSync.Sync(
$"GitHubSync update - {repoContext.TempBranchName}",
$"GitHubSync-{DateTimeOffset.UtcNow:yyyyMMdd-HHmmss}",
$"GitHubSync update - {repoContext.TempBranchName}");
await repoContext.VerifyPullRequest(sync.Single());
}

Expand All @@ -42,7 +48,11 @@ public async Task SyncPrMerge()
repoSync.RemoveBlob("README.md");
repoSync.AddTargetRepository(new(credentials, Client.RepositoryOwner, "GitHubSync.TestRepository", repoContext.TempBranchName));

var sync = await repoSync.Sync(SyncOutput.MergePullRequest);
var sync = await repoSync.Sync(
$"GitHubSync update - {repoContext.TempBranchName}",
$"GitHubSync-{DateTimeOffset.UtcNow:yyyyMMdd-HHmmss}",
$"GitHubSync update - {repoContext.TempBranchName}",
SyncOutput.MergePullRequest);
await repoContext.VerifyPullRequest(sync.Single());
}

Expand All @@ -57,7 +67,11 @@ public async Task SyncCommit()
repoSync.RemoveBlob("README.md");
repoSync.AddTargetRepository(new(credentials, Client.RepositoryOwner, "GitHubSync.TestRepository", repoContext.TempBranchName));

var sync = await repoSync.Sync(SyncOutput.CreateCommit);
var sync = await repoSync.Sync(
$"GitHubSync update - {repoContext.TempBranchName}",
$"GitHubSync-{DateTimeOffset.UtcNow:yyyyMMdd-HHmmss}",
$"GitHubSync update - {repoContext.TempBranchName}",
SyncOutput.CreateCommit);
await repoContext.VerifyCommit(sync.Single());
}

Expand Down
6 changes: 5 additions & 1 deletion src/Tests/Snippets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ public static async Task SyncPr(ICredentials octokitCredentials)
branch: "master"));

// Run the sync
await repoSync.Sync(syncOutput: SyncOutput.MergePullRequest);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can this be part of a new snippet

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should prTitle, branchName, & commitMessage for the Sync method in the RepoSync class be optional params with default values?

await repoSync.Sync(
pullRequestTitle: "Pull request title",
branchName: "GitBranchName",
commitMessage: "Commit message",
syncOutput: SyncOutput.MergePullRequest);

#endregion
}
Expand Down