add(repository): discover repository informations
This implemtation determines the git remote origin url and extracts the information for the owner and repo names for the api url's. WIP for #2.
This commit is contained in:
@@ -5,11 +5,26 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"gitea.yves-biener.de/yves-biener/gitwarrior/internal/gitea"
|
"gitea.yves-biener.de/yves-biener/gitwarrior/internal/gitea"
|
||||||
|
"gitea.yves-biener.de/yves-biener/gitwarrior/internal/gitw"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
repository := gitea.NewRepository("gitwarrior", "yves-biener")
|
// TODO: server url may be also be derived from the git configuration?
|
||||||
server := gitea.NewGitea("https://gitea.yves-biener.de")
|
server := gitea.NewGitea("https://gitea.yves-biener.de")
|
||||||
|
|
||||||
|
repository, err := gitw.Discover()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
repository, err = server.VerifyRepository(repository)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(-1)
|
||||||
|
}
|
||||||
|
fmt.Printf("%#v\n", repository)
|
||||||
|
fmt.Println("---")
|
||||||
|
|
||||||
issue, err := server.GetIssue(repository, 1)
|
issue, err := server.GetIssue(repository, 1)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
package gitea
|
package gitea
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
type Repository struct {
|
type Repository struct {
|
||||||
Id uint
|
Id uint
|
||||||
@@ -15,3 +19,21 @@ func NewRepository(name, owner string) (repo Repository) {
|
|||||||
repo.Full_name = fmt.Sprintf("%s/%s", owner, name)
|
repo.Full_name = fmt.Sprintf("%s/%s", owner, name)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (gitea *Gitea) VerifyRepository(repository Repository) (Repository, error) {
|
||||||
|
url := fmt.Sprintf("%s/repos/%s", gitea.Url(), repository.Full_name)
|
||||||
|
response, err := http.Get(url)
|
||||||
|
if err != nil {
|
||||||
|
return repository, err
|
||||||
|
}
|
||||||
|
defer response.Body.Close()
|
||||||
|
var data map[string]interface{}
|
||||||
|
decoder := json.NewDecoder(response.Body)
|
||||||
|
if err = decoder.Decode(&data); err != nil {
|
||||||
|
return repository, err
|
||||||
|
}
|
||||||
|
// extract the id value to the current repository
|
||||||
|
// NOTE: there may be other informations we also want to capture here as well
|
||||||
|
repository.Id = uint(data["id"].(float64))
|
||||||
|
return repository, nil
|
||||||
|
}
|
||||||
|
|||||||
37
internal/gitw/discover.go
Normal file
37
internal/gitw/discover.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package gitw
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os/exec"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"gitea.yves-biener.de/yves-biener/gitwarrior/internal/gitea"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Discover() (gitea.Repository, error) {
|
||||||
|
var repository gitea.Repository
|
||||||
|
cmd := exec.Command("git", "remote", "get-url", "origin")
|
||||||
|
out, err := cmd.Output()
|
||||||
|
if err != nil {
|
||||||
|
return repository, err
|
||||||
|
}
|
||||||
|
re := regexp.MustCompile("^git@.*:(.*).git")
|
||||||
|
matches := re.FindAllStringSubmatch(string(out), -1)
|
||||||
|
var match string
|
||||||
|
if len(matches) > 0 && len(matches[0]) == 2 {
|
||||||
|
match = matches[0][1] // get the first match which is the Full_name of the repository
|
||||||
|
if len(match) <= 0 {
|
||||||
|
return repository, errors.New("No matching group found. Are you in a git repository?")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return repository, errors.New("No matching group found. Are you in a git repository?")
|
||||||
|
}
|
||||||
|
repo := strings.Split(match, "/")
|
||||||
|
if len(repo) != 2 {
|
||||||
|
msg := fmt.Sprintf("Expected remote url to contain {owner}/{repo}, but found: %#v", repo)
|
||||||
|
return repository, errors.New(msg)
|
||||||
|
}
|
||||||
|
return gitea.NewRepository(repo[1], repo[0]), nil
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user