diff --git a/Octokit.Reactive/Clients/IObservableRepositoryBranchesClient.cs b/Octokit.Reactive/Clients/IObservableRepositoryBranchesClient.cs index eb9e41c26d..a37cb260e2 100644 --- a/Octokit.Reactive/Clients/IObservableRepositoryBranchesClient.cs +++ b/Octokit.Reactive/Clients/IObservableRepositoryBranchesClient.cs @@ -1,15 +1,14 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; - namespace Octokit.Reactive { - /// - /// A client for GitHub's Repository Branches API. - /// - /// - /// See the Repository Branches API documentation for more details. - /// + /// + /// A client for GitHub's Repository Branches API. + /// + /// + /// See the Repository Branches API documentation for more details. + /// public interface IObservableRepositoryBranchesClient { /// @@ -644,5 +643,17 @@ public interface IObservableRepositoryBranchesClient /// The name of the branch /// List of users with push access to remove IObservable DeleteProtectedBranchUserRestrictions(long repositoryId, string branch, BranchProtectionUserCollection users); + + /// + /// Renames a branch in a repository + /// + /// + /// See the API documentation for more details + /// + /// The owner of the repository + /// The name of the repository + /// The name of the branch to rename + /// The new name of the branch + IObservable RenameBranch(string owner, string repository, string branch, string newName); } } diff --git a/Octokit.Reactive/Clients/ObservableRepositoryBranchesClient.cs b/Octokit.Reactive/Clients/ObservableRepositoryBranchesClient.cs index ac87bb1b0d..8eea00feab 100644 --- a/Octokit.Reactive/Clients/ObservableRepositoryBranchesClient.cs +++ b/Octokit.Reactive/Clients/ObservableRepositoryBranchesClient.cs @@ -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 @@ -297,7 +299,7 @@ public IObservable UpdateRequiredStatusChe /// /// The owner of the repository /// The name of the repository - /// The name of the branch + /// The name of the branch public IObservable DeleteRequiredStatusChecks(string owner, string name, string branch) { Ensure.ArgumentNotNullOrEmptyString(owner, nameof(owner)); @@ -314,7 +316,7 @@ public IObservable DeleteRequiredStatusChecks(string owner, string name, s /// See the API documentation for more details /// /// The Id of the repository - /// The name of the branch + /// The name of the branch public IObservable DeleteRequiredStatusChecks(long repositoryId, string branch) { Ensure.ArgumentNotNullOrEmptyString(branch, nameof(branch)); @@ -1021,5 +1023,25 @@ public IObservable DeleteProtectedBranchUserRestrictions(long repositoryId return _client.DeleteProtectedBranchUserRestrictions(repositoryId, branch, users).ToObservable().SelectMany(x => x); } - } + + /// + /// Renames a branch in a repository + /// + /// + /// See the API documentation for more details + /// + /// The owner of the repository + /// The name of the repository + /// The name of the branch to rename + /// The new name of the branch + public IObservable 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(); + } + } } diff --git a/Octokit.Tests/Clients/RepositoryBranchesClientTests.cs b/Octokit.Tests/Clients/RepositoryBranchesClientTests.cs index 6d5160ec55..ed0aeb8918 100644 --- a/Octokit.Tests/Clients/RepositoryBranchesClientTests.cs +++ b/Octokit.Tests/Clients/RepositoryBranchesClientTests.cs @@ -1399,5 +1399,50 @@ public async Task EnsuresNonNullArguments() await Assert.ThrowsAsync(() => client.DeleteProtectedBranchUserRestrictions(1, "", usersToRemove)); } } - } + + public class TheRenameBranchMethod + { + [Fact] + public void RequestsTheCorrectUrl() + { + var connection = Substitute.For(); + var client = new RepositoryBranchesClient(connection); + + client.RenameBranch("owner", "repo", "branch", "new_name"); + + connection.Received() + .Post(Arg.Is(u => u.ToString() == "repos/owner/repo/branches/branch/rename"), Arg.Any()); + } + + [Fact] + public async Task PassesTheCorrectNewBranchParameter() + { + var connection = Substitute.For(); + var client = new RepositoryBranchesClient(connection); + var newBranch = "a"; + + await client.RenameBranch("owner", "repo", "branch", newBranch); + + connection.Received().Post( + Arg.Any(), + Arg.Is(o => o.GetType().GetProperty("new_name").GetValue(o).ToString() == newBranch)); + } + + [Fact] + public async Task EnsuresNonNullArguments() + { + var client = new RepositoryBranchesClient(Substitute.For()); + + await Assert.ThrowsAsync(() => client.RenameBranch(null, "repo", "branch", "new_name")); + await Assert.ThrowsAsync(() => client.RenameBranch("owner", null, "branch", "new_name")); + await Assert.ThrowsAsync(() => client.RenameBranch("owner", "repo", null, "new_name")); + await Assert.ThrowsAsync(() => client.RenameBranch("owner", "repo", "branch", null)); + + await Assert.ThrowsAsync(() => client.RenameBranch("", "repo", "branch", "new_name")); + await Assert.ThrowsAsync(() => client.RenameBranch("owner", "", "branch", "new_name")); + await Assert.ThrowsAsync(() => client.RenameBranch("owner", "repo", "", "new_name")); + await Assert.ThrowsAsync(() => client.RenameBranch("owner", "repo", "branch", "")); + } + } + } } diff --git a/Octokit/Clients/IRepositoryBranchesClient.cs b/Octokit/Clients/IRepositoryBranchesClient.cs index eec7c6026c..ed4081e57d 100644 --- a/Octokit/Clients/IRepositoryBranchesClient.cs +++ b/Octokit/Clients/IRepositoryBranchesClient.cs @@ -652,5 +652,17 @@ public interface IRepositoryBranchesClient /// The name of the branch /// List of users with push access to remove Task> DeleteProtectedBranchUserRestrictions(long repositoryId, string branch, BranchProtectionUserCollection users); + + /// + /// Renames a branch in a repository + /// + /// + /// See the API documentation for more details + /// + /// The owner of the repository + /// The name of the repository + /// The name of the branch to rename + /// The new name of the branch + Task RenameBranch(string owner, string repository, string branch, string newName); } } diff --git a/Octokit/Clients/RepositoryBranchesClient.cs b/Octokit/Clients/RepositoryBranchesClient.cs index 8126fc0ea6..15225c453a 100644 --- a/Octokit/Clients/RepositoryBranchesClient.cs +++ b/Octokit/Clients/RepositoryBranchesClient.cs @@ -1,15 +1,14 @@ using System.Collections.Generic; using System.Net; using System.Threading.Tasks; - namespace Octokit { - /// - /// A client for GitHub's Repository Branches API. - /// - /// - /// See the Repository Branches API documentation for more details. - /// + /// + /// A client for GitHub's Repository Branches API. + /// + /// + /// See the Repository Branches API documentation for more details. + /// public class RepositoryBranchesClient : ApiClient, IRepositoryBranchesClient { /// @@ -1177,5 +1176,26 @@ public Task> DeleteProtectedBranchUserRestrictions(long repo return ApiConnection.Delete>(ApiUrls.RepoRestrictionsUsers(repositoryId, branch), users); } + + /// + /// Renames a branch in a repository + /// + /// + /// See the API documentation for more details + /// + /// The owner of the repository + /// The name of the repository + /// The name of the branch to rename + /// The new name of the branch + [ManualRoute("POST", "/repos/{owner}/{repo}/branches/{branch}/rename")] + public Task 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(ApiUrls.RepositoryBranchRename(owner, repository, branch), new { new_name = newName }); + } } } diff --git a/Octokit/Helpers/ApiUrls.cs b/Octokit/Helpers/ApiUrls.cs index 4b91bb9585..41cb5ca958 100644 --- a/Octokit/Helpers/ApiUrls.cs +++ b/Octokit/Helpers/ApiUrls.cs @@ -5506,7 +5506,7 @@ public static Uri CodespaceStop(string codespaceName) { return "user/codespaces/{0}/stop".FormatUri(codespaceName); } - + /// /// Returns the that lists the artifacts for a repository. /// @@ -5517,7 +5517,7 @@ public static Uri ListArtifacts(string owner, string repository) { return "repos/{0}/{1}/actions/artifacts".FormatUri(owner, repository); } - + /// /// Returns the for the specified artifact. /// @@ -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); } - + /// /// Returns the to list the artifacts for a workflow. /// @@ -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); } + + /// + /// Returns the to rename a repository branch. + /// + /// The owner of the repository + /// The name of the repository + /// The name of the branch to rename + /// + public static Uri RepositoryBranchRename(string owner, string repository, string branch) + { + return "repos/{0}/{1}/branches/{2}/rename".FormatUri(owner, repository, branch); + } } } diff --git a/build/Utilities/BuildVersion.cs b/build/Utilities/BuildVersion.cs index 7c4985981c..619a46d069 100644 --- a/build/Utilities/BuildVersion.cs +++ b/build/Utilities/BuildVersion.cs @@ -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;