前言

需求是通过gitlab的api获取其中的数据。

gitlab官方API文档:https://docs.gitlab.com/ee/api/users.html
在这里插入图片描述

gitlab的github地址:https://github.com/eutkin/gitlab4j-api/
在这里插入图片描述

获取用户 access token

首先我们需要获取gitlab的用户 access token, 并且在请求的时候附带这个token,gitlab才能认证我们的身份并返回数据。

1.登录 GitLab。
在这里插入图片描述

2.登录后,点击右上角的头像图标然后选择 Preferences。
在这里插入图片描述
在这里插入图片描述

3.在 access token 界面就可以新建token了。
在这里插入图片描述

当你是group的管理员之后,就可以通过该token获取users了。

Spring boot项目集成GitLab依赖

1 pom依赖

<dependency>
	<groupId>org.gitlab4j</groupId>
	<artifactId>gitlab4j-api</artifactId>
	<version>5.3.0</version>
</dependency>

2 配置文件

server:
  port: 8899

spring:
  # 配置数据源
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/db?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2b8
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.zaxxer.hikari.HikariDataSource

  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher


mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  # 扫描通用枚举包
  type-enums-package: com.mry.code.count.enums


files:
  upload:
    path: C:/file/ # 文件存储位置

code:
  count:
    gitlab:
      url: http://127.0.0.1:8181/
      authToken: xxxxxxxx

3 启动类

@MapperScan("com.mry.code.count.mapper")
@EnableTransactionManagement // 事务
@SpringBootApplication
public class SpringbootCodeCountServerApplication {
   

    public static void main(String[] args) {
   
        SpringApplication.run(SpringbootCodeCountServerApplication.class, args);
    }

}

4 核心代码

@Service
@Slf4j
public class CodeCountService {
   

    @Value("${code.count.gitlab.url}")
    private String url;

    @Value("${code.count.gitlab.authToken}")
    private String authToken;

    /**
     * 多条件分页查询
     *
     * @param current 当前页
     * @param size    页面显示的数据条数
     * @param project
     * @return
     */
    public ResponseDTO list(Integer current, Integer size, Project project) throws Exception {
   
        GitLabApi gitLabAPI = new GitLabApi(url, authToken)

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部