Skip to content

Commit

Permalink
[feat]: Add RenameBranch method to RepositoryBranchesClient (#2799)
Browse files Browse the repository at this point in the history
* added RenameBranch method to RepositoryBranchesClient

* revert forced version

* tabs -> spaces :(

---------

Co-authored-by: Nick Floyd <139819+nickfloyd@users.noreply.github.com>
Co-authored-by: Keegan Campbell <me@kfcampbell.com>
  • Loading branch information
3 people authored Oct 30, 2023
1 parent 35d2ee4 commit 335632b
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 22 deletions.
25 changes: 18 additions & 7 deletions Octokit.Reactive/Clients/IObservableRepositoryBranchesClient.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace Octokit.Reactive
{
/// <summary>
/// A client for GitHub's Repository Branches API.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/branches">Repository Branches API documentation</a> for more details.
/// </remarks>
/// <summary>
/// A client for GitHub's Repository Branches API.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/branches">Repository Branches API documentation</a> for more details.
/// </remarks>
public interface IObservableRepositoryBranchesClient
{
/// <summary>
Expand Down Expand Up @@ -644,5 +643,17 @@ public interface IObservableRepositoryBranchesClient
/// <param name="branch">The name of the branch</param>
/// <param name="users">List of users with push access to remove</param>
IObservable<User> DeleteProtectedBranchUserRestrictions(long repositoryId, string branch, BranchProtectionUserCollection users);

/// <summary>
/// Renames a branch in a repository
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/rest/branches/branches?apiVersion=2022-11-28#rename-a-branch">API documentation</a> for more details
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="repository">The name of the repository</param>
/// <param name="branch">The name of the branch to rename</param>
/// <param name="newName">The new name of the branch</param>
IObservable<Branch> RenameBranch(string owner, string repository, string branch, string newName);
}
}
28 changes: 25 additions & 3 deletions Octokit.Reactive/Clients/ObservableRepositoryBranchesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using System.Diagnostics.CodeAnalysis;
using System.Reactive.Linq;
using System.Reactive.Threading.Tasks;
using System.Threading.Tasks;

using Octokit.Reactive.Internal;

namespace Octokit.Reactive
Expand Down Expand Up @@ -297,7 +299,7 @@ public IObservable<BranchProtectionRequiredStatusChecks> UpdateRequiredStatusChe
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="name">The name of the repository</param>
/// <param name="branch">The name of the branch</param>
/// <param name="branch">The name of the branch</param>
public IObservable<bool> DeleteRequiredStatusChecks(string owner, string name, string branch)
{
Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner));
Expand All @@ -314,7 +316,7 @@ public IObservable<bool> DeleteRequiredStatusChecks(string owner, string name, s
/// See the <a href="https://developer.github.com/v3/repos/branches/#remove-required-status-checks-of-protected-branch">API documentation</a> for more details
/// </remarks>
/// <param name="repositoryId">The Id of the repository</param>
/// <param name="branch">The name of the branch</param>
/// <param name="branch">The name of the branch</param>
public IObservable<bool> DeleteRequiredStatusChecks(long repositoryId, string branch)
{
Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch));
Expand Down Expand Up @@ -1021,5 +1023,25 @@ public IObservable<User> DeleteProtectedBranchUserRestrictions(long repositoryId

return _client.DeleteProtectedBranchUserRestrictions(repositoryId, branch, users).ToObservable().SelectMany(x => x);
}
}

/// <summary>
/// Renames a branch in a repository
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/rest/branches/branches?apiVersion=2022-11-28#rename-a-branch">API documentation</a> for more details
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="repository">The name of the repository</param>
/// <param name="branch">The name of the branch to rename</param>
/// <param name="newName">The new name of the branch</param>
public IObservable<Branch> RenameBranch(string owner, string repository, string branch, string newName)
{
Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(repository, nameof(repository));
Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch));
Ensure.ArgumentNotNullOrEmptyString(newName, nameof(newName));

return _client.RenameBranch(owner, repository, branch, newName).ToObservable();
}
}
}
47 changes: 46 additions & 1 deletion Octokit.Tests/Clients/RepositoryBranchesClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1399,5 +1399,50 @@ public async Task EnsuresNonNullArguments()
await Assert.ThrowsAsync<ArgumentException>(() => client.DeleteProtectedBranchUserRestrictions(1, "", usersToRemove));
}
}
}

public class TheRenameBranchMethod
{
[Fact]
public void RequestsTheCorrectUrl()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryBranchesClient(connection);

client.RenameBranch("owner", "repo", "branch", "new_name");

connection.Received()
.Post<Branch>(Arg.Is<Uri>(u => u.ToString() == "repos/owner/repo/branches/branch/rename"), Arg.Any<object>());
}

[Fact]
public async Task PassesTheCorrectNewBranchParameter()
{
var connection = Substitute.For<IApiConnection>();
var client = new RepositoryBranchesClient(connection);
var newBranch = "a";

await client.RenameBranch("owner", "repo", "branch", newBranch);

connection.Received().Post<Branch>(
Arg.Any<Uri>(),
Arg.Is<object>(o => o.GetType().GetProperty("new_name").GetValue(o).ToString() == newBranch));
}

[Fact]
public async Task EnsuresNonNullArguments()
{
var client = new RepositoryBranchesClient(Substitute.For<IApiConnection>());

await Assert.ThrowsAsync<ArgumentNullException>(() => client.RenameBranch(null, "repo", "branch", "new_name"));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.RenameBranch("owner", null, "branch", "new_name"));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.RenameBranch("owner", "repo", null, "new_name"));
await Assert.ThrowsAsync<ArgumentNullException>(() => client.RenameBranch("owner", "repo", "branch", null));

await Assert.ThrowsAsync<ArgumentException>(() => client.RenameBranch("", "repo", "branch", "new_name"));
await Assert.ThrowsAsync<ArgumentException>(() => client.RenameBranch("owner", "", "branch", "new_name"));
await Assert.ThrowsAsync<ArgumentException>(() => client.RenameBranch("owner", "repo", "", "new_name"));
await Assert.ThrowsAsync<ArgumentException>(() => client.RenameBranch("owner", "repo", "branch", ""));
}
}
}
}
12 changes: 12 additions & 0 deletions Octokit/Clients/IRepositoryBranchesClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -652,5 +652,17 @@ public interface IRepositoryBranchesClient
/// <param name="branch">The name of the branch</param>
/// <param name="users">List of users with push access to remove</param>
Task<IReadOnlyList<User>> DeleteProtectedBranchUserRestrictions(long repositoryId, string branch, BranchProtectionUserCollection users);

/// <summary>
/// Renames a branch in a repository
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/rest/branches/branches?apiVersion=2022-11-28#rename-a-branch">API documentation</a> for more details
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="repository">The name of the repository</param>
/// <param name="branch">The name of the branch to rename</param>
/// <param name="newName">The new name of the branch</param>
Task<Branch> RenameBranch(string owner, string repository, string branch, string newName);
}
}
34 changes: 27 additions & 7 deletions Octokit/Clients/RepositoryBranchesClient.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;

namespace Octokit
{
/// <summary>
/// A client for GitHub's Repository Branches API.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/branches">Repository Branches API documentation</a> for more details.
/// </remarks>
/// <summary>
/// A client for GitHub's Repository Branches API.
/// </summary>
/// <remarks>
/// See the <a href="https://developer.github.com/v3/repos/branches">Repository Branches API documentation</a> for more details.
/// </remarks>
public class RepositoryBranchesClient : ApiClient, IRepositoryBranchesClient
{
/// <summary>
Expand Down Expand Up @@ -1177,5 +1176,26 @@ public Task<IReadOnlyList<User>> DeleteProtectedBranchUserRestrictions(long repo

return ApiConnection.Delete<IReadOnlyList<User>>(ApiUrls.RepoRestrictionsUsers(repositoryId, branch), users);
}

/// <summary>
/// Renames a branch in a repository
/// </summary>
/// <remarks>
/// See the <a href="https://docs.github.com/en/rest/branches/branches?apiVersion=2022-11-28#rename-a-branch">API documentation</a> for more details
/// </remarks>
/// <param name="owner">The owner of the repository</param>
/// <param name="repository">The name of the repository</param>
/// <param name="branch">The name of the branch to rename</param>
/// <param name="newName">The new name of the branch</param>
[ManualRoute("POST", "/repos/{owner}/{repo}/branches/{branch}/rename")]
public Task<Branch> RenameBranch(string owner, string repository, string branch, string newName)
{
Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner));
Ensure.ArgumentNotNullOrEmptyString(repository, nameof(repository));
Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch));
Ensure.ArgumentNotNullOrEmptyString(newName, nameof(newName));

return ApiConnection.Post<Branch>(ApiUrls.RepositoryBranchRename(owner, repository, branch), new { new_name = newName });
}
}
}
18 changes: 15 additions & 3 deletions Octokit/Helpers/ApiUrls.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5506,7 +5506,7 @@ public static Uri CodespaceStop(string codespaceName)
{
return "user/codespaces/{0}/stop".FormatUri(codespaceName);
}

/// <summary>
/// Returns the <see cref="Uri"/> that lists the artifacts for a repository.
/// </summary>
Expand All @@ -5517,7 +5517,7 @@ public static Uri ListArtifacts(string owner, string repository)
{
return "repos/{0}/{1}/actions/artifacts".FormatUri(owner, repository);
}

/// <summary>
/// Returns the <see cref="Uri"/> for the specified artifact.
/// </summary>
Expand All @@ -5542,7 +5542,7 @@ public static Uri DownloadArtifact(string owner, string repository, long artifac
{
return "repos/{0}/{1}/actions/artifacts/{2}/{3}".FormatUri(owner, repository, artifactId, archiveFormat);
}

/// <summary>
/// Returns the <see cref="Uri"/> to list the artifacts for a workflow.
/// </summary>
Expand All @@ -5554,5 +5554,17 @@ public static Uri ListWorkflowArtifacts(string owner, string repository, long ru
{
return "repos/{0}/{1}/actions/runs/{2}/artifacts".FormatUri(owner, repository, runId);
}

/// <summary>
/// Returns the <see cref="Uri"/> to rename a repository branch.
/// </summary>
/// <param name="owner">The owner of the repository</param>
/// <param name="repository">The name of the repository</param>
/// <param name="branch">The name of the branch to rename</param>
/// <returns></returns>
public static Uri RepositoryBranchRename(string owner, string repository, string branch)
{
return "repos/{0}/{1}/branches/{2}/rename".FormatUri(owner, repository, branch);
}
}
}
2 changes: 1 addition & 1 deletion build/Utilities/BuildVersion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static BuildVersion Calculate(Context context)

// Run in interactive mode to get the properties for the rest of the script
var assertedversions = GitVersionRunner.Run(context, GitVersionOutput.Json);

version = assertedversions.MajorMinorPatch;
semVersion = assertedversions.LegacySemVerPadded;
fullSemVer = assertedversions.FullSemVer;
Expand Down

0 comments on commit 335632b

Please sign in to comment.