gitlab公共仓guest用户没有克隆权限?
【摘要】 guest用户没有clone权限但是可以看,clone权限需要申请成为开发成员;如果感觉申请成为成员比较麻烦,那么你可以试试下面这段下载脚本;依赖FileSaver.js,JSZip v3.5.0,jQuery v3.5.1下载文本文件async function downfile(zip,filepath,fileurl){ return new Promise((resolve,reje...
guest用户没有clone权限但是可以看,clone权限需要申请成为开发成员;
如果感觉申请成为成员比较麻烦,那么你可以试试下面这段下载脚本;
依赖FileSaver.js,JSZip v3.5.0,jQuery v3.5.1
-
下载文本文件
function downfile(zip,filepath,fileurl){
return new Promise((resolve,reject)=>{
fetch(fileurl)
.then(response => response.json())
.then(data => {
let htmlstr = data.html
var template = $.parseHTML(htmlstr);
var final = $(template).find("code");
let savestr = final.text();
var blob = new Blob([savestr], { type: "text/plain;charset=utf-8" });
zip.file(filepath,blob,{ binary: true, createFolders: true });
resolve(true);
})
.catch(console.error);
});
}
-
遍历目录树
function looptree(zip,treeurl){
return new Promise((resolve,reject)=>{
fetch(treeurl).then(response => response.json()).then(async data => {
for(let i=0;i<data.length;++i){
let value = data[i];
if(value.type=='tree'){
path = treeurl.replace(/path=[^&]+/,'')
await looptree(zip,treeurl+'&path='+encodeURIComponent(value.path))
}else if(value.type=='blob' &&
(value.name.endsWith('.h') || value.name.endsWith('.cpp') || value.name.endsWith('.js') || //指定了一些后缀下载,只能下载文本类的文件
value.name.endsWith('.cc') || value.name.endsWith('.cxx') || value.name.endsWith('.sh') || value.name.endsWith('.bat') ) ){
await downfile(zip,value.path,'https://code.你访问的gitlab地址.com/udtest/libdtcenter/blob/分支名/'+value.path+'?format=json');
}
}
resolve(true);
}).catch(console.error);
})
}
-
开始下载
async function startDownload(){//api/v3 我测试的这个部署使用的的是gitlab v3版本,其他版本能需要作些调整
var zip = new JSZip();
await looptree(zip,'https://code.你访问的gitlab地址.com/api/v3/projects/仓数字id/repository/tree?private_token=你的私钥(一般可以在个人设置里找到)&id=96271&ref_name=分支名(例如master/develop)&path=子路径名(不带path参数则为整个项目,path可以指定下载特定子目录)');
zip.generateAsync({type:"blob"})
.then(function(content) {
// see FileSaver.js
saveAs(content, "example.zip");
});
}
- 图片和markdown的处理
上面只包含代码和文本类的下载过程,图片和md文件需要做类似处理;
【声明】本内容来自华为云开发者社区博主,不代表华为云及华为云开发者社区的观点和立场。转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)