【问题标题】:Git post-receive hook, send curl commit message to Discord WebhookGit post-receive hook,将 curl 提交消息发送到 Discord Webhook
【发布时间】:2019-01-16 23:42:42
【问题描述】:

每当有人向主人推送时,我都会尝试在我们的不和谐中发布一条信息消息。

我有一个如下所示的接收后 bash 脚本:

#!/bin/bash

while read oldrev newrev ref
do
    if [[ $ref =~ .*/master$ ]];
    then

        tail=$(git log -1 --pretty=format:'%h %cn: %s%b' $newrev)       
        url='https://discordapp.com/api/webhooks/validapikey'
        curl -i \
        -H "Accept: application/json" \
        -H "Content-Type:application/json" \
        -X POST \
        --data  '{"content": "'$tail'"}' $url
    fi
done

如果我将 tail 输出到文件,我会得到预期的字符串

6baf5 user: last commit message

但该消息并未在 discord 上发布

如果我将 $tail 替换为“hello”,它就会被发布。

【问题讨论】:

  • 不和谐的想法很不错,我喜欢它,伙计

标签: bash curl discord githooks


【解决方案1】:

3 条建议:

a) -d "{\"content\": \"${tail}\"}"

b) 您可以使用与您的项目相同的语言编写它,例如 Python 或 NodeJS,这总是比 bash 更好(使用相同的名称并使其可执行)

c) 为避免在每台开发机器中维护这一点,您可以使用 https://pypi.org/project/hooks4git 或任何其他提供 git hook 管理的方法在您的 repo 中对该逻辑进行版本控制。

【讨论】:

  • 另外,如果你可以使用 BitBucket/Pipelines (private repo) 或 TravisCI (github.com public repo),它们只需要通过配置来完成这个通知逻辑。
  • a) 有效!对于 b) 和 c) 我们有一个独立的 git 服务器,我们在其中放置了钩子,我们不想仅为钩子安装语言,但感谢您的建议和解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-18
  • 1970-01-01
  • 2015-03-22
  • 2012-01-05
  • 2020-03-20
  • 2014-06-02
  • 2013-07-17
相关资源
最近更新 更多