From c5ce6086392126fe942b9d035cc8a1d15afe36dd Mon Sep 17 00:00:00 2001 From: Jordan Dominion Date: Fri, 16 Aug 2024 17:23:46 -0400 Subject: [PATCH] Add support for Workflows `CreateDispatch` via repository ID (#2960) Add support for workflow dispatch via repository ID --- .../IObservableActionsWorkflowsClient.cs | 25 +++++++++++- .../ObservableActionsWorkflowsClient.cs | 37 +++++++++++++++++- .../Clients/ActionsWorkflowsClientTests.cs | 3 ++ .../Clients/ActionsWorkflowsClientTests.cs | 39 ++++++++++++++++++- .../ObservableActionsWorkflowsClientTests.cs | 34 +++++++++++++++- Octokit/Clients/ActionsWorkflowsClient.cs | 35 +++++++++++++++++ Octokit/Clients/IActionsWorkflowsClient.cs | 26 ++++++++++++- Octokit/Helpers/ApiUrls.cs | 22 +++++++++++ 8 files changed, 211 insertions(+), 10 deletions(-) diff --git a/Octokit.Reactive/Clients/IObservableActionsWorkflowsClient.cs b/Octokit.Reactive/Clients/IObservableActionsWorkflowsClient.cs index 9c5f055bbc..74773d0478 100644 --- a/Octokit.Reactive/Clients/IObservableActionsWorkflowsClient.cs +++ b/Octokit.Reactive/Clients/IObservableActionsWorkflowsClient.cs @@ -12,7 +12,7 @@ namespace Octokit.Reactive public interface IObservableActionsWorkflowsClient { /// - /// Manually triggers a GitHub Actions workflow run in a repository by Id. + /// Manually triggers a GitHub Actions workflow run in a repository by slug. /// /// /// https://developer.github.com/v3/actions/workflows/#create-a-workflow-dispatch-event @@ -24,7 +24,7 @@ public interface IObservableActionsWorkflowsClient IObservable CreateDispatch(string owner, string name, string workflowFileName, CreateWorkflowDispatch createDispatch); /// - /// Manually triggers a GitHub Actions workflow run in a repository by Id. + /// Manually triggers a GitHub Actions workflow run in a repository by slug. /// /// /// https://developer.github.com/v3/actions/workflows/#create-a-workflow-dispatch-event @@ -35,6 +35,27 @@ public interface IObservableActionsWorkflowsClient /// The parameters to use to trigger the workflow run. IObservable CreateDispatch(string owner, string name, long workflowId, CreateWorkflowDispatch createDispatch); + /// + /// Manually triggers a GitHub Actions workflow run in a repository by Id. + /// + /// + /// https://developer.github.com/v3/actions/workflows/#create-a-workflow-dispatch-event + /// + /// The Id of the repository. + /// The workflow file name. + /// The parameters to use to trigger the workflow run. + IObservable CreateDispatch(long repositoryId, string workflowFileName, CreateWorkflowDispatch createDispatch); + + /// + /// Manually triggers a GitHub Actions workflow run in a repository by Id. + /// + /// + /// https://developer.github.com/v3/actions/workflows/#create-a-workflow-dispatch-event + /// + /// The Id of the repository. + /// The Id of the workflow. + /// The parameters to use to trigger the workflow run. + IObservable CreateDispatch(long repositoryId, long workflowId, CreateWorkflowDispatch createDispatch); /// /// Disables a specific workflow in a repository by Id. /// diff --git a/Octokit.Reactive/Clients/ObservableActionsWorkflowsClient.cs b/Octokit.Reactive/Clients/ObservableActionsWorkflowsClient.cs index bd6bb02224..7ec222b151 100644 --- a/Octokit.Reactive/Clients/ObservableActionsWorkflowsClient.cs +++ b/Octokit.Reactive/Clients/ObservableActionsWorkflowsClient.cs @@ -23,7 +23,7 @@ public ObservableActionsWorkflowsClient(IGitHubClient client) } /// - /// Manually triggers a GitHub Actions workflow run in a repository by Id. + /// Manually triggers a GitHub Actions workflow run in a repository by slug. /// /// /// https://developer.github.com/v3/actions/workflows/#create-a-workflow-dispatch-event @@ -43,7 +43,7 @@ public IObservable CreateDispatch(string owner, string name, string workfl } /// - /// Manually triggers a GitHub Actions workflow run in a repository by Id. + /// Manually triggers a GitHub Actions workflow run in a repository by slug. /// /// /// https://developer.github.com/v3/actions/workflows/#create-a-workflow-dispatch-event @@ -61,6 +61,39 @@ public IObservable CreateDispatch(string owner, string name, long workflow return _client.CreateDispatch(owner, name, workflowId, createDispatch).ToObservable(); } + /// + /// Manually triggers a GitHub Actions workflow run in a repository by slug. + /// + /// + /// https://developer.github.com/v3/actions/workflows/#create-a-workflow-dispatch-event + /// + /// The Id of the repository. + /// The workflow file name. + /// The parameters to use to trigger the workflow run. + public IObservable CreateDispatch(long repositoryId, string workflowFileName, CreateWorkflowDispatch createDispatch) + { + Ensure.ArgumentNotNullOrEmptyString(workflowFileName, nameof(workflowFileName)); + Ensure.ArgumentNotNull(createDispatch, nameof(createDispatch)); + + return _client.CreateDispatch(repositoryId, workflowFileName, createDispatch).ToObservable(); + } + + /// + /// Manually triggers a GitHub Actions workflow run in a repository by Id. + /// + /// + /// https://developer.github.com/v3/actions/workflows/#create-a-workflow-dispatch-event + /// + /// The Id of the repository. + /// The Id of the workflow. + /// The parameters to use to trigger the workflow run. + public IObservable CreateDispatch(long repositoryId, long workflowId, CreateWorkflowDispatch createDispatch) + { + Ensure.ArgumentNotNull(createDispatch, nameof(createDispatch)); + + return _client.CreateDispatch(repositoryId, workflowId, createDispatch).ToObservable(); + } + /// /// Disables a specific workflow in a repository by Id. /// diff --git a/Octokit.Tests.Integration/Clients/ActionsWorkflowsClientTests.cs b/Octokit.Tests.Integration/Clients/ActionsWorkflowsClientTests.cs index 59f9e923a5..2ea206b408 100644 --- a/Octokit.Tests.Integration/Clients/ActionsWorkflowsClientTests.cs +++ b/Octokit.Tests.Integration/Clients/ActionsWorkflowsClientTests.cs @@ -105,14 +105,17 @@ public async Task CanDispatchWorkflow() { var owner = context.Repository.Owner.Login; var name = context.Repository.Name; + var repoId = context.Repository.Id; var workflowFileName = await CreateWorkflow(github, context); var reference = "main"; await fixture.CreateDispatch(owner, name, workflowFileName, new CreateWorkflowDispatch(reference)); + await fixture.CreateDispatch(repoId, workflowFileName, new CreateWorkflowDispatch(reference)); var workflowId = await GetWorkflowId(github, context, workflowFileName); await fixture.CreateDispatch(owner, name, workflowId, new CreateWorkflowDispatch(reference)); + await fixture.CreateDispatch(repoId, workflowId, new CreateWorkflowDispatch(reference)); } } diff --git a/Octokit.Tests/Clients/ActionsWorkflowsClientTests.cs b/Octokit.Tests/Clients/ActionsWorkflowsClientTests.cs index 45642f27fe..f889b2de8c 100644 --- a/Octokit.Tests/Clients/ActionsWorkflowsClientTests.cs +++ b/Octokit.Tests/Clients/ActionsWorkflowsClientTests.cs @@ -32,7 +32,7 @@ public void AreNotNull() public class TheCreateDispatchMethod { [Fact] - public async Task RequestsCorrectUrlByWorkflowId() + public async Task RequestsCorrectUrlByWorkflowIdRepoSlug() { var connection = Substitute.For(); var client = new ActionsWorkflowsClient(connection); @@ -47,7 +47,7 @@ public async Task RequestsCorrectUrlByWorkflowId() } [Fact] - public async Task RequestsCorrectUrlByWorkflowFileName() + public async Task RequestsCorrectUrlByWorkflowFileNameRepoSlug() { var connection = Substitute.For(); var client = new ActionsWorkflowsClient(connection); @@ -61,6 +61,36 @@ public async Task RequestsCorrectUrlByWorkflowFileName() createDispatch); } + [Fact] + public async Task RequestsCorrectUrlByWorkflowIdRepoId() + { + var connection = Substitute.For(); + var client = new ActionsWorkflowsClient(connection); + + var createDispatch = new CreateWorkflowDispatch("ref"); + + await client.CreateDispatch(321, 123, createDispatch); + + connection.Received().Post( + Arg.Is(u => u.ToString() == "repositories/321/actions/workflows/123/dispatches"), + createDispatch); + } + + [Fact] + public async Task RequestsCorrectUrlByWorkflowFileNameRepoId() + { + var connection = Substitute.For(); + var client = new ActionsWorkflowsClient(connection); + + var createDispatch = new CreateWorkflowDispatch("ref"); + + await client.CreateDispatch(321, "main.yaml", createDispatch); + + connection.Received().Post( + Arg.Is(u => u.ToString() == "repositories/321/actions/workflows/main.yaml/dispatches"), + createDispatch); + } + [Fact] public async Task EnsuresNonNullArguments() { @@ -77,6 +107,9 @@ public async Task EnsuresNonNullArguments() await Assert.ThrowsAsync(() => client.CreateDispatch("fake", null, "main.yaml", createDispatch)); await Assert.ThrowsAsync(() => client.CreateDispatch("fake", "repo", null, createDispatch)); await Assert.ThrowsAsync(() => client.CreateDispatch("fake", "repo", "main.yaml", null)); + + await Assert.ThrowsAsync(() => client.CreateDispatch(4321, 123, null)); + await Assert.ThrowsAsync(() => client.CreateDispatch(4321, null, createDispatch)); } [Fact] @@ -93,6 +126,8 @@ public async Task EnsuresNonEmptyArguments() await Assert.ThrowsAsync(() => client.CreateDispatch("", "repo", "main.yaml", createDispatch)); await Assert.ThrowsAsync(() => client.CreateDispatch("fake", "", "main.yaml", createDispatch)); await Assert.ThrowsAsync(() => client.CreateDispatch("fake", "repo", "", createDispatch)); + + await Assert.ThrowsAsync(() => client.CreateDispatch(4321, "", createDispatch)); } } diff --git a/Octokit.Tests/Reactive/ObservableActionsWorkflowsClientTests.cs b/Octokit.Tests/Reactive/ObservableActionsWorkflowsClientTests.cs index 0bf644a46f..2fb65d9b1a 100644 --- a/Octokit.Tests/Reactive/ObservableActionsWorkflowsClientTests.cs +++ b/Octokit.Tests/Reactive/ObservableActionsWorkflowsClientTests.cs @@ -33,7 +33,7 @@ public void AreNotNull() public class TheCreateDispatchMethod { [Fact] - public async Task RequestsCorrectUrlByWorkflowId() + public async Task RequestsCorrectUrlByWorkflowIdRepoSlug() { var connection = Substitute.For(); var client = new ObservableActionsWorkflowsClient(connection); @@ -46,7 +46,7 @@ public async Task RequestsCorrectUrlByWorkflowId() } [Fact] - public async Task RequestsCorrectUrlByWorkflowFileName() + public async Task RequestsCorrectUrlByWorkflowFileNameRepoSlug() { var connection = Substitute.For(); var client = new ObservableActionsWorkflowsClient(connection); @@ -57,6 +57,31 @@ public async Task RequestsCorrectUrlByWorkflowFileName() connection.Received().Actions.Workflows.CreateDispatch("fake", "repo", "main.yaml", createDispatch); } + [Fact] + public async Task RequestsCorrectUrlByWorkflowIdRepoId() + { + var connection = Substitute.For(); + var client = new ObservableActionsWorkflowsClient(connection); + + var createDispatch = new CreateWorkflowDispatch("ref"); + + client.CreateDispatch(1234, 123, createDispatch); + + connection.Received().Actions.Workflows.CreateDispatch(1234, 123, createDispatch); + } + + [Fact] + public async Task RequestsCorrectUrlByWorkflowFileNameRepoId() + { + var connection = Substitute.For(); + var client = new ObservableActionsWorkflowsClient(connection); + + var createDispatch = new CreateWorkflowDispatch("ref"); + + client.CreateDispatch(1234, "main.yaml", createDispatch); + + connection.Received().Actions.Workflows.CreateDispatch(1234, "main.yaml", createDispatch); + } [Fact] public async Task EnsuresNonNullArguments() @@ -74,6 +99,9 @@ public async Task EnsuresNonNullArguments() Assert.Throws(() => client.CreateDispatch("fake", null, "main.yaml", createDispatch)); Assert.Throws(() => client.CreateDispatch("fake", "repo", null, createDispatch)); Assert.Throws(() => client.CreateDispatch("fake", "repo", "main.yaml", null)); + + Assert.Throws(() => client.CreateDispatch(4321, 123, null)); + Assert.Throws(() => client.CreateDispatch(4321, null, createDispatch)); } [Fact] @@ -90,6 +118,8 @@ public async Task EnsuresNonEmptyArguments() Assert.Throws(() => client.CreateDispatch("", "repo", "main.yaml", createDispatch)); Assert.Throws(() => client.CreateDispatch("fake", "", "main.yaml", createDispatch)); Assert.Throws(() => client.CreateDispatch("fake", "repo", "", createDispatch)); + + Assert.Throws(() => client.CreateDispatch(4321, "", createDispatch)); } } diff --git a/Octokit/Clients/ActionsWorkflowsClient.cs b/Octokit/Clients/ActionsWorkflowsClient.cs index 7c72e98337..bda62f9ebc 100644 --- a/Octokit/Clients/ActionsWorkflowsClient.cs +++ b/Octokit/Clients/ActionsWorkflowsClient.cs @@ -62,6 +62,41 @@ public Task CreateDispatch(string owner, string name, long workflowId, CreateWor return ApiConnection.Post(ApiUrls.ActionsDispatchWorkflow(owner, name, workflowId), createDispatch); } + /// + /// Manually triggers a GitHub Actions workflow run in a repository by file name. + /// + /// + /// https://developer.github.com/v3/actions/workflows/#create-a-workflow-dispatch-event + /// + /// The Id of the repository. + /// The workflow file name. + /// The parameters to use to trigger the workflow run. + [ManualRoute("POST", "/repositories/{id}/actions/workflows/{workflow_id}/dispatches")] + public Task CreateDispatch(long repositoryId, string workflowFileName, CreateWorkflowDispatch createDispatch) + { + Ensure.ArgumentNotNullOrEmptyString(workflowFileName, nameof(workflowFileName)); + Ensure.ArgumentNotNull(createDispatch, nameof(createDispatch)); + + return ApiConnection.Post(ApiUrls.ActionsDispatchWorkflow(repositoryId, workflowFileName), createDispatch); + } + + /// + /// Manually triggers a GitHub Actions workflow run in a repository by Id. + /// + /// + /// https://developer.github.com/v3/actions/workflows/#create-a-workflow-dispatch-event + /// + /// The Id of the repository. + /// The Id of the workflow. + /// The parameters to use to trigger the workflow run. + [ManualRoute("POST", "/repositories/{id}/actions/workflows/{workflow_id}/dispatches")] + public Task CreateDispatch(long repositoryId, long workflowId, CreateWorkflowDispatch createDispatch) + { + Ensure.ArgumentNotNull(createDispatch, nameof(createDispatch)); + + return ApiConnection.Post(ApiUrls.ActionsDispatchWorkflow(repositoryId, workflowId), createDispatch); + } + /// /// Disables a specific workflow in a repository by file name. /// diff --git a/Octokit/Clients/IActionsWorkflowsClient.cs b/Octokit/Clients/IActionsWorkflowsClient.cs index 24f0ee791f..0652b8676c 100644 --- a/Octokit/Clients/IActionsWorkflowsClient.cs +++ b/Octokit/Clients/IActionsWorkflowsClient.cs @@ -11,7 +11,7 @@ namespace Octokit public interface IActionsWorkflowsClient { /// - /// Manually triggers a GitHub Actions workflow run in a repository by Id. + /// Manually triggers a GitHub Actions workflow run in a repository by slug. /// /// /// https://developer.github.com/v3/actions/workflows/#create-a-workflow-dispatch-event @@ -23,7 +23,7 @@ public interface IActionsWorkflowsClient Task CreateDispatch(string owner, string name, string workflowFileName, CreateWorkflowDispatch createDispatch); /// - /// Manually triggers a GitHub Actions workflow run in a repository by Id. + /// Manually triggers a GitHub Actions workflow run in a repository by slug. /// /// /// https://developer.github.com/v3/actions/workflows/#create-a-workflow-dispatch-event @@ -34,6 +34,28 @@ public interface IActionsWorkflowsClient /// The parameters to use to trigger the workflow run. Task CreateDispatch(string owner, string name, long workflowId, CreateWorkflowDispatch createDispatch); + /// + /// Manually triggers a GitHub Actions workflow run in a repository by Id. + /// + /// + /// https://developer.github.com/v3/actions/workflows/#create-a-workflow-dispatch-event + /// + /// The Id of the repository. + /// The workflow file name. + /// The parameters to use to trigger the workflow run. + Task CreateDispatch(long repositoryId, string workflowFileName, CreateWorkflowDispatch createDispatch); + + /// + /// Manually triggers a GitHub Actions workflow run in a repository by Id. + /// + /// + /// https://developer.github.com/v3/actions/workflows/#create-a-workflow-dispatch-event + /// + /// The Id of the repository. + /// The Id of the workflow. + /// The parameters to use to trigger the workflow run. + Task CreateDispatch(long repositoryId, long workflowId, CreateWorkflowDispatch createDispatch); + /// /// Disables a specific workflow in a repository by Id. /// diff --git a/Octokit/Helpers/ApiUrls.cs b/Octokit/Helpers/ApiUrls.cs index a7437f17bf..62c752a596 100644 --- a/Octokit/Helpers/ApiUrls.cs +++ b/Octokit/Helpers/ApiUrls.cs @@ -5042,6 +5042,28 @@ public static Uri ActionsDispatchWorkflow(string owner, string repo, string work return "repos/{0}/{1}/actions/workflows/{2}/dispatches".FormatUri(owner, repo, workflowFileName.UriEncode()); } + /// + /// Returns the that disables an Actions workflow for a repository. + /// + /// The Id of the repository. + /// The Id of the workflow. + /// The that gets an Actions workflow for a repository. + public static Uri ActionsDispatchWorkflow(long repositoryId, long workflowId) + { + return "repositories/{0}/actions/workflows/{1}/dispatches".FormatUri(repositoryId, workflowId); + } + + /// + /// Returns the that disables an Actions workflow for a repository. + /// + /// The Id of the repository. + /// The workflow file name. + /// The that gets an Actions workflow for a repository. + public static Uri ActionsDispatchWorkflow(long repositoryId, string workflowFileName) + { + return "repositories/{0}/actions/workflows/{1}/dispatches".FormatUri(repositoryId, workflowFileName.UriEncode()); + } + /// /// Returns the that disables an Actions workflow for a repository. ///