如何在GitHub中对多个组织的所有仓库的任何推送都收到通知?

huangapple go评论93阅读模式
英文:

How can I be notified on any push, for all of the repositories of multiple organizations in GitHub?

问题

在一段时间内,我需要监控并进行代码审查,针对GitHub上多个组织的所有存储库的每次推送。

例如:

/Org1/Repo1
/Org1/Repo2
/Org1/Repo3
...
/Org1/Repo15

/Org2/Repo1
/Org2/Repo2
...
/Org2/Repo10

我尝试使用通知功能,并关注了这些存储库,以便它们出现在我的关注的存储库中。我将订阅更改为观看,并选择了在GitHub上通知我。但是,当同事推送新代码时,什么都没有发生。随着更多的推送完成,我根本没有收到任何通知。甚至没有一条通知。

那么,我该如何实现这个目标?

英文:

For a period of time I need to monitor and code-review every push that happens for ALL of the repositories of multiple organizations on GitHub.

For example:

/Org1/Repo1
/Org1/Repo2
/Org1/Repo3
...
/Org1/Repo15

/Org2/Repo1
/Org2/Repo2
...
/Org2/Repo10

I tried the Notifications and watched those repos so that they are on my Watched repos. I chagned the subscriptions => watching to Notify me: on GitHub. But when a colleague pushed a new code, nothing happened. And as more pushes are done, I get no notifications at all. Not even one notification.

So, how can I achieve that goal?

答案1

得分: 1

你可以通过为每个存储库设置webhook来实现这一点。GitHub的Webhooks允许您构建或设置与GitHub.com上的特定事件订阅相关的集成。当触发其中一个事件时,GitHub会将HTTP POST负载发送到配置的Webhook URL。Webhooks可用于更新外部问题跟踪器,触发CI构建,更新备份镜像,甚至部署到生产服务器。

每当存储库推送时,Webhook现在都会触发,发送POST请求到您指定的服务器URL。您可以让此服务器在接收到负载时发送电子邮件或其他形式的通知给您。请记住,您需要处理发送到您服务器的负载,并将其转化为对您有用的通知。

以下是如何为推送事件在存储库中设置Webhook的步骤:

  • 转到“Settings”选项卡。单击“Webhooks”
  • 单击“Add webhook”。在“Payload URL”框中输入您服务器的URL。
  • 选择“application/json”作为内容类型。
  • 选择“Just the push event.”选项。确保“Active”已选中。
  • 单击“Add webhook”以保存更改。

每当存储库推送时,Webhook现在都会触发,发送POST请求到您指定的服务器URL。您可以让此服务器在接收到负载时发送电子邮件或其他形式的通知给您。请记住,您需要处理发送到您服务器的负载,并将其转化为对您有用的通知。

不幸的是,这必须针对您想要监视的每个单独存储库进行操作。如果您需要为许多存储库执行此操作,您可能会发现使用GitHub的API来自动化此过程很有用。

在Python中使用PyGithub,可以像下面这样实现:

from github import Github

g = Github("access_token")

for org in ["Org1", "Org2"]:
    for repo in g.get_organization(org).get_repos():
        config = {"url": "https://your-server.com/payload", "content_type": "json"}
        events = ["push"]
        repo.create_hook("web", config, events, active=True)

上面的脚本将遍历指定组织中的所有存储库,并为推送事件创建Webhook。

您需要将"access_token"替换为您的实际访问令牌,将https://your-server.com/payload替换为您的实际服务器URL。

英文:

You can achieve this by setting up a webhook for push events in each repository. GitHub's webhooks allow you to build or set up integrations which subscribe to certain events on GitHub.com. When one of those events is triggered, GitHub sends a HTTP POST payload to the webhook configured URL. Webhooks can be used to update an external issue tracker, trigger CI builds, update a backup mirror or even deploy to your production server.

webhook will now fire every time there is a push to the repository, sending a POST request to the server URL you specified. You could have this server send you an email or another form of notification when it receives a payload. Keep in mind that you will have to handle the payload sent to your server and turn it into a useful notification for yourself.

Here is how you can set up a webhook for push events, in the repository,

  • Go to the "Settings" tab. Click on "Webhooks".
  • Click on "Add webhook". Enter the URL for your server in the "Payload URL" box.
  • Choose "application/json" for the content type.
  • Select "Just the push event." option. Make sure "Active" is checked.
  • Click "Add webhook" to save the changes

Webhook will now fire every time there is a push to the repository, sending a POST request to the server URL you specified. You could have this server send you an email or another form of notification when it receives a payload. Keep in mind that you will have to handle the payload sent to your server and turn it into a useful notification for yourself.

Unfortunately, this must be done for each individual repository you want to monitor. If you need to do this for many repositories, you might find it useful to use GitHub's API to automate the process.

It would look like something below in Python using PyGithub,

from github import Github

g = Github("access_token")

for org in ["Org1", "Org2"]:
    for repo in g.get_organization(org).get_repos():
        config = {"url": "https://your-server.com/payload", "content_type": "json"}
        events = ["push"]
        repo.create_hook("web", config, events, active=True)

Above script will iterate over all the repositories in the specified organizations and create a webhook for push events.

You need to replace "access_token" with your actual access token and https://your-server.com/payload with your actual server URL.

huangapple
  • 本文由 发表于 2023年7月31日 20:55:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803851.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定