From 81b16b7c270f7f8a5fbdc8ea97e3aab17e9215dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kacper=20Ma=C5=82achowski?= Date: Thu, 2 Jan 2025 12:11:00 +0100 Subject: [PATCH] Parse jenkins env --- cmd/image-builder/config.go | 53 ++++++++++++++++++++++++++++++-- cmd/image-builder/config_test.go | 42 +++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 3 deletions(-) diff --git a/cmd/image-builder/config.go b/cmd/image-builder/config.go index 839b56e6b748..8f1fce74890f 100644 --- a/cmd/image-builder/config.go +++ b/cmd/image-builder/config.go @@ -4,6 +4,7 @@ import ( "encoding/json" "fmt" "os" + "regexp" "slices" "strconv" @@ -157,7 +158,7 @@ func LoadGitStateConfig(ciSystem CISystem) (GitStateConfig, error) { case GithubActions: return loadGithubActionsGitState() case Jenkins: - return loadJenksingGitState() + return loadJenkinsGitState() default: // Unknown CI System, return error and empty git state return GitStateConfig{}, fmt.Errorf("unknown ci system, got %s", ciSystem) @@ -329,9 +330,55 @@ func loadGithubActionsGitState() (GitStateConfig, error) { } } -func loadJenksingGitState() (GitStateConfig, error) { +func loadJenkinsGitState() (GitStateConfig, error) { // Load from env specific for Jenkins Jobs - return GitStateConfig{}, fmt.Errorf("Jenkins is not supported as CI system") + prID, isPullRequest := os.LookupEnv("CHANGE_ID") + gitURL := os.Getenv("GIT_URL") + + owner, repo, err := extractOwnerAndRepoFromGitURL(gitURL) + if err != nil { + return GitStateConfig{}, fmt.Errorf("failed to extract owner and repository from git URL %s: %w", gitURL, err) + } + + baseCommitSHA := os.Getenv("GIT_COMMIT") + + gitState := GitStateConfig{ + RepositoryName: repo, + RepositoryOwner: owner, + JobType: "postsubmit", + BaseCommitSHA: baseCommitSHA, + } + + if isPullRequest { + pullNumber, err := strconv.Atoi(prID) + if err != nil { + return GitStateConfig{}, fmt.Errorf("failed to parse CHANGE_ID environment variable: %w", err) + } + + baseRef := os.Getenv("CHANGE_BRANCH") + pullRequestHeadSHA := os.Getenv("CHANGE_TARGET") + + gitState.JobType = "presubmit" + gitState.PullRequestNumber = pullNumber + gitState.BaseCommitRef = baseRef + gitState.PullHeadCommitSHA = pullRequestHeadSHA + gitState.isPullRequest = true + } + + return gitState, nil +} + +func extractOwnerAndRepoFromGitURL(gitURL string) (string, string, error) { + re := regexp.MustCompile(`.*/(.*)/(.*)`) + + matches := re.FindStringSubmatch(gitURL) + fmt.Println(matches, gitURL) + + if len(matches) != 3 { + return "", "", fmt.Errorf("failed to extract owner and repository from git URL") + } + + return matches[1], matches[2], nil } // DetermineUsedCISystem return CISystem bind to system in which image builder is running or error if unknown diff --git a/cmd/image-builder/config_test.go b/cmd/image-builder/config_test.go index a54cb4480122..5d5325924886 100644 --- a/cmd/image-builder/config_test.go +++ b/cmd/image-builder/config_test.go @@ -379,6 +379,48 @@ func TestLoadGitStateConfig(t *testing.T) { PullHeadCommitSHA: "e47034172c36d3e5fb407b5ba57adf0f7868599d", }, }, + { + name: "load data from push event for jenkins", + options: options{ + ciSystem: Jenkins, + }, + env: map[string]string{ + "BRANCH_NAME": "refs/heads/main", + "JENKINS_HOME": "/some/absolute/path", + "GIT_URL": "github.com/kyma-project/test-infra", + "GIT_COMMIT": "1234", + "CHANGE_TARGET": "refs/heads/main", + }, + gitState: GitStateConfig{ + RepositoryName: "test-infra", + RepositoryOwner: "kyma-project", + JobType: "postsubmit", + BaseCommitSHA: "1234", + }, + }, + { + name: "load data from pull request event for jenkins", + options: options{ + ciSystem: Jenkins, + }, + env: map[string]string{ + "BRANCH_NAME": "refs/heads/main", + "JENKINS_HOME": "/some/absolute/path", + "CHANGE_ID": "14", + "GIT_URL": "github.com/kyma-project/test-infra", + "GIT_COMMIT": "1234", + "CHANGE_TARGET": "4321", + }, + gitState: GitStateConfig{ + RepositoryName: "test-infra", + RepositoryOwner: "kyma-project", + JobType: "presubmit", + BaseCommitSHA: "1234", + PullRequestNumber: 14, + PullHeadCommitSHA: "4321", + isPullRequest: true, + }, + }, } for _, c := range tc {