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

     1  package dependencymatrix
     2  
     3  import (
     4  	"regexp"
     5  
     6  	"github.com/pkg/errors"
     7  )
     8  
     9  var (
    10  	dependencyUpdateRegex = regexp.MustCompile(`^(?m:chore\((?:deps|dependencies)\): (?:bump|update) (.*) from ([\w\.]*) to ([\w\.]*)$)`)
    11  	slugLinkRegex         = regexp.MustCompile(`^(?:([\w-]*?)?\/?([\w-]+)|(https?):\/\/([\w\.]*)\/([\w-]*)\/([\w-]*)(?:\.git)?)(?::([\w-]*))?$`)
    12  )
    13  
    14  // DependencyMessage is the parsed representation of a dependency update message on a commit
    15  type DependencyMessage struct {
    16  	URL         string
    17  	Owner       string
    18  	Host        string
    19  	Repo        string
    20  	FromVersion string
    21  	ToVersion   string
    22  	Component   string
    23  	Scheme      string
    24  }
    25  
    26  // ParseDependencyMessage parses a dependency update message on a commit and returns the DependencyMessage struct
    27  func ParseDependencyMessage(msg string) (*DependencyMessage, error) {
    28  	matches := dependencyUpdateRegex.FindStringSubmatch(msg)
    29  	if matches == nil {
    30  		// string does not match at all
    31  		return nil, nil
    32  	}
    33  	if len(matches) != 4 {
    34  		return nil, errors.Errorf("parsing %s as dependency update message", msg)
    35  	}
    36  	slug := matches[1]
    37  	var urlScheme, urlHost, owner, repo string
    38  	slugMatches := slugLinkRegex.FindStringSubmatch(slug)
    39  	if len(slugMatches) == 8 {
    40  		if slugMatches[6] != "" {
    41  			owner = slugMatches[5]
    42  			repo = slugMatches[6]
    43  			urlScheme = slugMatches[3]
    44  			urlHost = slugMatches[4]
    45  		} else {
    46  			owner = slugMatches[1]
    47  			repo = slugMatches[2]
    48  		}
    49  		update := &DependencyMessage{
    50  			Owner:       owner,
    51  			Repo:        repo,
    52  			Host:        urlHost,
    53  			Scheme:      urlScheme,
    54  			FromVersion: matches[2],
    55  			ToVersion:   matches[3],
    56  			Component:   slugMatches[7],
    57  		}
    58  		return update, nil
    59  	}
    60  	return nil, nil
    61  }