From ebdf9ed379017d948ae5508d50f5b6ae02fb8388 Mon Sep 17 00:00:00 2001 From: Stefan Prodan Date: Thu, 28 Oct 2021 10:30:24 +0300 Subject: [PATCH] e2e: Retry the GitHub API calls Signed-off-by: Stefan Prodan --- .github/workflows/bootstrap.yaml | 1 + tests/bootstrap/main.go | 26 ++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/.github/workflows/bootstrap.yaml b/.github/workflows/bootstrap.yaml index 74ed0ed0..60c86385 100644 --- a/.github/workflows/bootstrap.yaml +++ b/.github/workflows/bootstrap.yaml @@ -79,6 +79,7 @@ jobs: env: GITHUB_TOKEN: ${{ secrets.GITPROVIDER_BOT_TOKEN }} GITHUB_REPO_NAME: ${{ steps.vars.outputs.test_repo_name }} + GITHUB_ORG_NAME: fluxcd-testing - name: libgit2 run: | /tmp/flux create source git test-libgit2 \ diff --git a/tests/bootstrap/main.go b/tests/bootstrap/main.go index e7735d8e..3d59de2e 100644 --- a/tests/bootstrap/main.go +++ b/tests/bootstrap/main.go @@ -7,6 +7,7 @@ import ( "github.com/fluxcd/go-git-providers/github" "github.com/fluxcd/go-git-providers/gitprovider" + "k8s.io/client-go/util/retry" ) func main() { @@ -55,8 +56,10 @@ spec: Content: &patchContent, }, } + + orgName := os.Getenv("GITHUB_ORG_NAME") repoName := os.Getenv("GITHUB_REPO_NAME") - githubToken := os.Getenv("GITHUB_TOKEN") + githubToken := os.Getenv(github.TokenVariable) client, err := github.NewClient(github.WithOAuth2Token(githubToken)) if err != nil { log.Fatalf("error initializing github client: %s", err) @@ -64,12 +67,27 @@ spec: repoRef := gitprovider.OrgRepositoryRef{ OrganizationRef: gitprovider.OrganizationRef{ - Organization: "flux-testing", - Domain: "github.com", + Organization: orgName, + Domain: github.DefaultDomain, }, RepositoryName: repoName, } - repo, err := client.OrgRepositories().Get(context.Background(), repoRef) + + if ok, err := client.HasTokenPermission(context.Background(), gitprovider.TokenPermissionRWRepository); err != nil { + log.Fatalf("error getting token permission: %s", err) + } else { + if !ok { + log.Fatal("token has no write permissions") + } + } + + var repo gitprovider.OrgRepository + err = retry.OnError(retry.DefaultRetry, func(err error) bool { + return err != nil + }, func() error { + repo, err = client.OrgRepositories().Get(context.Background(), repoRef) + return err + }) if err != nil { log.Fatalf("error getting %s repository in org %s: %s", repoRef.RepositoryName, repoRef.Organization, err) }