首页 > 解决方案 > 有没有办法让我通过 nodejs 获取 git origin 分支代码

问题描述

触发推送事件时,我在项目中添加了一个 git 挂钩。

当 hook 被触发并且 ref 值为refs/heads/master时,prodindex.html文件将被更新。

我想阅读新index.html内容

router.post('/gitHook', async (ctx, next) => {
    const body = ctx.request.body;
    const matches = body.ref.match(/^refs\/heads\/(.*)/);
    const branchName = matches[1];
    console.log(branchName);
    if(branchName === 'master'){
        console.log('should get new code from git origin master')
    }
    await next();
});

标签: javascriptnode.jsgitkoa

解决方案


我使用包获得了index.html内容。gitlab

const { Gitlab } = require('gitlab');
const api = new Gitlab({
    host: 'myhost',
    token: 'myToken',
});

api.RepositoryFiles.show(body.project_id, '/build/index.html', body.ref).then(res => {
    const content = res.content;
    const stringContent = new Buffer(content, 'base64').toString();
    console.log(stringContent);
});

推荐阅读