1616
1717package com .flow .platform .api .service ;
1818
19- import com .flow .platform .api .config .AppConfig ;
2019import com .flow .platform .api .domain .envs .GitEnvs ;
2120import com .flow .platform .api .domain .node .Node ;
2221import com .flow .platform .api .git .GitClientBuilder ;
2322import com .flow .platform .api .git .GitHttpClientBuilder ;
23+ import com .flow .platform .api .git .GitLabClientBuilder ;
2424import com .flow .platform .api .git .GitSshClientBuilder ;
2525import com .flow .platform .api .util .EnvUtil ;
2626import com .flow .platform .api .util .NodeUtil ;
3232import com .flow .platform .util .git .GitException ;
3333import com .flow .platform .util .git .model .GitCommit ;
3434import com .flow .platform .util .git .model .GitSource ;
35- import com .google .common .collect .Sets ;
3635import java .io .IOException ;
3736import java .nio .file .Files ;
3837import java .nio .file .Path ;
3938import java .nio .file .Paths ;
40- import java .util .ArrayList ;
41- import java .util .Collection ;
4239import java .util .HashMap ;
4340import java .util .List ;
4441import java .util .Map ;
4542import javax .annotation .PostConstruct ;
4643import org .eclipse .jgit .lib .ProgressMonitor ;
47- import org .eclipse .jgit .lib .Ref ;
4844import org .springframework .beans .factory .annotation .Autowired ;
4945import org .springframework .stereotype .Service ;
5046
5450@ Service
5551public class GitServiceImpl implements GitService {
5652
53+ private class EmptyProgressListener implements ProgressListener {
54+
55+ @ Override
56+ public void onStart () {
57+
58+ }
59+
60+ @ Override
61+ public void onStartTask (String task ) {
62+
63+ }
64+
65+ @ Override
66+ public void onProgressing (String task , int total , int progress ) {
67+
68+ }
69+
70+ @ Override
71+ public void onFinishTask (String task ) {
72+
73+ }
74+ }
75+
5776 private final static Logger LOGGER = new Logger (GitService .class );
5877
5978 private final Map <GitSource , Class <? extends GitClientBuilder >> clientBuilderType = new HashMap <>(6 );
@@ -65,32 +84,27 @@ public class GitServiceImpl implements GitService {
6584 public void init () {
6685 clientBuilderType .put (GitSource .UNDEFINED_SSH , GitSshClientBuilder .class );
6786 clientBuilderType .put (GitSource .UNDEFINED_HTTP , GitHttpClientBuilder .class );
87+ clientBuilderType .put (GitSource .GITLAB , GitLabClientBuilder .class );
6888 }
6989
7090 @ Override
71- public String clone (Node node , String filePath , ProgressListener progressListener ) throws GitException {
91+ public String fetch (Node node , String filePath , ProgressListener progressListener ) throws GitException {
7292 GitClient client = gitClientInstance (node );
7393
74- if (progressListener ! = null ) {
75- progressListener . onStart ();
94+ if (progressListener = = null ) {
95+ progressListener = new EmptyProgressListener ();
7696 }
7797
98+ progressListener .onStart ();
7899 String branch = node .getEnv (GitEnvs .FLOW_GIT_BRANCH , "master" );
79- client .clone (branch , Sets .newHashSet (filePath ), new GitCloneProgressMonitor (progressListener ));
80-
81- if (progressListener != null ) {
82- progressListener .onFinish ();
83- }
84-
85- return fetch (client , filePath );
100+ return client .fetch (branch , filePath , new GitCloneProgressMonitor (progressListener ));
86101 }
87102
88103 @ Override
89104 public List <String > branches (Node node ) {
90105 GitClient client = gitClientInstance (node );
91106 try {
92- Collection <Ref > branches = client .branches ();
93- return toRefString (branches );
107+ return client .branches ();
94108 } catch (GitException e ) {
95109 throw new IllegalStatusException ("Cannot load branch list from git: " + e .getMessage ());
96110 }
@@ -100,8 +114,7 @@ public List<String> branches(Node node) {
100114 public List <String > tags (Node node ) {
101115 GitClient client = gitClientInstance (node );
102116 try {
103- Collection <Ref > tags = client .tags ();
104- return toRefString (tags );
117+ return client .tags ();
105118 } catch (GitException e ) {
106119 throw new IllegalStatusException ("Cannot load tag list from git: " + e .getMessage ());
107120 }
@@ -142,26 +155,19 @@ public void start(int totalTasks) {
142155
143156 @ Override
144157 public void beginTask (String title , int totalWork ) {
145- this .currentTask = title ;
146- this .currentTotalWork = totalWork ;
147-
148- if (progressListener != null ) {
149- progressListener .onStartTask (title );
150- }
158+ currentTask = title ;
159+ currentTotalWork = totalWork ;
160+ progressListener .onStartTask (title );
151161 }
152162
153163 @ Override
154164 public void update (int completed ) {
155- if (progressListener != null ) {
156- progressListener .onProgressing (currentTask , currentTotalWork , completed );
157- }
165+ progressListener .onProgressing (currentTask , currentTotalWork , completed );
158166 }
159167
160168 @ Override
161169 public void endTask () {
162- if (progressListener != null ) {
163- progressListener .onFinishTask (currentTask );
164- }
170+ progressListener .onFinishTask (currentTask );
165171 }
166172
167173 @ Override
@@ -170,32 +176,20 @@ public boolean isCancelled() {
170176 }
171177 }
172178
173- private List <String > toRefString (Collection <Ref > refs ) {
174- List <String > refStringList = new ArrayList <>(refs .size ());
175-
176- for (Ref ref : refs ) {
177- // convert ref name from ref/head/master to master
178- String refName = ref .getName ();
179- int lastIndexOfSlash = refName .lastIndexOf ('/' );
180- String simpleName = refName .substring (lastIndexOfSlash + 1 );
181-
182- // add to result list
183- refStringList .add (simpleName );
184- }
185-
186- return refStringList ;
187- }
188-
189179 /**
190180 * Init git client from flow env
191181 *
192182 * - FLOW_GIT_SOURCE
193- * - FLOW_GIT_URL
183+ * - FLOW_GIT_URL : UNDEFINED_HTTP / UNDEFINED_SSH
194184 * - FLOW_GIT_BRANCH
195185 * - FLOW_GIT_SSH_PRIVATE_KEY
196186 * - FLOW_GIT_SSH_PUBLIC_KEY
197187 * - FLOW_GIT_HTTP_USER
198188 * - FLOW_GIT_HTTP_PASS
189+ *
190+ * - FLOW_GITLAB_HOST
191+ * - FLOW_GITLAB_TOKEN
192+ * - FLOW_GITLAB_PROJECT
199193 */
200194 private GitClient gitClientInstance (Node node ) {
201195 checkRequiredEnv (node );
@@ -215,9 +209,13 @@ private GitClient gitClientInstance(Node node) {
215209 throw new IllegalStatusException ("Fail to create GitClientBuilder instance: " + e .getMessage ());
216210 }
217211
218- GitClient client = builder .build ();
219- LOGGER .trace ("Git client initialized: %s" , client );
220- return client ;
212+ try {
213+ GitClient client = builder .build ();
214+ LOGGER .trace ("Git client initialized: %s" , client );
215+ return client ;
216+ } catch (GitException e ) {
217+ throw new IllegalStatusException ("Unable to init git client for " + source );
218+ }
221219 }
222220
223221 /**
@@ -228,29 +226,4 @@ private Path gitSourcePath(Node node) throws IOException {
228226 Files .createDirectories (flowWorkspace );
229227 return Paths .get (flowWorkspace .toString (), SOURCE_FOLDER_NAME );
230228 }
231-
232- /**
233- * Get target file from local git repo folder
234- */
235- private String fetch (GitClient gitClient , String filePath ) {
236- Path targetPath = Paths .get (gitClient .targetPath ().toString (), filePath );
237-
238- if (Files .exists (targetPath )) {
239- return getContent (targetPath );
240- }
241-
242- return null ;
243- }
244-
245- /**
246- * Get file content from source code folder of flow workspace
247- */
248- private String getContent (Path path ) {
249- try {
250- return com .google .common .io .Files .toString (path .toFile (), AppConfig .DEFAULT_CHARSET );
251- } catch (IOException e ) {
252- LOGGER .warn ("Fail to load file content from %s" , path .toString ());
253- return null ;
254- }
255- }
256229}
0 commit comments