基于gitlab API封装的git-helper工具

630 阅读1分钟

背景

项目需求,需要从不同gitlab仓库中同步文件来用。翻遍了整个npm都没有找到能够直接搬的砖。没有办法只能自己封装一个。

Gitlab获取访问令牌

1. 进入 User Settings

image.png

2. 点击侧栏 Access Tokens

4890cb45a5adf3668e2be47469e1aee.jpg

3. 输入token name,选择权限和截止日期(截止日期可不填)并创建

144b0084432a1c3fd88fe72127fa506.jpg

4. 创建成功后,token会显示在最上放,记得保存否则刷新没

image.png

安装 git-helper

npm install @fxs0819/git-helper

使用

/** github */
const h = new GitHelper({
    provider: 'github',
    repository: 'https://github.com/xxx.git',
    branch: 'master',
    config: {
        userName: 'xxx', // Github Basic Auth Username
        password: 'xxx' // Github Basic Auth Password
    }
});
const f = await h.readFile('package.json');

// 或

const h = new GitHelper({
    provider: 'github',
    repository: 'https://github.com/xxx.git',
    branch: 'master',
    config: {
        oauth2Token: 'xxx' // Github OAuth2 Token
    }
});
const f = await h.readFile('package.json');
/** gitlab */
const h = new GitHelper({
    provider: 'gitlab',
    repository: 'https://gitlab.com/xx.git',
    branch: 'master',
    config: {
        privateToken: 'xxx' // Gitlab Personal Access Tokens
    }
});
const f = await h.readFile('package.json');

参考

Gitlab Docs

最后

虽然名字叫git-helper但目前仅支持单文件下载,还不足以匹配helper。 希望能帮助到有需要的掘友。

第一次分享望多指教。。。( •́ω•̩̥̀ )