关于gitlab api的一点使用,以及简单的gitlab工具类

    工作中难免需要使用gitlab api来完成某些任务,现在写点这几天对gitlab api的一点使用心得。

     公司有实时计算平台,上面的文件是保存git中,但是平台需要进行版本控制,也就是需要回滚、对比等操作,单纯的使用git命令,底层调用Linux命令太麻烦,比如需要回滚到某文件的某个commitID,需要使用git log filename拿到commitid,然后git reset 也好,其余git命令也好,完成后也只是在本地完成,还需要push上去,当然,每次操作前也需要先pull到最新的文件。这样会导致平台再版本控制模块很浪费时间资源,因此需要使用gitlab api来完成。

    在使用gitlab api中也发现了官网的某个api的不完整性,以及v3 v4版本的差异,具体每个api的含义不做解释,真正需要用到gitlab api的当然知道怎么使用,下面就放上我的gitlabUtils方法,仅供参考。

package com.utils;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;


import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.TimeUnit;

public class GitlabUtils {

    public static final Integer PROJECT_ID=xxxxx;

    private static final String PRIVATE_TOKEN = "xxxxx";//使用gitlab的应该知道这里是什么吧

    private static final String CONTENT_URL = "https://code.xxx.com.cn/api/v3/projects/%d/repository/blobs/%s";

    private static final String URL = "https://code.xxx.com.cn/api/v3/projects/%d/repository/files";

    private static final String CURRENT_VERSION = "https://code.xxx.com.cn/api/v3/projects/%d/repository/files";

    private static final String DIFF_URL = "https://code.xxxp.com.cn/api/v4/projects/%d/repository/commits/%s/diff";

    private static final String HISTORY_URL = "https://code.xxx.com.cn/api/v4/projects/%d/repository/commits";

    private static final String VIEW_URL = "https://code.xxx.com.cn/api/v3/projects/%d/repository/files";

    public static final String BRANCH = "master";

    /**
     *
     * @param projectId
     * @param branch
     * @param path
     * @return  查看文件内容
     * @throws UnsupportedEncodingException
     */
    public static String getViewFileContent(Integer projectId,
                                       String branch,
                                       String path) throws UnsupportedEncodingException {

        Request request = new Request.Builder()
                .url(String.format(VIEW_URL + "?file_path=%s&ref=%s", projectId, path, branch))
                .header("PRIVATE-TOKEN", PRIVATE_TOKEN)
                .get()
                .build();

        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(5, TimeUnit.SECONDS)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                String row = Objects.requireNonNull(response.body()).string();
                // log.info("get catalogue files {}: {}", path, row);
                return row;
            }
        } catch (Exception e) {
            // log.error("get catalogue files " + path + " error.", e);
        }
        retur
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值