2

我有这个主要工作。Github 发送的所有事件最终都在我的 SQS 队列中。webhook 正在组织级别发送带有“Send me everthing”的应用程序/json。我在我的 API Gateway 中使用以下模板映射:

Action=SendMessage&MessageBody={
  "bodyJson":"$util.base64Encode($util.json('$'))",
  "requestId":"$context.requestId",
  "resourcePath":"$context.resourcePath",
  "apiId":"$context.apiId",
  "stage":"$context.stage",
  "resourceId":"$context.resourceId",
  "path":"$context.path",
  "protocol":"$context.protocol",
  "requestTimeEpoch":"$context.requestTimeEpoch",
  "X-GitHub-Event":"$method.request.header.X-GitHub-Event",
  "X-GitHub-Delivery":"$method.request.header.X-GitHub-Delivery",
  "X-Hub-Signature":"$method.request.header.X-Hub-Signature"
}

(我也试过'"bodyJson":"$util.base64Encode($util.body)",')

来自 Github 的大约 5% 的事件通知(各种事件类型,例如:状态、推送)无法正确转换。当我从队列中去 base64 解码消息时,我得到部分消息/部分随机垃圾。注意到每次重试时重新发送相同的事件都会失败。(不过,该事件在 Github UI 中看起来像格式正确的 json。)我正在登录到 cloudwatch,但日志被截断了。所以我不知道有多少原始消息通过了。我还尝试将 application/json 设置为二进制媒体类型。这只是导致所有事件在转换时失败并将 500 返回到 Github。任何人都知道我做错了什么或者这是否是一个错误?

更新 需要多测试一点,但我想我已经弄清楚了。我们需要对 base64Encoded bodyJson 进行 urlEncode 编码,因为 base64 编码输出包括不经过 url 编码的 application/x-www-form-urlencoded 不兼容的字符(+ 和 /):

            Action=SendMessage&MessageBody={
              "bodyJson":"$util.urlEncode($util.base64Encode($input.body))",
              "requestId":"$context.requestId",
              "resourcePath":"$context.resourcePath",
              "apiId":"$context.apiId",
              "stage":"$context.stage",
              "resourceId":"$context.resourceId",
              "path":"$context.path",
              "protocol":"$context.protocol",
              "requestTimeEpoch":"$context.requestTimeEpoch",
              "X-GitHub-Event":"$method.request.header.X-GitHub-Event",
              "X-GitHub-Delivery":"$method.request.header.X-GitHub-Delivery",
              "X-Hub-Signature":"$method.request.header.X-Hub-Signature"
            }
4

0 回答 0