GitLab API 获取特定分支的所有提交记录

8

默认情况下,API GET /projects/:id/repository/commits 获取的是主分支的提交记录,但我想获取其他分支的提交记录。


1
它在文档中有说明:https://docs.gitlab.com/ee/api/commits.html#list-repository-commits - Oliver Charlesworth
看看奥利弗链接中的ref_name属性。 - Jawad
谢谢!我已经解决了。 - t.jl
3个回答

12
根据Gitlab文档,您可以添加参数“ref_name”,并指定要获取提交的分支:

GET /projects/:id/repository/commits?ref_name=my_branch_name

9
如果你想获取所有提交记录(而不仅仅是默认的20条),请将per_page属性值设置为,例如,9999 - Dawid Gałecki
3
每页的最大值为100,默认值为20。如果您使用上述评论中提到的9999,它将被忽略,最大值将被视为100。 - vipsy

3

我将补充K.Gol的回答:

需要添加参数:per_pagepage

对于参数per_page,超过100的值将被忽略。


https://docs.gitlab.com/ee/api/commits.html#get-references-a-commit-is-pushed-to - Hasan Tezcan

0

你可以使用我的递归 Node.js 代码获取特定分支的所有提交记录:

class GitlabAPI {
  host = "https://gitlab.com/api/v4";
  project = null;
  constructor(host, project) {
    this.host = host;
    this.project = project;
  }

  async loadCommits(branch, commits = [], page = 1) {
    const url = `${this.host}/projects/${this.project}/repository/commits`;

    const response = await axios.get(url, {
      params: { all: "true", per_page: 100, page, ref_name: branch },
    });

    console.log(`Parse ${page} page, results: ${response.data.length} commits`);

    if (response.data.length > 0) {
      return await this.loadCommits(branch, commits.concat(response.data), page + 1);
    } else {
      return commits;
    }
  }

  async commits(branch) {
    try {
      const commits = await this.loadCommits(branch);
      return commits;
    } catch (error) {
      console.log(error);
    }
  }
}

好了,完成:

const host = process.env.GITLAB_HOST;
const project_id = 325;
const branch = "master";
const GL = new GitlabAPI(host, project_id);

const commits = await GL.commits(branch);

编程愉快!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接