garm/test/integration/e2e/github_client_utils.go
Ionut Balutoiu 318bc52b57 Refactor integration E2E tests
* General cleanup of the integration tests Golang code. Move the
  `e2e.go` codebase into its own package and separate files.
* Reduce the overall log spam from the integration tests output.
* Add final GitHub workflow step that stops GARM server, and does the
  GitHub cleanup of any orphaned resources.
* Add `TODO` to implement cleanup of the orphaned GitHub webhooks.
  This is useful, if the uninstall of the webhooks failed.
* Add `TODO` for extra missing checks on the GitHub webhooks
  install / uninstall logic.

Signed-off-by: Ionut Balutoiu <ibalutoiu@cloudbasesolutions.com>
2023-08-24 15:22:46 +03:00

90 lines
2.9 KiB
Go

package e2e
import (
"context"
"fmt"
"log"
"github.com/google/go-github/v53/github"
"golang.org/x/oauth2"
)
func TriggerWorkflow(ghToken, orgName, repoName, workflowFileName, labelName string) {
log.Printf("Trigger workflow with label %s", labelName)
client := getGithubClient(ghToken)
eventReq := github.CreateWorkflowDispatchEventRequest{
Ref: "main",
Inputs: map[string]interface{}{
"sleep_time": "50",
"runner_label": labelName,
},
}
if _, err := client.Actions.CreateWorkflowDispatchEventByFileName(context.Background(), orgName, repoName, workflowFileName, eventReq); err != nil {
panic(err)
}
}
func GhOrgRunnersCleanup(ghToken, orgName, controllerID string) error {
log.Printf("Cleanup Github runners, labelled with controller ID %s, from org %s", controllerID, orgName)
client := getGithubClient(ghToken)
ghOrgRunners, _, err := client.Actions.ListOrganizationRunners(context.Background(), orgName, nil)
if err != nil {
return err
}
// Remove organization runners
controllerLabel := fmt.Sprintf("runner-controller-id:%s", controllerID)
for _, orgRunner := range ghOrgRunners.Runners {
for _, label := range orgRunner.Labels {
if label.GetName() == controllerLabel {
if _, err := client.Actions.RemoveOrganizationRunner(context.Background(), orgName, orgRunner.GetID()); err != nil {
// We don't fail if we can't remove a single runner. This
// is a best effort to try and remove all the orphan runners.
log.Printf("Failed to remove organization runner %s: %v", orgRunner.GetName(), err)
break
}
log.Printf("Removed organization runner %s", orgRunner.GetName())
break
}
}
}
return nil
}
func GhRepoRunnersCleanup(ghToken, orgName, repoName, controllerID string) error {
log.Printf("Cleanup Github runners, labelled with controller ID %s, from repo %s/%s", controllerID, orgName, repoName)
client := getGithubClient(ghToken)
ghRepoRunners, _, err := client.Actions.ListRunners(context.Background(), orgName, repoName, nil)
if err != nil {
return err
}
// Remove repository runners
controllerLabel := fmt.Sprintf("runner-controller-id:%s", controllerID)
for _, repoRunner := range ghRepoRunners.Runners {
for _, label := range repoRunner.Labels {
if label.GetName() == controllerLabel {
if _, err := client.Actions.RemoveRunner(context.Background(), orgName, repoName, repoRunner.GetID()); err != nil {
// We don't fail if we can't remove a single runner. This
// is a best effort to try and remove all the orphan runners.
log.Printf("Failed to remove repository runner %s: %v", repoRunner.GetName(), err)
break
}
log.Printf("Removed repository runner %s", repoRunner.GetName())
break
}
}
}
return nil
}
func getGithubClient(oauthToken string) *github.Client {
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: oauthToken})
tc := oauth2.NewClient(context.Background(), ts)
return github.NewClient(tc)
}