github.com/quickfeed/quickfeed@v0.0.0-20240507093252-ed8ca812a09c/scm/env_helper.go (about)

     1  package scm
     2  
     3  import (
     4  	"context"
     5  	"os"
     6  	"sync"
     7  	"testing"
     8  
     9  	"github.com/quickfeed/quickfeed/internal/env"
    10  	"github.com/quickfeed/quickfeed/internal/qtest"
    11  )
    12  
    13  func GetTestOrganization(t *testing.T) string {
    14  	t.Helper()
    15  	qfTestOrg := os.Getenv("QF_TEST_ORG")
    16  	if len(qfTestOrg) < 1 {
    17  		t.Skip("This test requires that the 'QF_TEST_ORG' is set and that you have access to said GitHub organization")
    18  	}
    19  	return qfTestOrg
    20  }
    21  
    22  func GetTestSCM(t *testing.T) (*GithubSCM, string) {
    23  	t.Helper()
    24  	accessToken := GetAccessToken(t)
    25  	scmClient := NewGithubSCMClient(qtest.Logger(t), accessToken)
    26  	user, _, err := scmClient.client.Users.Get(context.Background(), "")
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	return scmClient, *user.Login
    31  }
    32  
    33  func GetAccessToken(t *testing.T) string {
    34  	t.Helper()
    35  	accessToken := os.Getenv("GITHUB_ACCESS_TOKEN")
    36  	if len(accessToken) < 1 {
    37  		t.Skip("This test requires that 'GITHUB_ACCESS_TOKEN' is set.")
    38  	}
    39  	return accessToken
    40  }
    41  
    42  const (
    43  	envFile               = ".env-testing"
    44  	appName               = "QuickFeed Testing App"
    45  	appCreateInstructions = `The %s is not configured in %s.
    46  To create the %s and configure it, run:
    47  %% cd web/manifest ; GITHUB_APP=1 go test -v -run TestCreateQuickFeedApp`
    48  	InstallInstructions = `You may need to manually install the %s on the %s organization.
    49  
    50  From the user that installed the %s:
    51  Select Settings -> Developer settings -> GitHub Apps ->
    52  Select Edit: QuickFeed Testing ->
    53  Select Install App (left menu)
    54  Find your test organization in the list and click Install
    55  `
    56  )
    57  
    58  var (
    59  	mgr  *Manager
    60  	once sync.Once
    61  )
    62  
    63  func GetSCMManager(t *testing.T) *Manager {
    64  	if mgr != nil {
    65  		return mgr
    66  	}
    67  	once.Do(func() {
    68  		t.Helper()
    69  		if os.Getenv("GITHUB_APP") == "" {
    70  			t.Skipf("Skipping test. To run: GITHUB_APP=1 go test -v -run %s", t.Name())
    71  		}
    72  		// Load environment variables from $QUICKFEED/.env-testing.
    73  		// Will not override variables already defined in the environment.
    74  		if err := env.Load(env.RootEnv(envFile)); err != nil {
    75  			t.Fatal(err)
    76  		}
    77  		if !env.HasAppID() {
    78  			t.Fatalf(appCreateInstructions, appName, envFile, appName)
    79  		}
    80  		scmConfig, err := NewSCMConfig()
    81  		if err != nil {
    82  			t.Fatal(err)
    83  		}
    84  		mgr = NewSCMManager(scmConfig)
    85  	})
    86  	return mgr
    87  }
    88  
    89  func GetAppSCM(t *testing.T) SCM {
    90  	t.Helper()
    91  	if os.Getenv("GITHUB_APP") == "" {
    92  		t.Skipf("Skipping test. To run: GITHUB_APP=1 go test -v -run %s", t.Name())
    93  	}
    94  	if mgr == nil {
    95  		GetSCMManager(t)
    96  	}
    97  	qfTestOrg := GetTestOrganization(t)
    98  	appSCM, err := mgr.GetOrCreateSCM(context.Background(), qtest.Logger(t), qfTestOrg)
    99  	if err != nil {
   100  		t.Logf(InstallInstructions, appName, qfTestOrg, appName)
   101  		t.Fatal(err)
   102  	}
   103  	return appSCM
   104  }