Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: push autoresolved merge conflict on backend automatically #13544

Merged
merged 4 commits into from
Sep 26, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 27 additions & 20 deletions backend/src/Designer/Controllers/RepositoryController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,39 @@
using Altinn.Studio.Designer.Configuration;
using Altinn.Studio.Designer.Enums;
using Altinn.Studio.Designer.Helpers;
using Altinn.Studio.Designer.Hubs.SyncHub;
using Altinn.Studio.Designer.Models;
using Altinn.Studio.Designer.RepositoryClient.Model;
using Altinn.Studio.Designer.Services.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using RepositoryModel = Altinn.Studio.Designer.RepositoryClient.Model.Repository;

namespace Altinn.Studio.Designer.Controllers
{
/// <summary>
/// This is the API controller for functionality related to repositories.
/// </summary>
/// <remarks>
/// Initializes a new instance of the <see cref="RepositoryController"/> class.
/// </remarks>
/// <param name="giteaWrapper">the gitea wrapper</param>
/// <param name="sourceControl">the source control</param>
/// <param name="repository">the repository control</param>
/// <param name="userRequestsSynchronizationService">An <see cref="IUserRequestsSynchronizationService"/> used to control parallel execution of user requests.</param>
/// <param name="syncHub">websocket syncHub</param>
[Authorize]
[AutoValidateAntiforgeryToken]
[Route("designer/api/repos")]
public class RepositoryController : ControllerBase
public class RepositoryController(IGitea giteaWrapper, ISourceControl sourceControl, IRepository repository, IUserRequestsSynchronizationService userRequestsSynchronizationService, IHubContext<SyncHub, ISyncClient> syncHub) : ControllerBase
{
private readonly IGitea _giteaApi;
private readonly ISourceControl _sourceControl;
private readonly IRepository _repository;
private readonly IUserRequestsSynchronizationService _userRequestsSynchronizationService;

/// <summary>
/// Initializes a new instance of the <see cref="RepositoryController"/> class.
/// </summary>
/// <param name="giteaWrapper">the gitea wrapper</param>
/// <param name="sourceControl">the source control</param>
/// <param name="repository">the repository control</param>
/// <param name="userRequestsSynchronizationService">An <see cref="IUserRequestsSynchronizationService"/> used to control parallel execution of user requests.</param>
public RepositoryController(IGitea giteaWrapper, ISourceControl sourceControl, IRepository repository, IUserRequestsSynchronizationService userRequestsSynchronizationService)
{
_giteaApi = giteaWrapper;
_sourceControl = sourceControl;
_repository = repository;
_userRequestsSynchronizationService = userRequestsSynchronizationService;
}
private readonly IGitea _giteaApi = giteaWrapper;
private readonly ISourceControl _sourceControl = sourceControl;
private readonly IRepository _repository = repository;
private readonly IUserRequestsSynchronizationService _userRequestsSynchronizationService = userRequestsSynchronizationService;
private readonly IHubContext<SyncHub, ISyncClient> _syncHub = syncHub;

/// <summary>
/// Returns a list over repositories
Expand Down Expand Up @@ -323,6 +319,17 @@ public async Task CommitAndPushRepo([FromBody] CommitInfo commitInfo)
{
await _sourceControl.PushChangesForRepository(commitInfo);
}
catch (LibGit2Sharp.NonFastForwardException)
{
RepoStatus repoStatus = await _sourceControl.PullRemoteChanges(commitInfo.Org, commitInfo.Repository);
await _sourceControl.Push(commitInfo.Org, commitInfo.Repository);
foreach (RepositoryContent repoContent in repoStatus?.ContentStatus)
{
Source source = new(Path.GetFileName(repoContent.FilePath), repoContent.FilePath);
SyncSuccess syncSuccess = new(source);
await _syncHub.Clients.Group(developer).FileSyncSuccess(syncSuccess);
}
}
finally
{
semaphore.Release();
Expand Down
12 changes: 11 additions & 1 deletion backend/src/Designer/Services/Implementation/SourceControlSI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ public async Task<string> CloneRemoteRepository(string org, string repository, s
/// <inheritdoc />
public async Task<RepoStatus> PullRemoteChanges(string org, string repository)
{
RepoStatus status = new();
RepoStatus status = new()
{
ContentStatus = []
};
using (var repo = new LibGit2Sharp.Repository(FindLocalRepoLocation(org, repository)))
{
PullOptions pullOptions = new()
Expand All @@ -99,11 +102,18 @@ public async Task<RepoStatus> PullRemoteChanges(string org, string repository)

try
{
Tree head = repo.Head.Tip.Tree;
MergeResult mergeResult = Commands.Pull(
repo,
new LibGit2Sharp.Signature("my name", "my email", DateTimeOffset.Now), // I dont want to provide these
pullOptions);

TreeChanges treeChanges = repo.Diff.Compare<TreeChanges>(head, mergeResult.Commit?.Tree);
foreach (TreeEntryChanges change in treeChanges.Modified)
{
status.ContentStatus.Add(new RepositoryContent { FilePath = change.Path, FileStatus = Enums.FileStatus.ModifiedInWorkdir });
}

if (mergeResult.Status == MergeStatus.Conflicts)
{
status.RepositoryStatus = Enums.RepositoryStatus.MergeConflict;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ public async Task GetBranches_And_Branch_ShouldBehaveAsExpected(string org)

[Theory]
[InlineData(GiteaConstants.TestOrgUsername)]
public async Task Commit_AndPush_NonPulled_ShouldReturnConflict(string org)
public async Task PushWithConflictingChangesRemotely_ShouldReturnConflict(string org)
{
string targetRepo = TestDataHelper.GenerateTestRepoName("-gitea");
await CreateAppUsingDesigner(org, targetRepo);
Expand All @@ -198,7 +198,7 @@ public async Task Commit_AndPush_NonPulled_ShouldReturnConflict(string org)
createFileResponse.StatusCode.Should().Be(HttpStatusCode.Created);

// Add a file to local repo and try to push with designer
await File.WriteAllTextAsync($"{CreatedFolderPath}/test.txt", "I am a new file from studio.");
await File.WriteAllTextAsync($"{CreatedFolderPath}/test2.txt", "I am a new file from studio.");
using var commitAndPushContent = new StringContent(GetCommitInfoJson("test commit", org, targetRepo), Encoding.UTF8, MediaTypeNames.Application.Json);
using HttpResponseMessage commitAndPushResponse = await HttpClient.PostAsync($"designer/api/repos/repo/{org}/{targetRepo}/commit-and-push", commitAndPushContent);
commitAndPushResponse.StatusCode.Should().Be(HttpStatusCode.Conflict);
Expand Down
Loading