git-project-manager/internal/remotes/projects/projects.go

98 lines
2.0 KiB
Go
Raw Normal View History

2024-01-15 20:39:35 +00:00
package projects
import (
"crypto/sha1"
2024-01-15 20:39:35 +00:00
"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
}
2024-01-16 21:14:53 +00:00
func NewProjectLanguages() *ProjectLanguages {
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)
}
// 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)
}
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
}