Try   HackMD

AP平台下載gitlab檔案 - GitLab API

使用場景

AP平台需要掌握bot下載的位置,並且提供給全集團下載使用。
因此會遇上跨區使用上的問題。
gitlab具有掌握token便可以探訪到project的功能,將token不被洩密的狀況下
由AP平台在背後全權操作。


檔案位置

用途 位置
檔案 push 在 gitlab -> project 內
版本list gitlab -> releases

AP平台行為

拿取 Releases list

  • 後台拿 token 打 gitlab API - (get releases)
    範例 : https://gitlab.com/api/v4/projects/:id/releases
    GitLab API - Releases

下載指定檔案

  • 後台拿 token 打 gitlab API - (Download)
    範例 : https://gitlab.com/api/v4/projects/:id/repository/files/:file_path
    GitLab API - Repository files

:file_path格式

  • URL Encode
指向 網址格式
原本網址 https://gitlab.com/user/test/-/blob/main/src/index.ejs
Encode blob%2Fmain%2Fsrc%2Findex.ejs

ts code

/** 拿取List */ function getGitlabList() { const reqUrl = 'https://gitlab.com/api/v4/projects/:id/releases/'; const reqHeader = new Headers({ 'PRIVATE-TOKEN': 'TOKEN' }); const reqOptions: RequestInit = { method: 'GET', headers: reqHeader, }; fetch(reqUrl, reqOptions) .then((esponse: Response) => { console.log(esponse.json()) }) } /** 下載 */ function getDownload() { const reqUrl = 'https://gitlab.com/api/v4/projects/:id/repository/files/:file_path/raw?ref=master'; const reqHeader = new Headers({ 'PRIVATE-TOKEN': 'TOKEN' }); const reqOptions: RequestInit = { method: 'GET', headers: reqHeader, }; fetch(reqUrl, reqOptions) .then((esponse: Response) => { console.log(esponse) return esponse.blob() }) .then((blob) => { var a = document.createElement('a'); var url = window.URL.createObjectURL(blob); var filename = 'bot.exe'; a.href = url; a.download = filename; a.click(); return window.URL.revokeObjectURL(url); }) }
tags: Git