Skip to content

Commit 035577d

Browse files
author
Yang Guo
committed
enable to list project for gitlab
1 parent 81fb065 commit 035577d

File tree

5 files changed

+179
-8
lines changed

5 files changed

+179
-8
lines changed

platform-util-git/src/main/java/com/flow/platform/util/git/GitClient.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package com.flow.platform.util.git;
1818

1919
import com.flow.platform.util.git.model.GitCommit;
20+
import com.flow.platform.util.git.model.GitProject;
2021
import java.io.File;
2122
import java.nio.file.Path;
2223
import java.util.List;
@@ -72,6 +73,16 @@ public interface GitClient {
7273
*/
7374
void pull(String branch, ProgressMonitor monitor) throws GitException;
7475

76+
/**
77+
* Git project list
78+
*/
79+
List<GitProject> projects() throws GitException;
80+
81+
/**
82+
* Set current for branches, tags and commit
83+
*/
84+
void setCurrentProject(GitProject project) throws GitException;
85+
7586
/**
7687
* Load all branches from git
7788
*/

platform-util-git/src/main/java/com/flow/platform/util/git/GitLabClient.java

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package com.flow.platform.util.git;
1818

1919
import com.flow.platform.util.git.model.GitCommit;
20+
import com.flow.platform.util.git.model.GitProject;
21+
import com.google.common.base.Strings;
2022
import java.io.File;
2123
import java.io.FileNotFoundException;
2224
import java.io.IOException;
@@ -54,14 +56,7 @@ public GitLabClient(String host, String token, String project) throws GitExcepti
5456

5557
// init gitlab project if project name given
5658
if (project != null) {
57-
try {
58-
this.project = this.connect.getProject(project);
59-
} catch (IOException e) {
60-
if (e instanceof FileNotFoundException) {
61-
throw new GitException("Project not found: " + e.getMessage());
62-
}
63-
throw new GitException(e.getMessage());
64-
}
59+
setCurrentProject(project);
6560
}
6661
}
6762

@@ -90,6 +85,8 @@ public File clone(String branch, Set<String> checkoutFiles, ProgressMonitor moni
9085

9186
@Override
9287
public String fetch(String branch, String filePath, ProgressMonitor monitor) throws GitException {
88+
checkProject();
89+
9390
try {
9491
GitlabRepositoryFile file = connect.getRepositoryFile(project, filePath, branch);
9592
String base64Content = file.getContent();
@@ -105,8 +102,39 @@ public void pull(String branch, ProgressMonitor monitor) throws GitException {
105102
throw new UnsupportedOperationException("Pull not supported for GitLab");
106103
}
107104

105+
@Override
106+
public List<GitProject> projects() throws GitException {
107+
try {
108+
List<GitlabProject> projects = connect.getMembershipProjects();
109+
List<GitProject> results = new ArrayList<>(projects.size());
110+
for (GitlabProject project : projects) {
111+
Integer id = project.getId();
112+
String name = project.getName();
113+
String fullName = project.getNameWithNamespace();
114+
results.add(new GitProject(id.toString(), name, fullName));
115+
}
116+
return results;
117+
} catch (IOException e) {
118+
throw new GitException(e.getMessage());
119+
}
120+
}
121+
122+
/**
123+
* Get project id or full name from GitProject and set current project
124+
*/
125+
@Override
126+
public void setCurrentProject(GitProject project) throws GitException {
127+
String id = project.getId();
128+
if (Strings.isNullOrEmpty(id)) {
129+
id = project.getFullName();
130+
}
131+
setCurrentProject(id);
132+
}
133+
108134
@Override
109135
public List<String> branches() throws GitException {
136+
checkProject();
137+
110138
try {
111139
List<GitlabBranch> branches = connect.getBranches(project);
112140
return toStringBranches(branches);
@@ -117,6 +145,8 @@ public List<String> branches() throws GitException {
117145

118146
@Override
119147
public List<String> tags() throws GitException {
148+
checkProject();
149+
120150
try {
121151
List<GitlabTag> tags = connect.getTags(project);
122152
return toStringTags(tags);
@@ -127,6 +157,8 @@ public List<String> tags() throws GitException {
127157

128158
@Override
129159
public GitCommit commit(String refName) throws GitException {
160+
checkProject();
161+
130162
try {
131163
List<GitlabCommit> lastCommits = connect.getLastCommits(project.getId(), refName);
132164
GitlabCommit gitlabCommit = lastCommits.get(0);
@@ -141,6 +173,27 @@ public GitCommit commit(String refName) throws GitException {
141173
}
142174
}
143175

176+
private void checkProject() throws GitException {
177+
if (project == null) {
178+
throw new GitException("Project is not set for GitLab");
179+
}
180+
}
181+
182+
private void setCurrentProject(String project) throws GitException {
183+
if (Strings.isNullOrEmpty(project)) {
184+
throw new GitException("Project id or name is missing on GitLab");
185+
}
186+
187+
try {
188+
this.project = this.connect.getProject(project);
189+
} catch (IOException e) {
190+
if (e instanceof FileNotFoundException) {
191+
throw new GitException("Project not found: " + e.getMessage());
192+
}
193+
throw new GitException(e.getMessage());
194+
}
195+
}
196+
144197
private List<String> toStringBranches(Collection<GitlabBranch> branches) {
145198
List<String> list = new ArrayList<>(branches.size());
146199
for (GitlabBranch branch : branches) {

platform-util-git/src/main/java/com/flow/platform/util/git/JGitBasedClient.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.flow.platform.util.ExceptionUtil;
2020
import com.flow.platform.util.git.model.GitCommit;
21+
import com.flow.platform.util.git.model.GitProject;
2122
import com.google.common.base.Strings;
2223
import com.google.common.collect.Sets;
2324
import java.io.BufferedReader;
@@ -161,6 +162,16 @@ public void pull(String branch, ProgressMonitor monitor) throws GitException {
161162
}
162163
}
163164

165+
@Override
166+
public List<GitProject> projects() throws GitException {
167+
throw new UnsupportedOperationException("Unable to list git projects for JGit based client");
168+
}
169+
170+
@Override
171+
public void setCurrentProject(GitProject project) throws GitException {
172+
throw new UnsupportedOperationException("Unable to set git project for JGit based client");
173+
}
174+
164175
@Override
165176
public List<String> branches() throws GitException {
166177
try {
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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.util.git.model;
18+
19+
import java.io.Serializable;
20+
21+
/**
22+
* @author yang
23+
*/
24+
public class GitProject implements Serializable {
25+
26+
private String id;
27+
28+
private String name;
29+
30+
private String fullName;
31+
32+
public GitProject() {
33+
}
34+
35+
public GitProject(String id, String name, String fullName) {
36+
this.id = id;
37+
this.name = name;
38+
this.fullName = fullName;
39+
}
40+
41+
public String getId() {
42+
return id;
43+
}
44+
45+
public void setId(String id) {
46+
this.id = id;
47+
}
48+
49+
public String getName() {
50+
return name;
51+
}
52+
53+
public void setName(String name) {
54+
this.name = name;
55+
}
56+
57+
public String getFullName() {
58+
return fullName;
59+
}
60+
61+
public void setFullName(String fullName) {
62+
this.fullName = fullName;
63+
}
64+
65+
@Override
66+
public boolean equals(Object o) {
67+
if (this == o) {
68+
return true;
69+
}
70+
if (o == null || getClass() != o.getClass()) {
71+
return false;
72+
}
73+
74+
GitProject that = (GitProject) o;
75+
76+
return id.equals(that.id);
77+
}
78+
79+
@Override
80+
public int hashCode() {
81+
return id.hashCode();
82+
}
83+
}

platform-util-git/src/test/java/com/flow/platform/util/git/test/GitLabClientTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.flow.platform.util.git.GitException;
2323
import com.flow.platform.util.git.GitLabClient;
2424
import com.flow.platform.util.git.model.GitCommit;
25+
import com.flow.platform.util.git.model.GitProject;
2526
import com.google.common.base.Strings;
2627
import java.util.List;
2728
import org.junit.Assert;
@@ -71,6 +72,18 @@ public void should_get_commit_info() throws Throwable {
7172
Assert.assertNotNull(commit.getMessage());
7273
}
7374

75+
@Test
76+
public void should_list_all_projects() throws Throwable {
77+
List<GitProject> projects = client.projects();
78+
Assert.assertNotNull(projects);
79+
Assert.assertTrue(projects.size() >= 1);
80+
81+
GitProject project = projects.get(0);
82+
Assert.assertNotNull(project.getId());
83+
Assert.assertNotNull(project.getName());
84+
Assert.assertNotNull(project.getFullName());
85+
}
86+
7487
@Test
7588
public void should_list_branches() throws Throwable {
7689
List<String> branches = client.branches();

0 commit comments

Comments
 (0)