22
33import com .fasterxml .jackson .databind .ObjectMapper ;
44import com .flowci .core .common .domain .GitSource ;
5- import com .flowci .core .common .manager .SessionManager ;
65import com .flowci .core .common .manager .SpringEventManager ;
6+ import com .flowci .core .git .client .GerritAPIClient ;
77import com .flowci .core .git .client .GitAPIClient ;
88import com .flowci .core .git .client .GithubAPIClient ;
99import com .flowci .core .git .dao .GitConfigDao ;
1010import com .flowci .core .git .domain .GitCommitStatus ;
1111import com .flowci .core .git .domain .GitConfig ;
12+ import com .flowci .core .git .domain .GitConfigWithHost ;
1213import com .flowci .core .job .domain .Job ;
1314import com .flowci .core .job .event .JobFinishedEvent ;
1415import com .flowci .core .job .util .JobContextHelper ;
16+ import com .flowci .core .secret .domain .AuthSecret ;
1517import com .flowci .core .secret .domain .Secret ;
1618import com .flowci .core .secret .domain .TokenSecret ;
1719import com .flowci .core .secret .event .GetSecretEvent ;
@@ -49,6 +51,7 @@ public class GitServiceImpl implements GitService {
4951 @ PostConstruct
5052 public void init () {
5153 handlers .put (GitSource .GITHUB , new GitHubOperationHandler ());
54+ handlers .put (GitSource .GERRIT , new GerritOperationHandler ());
5255 }
5356
5457 @ Override
@@ -111,30 +114,35 @@ private OperationHandler getHandler(GitSource source) {
111114 return handler ;
112115 }
113116
114- private interface OperationHandler {
117+ private abstract class OperationHandler {
115118
116- GitConfig save (GitConfig config );
119+ abstract GitConfig save (GitConfig config );
117120
118- void writeCommit (GitCommitStatus commit , GitConfig config );
121+ abstract void writeCommit (GitCommitStatus commit , GitConfig config );
122+
123+ Secret fetch (String name , Class <?> expected ) {
124+ var event = eventManager .publish (new GetSecretEvent (GitServiceImpl .this , name ));
125+ Secret secret = event .getFetched ();
126+
127+ if (!expected .isInstance (secret )) {
128+ throw new ArgumentException ("Secret type is not matched" );
129+ }
130+
131+ return secret ;
132+ }
119133 }
120134
121- private class GitHubOperationHandler implements OperationHandler {
135+ private class GitHubOperationHandler extends OperationHandler {
122136
123137 private final GitAPIClient client = new GithubAPIClient (httpClient , objectMapper );
124138
125139 @ Override
126140 public GitConfig save (GitConfig config ) {
127- var event = eventManager .publish (new GetSecretEvent (this , config .getSecret ()));
128- Secret secret = event .getFetched ();
129-
130- if (!(secret instanceof TokenSecret )) {
131- throw new ArgumentException ("Token secret is required" );
132- }
141+ Secret secret = fetch (config .getSecret (), TokenSecret .class );
133142
134143 var optional = gitConfigDao .findBySource (GitSource .GITHUB );
135144 if (optional .isEmpty ()) {
136- GitConfig c = new GitConfig (GitSource .GITHUB , secret .getName ());
137- return gitConfigDao .save (c );
145+ return gitConfigDao .save (config );
138146 }
139147
140148 GitConfig c = optional .get ();
@@ -144,14 +152,39 @@ public GitConfig save(GitConfig config) {
144152
145153 @ Override
146154 public void writeCommit (GitCommitStatus commit , GitConfig config ) {
147- var c = get (GitSource .GITHUB );
148- var event = eventManager .publish (new GetSecretEvent (GitServiceImpl .this , c .getSecret ()));
149- Secret secret = event .getFetched ();
155+ Secret secret = fetch (config .getSecret (), TokenSecret .class );
156+ config .setSecretObj (secret );
157+ client .writeCommitStatus (commit , config );
158+ }
159+ }
160+
161+ private class GerritOperationHandler extends OperationHandler {
162+
163+ private final GitAPIClient client = new GerritAPIClient (httpClient , objectMapper );
164+
165+ @ Override
166+ public GitConfig save (GitConfig config ) {
167+ if (!(config instanceof GitConfigWithHost )) {
168+ throw new ArgumentException ("GitConfigWithHost is required" );
169+ }
150170
151- if (!(secret instanceof TokenSecret )) {
152- throw new ArgumentException ("Token secret is required" );
171+ var c = (GitConfigWithHost ) config ;
172+ Secret secret = fetch (config .getSecret (), AuthSecret .class );
173+
174+ var optional = gitConfigDao .findBySource (GitSource .GERRIT );
175+ if (optional .isEmpty ()) {
176+ return gitConfigDao .save (c );
153177 }
154178
179+ var exist = (GitConfigWithHost ) optional .get ();
180+ exist .setSecret (secret .getName ());
181+ exist .setHost (c .getHost ());
182+ return gitConfigDao .save (exist );
183+ }
184+
185+ @ Override
186+ public void writeCommit (GitCommitStatus commit , GitConfig config ) {
187+ Secret secret = fetch (config .getSecret (), AuthSecret .class );
155188 config .setSecretObj (secret );
156189 client .writeCommitStatus (commit , config );
157190 }
0 commit comments