2024-01-15 20:39:35 +00:00
|
|
|
package projects
|
|
|
|
|
|
|
|
import (
|
2024-12-30 21:42:45 +00:00
|
|
|
"crypto/sha1"
|
2024-01-15 20:39:35 +00:00
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-git/go-git/v5"
|
|
|
|
)
|
|
|
|
|
2025-01-01 01:12:02 +00:00
|
|
|
// Git project metadata
|
|
|
|
// Do not use Project.ID directly (remotes may conflict), use Project.GetID()
|
2024-01-15 20:39:35 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-01-16 21:14:53 +00:00
|
|
|
func NewProjectLanguages() *ProjectLanguages {
|
2024-12-30 21:42:45 +00:00
|
|
|
var pLangs ProjectLanguages = make([]*ProjectLanguage, 0)
|
2024-01-16 21:14:53 +00:00
|
|
|
return &pLangs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pl *ProjectLanguages) AddLanguage(lang *ProjectLanguage) {
|
|
|
|
*pl = append(*pl, lang)
|
|
|
|
}
|
|
|
|
|
2025-01-01 01:12:02 +00:00
|
|
|
// Gets a unique ID using a short-sha of the http repo
|
2024-12-30 21:42:45 +00:00
|
|
|
// along with the numerical ID of the project.
|
|
|
|
// Uses SSH URL and then Remote if previous is empty
|
|
|
|
func (p *Project) GetID() string {
|
2025-01-01 01:12:02 +00:00
|
|
|
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
|
2024-12-30 21:42:45 +00:00
|
|
|
}
|
|
|
|
|
2025-01-01 01:12:02 +00:00
|
|
|
return GetRemoteSha(remote)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetRemoteSha(remote string) string {
|
|
|
|
return fmt.Sprintf("%x", sha1.Sum([]byte(remote)))[:12]
|
2024-12-30 21:42:45 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 20:39:35 +00:00
|
|
|
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
|
|
|
|
}
|