我们正在使用Webhooks收听Jira问题事件。每当发生故障单上的活动时,JIRA API都会通知在Google Cloud Function内部运行的HTTP端点。在此Cloud Function中,我们只是通过以下方式将未更改的请求转发到Pub / Sub:
def forward_to_pubsub(request):
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project_id, topic_id)
# Request body contains user, project, issue basically everything we care about
jira_body = request.json
publisher.publish(topic_path, data=json.dumps(jira_body).encode('utf-8'))
这似乎是不必要的跃点。无论如何,是否将Pub / Sub配置为HTTP Webhook的目标?两种方法都无法从文档中找出答案。
答案 0 :(得分:1)
否,不可能。发布/订阅主题没有可用作Webhooks的相应HTTP终结点。如果要从最终用户应用程序触发发布/订阅主题,则需要在它们之间使用前端服务器:
有关更多详细信息,请参见https://cloud.google.com/pubsub/docs/overview#endpoints。
答案 1 :(得分:1)
实际上,可以直接使用REST API通过HTTP将消息发布到Pub / Sub主题。从https://cloud.google.com/pubsub/docs/publisher#rest的快速入门指南-
要发布消息,请发送POST请求,如下所示:
POST https://pubsub.googleapis.com/v1/projects/PROJECT_ID/topics/TOPIC_ID:publish
Authorization: Bearer $(gcloud auth application-default print-access-token)
替换以下内容:
在请求正文中指定以下字段:
{
"messages": [
{
"attributes": {
"KEY": "VALUE",
...
},
"data": MESSAGE_DATA,
}
]
}
替换以下内容:
如果请求成功,则响应是带有消息ID的JSON对象。以下示例是带有消息ID的响应:
{
"messageIds": [
"19916711285"
]
}
发布消息后,发布/订阅服务将消息ID返回给发布者。
在使用Google REST API时,还有一个REST API reference和一个overview of service endpoints and common instructions,应该会有所帮助。
请注意,发布请求必须确认到projects.topic.publish method specification,因此仍然需要从JIRA webhook通知中进行转换。