package projects import ( "crypto/sha1" "fmt" "strings" "time" "github.com/go-git/go-git/v5" ) 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 URL // along with the numerical ID of the project. // Uses SSH URL and then Remote if previous is empty func (p *Project) GetID() string { shaText := p.HTTPURLToRepo if shaText == "" && p.SSHURLToRepo != "" { shaText = p.SSHURLToRepo } else if shaText == "" { shaText = p.Remote } shortSha := fmt.Sprintf("%x", sha1.Sum([]byte(shaText)))[:12] return fmt.Sprintf("%s||%d", shortSha, p.ID) } 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 }