java 使用 Github Enterprise Server API

文章介绍了如何通过GitHubAPI进行身份验证,并提供了示例代码,包括获取用户信息、仓库、分支,创建新Repository和branch,以及上传文件的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、apiurl及身份验证

        Github apiurl:https://api.github.com

        Github Enterprise Server apiurl:http(s)://hostname/api/v3

二、几个常用API的调用

1.get获取repo、branch

    /**
     * 获取所有repo
     * @param apiurl
     * @param accesstoken
     * @return
     */
    public String getRepos(String apiurl, String accesstoken){
        String url = apiurl+"/user/repos";

        Map<String,String> headers = new HashMap<>();
        headers.put("Content-Type","application/json");

        HttpResponse response = HttpRequest.get(url)
                .headerMap(headers, false)
                .bearerAuth(accesstoken)
                .timeout(5 * 60 * 1000)
                .execute();
        return response.body();
    }

    /**
     * 获取用户信息
     * @param apiurl
     * @param accesstoken
     * @return
     */
    public String getUser(String apiurl, String accesstoken){
        String url = apiurl+"/user";

        Map<String,String> headers = new HashMap<>();
        headers.put("Content-Type","application/json");

        HttpResponse response = HttpRequest.get(url)
                .headerMap(headers, false)
                .bearerAuth(accesstoken)
                .timeout(5 * 60 * 1000)
                .execute();
        return response.body();
    }

    /**
     * 获取指定branch
     * @param apiurl
     * @param accesstoken
     * @param username
     * @param repo
     * @param branch
     * @return
     */
    public String getBranch(String apiurl, String accesstoken, String username, String repo, String branch){
        // /repos/{owner}/{repo}/branches/{branch}
        String url = apiurl+"/repos/"+username+"/"+repo+"/branches/"+branch;

        Map<String,String> headers = new HashMap<>();
        headers.put("Content-Type","application/json");

        HttpResponse response = HttpRequest.get(url)
                .headerMap(headers, false)
                .bearerAuth(accesstoken)
                .timeout(5 * 60 * 1000)
                .execute();
        return response.body();
    }

    /**
     * 获取知道repo
     * @param apiurl
     * @param accesstoken
     * @param username
     * @param repo
     * @return
     */
    public String getRepo(String apiurl, String accesstoken, String username, String repo){
        // /repos/{owner}/{repo}/branches/{branch}
        String url = apiurl+"/repos/"+username+"/"+repo;

        Map<String,String> headers = new HashMap<>();
        headers.put("Content-Type","application/json");

        HttpResponse response = HttpRequest.get(url)
                .headerMap(headers, false)
                .bearerAuth(accesstoken)
                .timeout(5 * 60 * 1000)
                .execute();
        return response.body();
    }

2.新建Repository

    /**
     * 新建Repository
     */
    public String createRepo(String apiurl, String accesstoken, String name){
        String url = apiurl + "/user/repos";
        cn.hutool.json.JSONObject json = new cn.hutool.json.JSONObject();
        json.set("name",name);
        json.set("description","");
        json.set("private",false);
        json.set("auto_init",true); // 初始化仓库

        Map<String,String> headers = new HashMap<>();
        headers.put("Content-Type","application/json");

        HttpResponse response = HttpRequest.post(url)
                .headerMap(headers, false)
                .bearerAuth(accesstoken)
                .body(String.valueOf(json))
                .timeout(5 * 60 * 1000)
                .execute();
        return response.body();
    }

3.新建branch

github api 中没有直接创建branch的接口。

    /**
     * 新建branch
     */
    public String createBranch(String apiurl, String accesstoken, String username, String repo, String branch){
        HttpResponse response = HttpRequest.get(apiurl+"/repos/"+username+"/"+repo+"/commits")
                .bearerAuth(accesstoken)
                .timeout(5 * 60 * 1000)
                .execute();
        JsonArray commits = new JsonParser().parse(response.body()).getAsJsonArray();
        if (commits == null || commits.size() == 0) {
            System.out.println("仓库为空");
            return null;
        }
        // 获取到仓库的第一个提交的SHA值
        JsonElement firstCommit = commits.get(0);
        String sha = firstCommit.getAsJsonObject().get("sha").getAsString();
        String ref = "refs/heads/"+branch;
        response = HttpRequest.post(apiurl+"/repos/"+username+"/"+repo+"/git/refs")
                .bearerAuth(accesstoken) // token
                .body("{" +
                        "\"ref\":\"" + ref + "\"," +
                        "\"sha\":\"" + sha + "\"" +
                        "}")
                .timeout(5 * 60 * 1000)
                .execute();
        return response.body();
    }

4.上传文件

        v3版本,在请求头中添加 application/vnd.github.v3+json

    /**
     * 上传文件
     */
    public String uploadFile(String apiurl, String accesstoken, String username, String repo, String branch, MultipartFile file) {
        // https://api.github.com/repos/{用户名}/{仓库名}/{path}
        String url = apiurl+"/repos/"+username+"/"+repo+"/contents/"+file.getOriginalFilename();
        Map<String,String> headers = new HashMap<>();
        headers.put("Accept","application/vnd.github.v3+json");
        headers.put("Content-Type","application/json");
        cn.hutool.json.JSONObject json = new cn.hutool.json.JSONObject();
        json.set("message","");
        json.set("branch",branch);
        try {
            json.set("content", Base64.getEncoder().encodeToString(file.getBytes()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpResponse response = HttpRequest.put(url)
                .headerMap(headers, false)
                .bearerAuth(accesstoken)
                .body(String.valueOf(json))
                .timeout(5 * 60 * 1000)
                .execute();
        return response.body();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值