Cleaning up leftover Github webhooks for org/repo
Signed-off-by: Mihaela Balutoiu <mbalutoiu@cloudbasesolutions.com>
This commit is contained in:
parent
0da5f106a0
commit
f9c3f30ae4
6 changed files with 155 additions and 15 deletions
|
|
@ -83,6 +83,119 @@ func GhRepoRunnersCleanup(ghToken, orgName, repoName, controllerID string) error
|
|||
return nil
|
||||
}
|
||||
|
||||
func ValidateOrgWebhookInstalled(ghToken, url, orgName string) {
|
||||
hook, err := getGhOrgWebhook(url, ghToken, orgName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if hook == nil {
|
||||
panic(fmt.Errorf("github webhook with url %s, for org %s was not properly installed", url, orgName))
|
||||
}
|
||||
}
|
||||
|
||||
func ValidateOrgWebhookUninstalled(ghToken, url, orgName string) {
|
||||
hook, err := getGhOrgWebhook(url, ghToken, orgName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if hook != nil {
|
||||
panic(fmt.Errorf("github webhook with url %s, for org %s was not properly uninstalled", url, orgName))
|
||||
}
|
||||
}
|
||||
|
||||
func ValidateRepoWebhookInstalled(ghToken, url, orgName, repoName string) {
|
||||
hook, err := getGhRepoWebhook(url, ghToken, orgName, repoName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if hook == nil {
|
||||
panic(fmt.Errorf("github webhook with url %s, for repo %s/%s was not properly installed", url, orgName, repoName))
|
||||
}
|
||||
}
|
||||
|
||||
func ValidateRepoWebhookUninstalled(ghToken, url, orgName, repoName string) {
|
||||
hook, err := getGhRepoWebhook(url, ghToken, orgName, repoName)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if hook != nil {
|
||||
panic(fmt.Errorf("github webhook with url %s, for repo %s/%s was not properly uninstalled", url, orgName, repoName))
|
||||
}
|
||||
}
|
||||
|
||||
func GhOrgWebhookCleanup(ghToken, webhookURL, orgName string) error {
|
||||
log.Printf("Cleanup Github webhook with url %s for org %s", webhookURL, orgName)
|
||||
hook, err := getGhOrgWebhook(webhookURL, ghToken, orgName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Remove organization webhook
|
||||
if hook != nil {
|
||||
client := getGithubClient(ghToken)
|
||||
if _, err := client.Organizations.DeleteHook(context.Background(), orgName, hook.GetID()); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("Github webhook with url %s for org %s was removed", webhookURL, orgName)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func GhRepoWebhookCleanup(ghToken, webhookURL, orgName, repoName string) error {
|
||||
log.Printf("Cleanup Github webhook with url %s for repo %s/%s", webhookURL, orgName, repoName)
|
||||
|
||||
hook, err := getGhRepoWebhook(webhookURL, ghToken, orgName, repoName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Remove repository webhook
|
||||
if hook != nil {
|
||||
client := getGithubClient(ghToken)
|
||||
if _, err := client.Repositories.DeleteHook(context.Background(), orgName, repoName, hook.GetID()); err != nil {
|
||||
return err
|
||||
}
|
||||
log.Printf("Github webhook with url %s for repo %s/%s was removed", webhookURL, orgName, repoName)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func getGhOrgWebhook(url, ghToken, orgName string) (*github.Hook, error) {
|
||||
client := getGithubClient(ghToken)
|
||||
ghOrgHooks, _, err := client.Organizations.ListHooks(context.Background(), orgName, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, hook := range ghOrgHooks {
|
||||
hookURL, ok := hook.Config["url"].(string)
|
||||
if ok && hookURL == url {
|
||||
return hook, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func getGhRepoWebhook(url, ghToken, orgName, repoName string) (*github.Hook, error) {
|
||||
client := getGithubClient(ghToken)
|
||||
ghRepoHooks, _, err := client.Repositories.ListHooks(context.Background(), orgName, repoName, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, hook := range ghRepoHooks {
|
||||
hookURL, ok := hook.Config["url"].(string)
|
||||
if ok && hookURL == url {
|
||||
return hook, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func getGithubClient(oauthToken string) *github.Client {
|
||||
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: oauthToken})
|
||||
tc := oauth2.NewClient(context.Background(), ts)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue