Skip to content

Commit 2b35ba7

Browse files
committed
feat : 정답 처리 시 깃허브 레포 자동 커밋 & 푸시 기능 구현
1 parent e3bd4df commit 2b35ba7

File tree

9 files changed

+448
-195
lines changed

9 files changed

+448
-195
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.ezcode.codetest.application.submission.dto.request.github;
2+
3+
import java.time.LocalDateTime;
4+
import java.time.format.DateTimeFormatter;
5+
6+
import org.ezcode.codetest.application.submission.model.SubmissionContext;
7+
import org.ezcode.codetest.domain.user.model.entity.UserGithubInfo;
8+
9+
public record GitHubPushRequest(
10+
11+
String owner,
12+
13+
String repo,
14+
15+
String branch,
16+
17+
String accessToken,
18+
19+
Long problemId,
20+
21+
String problemTitle,
22+
23+
String problemDescription,
24+
25+
Long averageMemoryUsage,
26+
27+
Long averageExecutionTime,
28+
29+
String sourceCode,
30+
31+
String submittedAt
32+
33+
) {
34+
public static GitHubPushRequest of(SubmissionContext ctx, UserGithubInfo info) {
35+
return new GitHubPushRequest(
36+
info.getOwner(),
37+
info.getRepo(),
38+
info.getBranch(),
39+
info.getGithubAccessToken(),
40+
ctx.getProblem().getId(),
41+
ctx.getProblem().getTitle(),
42+
ctx.getProblem().getDescription(),
43+
ctx.aggregator().averageMemoryUsage(),
44+
ctx.aggregator().averageExecutionTime(),
45+
ctx.getSourceCode(),
46+
LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))
47+
);
48+
}
49+
}

src/main/java/org/ezcode/codetest/application/submission/model/SubmissionContext.java

Lines changed: 123 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import org.ezcode.codetest.application.submission.dto.event.payload.SubmissionFinalResultPayload;
44
import org.ezcode.codetest.domain.language.model.entity.Language;
55
import org.ezcode.codetest.domain.problem.model.ProblemInfo;
6-
import org.ezcode.codetest.domain.problem.model.entity.Category;
76
import org.ezcode.codetest.domain.problem.model.entity.Problem;
7+
import org.ezcode.codetest.domain.problem.model.entity.ProblemCategory;
88
import org.ezcode.codetest.domain.problem.model.entity.Testcase;
99
import org.ezcode.codetest.domain.submission.model.SubmissionAggregator;
1010
import org.ezcode.codetest.domain.user.model.entity.User;
@@ -18,116 +18,136 @@
1818

1919
public record SubmissionContext(
2020

21-
SubmissionAggregator aggregator,
21+
SubmissionAggregator aggregator,
2222

23-
AtomicInteger passedCount,
23+
AtomicInteger passedCount,
2424

25-
AtomicInteger processedCount,
25+
AtomicInteger processedCount,
2626

27-
AtomicReference<String> message,
27+
AtomicReference<String> message,
2828

29-
CountDownLatch latch,
29+
CountDownLatch latch,
3030

31-
AtomicBoolean notified,
31+
AtomicBoolean notified,
3232

33-
User user,
33+
User user,
3434

35-
Language language,
35+
Language language,
3636

37-
ProblemInfo problemInfo,
37+
ProblemInfo problemInfo,
3838

39-
SubmissionMessage msg
39+
SubmissionMessage msg
4040
) {
41-
public static SubmissionContext initialize(
42-
User user, Language language, ProblemInfo problemInfo, SubmissionMessage msg
43-
) {
44-
return new SubmissionContext(
45-
new SubmissionAggregator(),
46-
new AtomicInteger(0),
47-
new AtomicInteger(0),
48-
new AtomicReference<>("Accepted"),
49-
new CountDownLatch(problemInfo.getTestcaseCount()),
50-
new AtomicBoolean(false),
51-
user,
52-
language,
53-
problemInfo,
54-
msg
55-
);
56-
}
57-
58-
public SubmissionFinalResultPayload toFinalResult() {
59-
return new SubmissionFinalResultPayload(
60-
this.getTestcaseCount(),
61-
this.getPassedCount(),
62-
this.getCurrentMessage()
63-
);
64-
}
65-
66-
public void incrementPassedCount() {
67-
passedCount.incrementAndGet();
68-
}
69-
70-
public void incrementProcessedCount() {
71-
processedCount.incrementAndGet();
72-
}
73-
74-
public int getPassedCount() {
75-
return passedCount.get();
76-
}
77-
78-
public String getCurrentMessage() {
79-
return message.get();
80-
}
81-
82-
public void updateMessage(String message) {
83-
this.message.set(message);
84-
}
85-
86-
public void countDown() {
87-
latch.countDown();
88-
}
89-
90-
public List<Testcase> getTestcases() {
91-
return problemInfo.testcaseList();
92-
}
93-
94-
public int getTestcaseCount() {
95-
return problemInfo.getTestcaseCount();
96-
}
97-
98-
public String getSourceCode() {
99-
return msg.sourceCode();
100-
}
101-
102-
public long getJudge0Id() {
103-
return language.getJudge0Id();
104-
}
105-
106-
public String getInput(int seqId) {
107-
return getTestcases().get(seqId - 1).getInput();
108-
}
109-
110-
public String getExpectedOutput(int seqId) {
111-
return getTestcases().get(seqId - 1).getOutput();
112-
}
113-
114-
public long getTimeLimit() {
115-
return problemInfo.getTimeLimit();
116-
}
117-
118-
public long getMemoryLimit() {
119-
return problemInfo.getMemoryLimit();
120-
}
121-
122-
public String getSessionKey() {
123-
return msg.sessionKey();
124-
}
125-
126-
public Problem getProblem() {
127-
return problemInfo.problem();
128-
}
129-
130-
public List<String> getProblemCategories() {
131-
return problemInfo.problemCategories().stream().map(Category::getKorName).toList();
132-
}
41+
public static SubmissionContext initialize(
42+
User user, Language language, ProblemInfo problemInfo, SubmissionMessage msg
43+
) {
44+
return new SubmissionContext(
45+
new SubmissionAggregator(),
46+
new AtomicInteger(0),
47+
new AtomicInteger(0),
48+
new AtomicReference<>("Accepted"),
49+
new CountDownLatch(problemInfo.getTestcaseCount()),
50+
new AtomicBoolean(false),
51+
user,
52+
language,
53+
problemInfo,
54+
msg
55+
);
56+
}
57+
58+
public SubmissionFinalResultPayload toFinalResult() {
59+
return new SubmissionFinalResultPayload(
60+
this.getTestcaseCount(),
61+
this.getPassedCount(),
62+
this.getCurrentMessage()
63+
);
64+
}
65+
66+
public void incrementPassedCount() {
67+
passedCount.incrementAndGet();
68+
}
69+
70+
public void incrementProcessedCount() {
71+
processedCount.incrementAndGet();
72+
}
73+
74+
public int getPassedCount() {
75+
return passedCount.get();
76+
}
77+
78+
public String getCurrentMessage() {
79+
return message.get();
80+
}
81+
82+
public void updateMessage(String message) {
83+
this.message.set(message);
84+
}
85+
86+
public void countDown() {
87+
latch.countDown();
88+
}
89+
90+
public List<Testcase> getTestcases() {
91+
return problemInfo.testcaseList();
92+
}
93+
94+
public int getTestcaseCount() {
95+
return problemInfo.getTestcaseCount();
96+
}
97+
98+
public String getSourceCode() {
99+
return msg.sourceCode();
100+
}
101+
102+
public long getJudge0Id() {
103+
return language.getJudge0Id();
104+
}
105+
106+
public String getInput(int seqId) {
107+
return getTestcases().get(seqId - 1).getInput();
108+
}
109+
110+
public String getExpectedOutput(int seqId) {
111+
return getTestcases().get(seqId - 1).getOutput();
112+
}
113+
114+
public long getTimeLimit() {
115+
return problemInfo.getTimeLimit();
116+
}
117+
118+
public long getMemoryLimit() {
119+
return problemInfo.getMemoryLimit();
120+
}
121+
122+
public String getSessionKey() {
123+
return msg.sessionKey();
124+
}
125+
126+
public Problem getProblem() {
127+
return problemInfo.problem();
128+
}
129+
130+
public List<String> getCategories() {
131+
return getProblem().getCategories().stream().map(ProblemCategory::getCategoryForKor).toList();
132+
}
133+
134+
public void incrementTotalSubmissions() {
135+
getProblem().incrementTotalSubmissions();
136+
}
137+
138+
public void incrementCorrectSubmissions() {
139+
getProblem().incrementCorrectSubmissions();
140+
}
141+
142+
public boolean isGitPushStatus(){
143+
return user.isGitPushStatus();
144+
}
145+
146+
public Long getUserId() {
147+
return user.getId();
148+
}
149+
150+
public boolean isPassed() {
151+
return getPassedCount() == getTestcaseCount();
152+
}
133153
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.ezcode.codetest.application.submission.port;
2+
3+
import org.ezcode.codetest.application.submission.dto.request.github.GitHubPushRequest;
4+
5+
public interface GitHubClient {
6+
void commitAndPushToRepo(GitHubPushRequest request);
7+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.ezcode.codetest.application.submission.service;
2+
3+
import org.ezcode.codetest.application.submission.dto.request.github.GitHubPushRequest;
4+
import org.ezcode.codetest.application.submission.model.SubmissionContext;
5+
import org.ezcode.codetest.application.submission.port.GitHubClient;
6+
import org.ezcode.codetest.domain.user.model.entity.UserGithubInfo;
7+
import org.ezcode.codetest.domain.user.service.UserGithubService;
8+
import org.springframework.stereotype.Service;
9+
10+
import lombok.RequiredArgsConstructor;
11+
12+
@Service
13+
@RequiredArgsConstructor
14+
public class GitHubService {
15+
16+
private final GitHubClient gitHubClient;
17+
private final UserGithubService userGithubService;
18+
19+
public void commitAndPushToRepo(SubmissionContext ctx) {
20+
if (ctx.isGitPushStatus() && ctx.isPassed()) {
21+
UserGithubInfo info = userGithubService.getUserGithubInfoById(ctx.getUserId());
22+
gitHubClient.commitAndPushToRepo(GitHubPushRequest.of(ctx, info));
23+
}
24+
}
25+
}

src/main/java/org/ezcode/codetest/application/submission/service/JudgementService.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.ezcode.codetest.domain.submission.model.TestcaseEvaluationInput;
2525
import org.ezcode.codetest.domain.submission.service.SubmissionDomainService;
2626
import org.springframework.stereotype.Service;
27-
import org.springframework.transaction.annotation.Transactional;
2827

2928
import lombok.RequiredArgsConstructor;
3029
import lombok.extern.slf4j.Slf4j;
@@ -56,7 +55,6 @@ public void runTestcases(SubmissionContext ctx) throws InterruptedException {
5655
}
5756
}
5857

59-
@Transactional
6058
public void finalizeAndPublish(SubmissionContext ctx) {
6159
SubmissionResult submissionResult = submissionDomainService.finalizeSubmission(ctx);
6260

src/main/java/org/ezcode/codetest/application/submission/service/SubmissionService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ public class SubmissionService {
4949
private final ExceptionNotifier exceptionNotifier;
5050
private final LockManager lockManager;
5151
private final JudgementService judgementService;
52+
private final GitHubService gitHubService;
5253

5354
public String enqueueCodeSubmission(Long problemId, CodeSubmitRequest request, AuthUser authUser) {
5455

@@ -66,6 +67,7 @@ public String enqueueCodeSubmission(Long problemId, CodeSubmitRequest request, A
6667
}
6768

6869
@Async("judgeSubmissionExecutor")
70+
@Transactional
6971
public void submitCodeStream(SubmissionMessage msg) {
7072
try {
7173
log.info("[Submission RUN] Thread = {}", Thread.currentThread().getName());
@@ -75,6 +77,7 @@ public void submitCodeStream(SubmissionMessage msg) {
7577
judgementService.publishInitTestcases(ctx);
7678
judgementService.runTestcases(ctx);
7779
judgementService.finalizeAndPublish(ctx);
80+
gitHubService.commitAndPushToRepo(ctx);
7881
} catch (Exception e) {
7982
judgementService.publishSubmissionError(msg.sessionKey(), e);
8083
exceptionNotifier.notifyException("submitCodeStream", e);

0 commit comments

Comments
 (0)