基于 OpenWhisk 实时监测静态网页是否有乱码(2)实例-2
- UID
- 1066743
|
基于 OpenWhisk 实时监测静态网页是否有乱码(2)实例-2
创建OpenWhisk GitHub触发器可以通过以下步骤为garbage-test-app创建GitHub触发器:
- 首先为测试应用程序创建 package binding,创建 package binding 时需要指定 github repository,accessToken和管理用户;
1
| $ wsk package update myGit -p repository icnbrave/garbage-test-app -p accessToken <GITHUB_ACCESSTOKEN> -p username <GITHUB_USERNAME>
|
- 为 github push 事件创建触发器,即绑定的 github repo 有 push 时,该触发器被触发。
1
| $ wsk trigger create gitTrigger --feed myGit/webhook -p events push
|
当触发器创建成功之后,可以在GitHub Repository的Settings中会创建一个Webhook,GitHub就是通过该Webhook去触发OpenWhisk触发器,如图6所示:
图6:github webhooks设置自定义的Filter Action从GitHub事件中过滤出感兴趣数据GitHub的每个事件都会附带对应的 payload 数据,可以通过 查看每GitHub Event对应的payload数据,我们可以从payload中提取我们想要的数据。比如,我们可以从push事件对应的payload数据中提取最近几次commit中更新、添加的文件,然后只对这部分文件进行garbage char检测。
GitHub Push Event Payload部分数据如下:
代码清单1. GitHub Push Event Payload部分返回数据1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| ...
"commits": [
{
"id": "0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
"tree_id": "f9d2a07e9488b91af2641b26b9407fe22a451433",
"distinct": true,
"message": "Update README.md",
"timestamp": "2015-05-05T19:40:15-04:00",
"url":
"https://github.com/baxterthehacker/public-repo/commit/0d1a26e67d8f5eaf1f6ba5c57fc3c7d91ac0fd1c",
"author": {
"name": "baxterthehacker",
"email": "baxterthehacker@users.noreply.github.com",
"username": "baxterthehacker"
},
"committer": {
"name": "baxterthehacker",
"email": "baxterthehacker@users.noreply.github.com",
"username": "baxterthehacker"
},
"added": [
],
"removed": [
],
"modified": [
"README.md"
]
}
],
...
|
通过如下命令将以下 创建getPushPayloadAction来接收并且过滤当前push事件中新增或更改的文件,然后得出他们在Bluemix上的URL并送到下一个action:
1
| $ wsk action create getPushPayloadAction getPushPayload.js
|
代码清单2. getPushPayload.js1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| function main(params){
var testAppUrl = "https://garbagetestapp.mybluemix.net/";
var head_commit = params["head_commit"] || "";
var files = [];
if(head_commit != ""){
files=files.concat(head_commit["added"]);
files=files.concat(head_commit["modified"]);
}
console.log("files: ", files);
// files map to test app link
var urls=[];
for(var i=0,len=files.length; i<len; i++){
urls.push(testAppUrl + files);
}
return {payload: urls.join(",")};
}
|
然后通过如下命令创建一个Rule将GitHub Trigger与该Action进行关联:
1
| $ wsk rule create git2slackRule gitTrigger getPushPayloadAction
|
关联之后, garbage-test-app repo 任何 push event 都会触发这个Action。 |
|
|
|
|
|