Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,6 @@ public List<String> getCategories() {
return problemInfo.categories();
}

public void incrementTotalSubmissions() {
getProblem().incrementTotalSubmissions();
}

public void incrementCorrectSubmissions() {
getProblem().incrementCorrectSubmissions();
}

public boolean isGitPushStatus() {
return user.getGitPushStatus();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
import org.ezcode.codetest.application.submission.dto.request.github.GitHubPushRequest;

public interface GitHubClient {
void commitAndPushToRepo(GitHubPushRequest request);

boolean isSourceCodeNewOrChanged(GitHubPushRequest req);

void commitAndPushToRepo(GitHubPushRequest req);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,17 @@ public void pushSolutionToRepo(SubmissionContext ctx) {
return;
}

eventService.publishGitPushStatus(GitPushStatusEvent.started(ctx));
UserGithubInfo info = userGithubService.getUserGithubInfoById(ctx.getUserId());

try {
UserGithubInfo info = userGithubService.getUserGithubInfoById(ctx.getUserId());
String decryptedToken = aesUtil.decrypt(info.getGithubAccessToken());
gitHubClient.commitAndPushToRepo(GitHubPushRequest.of(ctx, info, decryptedToken));
GitHubPushRequest req = GitHubPushRequest.of(ctx, info, decryptedToken);

if (!gitHubClient.isSourceCodeNewOrChanged(req)) {
return;
}

eventService.publishGitPushStatus(GitPushStatusEvent.started(ctx));
gitHubClient.commitAndPushToRepo(req);
eventService.publishGitPushStatus(GitPushStatusEvent.succeeded(ctx));
} catch (Exception e) {
exceptionNotifier.notifyException("commitAndPush", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public String getInput() {
}

public String getOutput() {
return this.output.replace("\\n", "\n");
if (output != null) return this.output.replace("\\n", "\n");
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ public class GitHubClientImpl implements GitHubClient {
private final GitBlobCalculator blobCalculator;

@Override
public void commitAndPushToRepo(GitHubPushRequest req) {
public boolean isSourceCodeNewOrChanged(GitHubPushRequest req) {
String codeBlobSha = blobCalculator.calculateBlobSha(req.sourceCode());
Optional<String> existingSha = gitHubApiClient.fetchSourceBlobSha(req);

if (existingSha.map(codeBlobSha::equals).orElse(false)) {
return;
}
return !existingSha.map(codeBlobSha::equals).orElse(false);
}

@Override
public void commitAndPushToRepo(GitHubPushRequest req) {
String codeBlobSha = blobCalculator.calculateBlobSha(req.sourceCode());

List<Map<String, Object>> entries = templateBuilder.buildGitTreeEntries(req, codeBlobSha);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,38 @@ void noPushWhenNotPassed() {
then(exceptionNotifier).should(never()).notifyException(anyString(), any());
}

@Test
@DisplayName("코드 변경 없음 -> 푸시 생략")
void noPushWhenCodeNotChanged() throws Exception {

// given
given(ctx.isGitPushStatus()).willReturn(true);
given(ctx.isPassed()).willReturn(true);

given(ctx.getSessionKey()).willReturn(sessionKey);
given(ctx.getUserId()).willReturn(userId);

given(userGithubService.getUserGithubInfoById(userId)).willReturn(info);
given(info.getGithubAccessToken()).willReturn(encryptedToken);

given(aesUtil.decrypt(encryptedToken)).willReturn(decryptedToken);

try (var ms = mockStatic(GitHubPushRequest.class)) {
ms.when(() -> GitHubPushRequest.of(ctx, info, decryptedToken)).thenReturn(req);

given(gitHubClient.isSourceCodeNewOrChanged(req)).willReturn(false);

// when
gitHubPushService.pushSolutionToRepo(ctx);

// then
then(gitHubClient).should(never()).commitAndPushToRepo(any());
then(eventService).should(never()).publishGitPushStatus(GitPushStatusEvent.started(ctx));
then(eventService).should(never()).publishGitPushStatus(GitPushStatusEvent.succeeded(ctx));
then(exceptionNotifier).should(never()).notifyException(anyString(), any());
}
}

@Test
@DisplayName("활성화 & 정답 제출 -> 메서드 실행 및 시작 & 성공 이벤트 발행")
void pushSuccess() throws Exception {
Expand All @@ -124,6 +156,8 @@ void pushSuccess() throws Exception {
try (var ms = mockStatic(GitHubPushRequest.class)) {
ms.when(() -> GitHubPushRequest.of(ctx, info, decryptedToken)).thenReturn(req);

given(gitHubClient.isSourceCodeNewOrChanged(req)).willReturn(true);

gitHubPushService.pushSolutionToRepo(ctx);

then(eventService).should().publishGitPushStatus(GitPushStatusEvent.started(ctx));
Expand Down Expand Up @@ -158,6 +192,8 @@ void pushFailure() throws Exception {
ms.when(() -> GitHubPushRequest.of(ctx, info, decryptedToken))
.thenReturn(req);

given(gitHubClient.isSourceCodeNewOrChanged(req)).willReturn(true);

gitHubPushService.pushSolutionToRepo(ctx);

then(eventService).should().publishGitPushStatus(GitPushStatusEvent.started(ctx));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,27 @@ void blobUnchanged_earlyReturn() {
given(gitHubApiClient.fetchSourceBlobSha(request)).willReturn(Optional.of(sha));

// when
gitHubClientImpl.commitAndPushToRepo(request);
boolean result = gitHubClientImpl.isSourceCodeNewOrChanged(request);

// then
then(gitHubApiClient).should(never()).fetchCommitContext(any());
then(templateBuilder).should(never()).buildGitTreeEntries(any(), any());
then(gitHubApiClient).should(never()).createTree(any(), any(), anyList());
then(gitHubApiClient).should(never()).commitAndPush(any(), any(), any());
assert !result;
}

@Test
@DisplayName("새 SHA == 트리 SHA -> 얼리 리턴")
void newBlobEqualsTreeBlob_earlyReturn() {

// given
String oldSha = "old";
String newSha = "new";
given(request.sourceCode()).willReturn(sourceCode);
given(blobCalculator.calculateBlobSha(request.sourceCode())).willReturn(newSha);
given(gitHubApiClient.fetchSourceBlobSha(request)).willReturn(Optional.of(oldSha));
given(gitHubApiClient.fetchCommitContext(request)).willReturn(ctx);

List<Map<String, Object>> entries = List.of(Map.of());
given(templateBuilder.buildGitTreeEntries(request, newSha)).willReturn(entries);

given(ctx.baseTreeSha()).willReturn("tree");
given(gitHubApiClient.createTree(request, "tree", entries)).willReturn("tree");
given(gitHubApiClient.fetchCommitContext(request)).willReturn(ctx);

// when
gitHubClientImpl.commitAndPushToRepo(request);
Expand All @@ -98,24 +93,27 @@ void newBlobEqualsTreeBlob_earlyReturn() {
void blobAndTreeChanged_commitAndPush() {

// given
String oldSha = "old";
String newSha = "new";
given(blobCalculator.calculateBlobSha(request.sourceCode())).willReturn(newSha);
given(gitHubApiClient.fetchSourceBlobSha(request)).willReturn(Optional.of(oldSha));
given(gitHubApiClient.fetchCommitContext(request)).willReturn(ctx);
String baseTree = "tree";
String head = "head";
String newTree = "newTree";

List<Map<String,Object>> entries = List.of(Map.of());

given(request.sourceCode()).willReturn(sourceCode);
given(blobCalculator.calculateBlobSha(sourceCode)).willReturn(newSha);
given(templateBuilder.buildGitTreeEntries(request, newSha)).willReturn(entries);

given(ctx.baseTreeSha()).willReturn("tree");
given(ctx.headCommitSha()).willReturn("head");
String newTree = "newTree";
given(gitHubApiClient.createTree(request, "tree", entries)).willReturn(newTree);
given(gitHubApiClient.fetchCommitContext(request)).willReturn(ctx);

given(ctx.baseTreeSha()).willReturn(baseTree);
given(ctx.headCommitSha()).willReturn(head);
given(gitHubApiClient.createTree(request, baseTree, entries)).willReturn(newTree);

// when
gitHubClientImpl.commitAndPushToRepo(request);

// then
then(gitHubApiClient).should().commitAndPush(request, "head", newTree);
then(gitHubApiClient).should().commitAndPush(request, head, newTree);
}
}