github.com/jenkins-x/jx/v2@v2.1.155/pkg/tekton/pullrefs.go (about)

     1  package tekton
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  // PullRefs is the result of parsing the Prow PULL_REFS
     9  type PullRefs struct {
    10  	BaseBranch string
    11  	BaseSha    string
    12  	ToMerge    map[string]string
    13  }
    14  
    15  // ParsePullRefs parses the Prow PULL_REFS env var formatted string and converts to a map of branch:sha
    16  func ParsePullRefs(pullRefs string) (*PullRefs, error) {
    17  	kvs := strings.Split(pullRefs, ",")
    18  	answer := PullRefs{
    19  		ToMerge: make(map[string]string),
    20  	}
    21  	for i, kv := range kvs {
    22  		s := strings.Split(kv, ":")
    23  		if len(s) != 2 {
    24  			return nil, fmt.Errorf("incorrect format for branch:sha %s", kv)
    25  		}
    26  		if i == 0 {
    27  			answer.BaseBranch = s[0]
    28  			answer.BaseSha = s[1]
    29  		} else {
    30  			answer.ToMerge[s[0]] = s[1]
    31  		}
    32  	}
    33  	return &answer, nil
    34  }
    35  
    36  func (pr *PullRefs) String() string {
    37  	s := fmt.Sprintf("%s:%s", pr.BaseBranch, pr.BaseSha)
    38  	for key, value := range pr.ToMerge {
    39  		s = fmt.Sprintf("%s,%s:%s", s, key, value)
    40  	}
    41  	return s
    42  }