2019-01-20 01:15:59 +01:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
using namespace ircd;
|
|
|
|
|
|
|
|
mapi::header
|
|
|
|
IRCD_MODULE
|
|
|
|
{
|
|
|
|
"Webhook Handler"
|
|
|
|
};
|
|
|
|
|
|
|
|
conf::item<std::string>
|
|
|
|
webhook_secret
|
|
|
|
{
|
|
|
|
{ "name", "webhook.secret" }
|
|
|
|
};
|
|
|
|
|
|
|
|
conf::item<std::string>
|
|
|
|
webhook_user
|
|
|
|
{
|
|
|
|
{ "name", "webhook.user" }
|
|
|
|
};
|
|
|
|
|
|
|
|
conf::item<std::string>
|
|
|
|
webhook_room
|
|
|
|
{
|
|
|
|
{ "name", "webhook.room" }
|
|
|
|
};
|
|
|
|
|
|
|
|
conf::item<std::string>
|
|
|
|
webhook_url
|
|
|
|
{
|
|
|
|
{ "name", "webhook.url" },
|
|
|
|
{ "default", "/webhook" }
|
|
|
|
};
|
|
|
|
|
|
|
|
resource
|
|
|
|
webhook_resource
|
|
|
|
{
|
|
|
|
string_view{webhook_url},
|
|
|
|
{
|
|
|
|
"Webhook Resource",
|
|
|
|
webhook_resource.DIRECTORY
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static resource::response
|
|
|
|
post__webhook(client &,
|
|
|
|
const resource::request &);
|
|
|
|
|
|
|
|
resource::method
|
|
|
|
webhook_post
|
|
|
|
{
|
|
|
|
webhook_resource, "POST", post__webhook
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
github_handle(client &,
|
|
|
|
const resource::request &);
|
|
|
|
|
|
|
|
resource::response
|
|
|
|
post__webhook(client &client,
|
|
|
|
const resource::request &request)
|
|
|
|
{
|
|
|
|
if(has(http::headers(request.head.headers), "X-GitHub-Event"_sv))
|
|
|
|
github_handle(client, request);
|
|
|
|
|
|
|
|
return resource::response
|
|
|
|
{
|
|
|
|
client, http::OK
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
github_validate(const string_view &sig,
|
|
|
|
const const_buffer &content,
|
|
|
|
const string_view &secret);
|
|
|
|
|
|
|
|
static string_view
|
|
|
|
github_find_commit_hash(const json::object &content);
|
|
|
|
|
|
|
|
static string_view
|
|
|
|
github_find_issue_number(const json::object &content);
|
|
|
|
|
2019-01-20 22:57:15 +01:00
|
|
|
static std::pair<string_view, string_view>
|
2019-01-20 01:15:59 +01:00
|
|
|
github_find_party(const json::object &content);
|
|
|
|
|
|
|
|
static std::ostream &
|
|
|
|
github_handle__push(std::ostream &,
|
|
|
|
const json::object &content);
|
|
|
|
|
2019-01-20 22:57:58 +01:00
|
|
|
static std::ostream &
|
|
|
|
github_handle__pull_request(std::ostream &,
|
|
|
|
const json::object &content);
|
|
|
|
|
2019-01-23 00:30:49 +01:00
|
|
|
static std::ostream &
|
|
|
|
github_handle__issues(std::ostream &,
|
|
|
|
const json::object &content);
|
|
|
|
|
2019-02-05 01:19:12 +01:00
|
|
|
static std::ostream &
|
|
|
|
github_handle__watch(std::ostream &,
|
|
|
|
const json::object &content);
|
|
|
|
|
2019-01-20 01:23:20 +01:00
|
|
|
static std::ostream &
|
|
|
|
github_handle__ping(std::ostream &,
|
|
|
|
const json::object &content);
|
|
|
|
|
2019-01-20 01:15:59 +01:00
|
|
|
static std::ostream &
|
|
|
|
github_heading(std::ostream &,
|
|
|
|
const string_view &type,
|
|
|
|
const json::object &content);
|
|
|
|
|
|
|
|
void
|
|
|
|
github_handle(client &client,
|
|
|
|
const resource::request &request)
|
|
|
|
{
|
|
|
|
const http::headers &headers
|
|
|
|
{
|
|
|
|
request.head.headers
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto sig
|
|
|
|
{
|
|
|
|
headers.at("X-Hub-Signature")
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!github_validate(sig, request.content, webhook_secret))
|
|
|
|
throw http::error
|
|
|
|
{
|
|
|
|
http::UNAUTHORIZED, "X-Hub-Signature verification failed"
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &type
|
|
|
|
{
|
|
|
|
headers.at("X-GitHub-Event")
|
|
|
|
};
|
|
|
|
|
|
|
|
const string_view &delivery
|
|
|
|
{
|
|
|
|
headers.at("X-GitHub-Delivery")
|
|
|
|
};
|
|
|
|
|
2019-01-20 01:56:20 +01:00
|
|
|
const unique_buffer<mutable_buffer> buf
|
|
|
|
{
|
|
|
|
48_KiB
|
|
|
|
};
|
|
|
|
|
2019-01-20 01:15:59 +01:00
|
|
|
std::stringstream out;
|
|
|
|
pubsetbuf(out, buf);
|
|
|
|
|
|
|
|
github_heading(out, type, request.content);
|
|
|
|
|
2019-01-20 01:23:20 +01:00
|
|
|
if(type == "ping")
|
|
|
|
github_handle__ping(out, request.content);
|
|
|
|
else if(type == "push")
|
2019-01-20 01:15:59 +01:00
|
|
|
github_handle__push(out, request.content);
|
2019-01-20 22:57:58 +01:00
|
|
|
else if(type == "pull_request")
|
|
|
|
github_handle__pull_request(out, request.content);
|
2019-01-23 00:30:49 +01:00
|
|
|
else if(type == "issues")
|
|
|
|
github_handle__issues(out, request.content);
|
2019-02-05 01:19:12 +01:00
|
|
|
else if(type == "watch")
|
|
|
|
github_handle__watch(out, request.content);
|
2019-01-20 01:15:59 +01:00
|
|
|
|
|
|
|
if(!string_view(webhook_room))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const auto room_id
|
|
|
|
{
|
|
|
|
m::room_id(string_view(webhook_room))
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!string_view(webhook_user))
|
|
|
|
return;
|
|
|
|
|
|
|
|
const m::user::id::buf user_id
|
|
|
|
{
|
|
|
|
string_view(webhook_user), my_host()
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto evid
|
|
|
|
{
|
|
|
|
m::msghtml(room_id, user_id, view(out, buf), "No alt text")
|
|
|
|
};
|
|
|
|
|
|
|
|
log::info
|
|
|
|
{
|
|
|
|
"Webhook [%s] '%s' delivered to %s %s",
|
|
|
|
delivery,
|
|
|
|
type,
|
|
|
|
string_view{room_id},
|
|
|
|
string_view{evid}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::ostream &
|
|
|
|
github_heading(std::ostream &out,
|
|
|
|
const string_view &type,
|
|
|
|
const json::object &content)
|
|
|
|
{
|
|
|
|
const json::object repository
|
|
|
|
{
|
|
|
|
content["repository"]
|
|
|
|
};
|
|
|
|
|
2019-01-21 23:24:54 +01:00
|
|
|
out << "<a href=\"" << unquote(repository["html_url"]) << "\">"
|
2019-01-20 22:57:15 +01:00
|
|
|
<< unquote(repository["full_name"])
|
2019-01-20 01:15:59 +01:00
|
|
|
<< "</a>";
|
|
|
|
|
|
|
|
const string_view commit_hash
|
|
|
|
{
|
|
|
|
github_find_commit_hash(content)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(commit_hash && type == "push")
|
2019-01-21 23:24:54 +01:00
|
|
|
out << " <b><font color=\"#FF5733\">";
|
2019-01-20 01:15:59 +01:00
|
|
|
else if(commit_hash && type == "pull_request")
|
2019-01-21 23:24:54 +01:00
|
|
|
out << " <b><font color=\"#CC00CC\">";
|
2019-01-20 01:15:59 +01:00
|
|
|
else if(commit_hash)
|
|
|
|
out << " <b>";
|
|
|
|
|
|
|
|
if(commit_hash)
|
|
|
|
out << commit_hash.substr(0, 8)
|
|
|
|
<< "</font></b>";
|
|
|
|
|
|
|
|
const string_view issue_number
|
|
|
|
{
|
|
|
|
github_find_issue_number(content)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(issue_number)
|
|
|
|
out << " <b>#" << issue_number << "</b>";
|
|
|
|
else
|
|
|
|
out << " " << type;
|
|
|
|
|
2019-01-20 22:57:15 +01:00
|
|
|
const auto party
|
2019-01-20 01:15:59 +01:00
|
|
|
{
|
|
|
|
github_find_party(content)
|
|
|
|
};
|
|
|
|
|
2019-01-20 22:57:15 +01:00
|
|
|
out << " by "
|
2019-01-21 23:24:54 +01:00
|
|
|
<< "<a href=\""
|
2019-01-20 22:57:15 +01:00
|
|
|
<< party.second
|
2019-01-21 23:24:54 +01:00
|
|
|
<< "\">"
|
2019-01-20 22:57:15 +01:00
|
|
|
<< party.first
|
|
|
|
<< "</a>";
|
2019-01-20 01:15:59 +01:00
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2019-02-05 01:18:35 +01:00
|
|
|
std::ostream &
|
2019-01-20 01:15:59 +01:00
|
|
|
github_handle__push(std::ostream &out,
|
|
|
|
const json::object &content)
|
|
|
|
{
|
|
|
|
const json::array commits
|
|
|
|
{
|
|
|
|
content["commits"]
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto count
|
|
|
|
{
|
|
|
|
size(commits)
|
|
|
|
};
|
|
|
|
|
|
|
|
if(!count)
|
|
|
|
{
|
2019-01-21 23:24:54 +01:00
|
|
|
out << " <font color=\"#FF0000\">";
|
2019-01-20 01:15:59 +01:00
|
|
|
if(content["ref"])
|
|
|
|
out << " " << unquote(content["ref"]);
|
|
|
|
|
|
|
|
out << " deleted</font>";
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(content["ref"])
|
2019-01-20 22:57:15 +01:00
|
|
|
{
|
|
|
|
const auto ref(unquote(content["ref"]));
|
|
|
|
out << " "
|
|
|
|
<< " "
|
|
|
|
<< token_last(ref, '/');
|
|
|
|
}
|
2019-01-20 01:15:59 +01:00
|
|
|
|
2019-01-21 23:24:54 +01:00
|
|
|
out << " <a href=\"" << unquote(content["compare"]) << "\">"
|
2019-01-20 01:15:59 +01:00
|
|
|
<< "<b>" << count << " commits</b>"
|
|
|
|
<< "</a>";
|
|
|
|
|
|
|
|
if(content["forced"] == "true")
|
|
|
|
out << " (rebase)";
|
|
|
|
|
|
|
|
out << "<pre>";
|
2019-02-05 11:30:56 +01:00
|
|
|
for(ssize_t i(count - 1); i >= 0; --i)
|
2019-01-20 01:15:59 +01:00
|
|
|
{
|
2019-02-05 11:30:56 +01:00
|
|
|
const json::object &commit(commits.at(i));
|
2019-01-20 01:15:59 +01:00
|
|
|
const auto url(unquote(commit["url"]));
|
|
|
|
const auto id(unquote(commit["id"]));
|
|
|
|
const auto sid(id.substr(0, 8));
|
2019-01-21 23:24:54 +01:00
|
|
|
out << " <a href=\"" << url << "\">"
|
2019-01-20 01:15:59 +01:00
|
|
|
<< "<b>" << sid << "</b>"
|
|
|
|
<< "</a>";
|
|
|
|
|
|
|
|
const json::object author(commit["author"]);
|
2019-01-26 02:27:40 +01:00
|
|
|
out << " <b>"
|
|
|
|
<< unquote(author["name"])
|
|
|
|
<< "</b>"
|
|
|
|
;
|
2019-01-20 01:15:59 +01:00
|
|
|
|
|
|
|
const json::object committer(commit["committer"]);
|
|
|
|
if(committer["email"] != author["email"])
|
2019-01-26 02:27:40 +01:00
|
|
|
out << " via <b>"
|
|
|
|
<< unquote(committer["name"])
|
|
|
|
<< "</b>"
|
|
|
|
;
|
2019-01-20 22:57:15 +01:00
|
|
|
|
|
|
|
const auto message(unquote(commit["message"]));
|
|
|
|
const auto summary
|
|
|
|
{
|
|
|
|
split(message, "\\n").first
|
|
|
|
};
|
2019-01-20 01:15:59 +01:00
|
|
|
|
2019-01-26 02:27:40 +01:00
|
|
|
out << " "
|
2019-01-20 01:15:59 +01:00
|
|
|
<< summary
|
2019-01-26 02:27:40 +01:00
|
|
|
;
|
2019-01-20 01:15:59 +01:00
|
|
|
|
|
|
|
out << "<br />";
|
|
|
|
}
|
|
|
|
|
|
|
|
out << "</pre>";
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2019-01-20 22:57:58 +01:00
|
|
|
static std::ostream &
|
|
|
|
github_handle__pull_request(std::ostream &out,
|
|
|
|
const json::object &content)
|
|
|
|
{
|
|
|
|
const json::object pr
|
|
|
|
{
|
|
|
|
content["pull_request"]
|
|
|
|
};
|
|
|
|
|
|
|
|
if(pr["merged"] != "true")
|
|
|
|
out << " "
|
|
|
|
<< "<b>"
|
|
|
|
<< unquote(content["action"])
|
|
|
|
<< "</b>"
|
|
|
|
;
|
|
|
|
|
|
|
|
if(pr["merged"] == "true")
|
|
|
|
out << ' '
|
|
|
|
<< "<b>"
|
2019-01-21 23:24:54 +01:00
|
|
|
<< "<font color=\"#CC00CC\">"
|
2019-01-20 22:57:58 +01:00
|
|
|
<< "merged"
|
|
|
|
<< "</font>"
|
|
|
|
<< "</b>"
|
|
|
|
;
|
|
|
|
|
|
|
|
if(pr.has("merged_by") && pr["merged_by"] != "null")
|
|
|
|
{
|
|
|
|
const json::object merged_by{pr["merged_by"]};
|
|
|
|
out << " "
|
|
|
|
<< "by "
|
2019-01-21 23:24:54 +01:00
|
|
|
<< "<a href=\""
|
2019-01-20 22:57:58 +01:00
|
|
|
<< unquote(merged_by["html_url"])
|
2019-01-21 23:24:54 +01:00
|
|
|
<< "\">"
|
2019-01-20 22:57:58 +01:00
|
|
|
<< unquote(merged_by["login"])
|
|
|
|
<< "</a>"
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pr["merged"] == "false") switch(hash(pr["mergeable"]))
|
|
|
|
{
|
|
|
|
default:
|
|
|
|
case hash("null"):
|
|
|
|
out << " / "
|
|
|
|
<< "<b>"
|
2019-01-21 23:24:54 +01:00
|
|
|
<< "<font color=\"#FFCC00\">"
|
2019-01-20 22:57:58 +01:00
|
|
|
<< "CHECKING MERGE"
|
|
|
|
<< "</font>"
|
|
|
|
<< "</b>"
|
|
|
|
;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case hash("true"):
|
|
|
|
out << " / "
|
|
|
|
<< "<b>"
|
2019-01-21 23:24:54 +01:00
|
|
|
<< "<font color=\"#33CC33\">"
|
2019-01-20 22:57:58 +01:00
|
|
|
<< "MERGEABLE"
|
|
|
|
<< "</font>"
|
|
|
|
<< "</b>"
|
|
|
|
;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case hash("false"):
|
|
|
|
out << " / "
|
|
|
|
<< "<b>"
|
2019-01-21 23:24:54 +01:00
|
|
|
<< "<font color=\"#CC0000\">"
|
2019-01-20 22:57:58 +01:00
|
|
|
<< "MERGE CONFLICT"
|
|
|
|
<< "</font>"
|
|
|
|
<< "</b>"
|
|
|
|
;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pr.has("additions"))
|
|
|
|
out << " / "
|
|
|
|
<< "<b>"
|
2019-01-21 23:24:54 +01:00
|
|
|
<< "<font color=\"#33CC33\">"
|
2019-01-20 22:57:58 +01:00
|
|
|
<< "++"
|
|
|
|
<< "</font>"
|
|
|
|
<< pr["additions"]
|
|
|
|
<< "</b>"
|
|
|
|
;
|
|
|
|
|
|
|
|
if(pr.has("deletions"))
|
|
|
|
out << " / "
|
|
|
|
<< "<b>"
|
2019-01-21 23:24:54 +01:00
|
|
|
<< "<font color=\"#CC0000\">"
|
2019-01-20 22:57:58 +01:00
|
|
|
<< "--"
|
|
|
|
<< "</font>"
|
|
|
|
<< pr["deletions"]
|
|
|
|
<< "</b>"
|
|
|
|
;
|
|
|
|
|
|
|
|
if(pr.has("changed_files"))
|
|
|
|
out << " / "
|
|
|
|
<< "<b>"
|
|
|
|
<< pr["changed_files"]
|
|
|
|
<< ' '
|
2019-01-21 23:24:54 +01:00
|
|
|
<< "<font color=\"#476b6b\">"
|
2019-01-20 22:57:58 +01:00
|
|
|
<< "files"
|
|
|
|
<< "</font>"
|
|
|
|
<< "</b>"
|
|
|
|
;
|
|
|
|
|
|
|
|
const json::object head
|
|
|
|
{
|
|
|
|
pr["head"]
|
|
|
|
};
|
|
|
|
|
|
|
|
out << " "
|
|
|
|
<< "<pre>"
|
2019-01-21 23:24:54 +01:00
|
|
|
<< "<a href=\""
|
2019-01-20 22:57:58 +01:00
|
|
|
<< unquote(pr["html_url"])
|
2019-01-21 23:24:54 +01:00
|
|
|
<< "\">"
|
2019-01-20 22:57:58 +01:00
|
|
|
<< "<b>"
|
|
|
|
<< unquote(head["sha"]).substr(0, 8)
|
|
|
|
<< "</b>"
|
|
|
|
<< "</a>"
|
|
|
|
<< " "
|
|
|
|
<< "<u>"
|
|
|
|
<< unquote(pr["title"])
|
|
|
|
<< "</u>"
|
|
|
|
<< "</pre>"
|
|
|
|
;
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2019-01-23 00:30:49 +01:00
|
|
|
static std::ostream &
|
|
|
|
github_handle__issues(std::ostream &out,
|
|
|
|
const json::object &content)
|
|
|
|
{
|
|
|
|
out << " "
|
|
|
|
<< "<b>"
|
|
|
|
<< unquote(content["action"])
|
|
|
|
<< "</b>"
|
|
|
|
;
|
|
|
|
|
|
|
|
const json::object issue
|
|
|
|
{
|
|
|
|
content["issue"]
|
|
|
|
};
|
|
|
|
|
|
|
|
switch(hash(unquote(content["action"])))
|
|
|
|
{
|
|
|
|
case "assigned"_:
|
|
|
|
case "unassigned"_:
|
|
|
|
{
|
|
|
|
const json::object assignee
|
|
|
|
{
|
|
|
|
content["assignee"]
|
|
|
|
};
|
|
|
|
|
|
|
|
out << " "
|
|
|
|
<< "<a href=\""
|
|
|
|
<< unquote(assignee["html_url"])
|
|
|
|
<< "\">"
|
|
|
|
<< unquote(assignee["login"])
|
|
|
|
<< "</a>"
|
|
|
|
;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
out << " "
|
|
|
|
<< "<a href=\""
|
|
|
|
<< unquote(issue["html_url"])
|
|
|
|
<< "\">"
|
|
|
|
<< "<b><u>"
|
|
|
|
<< unquote(issue["title"])
|
|
|
|
<< "</u></b>"
|
|
|
|
<< "</a>"
|
|
|
|
;
|
|
|
|
|
|
|
|
if(unquote(content["action"]) == "opened")
|
|
|
|
{
|
|
|
|
out << " "
|
|
|
|
<< "<blockquote>"
|
|
|
|
<< "<pre>"
|
|
|
|
;
|
|
|
|
|
|
|
|
static const auto delim("\\r\\n");
|
|
|
|
const auto body(unquote(issue["body"]));
|
|
|
|
auto lines(split(body, delim)); do
|
|
|
|
{
|
|
|
|
out << lines.first
|
|
|
|
<< "<br />"
|
|
|
|
;
|
|
|
|
|
|
|
|
lines = split(lines.second, delim);
|
|
|
|
}
|
|
|
|
while(!empty(lines.second));
|
|
|
|
|
|
|
|
out << ""
|
|
|
|
<< "</pre>"
|
|
|
|
<< "</blockquote>"
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2019-02-05 01:19:12 +01:00
|
|
|
std::ostream &
|
|
|
|
github_handle__watch(std::ostream &out,
|
|
|
|
const json::object &content)
|
|
|
|
{
|
|
|
|
const string_view action
|
|
|
|
{
|
|
|
|
unquote(content["action"])
|
|
|
|
};
|
|
|
|
|
|
|
|
if(action == "started")
|
|
|
|
{
|
|
|
|
out << " with a star";
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::ostream &
|
2019-01-20 01:23:20 +01:00
|
|
|
github_handle__ping(std::ostream &out,
|
|
|
|
const json::object &content)
|
|
|
|
{
|
|
|
|
out << "<pre>"
|
|
|
|
<< unquote(content["zen"])
|
|
|
|
<< "</pre>";
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2019-01-20 01:15:59 +01:00
|
|
|
/// Researched from yestifico bot
|
2019-02-05 01:18:35 +01:00
|
|
|
std::pair<string_view, string_view>
|
2019-01-20 01:15:59 +01:00
|
|
|
github_find_party(const json::object &content)
|
|
|
|
{
|
2019-01-20 22:57:15 +01:00
|
|
|
const json::object pull_request
|
|
|
|
{
|
|
|
|
content["pull_request"]
|
|
|
|
};
|
2019-01-20 01:15:59 +01:00
|
|
|
|
2019-01-20 22:57:15 +01:00
|
|
|
const json::object user
|
|
|
|
{
|
|
|
|
pull_request["user"]
|
|
|
|
};
|
2019-01-20 01:15:59 +01:00
|
|
|
|
2019-01-20 22:57:15 +01:00
|
|
|
if(!empty(user))
|
|
|
|
return
|
|
|
|
{
|
|
|
|
unquote(user["login"]),
|
|
|
|
unquote(user["html_url"])
|
|
|
|
};
|
|
|
|
|
|
|
|
const json::object sender
|
|
|
|
{
|
|
|
|
content["sender"]
|
|
|
|
};
|
|
|
|
|
|
|
|
return
|
|
|
|
{
|
|
|
|
unquote(sender["login"]),
|
|
|
|
unquote(sender["html_url"])
|
|
|
|
};
|
2019-01-20 01:15:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Researched from yestifico bot
|
2019-02-05 01:18:35 +01:00
|
|
|
ircd::string_view
|
2019-01-20 01:15:59 +01:00
|
|
|
github_find_issue_number(const json::object &content)
|
|
|
|
{
|
|
|
|
const json::object issue(content["issue"]);
|
|
|
|
if(!empty(issue))
|
|
|
|
return unquote(issue["number"]);
|
|
|
|
|
|
|
|
if(content["number"])
|
|
|
|
return unquote(content["number"]);
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Researched from yestifico bot
|
2019-02-05 01:18:35 +01:00
|
|
|
ircd::string_view
|
2019-01-20 01:15:59 +01:00
|
|
|
github_find_commit_hash(const json::object &content)
|
|
|
|
{
|
|
|
|
if(content["sha"])
|
|
|
|
return unquote(content["sha"]);
|
|
|
|
|
|
|
|
const json::object commit(content["commit"]);
|
|
|
|
if(!empty(commit))
|
|
|
|
return unquote(commit["sha"]);
|
|
|
|
|
|
|
|
const json::object head(content["head"]);
|
|
|
|
if(!empty(head))
|
|
|
|
return unquote(head["commit"]);
|
|
|
|
|
|
|
|
const json::object head_commit(content["head_commit"]);
|
|
|
|
if(!empty(head_commit))
|
|
|
|
return unquote(head_commit["id"]);
|
|
|
|
|
|
|
|
const json::object comment(content["comment"]);
|
|
|
|
if(!empty(comment))
|
|
|
|
return unquote(comment["commit_id"]);
|
|
|
|
|
|
|
|
if(content["commit"])
|
|
|
|
return unquote(content["commit"]);
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2019-02-05 01:18:35 +01:00
|
|
|
bool
|
2019-01-20 01:15:59 +01:00
|
|
|
github_validate(const string_view &sigheader,
|
|
|
|
const const_buffer &content,
|
|
|
|
const string_view &secret)
|
|
|
|
try
|
|
|
|
{
|
|
|
|
const auto sig
|
|
|
|
{
|
|
|
|
split(sigheader, "=")
|
|
|
|
};
|
|
|
|
|
|
|
|
crh::hmac hmac
|
|
|
|
{
|
|
|
|
sig.first, secret
|
|
|
|
};
|
|
|
|
|
|
|
|
hmac.update(content);
|
|
|
|
|
2019-02-06 04:03:25 +01:00
|
|
|
char ubuf[64], abuf[sizeof(ubuf) * 2];
|
|
|
|
if(unlikely(sizeof(ubuf) < hmac.length()))
|
|
|
|
throw ircd::panic
|
|
|
|
{
|
|
|
|
"HMAC algorithm '%s' digest exceeds buffer size.",
|
|
|
|
sig.first
|
|
|
|
};
|
|
|
|
|
2019-01-20 01:15:59 +01:00
|
|
|
const const_buffer hmac_bin
|
|
|
|
{
|
|
|
|
hmac.finalize(ubuf)
|
|
|
|
};
|
|
|
|
|
|
|
|
static_assert(sizeof(abuf) >= sizeof(ubuf) * 2);
|
|
|
|
const string_view hmac_hex
|
|
|
|
{
|
|
|
|
u2a(abuf, hmac_bin)
|
|
|
|
};
|
|
|
|
|
|
|
|
return hmac_hex == sig.second;
|
|
|
|
}
|
|
|
|
catch(const crh::error &e)
|
|
|
|
{
|
|
|
|
throw http::error
|
|
|
|
{
|
2019-02-06 04:03:25 +01:00
|
|
|
http::NOT_IMPLEMENTED, "The signature algorithm is not supported.",
|
2019-01-20 01:15:59 +01:00
|
|
|
};
|
|
|
|
}
|