提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
简介
在开发中,偶尔会有一些关于Gitlab的二开需求,本文将介绍使用Java语言操作Gitlab提供的API,进行仓库的相关操作
GitLab的部署可以查阅docker部署GitLab文章进行搭建
一、依赖、常量
Maven依赖
<dependency>
<groupId>org.gitlab4j</groupId>
<artifactId>gitlab4j-api</artifactId>
<version>4.19.0</version>
</dependency>
定义常量类
可以将常量抽取到配置文件XML中,通过@ConfigurationProperties读取,本文采用常量类方式进行抽取。
public class EduConstants {
/**
* GitLab远程主机地址
*/
public static final String GITLAB_REMOTE_USERNAME = "http://192.168.200.250:9980";
/**
* GitLab远程主机密码(自己搭建的时,设计的密码)
*/
public static final String GITLAB_REMOTE_PWD = "123456789";
/**
* gitLab部署远程主机地址(免邮箱注册用户时用到)
*/
public static final String REMOTE_HOSTNAME = "192.168.200.200";
/**
* gitLab部署远程主机用户名
*/
public static final String REMOTE_USERNAME = "root";
/**
* gitLab部署远程主机密码
*/
public static final String REMOTE_PWD = "root";
/**
* gitLab访问令牌tonken(根据个人环境生成密钥)
*/
public static final String GITLAB_TOKEN = "xxxxxxx-xxxxxx-xxxxxxxxx";
}
二、增删改查
1、新增私有仓库
实体类
public class CreateRepoDto {
/**
* gitLab-账号
*/
private String userName;
/**
* 项目Id
*/
private Integer projectId;
/**
* 项目名称
*/
private String projectName;
/**
* 项目介绍
*/
private String projectDescribe;
/**
* 项目是否开源
*/
private String visibility;
}
Controller
@RestController
@RequestMapping("/repo")
public class EduUserRepoController {
@Autowired
private EduUserRepoService eduUserRepoService;
/**
* 创建新的项目
* {
* "userName": "gitLab",
* "projectName": "testPublic",
* "projectDescribe": "自己搭建的内容"
* }
*/
@PostMapping("createRepo")
public AjaxResult createRepo(@RequestBody CreateRepoDto createRepoDto) {
String msg = eduUserRepoService.createRepo(createRepoDto);
if (StringUtils.isNotEmpty(msg)) {
return AjaxResult.error(msg);
}
return AjaxResult.success();
}
}
Service
@Service
public interface EduUserRepoService {
/**
* 创建新的项目
*/
String createRepo(CreateRepoDto createRepoDto);
}
ServiceImpl
public class EduUserRepoServiceImpl implements EduUserRepoService {
/**
* 创建新的项目
*/
@Override
public String createRepo(CreateRepoDto createRepoDto) {
//获取用户名称(默认是root用户)
String userName = "root";
//进行登录,GitLab地址、用户名、密码
GitLabApi gitlabapi = null;
try {
gitlabapi = GitLabApi.oauth2Login(
GITLAB_REMOTE_USERNAME,
userName,
GITLAB_REMOTE_PWD);
} catch (GitLabApiException e) {
}
String msg = "";
Project projectSpec = new Project()
.withName(createRepoDto.getProjectName());
//项目介绍不为空,则添加
if (StringUtils.isNotEmpty(createRepoDto.getProjectDescribe())) {
projectSpec.withDescription(createRepoDto.getProjectDescribe());
}
try {
gitLabApi.getProjectApi().createProject(projectSpec);
} catch (GitLabApiException e) {
//创建过程中出现的问题后,进行接收问题
msg = e.getMessage();
}
return msg;
}
}
2、删除指定仓库
Controller
@RestController
@RequestMapping("/repo")
public class EduUserRepoController {
@Autowired
private EduUserRepoService eduUserRepoService;
/**
* 删除指定仓库
*/
@GetMapping("deleteRepo")
public AjaxResult deleteRepo(String projectId) {
return AjaxResult.success(eduUserRepoService.deleteRepo(projectId));
}
}
Service
public interface EduUserRepoService {
/**
* 删除指定仓库
*
* @return
*/
Object deleteRepo(String projectId);
}
ServiceImpl
public class EduUserRepoServiceImpl implements EduUserRepoService {
/**
* 删除指定仓库
*
* @return
*/
@Override
public Object deleteRepo(String projectId) {
//获取用户名称(指定)
String userName = "root";
//进行登录,GitLab地址、用户名、密码
GitLabApi gitlabapi = null;
try {
gitlabapi = GitLabApi.oauth2Login(
GITLAB_REMOTE_USERNAME,
userName,
GITLAB_REMOTE_PWD);
} catch (GitLabApiException e) {
}
String msg = "";
try {
gitLabApi.getProjectApi().deleteProject(projectId);
} catch (GitLabApiException e) {
e.printStackTrace();
}
msg = "删除成功";
return msg;
}
}
3、修改项目简介和是否开源
Controller
@RestController
@RequestMapping("/repo")
public class EduUserRepoController {
@Autowired
private EduUserRepoService eduUserRepoService;
/**
* 修改项目简介和是否开源
*(Dto上文有使用到)
* @return
*/
@PostMapping("updateProject")
public AjaxResult updateProject(@RequestBody CreateRepoDto createRepoDto) {
return AjaxResult.success(eduUserRepoService.updateProject(createRepoDto));
}
}
Service
public interface EduUserRepoService {
/**
* 修改项目简介和是否开源
*/
Object updateProject(CreateRepoDto createRepoDto);
}
ServiceImpl
public class EduUserRepoServiceImpl implements EduUserRepoService {
/**
* 修改项目简介和是否开源
*/
@Override
public Object updateProject(CreateRepoDto createRepoDto) {
//获取用户名称
String userName = "root";
//进行登录,GitLab地址、用户名、密码
GitLabApi gitlabapi = null;
try {
gitlabapi = GitLabApi.oauth2Login(
GITLAB_REMOTE_USERNAME,
userName,
GITLAB_REMOTE_PWD);
} catch (GitLabApiException e) {
} Project project = new Project();
//根据项目的id,修改对应内容
project.setId(createRepoDto.getProjectId());
project.setDescription(createRepoDto.getProjectDescribe());
//修改仓库为私有仓库
if (createRepoDto.getVisibility().equals("private")) {
project.setVisibility(Visibility.PRIVATE);
System.out.println("修改项目为私有");
}
//修改仓库为公开仓库
if (createRepoDto.getVisibility().equals("public")) {
project.setVisibility(Visibility.PUBLIC);
System.out.println("修改项目为公开");
}
Object msg = "";
try {
gitLabApi.getProjectApi().updateProject(project);
msg = "修改成功";
} catch (GitLabApiException e) {
//e.printStackTrace();
}
return msg;
}
}
4、获取目录
通过API提供的 getTree 进行获取指定项目、目录、分支下的目录内容
// 参数一:项目的唯一id;参数二:所在目录地址,例如-(/、src、src/main),参数三:分支,例如-(main、master)
@Override
public List<TreeFileItem> getProject(String projectId, String filePath, String ref) {
//获取用户名称
String userName = SecurityUtils.getUsername();
return getAllFilesByProjectId(userName, projectId, filePath, ref);
}
// 方法如下↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
/*
* 获取指定的目录结构
* */
private static List<TreeFileItem> getAllFilesByProjectId(String userName, String id, String path, String ref) {
GitLabApi gitLabApi = loginGitLabApi(userName);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<TreeFileItem> list = new ArrayList<>();
try {
Date since = ISO8601.toDate("2017-01-01T00:00:00Z");
Date until = new Date(); // now
List<TreeItem> treeList = gitLabApi.getRepositoryApi().getTree(id, path, ref);
for (TreeItem item : treeList) {
TreeFileItem obj = new TreeFileItem();
obj.setItem(item);
List<Commit> clist = gitLabApi.getCommitsApi().getCommits(id, ref, since, until, item.getPath());
obj.setMessage(clist.get(0).getMessage());
obj.setCommittedDate(sdf.format(clist.get(0).getCommittedDate()));
list.add(obj);
}
} catch (Exception e) {
// e.printStackTrace();
}
return list;
}
5、获取指定文件内容
本文只演示README.md文件的内容展示,其他文件类似
通过 API 提供的 getFile 方法进行返回指定文件的内 容
@Override
public Object getFileInfo(String projectId, String filePath, String ref) {
//获取用户名称
String userName = SecurityUtils.getUsername();
GitLabApi gitLabApi = loginGitLabApi(userName);
RepositoryFile file = null;
String fileInfo = null;
try {
file = gitLabApi.getRepositoryFileApi().getFile(projectId, filePath, ref);
} catch (GitLabApiException e) {
e.printStackTrace();
}
if (StringUtils.isNotEmpty(file.getContent())) {
String content = file.getContent();
byte[] decode = Base64.getDecoder().decode(content.getBytes());
fileInfo = new String(decode);
}
return fileInfo;
}
三、后续更新~
催更私信作者