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:
@@ -1,6 +1,10 @@
|
||||
package gitea
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Repository struct {
|
||||
Id uint
|
||||
@@ -15,3 +19,21 @@ func NewRepository(name, owner string) (repo Repository) {
|
||||
repo.Full_name = fmt.Sprintf("%s/%s", owner, name)
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user