Skip to content

Commit a9d31b7

Browse files
author
yang.guo
authored
Merge pull request #109 from FlowCI/feature/api/gitlab_native
Feature/api/gitlab native
2 parents ce747b7 + bf808d5 commit a9d31b7

File tree

23 files changed

+668
-119
lines changed

23 files changed

+668
-119
lines changed
Binary file not shown.

platform-api/src/main/java/com/flow/platform/api/domain/envs/GitEnvs.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public enum GitEnvs implements EnvKey {
2626
*/
2727
FLOW_GIT_SOURCE,
2828

29+
/**
30+
* For diff FLOW_GIT_SOURCE
31+
* - UNDEFINED_SSH and UNDEFINED_HTTP is repo url
32+
* - GITLAB is gitlab host url
33+
*/
2934
FLOW_GIT_URL,
3035

3136
FLOW_GIT_BRANCH,
@@ -69,5 +74,12 @@ public enum GitEnvs implements EnvKey {
6974

7075
FLOW_GIT_HTTP_USER,
7176

72-
FLOW_GIT_HTTP_PASS
77+
FLOW_GIT_HTTP_PASS,
78+
79+
/**
80+
* Env variables for GitLab
81+
*/
82+
FLOW_GITLAB_TOKEN,
83+
84+
FLOW_GITLAB_PROJECT
7385
}

platform-api/src/main/java/com/flow/platform/api/git/GitClientBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.flow.platform.api.domain.envs.GitEnvs;
2020
import com.flow.platform.api.domain.node.Node;
2121
import com.flow.platform.util.git.GitClient;
22+
import com.flow.platform.util.git.GitException;
2223
import java.nio.file.Path;
2324

2425
/**
@@ -41,5 +42,5 @@ public GitClientBuilder(final Node node, final Path sourceFolder) {
4142
this.sourceFolder = sourceFolder;
4243
}
4344

44-
public abstract GitClient build();
45+
public abstract GitClient build() throws GitException;
4546
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2017 flow.ci
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.flow.platform.api.git;
18+
19+
import com.flow.platform.api.domain.envs.GitEnvs;
20+
import com.flow.platform.api.domain.node.Node;
21+
import com.flow.platform.util.git.GitClient;
22+
import com.flow.platform.util.git.GitException;
23+
import com.flow.platform.util.git.GitLabClient;
24+
import java.nio.file.Path;
25+
26+
/**
27+
* @author yang
28+
*/
29+
public class GitLabClientBuilder extends GitClientBuilder {
30+
31+
private final String host;
32+
33+
private final String token;
34+
35+
private final String project;
36+
37+
public GitLabClientBuilder(Node node, Path sourceFolder) {
38+
super(node, sourceFolder);
39+
host = node.getEnv(GitEnvs.FLOW_GIT_URL);
40+
token = node.getEnv(GitEnvs.FLOW_GITLAB_TOKEN);
41+
project = node.getEnv(GitEnvs.FLOW_GITLAB_PROJECT);
42+
}
43+
44+
@Override
45+
public GitClient build() throws GitException {
46+
return new GitLabClient(host, token, project);
47+
}
48+
}

platform-api/src/main/java/com/flow/platform/api/service/GitService.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ interface ProgressListener {
4646
void onProgressing(String task, int total, int progress);
4747

4848
void onFinishTask(String task);
49-
50-
void onFinish();
5149
}
5250

5351
/**
@@ -58,7 +56,7 @@ interface ProgressListener {
5856
* @param progress listener for git progress
5957
* @return file content
6058
*/
61-
String clone(Node node, String filePath, ProgressListener progress) throws GitException;
59+
String fetch(Node node, String filePath, ProgressListener progress) throws GitException;
6260

6361
/**
6462
* Fetch branches from git repo

platform-api/src/main/java/com/flow/platform/api/service/GitServiceImpl.java

Lines changed: 49 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
package com.flow.platform.api.service;
1818

19-
import com.flow.platform.api.config.AppConfig;
2019
import com.flow.platform.api.domain.envs.GitEnvs;
2120
import com.flow.platform.api.domain.node.Node;
2221
import com.flow.platform.api.git.GitClientBuilder;
2322
import com.flow.platform.api.git.GitHttpClientBuilder;
23+
import com.flow.platform.api.git.GitLabClientBuilder;
2424
import com.flow.platform.api.git.GitSshClientBuilder;
2525
import com.flow.platform.api.util.EnvUtil;
2626
import com.flow.platform.api.util.NodeUtil;
@@ -32,19 +32,15 @@
3232
import com.flow.platform.util.git.GitException;
3333
import com.flow.platform.util.git.model.GitCommit;
3434
import com.flow.platform.util.git.model.GitSource;
35-
import com.google.common.collect.Sets;
3635
import java.io.IOException;
3736
import java.nio.file.Files;
3837
import java.nio.file.Path;
3938
import java.nio.file.Paths;
40-
import java.util.ArrayList;
41-
import java.util.Collection;
4239
import java.util.HashMap;
4340
import java.util.List;
4441
import java.util.Map;
4542
import javax.annotation.PostConstruct;
4643
import org.eclipse.jgit.lib.ProgressMonitor;
47-
import org.eclipse.jgit.lib.Ref;
4844
import org.springframework.beans.factory.annotation.Autowired;
4945
import org.springframework.stereotype.Service;
5046

@@ -54,6 +50,29 @@
5450
@Service
5551
public 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
}

platform-api/src/main/java/com/flow/platform/api/service/node/YmlServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ private void findNodeCredential(Node node) {
242242
private ThreadPoolTaskExecutor findThreadPoolFromCache(String path) throws ExecutionException {
243243
return nodeThreadPool.get(path, () -> {
244244
ThreadPoolTaskExecutor taskExecutor = ThreadUtil
245-
.createTaskExecutor(NODE_THREAD_POOL_SIZE, NODE_THREAD_POOL_SIZE, 0, "git-clone-task");
245+
.createTaskExecutor(NODE_THREAD_POOL_SIZE, NODE_THREAD_POOL_SIZE, 0, "git-fetch-task");
246246
taskExecutor.initialize();
247247
return taskExecutor;
248248
});

platform-api/src/main/java/com/flow/platform/api/task/UpdateNodeYmlTask.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,16 @@ public UpdateNodeYmlTask(Node root,
6363
public void run() {
6464
String yml;
6565
try {
66-
yml = gitService.clone(root, AppConfig.DEFAULT_YML_FILE, new GitProgressListener());
66+
yml = gitService.fetch(root, AppConfig.DEFAULT_YML_FILE, new GitProgressListener());
67+
nodeService.updateYmlState(root, YmlStatusValue.GIT_LOADED, null);
6768
} catch (Throwable e) {
6869
// check yml status is running since exception will be throw if manual stop the git clone thread
6970
if (YmlStatusValue.isLoadingStatus(root.getEnv(FlowEnvs.FLOW_YML_STATUS))) {
7071
Throwable rootCause = ExceptionUtil.findRootCause(e);
71-
LOGGER.error("Unable to clone from git repo", rootCause);
72+
LOGGER.error("Unable to fetch from git repo", rootCause);
7273
nodeService.updateYmlState(root, YmlStatusValue.ERROR, rootCause.getMessage());
7374
}
75+
7476
return;
7577
}
7678

@@ -111,10 +113,5 @@ public void onProgressing(String task, int total, int progress) {
111113
public void onFinishTask(String task) {
112114
LOGGER.debug("Task finish: %s", task);
113115
}
114-
115-
@Override
116-
public void onFinish() {
117-
nodeService.updateYmlState(root, YmlStatusValue.GIT_LOADED, null);
118-
}
119116
}
120117
}

platform-api/src/test/java/com/flow/platform/api/test/TestBase.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
public abstract class TestBase {
9898

9999
protected final static String GITHUB_TEST_REPO_SSH = "[email protected]:flow-ci-plugin/for-testing.git";
100-
protected final static String GITHUB_TEST_REPO_HTTP = "git@github.com:flow-ci-plugin/for-testing.git";
100+
protected final static String GITHUB_TEST_REPO_HTTP = "https://github.com/flow-ci-plugin/for-testing.git";
101101

102102
static {
103103
System.setProperty("flow.api.env", "test");

0 commit comments

Comments
 (0)