admin: Add link to forge

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

View file

@ -6,6 +6,7 @@ import (
"encoding/base32"
"fmt"
"io"
"net/url"
"os"
"path"
"strings"
@ -46,6 +47,12 @@ type DirectAccessImporter interface {
GetLocalPath(p ...string) string
}
// ForgeLinkedImporter abstracts importer that are linked to a forge
type ForgeLinkedImporter interface {
GetThemeLink(th *fic.Theme) (*url.URL, error)
GetExerciceLink(e *fic.Exercice) (*url.URL, error)
}
// GlobalImporter stores the main importer instance to use for global imports.
var GlobalImporter Importer

View file

@ -2,9 +2,13 @@ package sync
import (
"bufio"
"net/url"
"os"
"regexp"
)
var gitRemoteRe = regexp.MustCompile(`^(?:(?:git@|https://)([\w.@]+)(?:/|:))((?:[\w-_]+)/(?:[\w-_/]+))(?:.git){0,1}(?:(?:/){0,1})$`)
func countFileInDir(dirname string) (int, error) {
files, err := os.ReadDir(dirname)
if err != nil {
@ -45,3 +49,11 @@ func (i GitImporter) stat(filename string) (os.FileInfo, error) {
func (i GitImporter) Kind() string {
return "git originated from " + i.Remote + " on " + i.li.Kind()
}
func getForgeBaseLink(remote string) (u *url.URL, err error) {
res := gitRemoteRe.FindStringSubmatch(remote)
u, err = url.Parse(res[2])
u.Scheme = "https"
u.Host = res[1]
return
}

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
}