私は実際にあなたと同じことをやっています。 githubとslackフックは根本的に異なるので、github webhooksをSlackメッセージに処理して入ってくるWebhook経由で投稿するには、途中に何かがある必要があります。
あなたは(順不同)カップル異なることを行うために必要になるだろう:
- はあなたを通知することを希望する特定のイベントのためのフックを送信するためにGithubのを設定します。
- (私は現在AWS SNSとラムダを使用しています)中間のユーザーを設定してください
- ウェブフック用のスラックを設定してください。
github webhooksでは、より強力なgithub APIを利用してフックを作成する必要があります。あなたはcurl
でこれを行うことができますが、それは痛みのようですので、私はそれを世話するためにJSスクリプトを使用しています。あなたはこのような何かを実行する前に、同じディレクトリにnpm install github bluebird
にする必要があります:
var GitHubApi = require("github");
var github = new GitHubApi({
// optional
debug: true,
protocol: "https",
host: "api.github.com", // should be api.github.com for GitHub
pathPrefix: "", // for some GHEs; none for GitHub
headers: {
"user-agent": "ocelotsloth-conf" // GitHub is happy with a unique user agent
},
Promise: require('bluebird'),
followRedirects: false, // default: true; there's currently an issue with non-get redirects, so allow ability to disable follow-redirects
timeout: 5000
});
// user token
github.authenticate({
type: "token",
token: "GITHUB_TOKEN_HERE",
});
// https://mikedeboer.github.io/node-github/#api-repos-createHook
github.repos.createHook({
owner: "ocelotsloth",
repo: "lib-ical",
name: "amazonsns",
events: [
//"commit_comment",
//"create",
//"delete",
//"gollum",
//"issue_comment",
"issues"
//"label",
//"milestone",
//"pull_request",
//"pull_request_review",
//"pull_request_review_comment",
//"push",
//"release"
],
config: {
aws_key: "AWS_KEY",
aws_secret: "AWS_SECRET",
sns_region: "us-east-1",
sns_topic: "SNS_TOPIC_ARN"
},
}, function(err, res) {
console.log(JSON.stringify(res, null, '\t'));
});
私が正しく動作するためにSNSトピックを設定についてしばらく前にブログ記事を以下覚えているが、私は場所を正確にそれを覚えていませんもうです。いくつかのグーグルが助けになるはずです。また、複雑さを避けたい場合には、githubがこれらを送信するための独自のサーバを設定し、AWSをまったく設定する必要がないようにする必要があります。その方法に関する具体的な指示については、https://mikedeboer.github.io/node-github/#api-repos-createHookを参照してください。フックを作成した後にeditHook
を使用する必要がありますので、最初に正しく取得するか、または編集してください。editHookへのメソッド呼び出しを変更し、id
を呼び出しに追加するだけです。
何か重要なことは、githubに送る別のEvents
のすべてを定義できることです。これらのすべてについて、それらのフォーマットと共に、https://developer.github.com/v3/activity/events/types/を見てください。
実際にこれらのイベントをスラックに投稿するには、現在このように見えるラムダスクリプトがあります(文字通り、これを書いたばかりで、発行イベントを投稿するだけではありませんが、出発点)。このスクリプトでは、npm install identify-github-event slack-webhook
が必要で、受信したウェブフックも設定してください。
var identifyGithubEvent = require('identify-github-event');
var SlackWebhook = require('slack-webhook')
// slack's link syntax
function link(url, txt) {
return "<" + url + "|" + txt + ">";
}
exports.handler = function(event, context) {
// 1. extract GitHub event from SNS message
var ghEvent = JSON.parse(event.Records[0].Sns.Message);
var eventType, eventName, numb;
console.log(ghEvent);
var ghEventType = identifyGithubEvent(ghEvent);
if (!ghEventType) {
return;
}
var text = "Event! " + ghEventType;
if (ghEventType === 'IssueCommentEvent') {
var who = link(ghEvent.comment.user.html_url, ghEvent.comment.user.login);
var what = link(ghEvent.issue.html_url, "Issue " + ghEvent.issue.number + ": \"" + ghEvent.issue.title + "\"");
text = who + " commented on " + what;
}
else if (ghEventType === 'IssuesEvent') {
var who = link(ghEvent.sender.html_url, ghEvent.sender.login);
var action = ghEvent.action;
var issueNumber = ghEvent.issue.number;
var issueName = link(ghEvent.issue.html_url, ghEvent.issue.title + "\"");
if (action === "opened" | action === "closed") {
text = {
attachments: [{
"fallback": who + " opened Issue" + issueNumber + ": " + issueName,
"color": "#36a64f",
"pretext": "New issue " + action + ":",
"author_name": ghEvent.sender.login,
"author_link": ghEvent.sender.html_url,
"thumb_url": ghEvent.sender.avatar_url,
"title": "#" + issueNumber + ": " + ghEvent.issue.title,
"title_link": ghEvent.issue.html_url,
"text": ghEvent.issue.body,
"fields": [
{
"title": "Status",
"value": ghEvent.issue.state,
"short": true
},
{
"title": "Labels",
"value": ghEvent.issue.labels.map(label => label.name).join("\n"),
"short": true
}
],
"footer": "lib-ical",
"footer_icon": "https://platform.slack-edge.com/img/default_application_icon.png",
"mrkdwn_in": ["text"]
}]
};
} else return;
}
// 'commit_comment':
// 'create':
// 'delete':
// 'issues':
// 'label':
// 'member':
// 'milestone':
// 'pull_request':
// 'pull_request_review':
// 'pull_request_review_comment':
// 'push':
// 'release':
var slack = new SlackWebhook('https://hooks.slack.com/services/SLACK-WEBHOOK-URL', {
defaults: {
username: 'GitHub -- user/project',
channel: '#CHANNEL-NAME',
icon_emoji: ':github:'
}
})
slack.send(text);
};
それははるかに完璧からだが、それは本当に素晴らしい結果を与える:それは問題の近くだその具体的な例
が、現在はそのスクリプトはまた、オープンで動作します。このスクリプトでは、マークダウン処理も制限されているため、問題にソースブロックが含まれていると、スラック内で正しくレンダリングされます。
私はこれがあなたのアプローチに役立つことを願っています。他に何かを詳しく教えてください。