Skip to content

Commit

Permalink
Parse jenkins env
Browse files Browse the repository at this point in the history
  • Loading branch information
KacperMalachowski committed Jan 2, 2025
1 parent 195fe5d commit 81b16b7
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 3 deletions.
53 changes: 50 additions & 3 deletions cmd/image-builder/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"os"
"regexp"
"slices"
"strconv"

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
42 changes: 42 additions & 0 deletions cmd/image-builder/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 81b16b7

Please sign in to comment.