package projects import ( "crypto/sha1" "fmt" "strings" "time" "github.com/go-git/go-git/v5" ) // Git project metadata // Do not use Project.ID directly (remotes may conflict), use Project.GetID() type Project struct { ID int Description string SSHURLToRepo string HTTPURLToRepo string WebURL string Name string NameWithNamespace string Path string PathWithNamespace string AvatarURL string LastActivityAt time.Time Readme string Remote string Owner string Languages *ProjectLanguages gitRepo *git.Repository } type ProjectLanguages []*ProjectLanguage type ProjectLanguage struct { Name string Percentage float32 } func NewProjectLanguages() *ProjectLanguages { var pLangs ProjectLanguages = make([]*ProjectLanguage, 0) return &pLangs } func (pl *ProjectLanguages) AddLanguage(lang *ProjectLanguage) { *pl = append(*pl, lang) } // Gets a unique ID using a short-sha of the http repo // along with the numerical ID of the project. // Uses SSH URL and then Remote if previous is empty func (p *Project) GetID() string { return fmt.Sprintf("%s||%d", p.GetRemoteSha(), p.ID) } func MakeID(remote string, projectID int) string { return fmt.Sprintf("%s||%d", GetRemoteSha(remote), projectID) } func (p *Project) GetRemoteSha() string { remote := p.Remote if remote == "" && p.HTTPURLToRepo != "" { remote = p.HTTPURLToRepo } else if remote == "" && p.WebURL != "" { remote = p.WebURL } return GetRemoteSha(remote) } func GetRemoteSha(remote string) string { return fmt.Sprintf("%x", sha1.Sum([]byte(remote)))[:12] } func (p *Project) String() string { var projectString string if p != nil { projectString = fmt.Sprintf("%s (%s)", p.Path, p.PathWithNamespace) } return projectString } func (p *Project) GetLanguage() *ProjectLanguage { if p.Languages == nil { return nil } var lang *ProjectLanguage var maxPcnt float32 for _, p := range *p.Languages { if p.Percentage > maxPcnt { lang = p } maxPcnt = p.Percentage } return lang } func (p *Project) SanitizedPath() string { return strings.Trim(p.PathWithNamespace, " '\"%<>|`") } func (p *Project) SetRepo(r *git.Repository) { p.gitRepo = r } func (p *Project) GetRepo() *git.Repository { return p.gitRepo }