diff --git a/src/Tgstation.Server.Api/Models/Internal/RepositorySettings.cs b/src/Tgstation.Server.Api/Models/Internal/RepositorySettings.cs index 4afc3d8394..b3fd28489f 100644 --- a/src/Tgstation.Server.Api/Models/Internal/RepositorySettings.cs +++ b/src/Tgstation.Server.Api/Models/Internal/RepositorySettings.cs @@ -9,12 +9,6 @@ namespace Tgstation.Server.Api.Models.Internal [Model(RightsType.Repository, ReadRight = RepositoryRights.Read, RequiresInstance = true)] public class RepositorySettings { - /// - /// The origin URL. If , the does not exist - /// - [Permissions(WriteRight = RepositoryRights.SetOrigin)] - public string Origin { get; set; } - /// /// The last commit recognized from /// diff --git a/src/Tgstation.Server.Api/Models/Repository.cs b/src/Tgstation.Server.Api/Models/Repository.cs index 004be123cf..3cec95577c 100644 --- a/src/Tgstation.Server.Api/Models/Repository.cs +++ b/src/Tgstation.Server.Api/Models/Repository.cs @@ -8,6 +8,12 @@ namespace Tgstation.Server.Api.Models /// public sealed class Repository : Internal.RepositorySettings { + /// + /// The origin URL. If , the does not exist + /// + [Permissions(WriteRight = RepositoryRights.SetOrigin)] + public string Origin { get; set; } + /// /// The commit HEAD points to /// diff --git a/src/Tgstation.Server.Host/Controllers/RepositoryController.cs b/src/Tgstation.Server.Host/Controllers/RepositoryController.cs index bded31639d..11a20fa482 100644 --- a/src/Tgstation.Server.Host/Controllers/RepositoryController.cs +++ b/src/Tgstation.Server.Host/Controllers/RepositoryController.cs @@ -11,6 +11,7 @@ using System.Threading.Tasks; using Tgstation.Server.Api.Models; using Tgstation.Server.Api.Rights; using Tgstation.Server.Host.Components; +using Tgstation.Server.Host.Core; using Tgstation.Server.Host.Models; using Tgstation.Server.Host.Security; @@ -32,6 +33,11 @@ namespace Tgstation.Server.Host.Controllers /// readonly Octokit.IGitHubClient gitHubClient; + /// + /// The for the + /// + readonly IJobManager jobManager; + /// /// Construct a /// @@ -39,10 +45,12 @@ namespace Tgstation.Server.Host.Controllers /// The for the /// The value of /// The value of - public RepositoryController(IDatabaseContext databaseContext, IAuthenticationContextFactory authenticationContextFactory, IInstanceManager instanceManager, Octokit.IGitHubClient gitHubClient) : base(databaseContext, authenticationContextFactory, true) + /// The value of + public RepositoryController(IDatabaseContext databaseContext, IAuthenticationContextFactory authenticationContextFactory, IInstanceManager instanceManager, Octokit.IGitHubClient gitHubClient, IJobManager jobManager) : base(databaseContext, authenticationContextFactory, true) { this.instanceManager = instanceManager ?? throw new ArgumentNullException(nameof(instanceManager)); this.gitHubClient = gitHubClient ?? throw new ArgumentNullException(nameof(gitHubClient)); + this.jobManager = jobManager ?? throw new ArgumentNullException(nameof(jobManager)); } static string GetAccessString(Api.Models.Internal.RepositorySettings repositorySettings) => repositorySettings.AccessUser != null ? String.Concat(repositorySettings.AccessUser, '@', repositorySettings.AccessToken) : null; @@ -109,12 +117,12 @@ namespace Tgstation.Server.Host.Controllers currentModel.AccessToken = model.AccessToken; - currentModel.AccessUser = model.AccessUser; - currentModel.Origin = model.Origin; //intentionally only these fields, user not allowed to change anything else atm + currentModel.AccessUser = model.AccessUser; //intentionally only these fields, user not allowed to change anything else atm var cloneBranch = model.Reference; + var origin = model.Origin; var repoManager = instanceManager.GetInstance(Instance).RepositoryManager; - using (var repo = await repoManager.CloneRepository(new Uri(currentModel.Origin), cloneBranch, GetAccessString(currentModel), cancellationToken).ConfigureAwait(false)) + using (var repo = await repoManager.CloneRepository(new Uri(origin), cloneBranch, GetAccessString(currentModel), cancellationToken).ConfigureAwait(false)) { if (repo == null) //clone conflict @@ -140,7 +148,6 @@ namespace Tgstation.Server.Host.Controllers if (currentModel == default) return StatusCode((int)HttpStatusCode.Gone); - currentModel.Origin = null; currentModel.LastOriginCommitSha = null; currentModel.AccessToken = null; currentModel.AccessUser = null; @@ -228,6 +235,8 @@ namespace Tgstation.Server.Host.Controllers using (var repo = await instanceManager.GetInstance(Instance).RepositoryManager.LoadRepository(cancellationToken).ConfigureAwait(false)) { + var startSha = repo.Head; + if (newTestMerges && !repo.IsGitHubRepository) return Conflict(new { message = "Cannot test merge on a non GitHub based repository!" }); @@ -323,6 +332,25 @@ namespace Tgstation.Server.Host.Controllers var api = currentModel.ToApi(); if (await PopulateApi(api, repo, currentModel.LastOriginCommitSha, null, cancellationToken).ConfigureAwait(false) || newTestMerges) await DatabaseContext.Save(cancellationToken).ConfigureAwait(false); + + //synchronize now because we don't care if we fail so its safe to fire/forget the job + + if (startSha != repo.Head) + { + var job = new Models.Job + { + Description = "Synchronize repository changes", + StartedBy = AuthenticationContext.User, + Instance = Instance + }; + await jobManager.RegisterOperation(job, async (paramJob, serviceProvider, ct) => + { + using (var repos = await instanceManager.GetInstance(Instance).RepositoryManager.LoadRepository(cancellationToken).ConfigureAwait(false)) + if (repos != null) + await repos.Sychronize(accessString, ct).ConfigureAwait(false); + }, cancellationToken).ConfigureAwait(false); + } + return Json(api); } }