Skip to content

Commit

Permalink
release-tool: Update phase 2 jobs to be considered phase 1 when forki…
Browse files Browse the repository at this point in the history
…ng a new branch

Signed-off-by: Or Shoval <oshoval@redhat.com>
  • Loading branch information
oshoval committed Dec 19, 2023
1 parent e7be9d9 commit 4e687a0
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 3 deletions.
1 change: 1 addition & 0 deletions releng/release-tool/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ go_library(
deps = [
"@com_github_google_go_github_v32//github:go_default_library",
"@com_github_masterminds_semver//:go_default_library",
"@io_k8s_sigs_yaml//:go_default_library",
"@org_golang_x_oauth2//:go_default_library",
],
)
Expand Down
79 changes: 76 additions & 3 deletions releng/release-tool/release-tool.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ import (
"strings"
"time"

"golang.org/x/oauth2"

"github.com/Masterminds/semver"
"github.com/google/go-github/v32/github"
"golang.org/x/oauth2"
"sigs.k8s.io/yaml"
)

type blockerListCacheEntry struct {
Expand Down Expand Up @@ -414,8 +414,14 @@ func (r *releaseData) forkProwJobs() error {

// create new prow configs if they don't already exist
if _, err := os.Stat(fullOutputConfig); err != nil && os.IsNotExist(err) {
updateJobConfig, err := updatePhase2Jobs(r.org+"/"+r.repo, fullJobConfig)
if err != nil {
return err
}
defer os.Remove(updateJobConfig)

log.Printf("Creating new prow yaml at path %s", fullOutputConfig)
cmd := exec.Command("/usr/bin/config-forker", "--job-config", fullJobConfig, "--version", version, "--output", fullOutputConfig)
cmd := exec.Command("/usr/bin/config-forker", "--job-config", updateJobConfig, "--version", version, "--output", fullOutputConfig)
bytes, err := cmd.CombinedOutput()
if err != nil {
log.Printf("ERROR: config-forker command output: %s : %s ", string(bytes), err)
Expand Down Expand Up @@ -1271,3 +1277,70 @@ func main() {
}
}
}

func updatePhase2Jobs(orgRepo, fullJobConfig string) (string, error) {
fileContent, err := ioutil.ReadFile(fullJobConfig)
if err != nil {
return "", fmt.Errorf("Error reading yaml file: %v", err)
}

var data map[string]interface{}
if err := yaml.Unmarshal(fileContent, &data); err != nil {
return "", fmt.Errorf("Error unmarshalling yaml: %v", err)
}

err = updatePresubmits(orgRepo, data)
if err != nil {
return "", err
}

updatedContent, err := yaml.Marshal(data)
if err != nil {
return "", fmt.Errorf("Error marshalling yaml: %v", err)
}

updateJobConfig := os.TempDir() + "/job-config.yaml"
if err := ioutil.WriteFile(updateJobConfig, updatedContent, 0644); err != nil {
return "", fmt.Errorf("Error writing updated yaml to file: %v", err)
}

return updateJobConfig, nil
}

func updatePresubmits(orgRepo string, data map[string]interface{}) error {
presubmits, ok := data["presubmits"].(map[string]interface{})
if !ok {
return fmt.Errorf("Missing or invalid 'presubmits' section in yaml")
}

repo, ok := presubmits[orgRepo].([]interface{})
if !ok {
return fmt.Errorf("Missing or invalid section for repository '%s' in yaml", orgRepo)
}

for i, job := range repo {
jobMap, ok := job.(map[string]interface{})
if !ok {
return fmt.Errorf("Invalid job format in yaml")
}

if shouldUpdateJob(jobMap) {
jobMap["always_run"] = true
presubmits[orgRepo].([]interface{})[i] = jobMap
}
}

return nil
}

func shouldUpdateJob(jobMap map[string]interface{}) bool {
optional, optionalOk := jobMap["optional"].(bool)
alwaysRun, alwaysRunOk := jobMap["always_run"].(bool)
runIfChanged, runIfChangedOk := jobMap["run_if_changed"].(string)
skipIfOnlyChanged, skipIfOnlyChangedOk := jobMap["skip_if_only_changed"].(string)

return (!alwaysRunOk || !alwaysRun) &&
(!optionalOk || !optional) &&
(!runIfChangedOk || runIfChanged == "") &&
(!skipIfOnlyChangedOk || skipIfOnlyChanged == "")
}

0 comments on commit 4e687a0

Please sign in to comment.