Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

释放想象力,使用github/gitlab/bitbucket的webhooks和API能做什么 #3

Closed
2 of 3 tasks
plantain-00 opened this issue Jan 23, 2016 · 0 comments
Closed
2 of 3 tasks

Comments

@plantain-00
Copy link
Owner

webhooks类似于事件。
举例来说,如果一个webhook被设置成发表评论后触发,设置的URL是http://example.com ,当有人发表评论后,github/gitlab/bitbucket的服务器会向http://example.com 发送一个http请求,请求中会包含一些信息,比如评论内容、作者、在哪里评论的等等。所以一般需要创建一个服务来处理这个http请求,在本地执行自定义的脚本。
那么可以做些什么呢?

测试

如果有了个pull request,可以发表一个包含开始测试命令的评论,服务收到请求后,根据评论内容匹配命令,执行相应的脚本,主要是pull这个分支的代码,执行测试。如果涉及到UI,还可以在测试环境执行切换分支的命令,成功后就可以在测试环境查看UI的变化了。
以github和node为例,执行指令:

function exec(command) {
    return new Promise((resolve, reject) => {
        childProcess.exec(command, (error, stdout, stderr) => {
            if (error) {
                reject(error);
                return;
            }

            resolve();
        });
    });
}

安全性

服务需要确定收到的请求没有被伪造,以github为例,github会对请求的body签名,所以服务需要对签名进行验证。
以github和node为例,计算签名:

function getSignature(body, application) {
    return "sha1=" + cryptoJs.HmacSHA1(body, application.secret).toString();
}

具体的文档,可见https://developer.github.com/webhooks/securing/

权限问题:可以设置白名单,只有白名单内的人的评论,才会触发脚本的执行。
以github和node为例,验证评论人:

        let operator = request.body.comment.user.login;
        if (application.operators.findIndex(value => value === operator) < 0) {
            response.end("not valid operator");
            return;
        }

反馈结果

以上做法,并不能确定测试是否成功。
可以创建一个robot账户,当脚本执行成功或失败后,以这个robot账户的身份通过API发表评论,评论的内容就是反馈的结果。
以github和node为例,发表评论:

function createComment(content, command) {
    let url = `https://api.github.com/repos/${command.owner}/${command.repo}/issues/${command.issueNumber}/comments`;
    return new Promise((resolve, reject) => {
        request({
            url: url,
            method: "post",
            json: true,
            body: {
                body: content
            },
            headers: {
                Authorization: `token ${settings.accessToken}`,
                "User-Agent": "test",
            },
        }, (error, incomingMessage, body) => {
            if (error) {
                console.log(error);
            } else if (incomingMessage.statusCode !== 201) {
                console.log(body);
            }

            resolve();
        });
    });
}

具体文档,可见https://developer.github.com/v3/issues/comments/#create-a-comment

部署和回滚

如果测试通过、code review通过、UI也OK,pull request就被合并了,如果发布一个带部署指令的评论,相应的部署脚本被执行,就可以实现部署了。
这是一个部署的例子:plantain-00/SubsNoti#168 ,这个例子里,部署指令是:robotdeployplease都在评论内容里。

checklist

一个pull request被创建后,robot立刻发布包含checklist的评论,所有参与人就可以按照这个checklist review代码,checklist中每一项的状态会一直存在,就像这样:

  • check 1
  • check 2
  • check 3

这是一个checklist的具体例子:DefinitelyTyped/DefinitelyTyped#7704

命令队列

同时执行多个命令很可能造成错误,所以收到命令后,应该先存到队列中,再依次执行。

自定义邮件通知

一个pull request被创建后,robot可以发邮件给多个相关的reviewer。

同意license

这个例子里,服务会查询作者有没有同意过license,如果没有,就会发评论提示,并加上cla-required tag,当作者通过链接同意了协议后,服务会移除cla-required tag,并加上cla-signed tag。
具体例子:microsoft/TypeScript#6512

结束

最后以Github文档中的一段(https://developer.github.com/webhooks/ ),来结束:

Webhooks allow you to build or set up integrations which subscribe to certain events on GitHub.com. When one of those events is triggered, we'll send a HTTP POST payload to the webhook's configured URL. Webhooks can be used to update an external issue tracker, trigger CI builds, update a backup mirror, or even deploy to your production server. You're only limited by your imagination.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant