Skip to content

Commit

Permalink
Merge pull request #1477 from TattsGroup/repo-merge-options
Browse files Browse the repository at this point in the history
Add repository settings to configure PR merge methods
  • Loading branch information
shiftkey authored Oct 4, 2016
2 parents 38f8da2 + de3e809 commit feb7561
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 20 deletions.
82 changes: 82 additions & 0 deletions Octokit.Tests.Integration/Clients/RepositoriesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,58 @@ public async Task UpdatesHasWikiWithRepositoryId()
Assert.Equal(false, _repository.HasWiki);
}

[IntegrationTest]
public async Task UpdatesMergeMethod()
{
var github = Helper.GetAuthenticatedClient();

using (var context = await github.CreateRepositoryContext("public-repo"))
{
var updateRepository = new RepositoryUpdate(context.RepositoryName)
{
AllowMergeCommit = false,
AllowSquashMerge = false,
AllowRebaseMerge = true
};

var editedRepository = await github.Repository.Edit(context.RepositoryOwner, context.RepositoryName, updateRepository);
Assert.False(editedRepository.AllowMergeCommit);
Assert.False(editedRepository.AllowSquashMerge);
Assert.True(editedRepository.AllowRebaseMerge);

var repository = await github.Repository.Get(context.RepositoryOwner, context.RepositoryName);
Assert.False(repository.AllowMergeCommit);
Assert.False(repository.AllowSquashMerge);
Assert.True(repository.AllowRebaseMerge);
}
}

[IntegrationTest]
public async Task UpdatesMergeMethodWithRepositoryId()
{
var github = Helper.GetAuthenticatedClient();

using (var context = await github.CreateRepositoryContext("public-repo"))
{
var updateRepository = new RepositoryUpdate(context.RepositoryName)
{
AllowMergeCommit = true,
AllowSquashMerge = true,
AllowRebaseMerge = false
};

var editedRepository = await github.Repository.Edit(context.RepositoryId, updateRepository);
Assert.True(editedRepository.AllowMergeCommit);
Assert.True(editedRepository.AllowSquashMerge);
Assert.False(editedRepository.AllowRebaseMerge);

var repository = await github.Repository.Get(context.RepositoryId);
Assert.True(repository.AllowMergeCommit);
Assert.True(repository.AllowSquashMerge);
Assert.False(repository.AllowRebaseMerge);
}
}

public void Dispose()
{
Helper.DeleteRepo(Helper.GetAuthenticatedClient().Connection, _repository);
Expand Down Expand Up @@ -664,6 +716,36 @@ public async Task ReturnsForkedRepositoryWithRepositoryId()
Assert.Equal("https://github.com/Haacked/libgit2sharp.git", repository.CloneUrl);
Assert.True(repository.Fork);
}

[IntegrationTest]
public async Task ReturnsRepositoryMergeOptions()
{
var github = Helper.GetAuthenticatedClient();

using (var context = await github.CreateRepositoryContext(Helper.MakeNameWithTimestamp("public-repo")))
{
var repository = await github.Repository.Get(context.RepositoryOwner, context.RepositoryName);

Assert.NotNull(repository.AllowRebaseMerge);
Assert.NotNull(repository.AllowSquashMerge);
Assert.NotNull(repository.AllowMergeCommit);
}
}

[IntegrationTest]
public async Task ReturnsRepositoryMergeOptionsWithRepositoryId()
{
var github = Helper.GetAuthenticatedClient();

using (var context = await github.CreateRepositoryContext(Helper.MakeNameWithTimestamp("public-repo")))
{
var repository = await github.Repository.Get(context.RepositoryId);

Assert.NotNull(repository.AllowRebaseMerge);
Assert.NotNull(repository.AllowSquashMerge);
Assert.NotNull(repository.AllowMergeCommit);
}
}
}

public class TheGetAllPublicMethod
Expand Down
18 changes: 12 additions & 6 deletions Octokit.Tests/Clients/RepositoriesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,10 @@ public async Task RequestsCorrectUrl()

await client.Get("owner", "name");

connection.Received().Get<Repository>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/name"));
connection.Received().Get<Repository>(
Arg.Is<Uri>(u => u.ToString() == "repos/owner/name"),
null,
"application/vnd.github.polaris-preview+json");
}

[Fact]
Expand All @@ -269,7 +272,10 @@ public async Task RequestsCorrectUrlWithRepositoryId()

await client.Get(1);

connection.Received().Get<Repository>(Arg.Is<Uri>(u => u.ToString() == "repositories/1"));
connection.Received().Get<Repository>(
Arg.Is<Uri>(u => u.ToString() == "repositories/1"),
null,
"application/vnd.github.polaris-preview+json");
}

[Fact]
Expand Down Expand Up @@ -1009,25 +1015,25 @@ public void PatchesCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoriesClient(connection);
var update = new RepositoryUpdate();
var update = new RepositoryUpdate("repo");

client.Edit("owner", "repo", update);

connection.Received()
.Patch<Repository>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/repo"), Arg.Any<RepositoryUpdate>());
.Patch<Repository>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/repo"), Arg.Any<RepositoryUpdate>(), "application/vnd.github.polaris-preview+json");
}

[Fact]
public void PatchesCorrectUrlWithRepositoryId()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoriesClient(connection);
var update = new RepositoryUpdate();
var update = new RepositoryUpdate("repo");

client.Edit(1, update);

connection.Received()
.Patch<Repository>(Arg.Is<Uri>(u => u.ToString() == "repositories/1"), Arg.Any<RepositoryUpdate>());
.Patch<Repository>(Arg.Is<Uri>(u => u.ToString() == "repositories/1"), Arg.Any<RepositoryUpdate>(), "application/vnd.github.polaris-preview+json");
}

[Fact]
Expand Down
16 changes: 8 additions & 8 deletions Octokit.Tests/Reactive/ObservableRepositoriesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,18 @@ public async Task IsALukeWarmObservable()
var response = Task.Factory.StartNew<IApiResponse<Repository>>(() =>
new ApiResponse<Repository>(new Response(), repository));
var connection = Substitute.For<IConnection>();
connection.Get<Repository>(Args.Uri, null, null).Returns(response);
connection.Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json").Returns(response);
var gitHubClient = new GitHubClient(connection);
var client = new ObservableRepositoriesClient(gitHubClient);
var observable = client.Get("stark", "ned");

connection.Received(1).Get<Repository>(Args.Uri, null, null);
connection.Received(1).Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json");

var result = await observable;
connection.Received(1).Get<Repository>(Args.Uri, null, null);
connection.Received(1).Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json");
var result2 = await observable;
// TODO: If we change this to a warm observable, we'll need to change this to Received(2)
connection.Received(1).Get<Repository>(Args.Uri, null, null);
connection.Received(1).Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json");

Assert.Same(repository, result);
Assert.Same(repository, result2);
Expand All @@ -117,18 +117,18 @@ public async Task IsALukeWarmObservableWithRepositoryId()
var response = Task.Factory.StartNew<IApiResponse<Repository>>(() =>
new ApiResponse<Repository>(new Response(), repository));
var connection = Substitute.For<IConnection>();
connection.Get<Repository>(Args.Uri, null, null).Returns(response);
connection.Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json").Returns(response);
var gitHubClient = new GitHubClient(connection);
var client = new ObservableRepositoriesClient(gitHubClient);
var observable = client.Get(1);

connection.Received(1).Get<Repository>(Args.Uri, null, null);
connection.Received(1).Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json");

var result = await observable;
connection.Received(1).Get<Repository>(Args.Uri, null, null);
connection.Received(1).Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json");
var result2 = await observable;
// TODO: If we change this to a warm observable, we'll need to change this to Received(2)
connection.Received(1).Get<Repository>(Args.Uri, null, null);
connection.Received(1).Get<Repository>(Args.Uri, null, "application/vnd.github.polaris-preview+json");

Assert.Same(repository, result);
Assert.Same(repository, result2);
Expand Down
9 changes: 5 additions & 4 deletions Octokit/Clients/RepositoriesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ public Task<Repository> Edit(string owner, string name, RepositoryUpdate update)
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");
Ensure.ArgumentNotNull(update, "update");
Ensure.ArgumentNotNull(update.Name, "update.Name");

return ApiConnection.Patch<Repository>(ApiUrls.Repository(owner, name), update);
return ApiConnection.Patch<Repository>(ApiUrls.Repository(owner, name), update, AcceptHeaders.SquashCommitPreview);
}

/// <summary>
Expand All @@ -206,7 +207,7 @@ public Task<Repository> Edit(long repositoryId, RepositoryUpdate update)
{
Ensure.ArgumentNotNull(update, "update");

return ApiConnection.Patch<Repository>(ApiUrls.Repository(repositoryId), update);
return ApiConnection.Patch<Repository>(ApiUrls.Repository(repositoryId), update, AcceptHeaders.SquashCommitPreview);
}

/// <summary>
Expand Down Expand Up @@ -259,7 +260,7 @@ public Task<Repository> Get(string owner, string name)
Ensure.ArgumentNotNullOrEmptyString(owner, "owner");
Ensure.ArgumentNotNullOrEmptyString(name, "name");

return ApiConnection.Get<Repository>(ApiUrls.Repository(owner, name));
return ApiConnection.Get<Repository>(ApiUrls.Repository(owner, name), null, AcceptHeaders.SquashCommitPreview);
}

/// <summary>
Expand All @@ -273,7 +274,7 @@ public Task<Repository> Get(string owner, string name)
/// <returns>A <see cref="Repository"/></returns>
public Task<Repository> Get(long repositoryId)
{
return ApiConnection.Get<Repository>(ApiUrls.Repository(repositoryId));
return ApiConnection.Get<Repository>(ApiUrls.Repository(repositoryId), null, AcceptHeaders.SquashCommitPreview);
}

/// <summary>
Expand Down
42 changes: 41 additions & 1 deletion Octokit/Models/Request/RepositoryUpdate.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics;
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;

Expand All @@ -11,38 +12,77 @@ namespace Octokit
[DebuggerDisplay("{DebuggerDisplay,nq}")]
public class RepositoryUpdate
{
[Obsolete("Please use the ctor RepositoryUpdate(string name) as Name is a required field")]
public RepositoryUpdate()
{

}

/// <summary>
/// Creates an object that describes an update to a repository on GitHub.
/// </summary>
/// <param name="name">The name of the repository. This is the only required parameter.</param>
public RepositoryUpdate(string name)
{
Ensure.ArgumentNotNullOrEmptyString(name, "name");

Name = name;
}

/// <summary>
/// Required. Gets or sets the repository name.
/// </summary>
public string Name { get; set; }

/// <summary>
/// Optional. Gets or sets the repository description. The default is null (do not update)
/// </summary>
public string Description { get; set; }

/// <summary>
/// Optional. Gets or sets the repository homepage url. The default is null (do not update).
/// </summary>
public string Homepage { get; set; }

/// <summary>
/// Gets or sets whether to make the repository private. The default is null (do not update).
/// </summary>
public bool? Private { get; set; }

/// <summary>
/// Gets or sets whether to enable issues for the repository. The default is null (do not update).
/// </summary>
public bool? HasIssues { get; set; }

/// <summary>
/// Optional. Gets or sets whether to enable the wiki for the repository. The default is null (do not update).
/// </summary>
public bool? HasWiki { get; set; }

/// <summary>
/// Optional. Gets or sets whether to enable downloads for the repository. The default is null (do not update).
/// </summary>
public bool? HasDownloads { get; set; }

/// <summary>
/// Optional. Gets or sets the default branch. The default is null (do not update).
/// </summary>
public string DefaultBranch { get; set; }

/// <summary>
/// Optional. Allows the "Rebase and Merge" method to be used.
/// </summary>
public bool? AllowRebaseMerge { get; set; }

/// <summary>
/// Optional. Allows the "Squash Merge" merge method to be used.
/// </summary>
public bool? AllowSquashMerge { get; set; }

/// <summary>
/// Optional. Allows the "Create a merge commit" merge method to be used.
/// </summary>
public bool? AllowMergeCommit { get; set; }

[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
internal string DebuggerDisplay
Expand Down
11 changes: 10 additions & 1 deletion Octokit/Models/Response/Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public Repository(long id)
Id = id;
}

public Repository(string url, string htmlUrl, string cloneUrl, string gitUrl, string sshUrl, string svnUrl, string mirrorUrl, long id, User owner, string name, string fullName, string description, string homepage, string language, bool @private, bool fork, int forksCount, int stargazersCount, string defaultBranch, int openIssuesCount, DateTimeOffset? pushedAt, DateTimeOffset createdAt, DateTimeOffset updatedAt, RepositoryPermissions permissions, Repository parent, Repository source, bool hasIssues, bool hasWiki, bool hasDownloads)
public Repository(string url, string htmlUrl, string cloneUrl, string gitUrl, string sshUrl, string svnUrl, string mirrorUrl, long id, User owner, string name, string fullName, string description, string homepage, string language, bool @private, bool fork, int forksCount, int stargazersCount, string defaultBranch, int openIssuesCount, DateTimeOffset? pushedAt, DateTimeOffset createdAt, DateTimeOffset updatedAt, RepositoryPermissions permissions, Repository parent, Repository source, bool hasIssues, bool hasWiki, bool hasDownloads, bool? allowRebaseMerge, bool? allowSquashMerge, bool? allowMergeCommit)
{
Url = url;
HtmlUrl = htmlUrl;
Expand Down Expand Up @@ -45,6 +45,9 @@ public Repository(string url, string htmlUrl, string cloneUrl, string gitUrl, st
HasIssues = hasIssues;
HasWiki = hasWiki;
HasDownloads = hasDownloads;
AllowRebaseMerge = allowRebaseMerge;
AllowSquashMerge = allowSquashMerge;
AllowMergeCommit = allowMergeCommit;
}

public string Url { get; protected set; }
Expand Down Expand Up @@ -104,6 +107,12 @@ public Repository(string url, string htmlUrl, string cloneUrl, string gitUrl, st
public bool HasWiki { get; protected set; }

public bool HasDownloads { get; protected set; }

public bool? AllowRebaseMerge { get; protected set; }

public bool? AllowSquashMerge { get; protected set; }

public bool? AllowMergeCommit { get; protected set; }

internal string DebuggerDisplay
{
Expand Down

0 comments on commit feb7561

Please sign in to comment.