gitlab 获取指定分支下指定路径文件夹的解决方案

第一步:

获取accessToken 及你的 项目 id :

  • 获取 accessToken ,点击用户头像进入setting 按图示操作,第 3 步 填写你发起请求的域名。
  • 获取项目 id , 简单粗暴方案 进入 你项目仓库页面后 直接 源码搜索 project_id, value 就是 id 值。

第二步

更gitlab提供api 调用即可,大功告成 🤣🤣🤣

javascript 复制代码
<script>
  async function getGitlabFolderData(gitlabUrl, projectId, branch, folderPath, privateToken) {
    const url = `${gitlabUrl}/api/v4/projects/${projectId}/repository/tree?ref=${branch}&path=${folderPath}`;
    const options = {
      method: 'GET',
      headers: {
        'PRIVATE-TOKEN': privateToken
      }
    };

    try {
      const response = await fetch(url, options);
      if (!response.ok) {
        throw new Error(`Failed to retrieve folder data: ${response.status} ${response.statusText}`);
      }
      const data = await response.json();
      return data;
    } catch (error) {
      console.error(error);
      return null;
    }
  }

  // 配置参数
  const gitlabUrl = "http://10.11.13.111";
  const projectId = 215; // 项目 ID
  const branch = "test"; // 查询的分支
  const folderPath = "public/test"; // 查询的文件夹路径
  const privateToken = "xxxxxxxxxx"; // 私有令牌

  // 获取gitlab 仓库指定路径文件夹数据
  window.onload = function() {
    getGitlabFolderData(gitlabUrl, projectId, branch, folderPath, privateToken)
      .then(folderData => {
        console.log(folderData)
      });
  }
</script>
相关推荐
IT大白鼠1 天前
GitLab 未认证 RCE 应急实战CVE-2026-97229(CVSS 10.0)
网络·安全·gitlab
7177777 天前
Git自建用什么工具比较好:Gitea、Forgejo、GitLab CE与Gitee的选型对比
git·gitlab·gitea
colinu9 天前
Create gitlab-runner
gitlab
master33610 天前
GitLab 10.x → 19.5.x 升级路线图
gitlab
长不大的杨瑞杰10 天前
OpenCode v2 GitLab 集成实战:Issue 驱动开发、MR 审查与 CI/CD 最小权限
人工智能·ci/cd·gitlab·ai编程·opencode
极小狐16 天前
极狐GitLab万人并发架构实践,大规模组织的扩容路线图
架构·gitlab·极狐gitlab
Lvan的前端笔记17 天前
GitLab CI 环境变量整理
ci/cd·gitlab
李迟18 天前
CICD实例:解决GitLab Runner无法执行CICD问题
gitlab
hanjq_code20 天前
gitlab本地文件 跨分支 忽略改动 不提交
gitlab
极小狐20 天前
极狐GitLab集群高可用方案,多节点架构与故障自愈实践
架构·gitlab·高可用·极狐gitlab