Created
June 6, 2024 19:20
-
-
Save archi144/3c3569e3ee906778725e163053dc1ffe to your computer and use it in GitHub Desktop.
Redeliver GitHub webhook for certain repos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"github.com/google/go-github/v62/github" | |
"log" | |
) | |
const ( | |
orgName = "" | |
token = "" // GH token | |
hookID = 0 // replace with your value | |
apiURL = "https://api.github.com" | |
) | |
var ( | |
repositoryIDs = []int64{} // id of repositories for which need to redeliver webhook | |
) | |
func inSearchedReposList(currentRepoID int64) bool { | |
for _, repoID := range repositoryIDs { | |
if currentRepoID == repoID { | |
return true | |
} | |
} | |
return false | |
} | |
func main() { | |
client, err := github.NewClient(nil).WithAuthToken(token).WithEnterpriseURLs(apiURL, "") | |
if err != nil { | |
log.Fatalf("Error during client's initialisation: %v", err) | |
} | |
ctx := context.TODO() | |
var hooks []*github.HookDelivery | |
var response *github.Response | |
var cursor string | |
for { | |
hooks, response, err = client.Organizations.ListHookDeliveries(ctx, orgName, hookID, &github.ListCursorOptions{PerPage: 100, Cursor: cursor}) | |
if err != nil { | |
log.Fatalf("List hook deliveries: %v", err) | |
} | |
for _, hook := range hooks { | |
if hook.GetStatusCode() != 200 && inSearchedReposList(hook.GetRepositoryID()) { | |
_, _, err := client.Organizations.RedeliverHookDelivery(ctx, orgName, hookID, hook.GetID()) | |
if err != nil { | |
log.Printf("Error during redelivering webhook: %v", err) | |
} | |
log.Printf("Webhook of date %v was delivered", hook.GetDeliveredAt()) | |
} | |
log.Printf("Webhook with status %d was skipped and delivery date %v was skipped. Rate limit left %v, cursor is %s", hook.GetStatusCode(), hook.GetDeliveredAt(), response.Rate, response.Cursor) | |
} | |
// cursor is used in pagination, each response from ListHookDeliveries returns a cursor to the next page | |
// if it's empty then we've reached the last page of avaialbe webhooks | |
if response.Cursor == "" { | |
log.Println("Cursor is empty, we've processed all the possible hooks") | |
return | |
} | |
cursor = response.Cursor | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment