diff --git a/src/Runner.Common/RunnerDotcomServer.cs b/src/Runner.Common/RunnerDotcomServer.cs index b73b252e1..c03e60bb7 100644 --- a/src/Runner.Common/RunnerDotcomServer.cs +++ b/src/Runner.Common/RunnerDotcomServer.cs @@ -15,9 +15,10 @@ namespace GitHub.Runner.Common [ServiceLocator(Default = typeof(RunnerDotcomServer))] public interface IRunnerDotcomServer : IRunnerService { - Task> GetRunnersAsync(int runnerGroupId, string githubUrl, string githubToken, string agentName); + Task> GetRunnerByNameAsync(string githubUrl, string githubToken, string agentName); Task AddRunnerAsync(int runnerGroupId, TaskAgent agent, string githubUrl, string githubToken, string publicKey); + Task ReplaceRunnerAsync(int runnerGroupId, TaskAgent agent, string githubUrl, string githubToken, string publicKey); Task> GetRunnerGroupsAsync(string githubUrl, string githubToken); } @@ -40,7 +41,7 @@ namespace GitHub.Runner.Common } - public async Task> GetRunnersAsync(int runnerGroupId, string githubUrl, string githubToken, string agentName) + public async Task> GetRunnerByNameAsync(string githubUrl, string githubToken, string agentName) { var githubApiUrl = ""; var gitHubUrlBuilder = new UriBuilder(githubUrl); @@ -129,6 +130,16 @@ namespace GitHub.Runner.Common } public async Task AddRunnerAsync(int runnerGroupId, TaskAgent agent, string githubUrl, string githubToken, string publicKey) + { + return await AddOrReplaceRunner(runnerGroupId, agent, githubUrl, githubToken, publicKey, false); + } + + public async Task ReplaceRunnerAsync(int runnerGroupId, TaskAgent agent, string githubUrl, string githubToken, string publicKey) + { + return await AddOrReplaceRunner(runnerGroupId, agent, githubUrl, githubToken, publicKey, true); + } + + private async Task AddOrReplaceRunner(int runnerGroupId, TaskAgent agent, string githubUrl, string githubToken, string publicKey, bool replace) { var gitHubUrlBuilder = new UriBuilder(githubUrl); var path = gitHubUrlBuilder.Path.Split('/', '\\', StringSplitOptions.RemoveEmptyEntries); @@ -151,9 +162,15 @@ namespace GitHub.Runner.Common {"updates_disabled", agent.DisableUpdate}, {"ephemeral", agent.Ephemeral}, {"labels", agent.Labels}, - {"public_key", publicKey} + {"public_key", publicKey}, }; + if (replace) + { + bodyObject.Add("runner_id", agent.Id); + bodyObject.Add("replace", replace); + } + var body = new StringContent(StringUtil.ConvertToJson(bodyObject), null, "application/json"); return await RetryRequest(githubApiUrl, githubToken, RequestType.Post, 3, "Failed to add agent", body); diff --git a/src/Runner.Listener/Configuration/ConfigurationManager.cs b/src/Runner.Listener/Configuration/ConfigurationManager.cs index 597b80078..b49244af4 100644 --- a/src/Runner.Listener/Configuration/ConfigurationManager.cs +++ b/src/Runner.Listener/Configuration/ConfigurationManager.cs @@ -244,7 +244,7 @@ namespace GitHub.Runner.Listener.Configuration List agents; if (runnerSettings.UseV2Flow) { - agents = await _dotcomServer.GetRunnersAsync(runnerSettings.PoolId, runnerSettings.GitHubUrl, registerToken, runnerSettings.AgentName); + agents = await _dotcomServer.GetRunnerByNameAsync(runnerSettings.GitHubUrl, registerToken, runnerSettings.AgentName); } else { @@ -263,7 +263,23 @@ namespace GitHub.Runner.Listener.Configuration try { - agent = await _runnerServer.ReplaceAgentAsync(runnerSettings.PoolId, agent); + if (runnerSettings.UseV2Flow) + { + var runner = await _dotcomServer.ReplaceRunnerAsync(runnerSettings.PoolId, agent, runnerSettings.GitHubUrl, registerToken, publicKeyXML); + runnerSettings.ServerUrlV2 = runner.RunnerAuthorization.ServerUrl; + + agent.Id = runner.Id; + agent.Authorization = new TaskAgentAuthorization() + { + AuthorizationUrl = runner.RunnerAuthorization.AuthorizationUrl, + ClientId = new Guid(runner.RunnerAuthorization.ClientId) + }; + } + else + { + agent = await _runnerServer.ReplaceAgentAsync(runnerSettings.PoolId, agent); + } + if (command.DisableUpdate && command.DisableUpdate != agent.DisableUpdate) { diff --git a/src/Sdk/DTWebApi/WebApi/ListRunnersResponse.cs b/src/Sdk/DTWebApi/WebApi/ListRunnersResponse.cs index 156c20fa9..755ff7104 100644 --- a/src/Sdk/DTWebApi/WebApi/ListRunnersResponse.cs +++ b/src/Sdk/DTWebApi/WebApi/ListRunnersResponse.cs @@ -41,7 +41,7 @@ namespace GitHub.DistributedTask.WebApi public List ToTaskAgents() { - return Runners.Select(runner => new TaskAgent() { Name = runner.Name }).ToList(); + return Runners.Select(runner => new TaskAgent() { Id = runner.Id, Name = runner.Name }).ToList(); } } diff --git a/src/Test/L0/Listener/Configuration/ConfigurationManagerL0.cs b/src/Test/L0/Listener/Configuration/ConfigurationManagerL0.cs index b4dfb397f..289a22d8a 100644 --- a/src/Test/L0/Listener/Configuration/ConfigurationManagerL0.cs +++ b/src/Test/L0/Listener/Configuration/ConfigurationManagerL0.cs @@ -115,7 +115,7 @@ namespace GitHub.Runner.Common.Tests.Listener.Configuration _runnerServer.Setup(x => x.AddAgentAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(expectedAgent)); _runnerServer.Setup(x => x.ReplaceAgentAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(expectedAgent)); - _dotcomServer.Setup(x => x.GetRunnersAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(Task.FromResult(expectedAgents)); + _dotcomServer.Setup(x => x.GetRunnerByNameAsync(It.IsAny(), It.IsAny(), It.IsAny())).Returns(Task.FromResult(expectedAgents)); _dotcomServer.Setup(x => x.GetRunnerGroupsAsync(It.IsAny(), It.IsAny())).Returns(Task.FromResult(expectedPools)); _dotcomServer.Setup(x => x.AddRunnerAsync(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny())).Returns(Task.FromResult(expectedRunner));