admin: Add link to forge

This commit is contained in:
nemunaire 2022-05-24 21:25:51 +02:00
parent 80917ae436
commit 2c76b5c7a3
7 changed files with 129 additions and 3 deletions

View file

@ -8,10 +8,13 @@ import (
"errors"
"fmt"
"log"
"net/url"
"os"
"os/exec"
"path"
"strings"
"srs.epita.fr/fic-server/libfic"
)
// GitImporter implements an Importer, where files to imports are located
@ -133,3 +136,75 @@ func (i GitImporter) Sync() error {
log.Println("Local git repository synchronized successfully")
return nil
}
func (i GitImporter) GetThemeLink(th *fic.Theme) (u *url.URL, err error) {
prefix := ""
if _, err = os.Stat(path.Join(i.li.Base, ".gitmodules")); !errors.Is(err, os.ErrNotExist) {
thdir := path.Join(i.li.Base, th.Path)
cmdremote := exec.Command("git", "-C", thdir, "remote", "get-url", "origin")
var stdout []byte
stdout, err = cmdremote.CombinedOutput()
if err != nil {
return
}
u, err = getForgeBaseLink(string(bytes.TrimSpace(stdout)))
// Search .git directory
for {
if _, err = os.Stat(path.Join(thdir, ".git")); !errors.Is(err, os.ErrNotExist) {
break
}
thdir, _ = path.Split(thdir)
}
prefix = strings.TrimPrefix(thdir, i.li.Base)
} else {
u, err = getForgeBaseLink(i.Remote)
}
if err != nil {
return
}
u.Path = path.Join(u.Path, "-", "tree", "master", strings.TrimPrefix(th.Path, prefix))
return
}
func (i GitImporter) GetExerciceLink(e *fic.Exercice) (u *url.URL, err error) {
prefix := ""
if _, err = os.Stat(path.Join(i.li.Base, ".gitmodules")); !errors.Is(err, os.ErrNotExist) {
exdir := path.Join(i.li.Base, e.Path)
cmdremote := exec.Command("git", "-C", exdir, "remote", "get-url", "origin")
var stdout []byte
stdout, err = cmdremote.CombinedOutput()
if err != nil {
return
}
u, err = getForgeBaseLink(string(bytes.TrimSpace(stdout)))
// Search .git directory
for {
if _, err = os.Stat(path.Join(exdir, ".git")); !errors.Is(err, os.ErrNotExist) {
break
}
exdir, _ = path.Split(exdir)
}
prefix = strings.TrimPrefix(exdir, i.li.Base)
} else {
u, err = getForgeBaseLink(i.Remote)
}
if err != nil {
return
}
u.Path = path.Join(u.Path, "-", "tree", "master", strings.TrimPrefix(e.Path, prefix))
return
}