github.com/redhat-appstudio/e2e-tests@v0.0.0-20240520140907-9709f6f59323/pkg/utils/build/git.go (about)

     1  package build
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strconv"
     7  
     8  	"github.com/redhat-appstudio/e2e-tests/pkg/clients/github"
     9  	"github.com/redhat-appstudio/e2e-tests/pkg/constants"
    10  	"github.com/redhat-appstudio/e2e-tests/pkg/utils"
    11  )
    12  
    13  // resolve the git url and revision from a pull request. If not found, return a default
    14  // that is set from environment variables.
    15  func ResolveGitDetails(repoUrlENV, repoRevisionENV string) (string, string, error) {
    16  	defaultGitURL := fmt.Sprintf("https://github.com/%s/%s", constants.DEFAULT_GITHUB_BUILD_ORG, constants.DEFAULT_GITHUB_BUILD_REPO)
    17  	defaultGitRevision := "main"
    18  	// If we are testing the changes from a pull request, APP_SUFFIX may contain the
    19  	// pull request ID. If it looks like an ID, then fetch information about the pull
    20  	// request and use it to determine which git URL and revision to use for the EC
    21  	// pipelines. NOTE: This is a workaround until Pipeline as Code supports passing
    22  	// the source repo URL: https://issues.redhat.com/browse/SRVKP-3427. Once that's
    23  	// implemented, remove the APP_SUFFIX support below and simply rely on the other
    24  	// environment variables to set the git revision and URL directly.
    25  	appSuffix := os.Getenv("APP_SUFFIX")
    26  	if pullRequestID, err := strconv.ParseInt(appSuffix, 10, 64); err == nil {
    27  		gh, err := github.NewGithubClient(utils.GetEnv(constants.GITHUB_TOKEN_ENV, ""), constants.DEFAULT_GITHUB_BUILD_ORG)
    28  		if err != nil {
    29  			return "", "", err
    30  		}
    31  		return gh.GetPRDetails(constants.DEFAULT_GITHUB_BUILD_REPO, int(pullRequestID))
    32  
    33  	}
    34  	return utils.GetEnv(repoUrlENV, defaultGitURL), utils.GetEnv(repoRevisionENV, defaultGitRevision), nil
    35  }