github.com/Rookout/GoSDK@v0.1.48/pkg/information/scm.go (about)

     1  package information
     2  
     3  import (
     4  	"os"
     5  
     6  	pb "github.com/Rookout/GoSDK/pkg/protobuf"
     7  )
     8  
     9  var GitConfig = struct {
    10  	Commit       string
    11  	RemoteOrigin string
    12  	Sources      map[string]string
    13  }{}
    14  
    15  func collectSources(info *AgentInformation) {
    16  	if len(GitConfig.Sources) == 0 {
    17  		return
    18  	}
    19  
    20  	info.Scm.Sources = make([]*pb.SCMInformation_SourceInfo, len(GitConfig.Sources))
    21  	i := 0
    22  	for remoteOriginUrl, commit := range GitConfig.Sources {
    23  		source := &pb.SCMInformation_SourceInfo{RemoteOriginUrl: remoteOriginUrl, Commit: commit}
    24  		info.Scm.Sources[i] = source
    25  		i += 1
    26  	}
    27  }
    28  
    29  func collectScm(info *AgentInformation) error {
    30  	info.Scm = &pb.SCMInformation{}
    31  
    32  	var gitRoot string
    33  	if GitConfig.Commit == "" || GitConfig.RemoteOrigin == "" {
    34  		if cwd, err := os.Getwd(); err == nil {
    35  			gitRoot = FindRoot(cwd)
    36  		}
    37  	}
    38  
    39  	if GitConfig.Commit == "" {
    40  		GitConfig.Commit = GetRevision(gitRoot)
    41  	}
    42  	if GitConfig.RemoteOrigin == "" {
    43  		GitConfig.RemoteOrigin = GetRemoteOrigin(gitRoot)
    44  	}
    45  
    46  	info.Scm.Commit = GitConfig.Commit
    47  	info.Scm.Origin = GitConfig.RemoteOrigin
    48  	collectSources(info)
    49  	return nil
    50  }