Browse Source

Refactor gitea module into methods

webdeploy
meskio 3 years ago
parent
commit
55537655ae
Signed by: meskio
GPG Key ID: 52B8F5AC97A2DA86
  1. 117
      gitea/gitea.go
  2. 7
      gitea/webhook.go
  3. 46
      server/gitea.go

117
gitea/gitea.go

@ -3,6 +3,7 @@ package gitea
import (
"code.gitea.io/gitea/modules/structs"
"code.gitea.io/sdk/gitea"
"fmt"
"log"
"net/url" // to check URL init vars
"reflect" // to compare structs
@ -10,21 +11,31 @@ import (
// Gitea holds the gitea functionality config vars
type Gitea struct {
GiteaURL, Token, CloneAddr, WebhookRepoURL, WebhookRepoSecret, WebhookURL, WebhookSecret, Repo string
giteaURL string
token string
cloneAddr string
webhookRepoURL string
webhookRepoSecret string
webhookURL string
webhookSecret string
}
type Repo struct {
user string
repo string
cloneAddr string
webhookRepoURL string
webhookRepoSecret string
client *gitea.Client
}
func isValidURL(toTest string) bool {
_, err := url.ParseRequestURI(toTest)
if err != nil {
return false
} else {
return true
}
return err == nil
}
// Init gitea config vars
func Init(giteaURL, token, cloneAddr, webhookRepoURL, webhookRepoSecret, webhookURL, webhookSecret string) *Gitea {
// check URL values
if !isValidURL(giteaURL) || !isValidURL(cloneAddr) || !isValidURL(webhookRepoURL) || !isValidURL(webhookURL) {
giteaURL = "" // to avoid process
@ -32,26 +43,34 @@ func Init(giteaURL, token, cloneAddr, webhookRepoURL, webhookRepoSecret, webhook
}
return &Gitea{
GiteaURL: giteaURL,
Token: token,
CloneAddr: cloneAddr,
WebhookRepoURL: webhookRepoURL,
WebhookRepoSecret: webhookRepoSecret,
WebhookURL: webhookURL,
WebhookSecret: webhookSecret,
giteaURL: giteaURL,
token: token,
cloneAddr: cloneAddr,
webhookRepoURL: webhookRepoURL,
webhookRepoSecret: webhookRepoSecret,
webhookURL: webhookURL,
webhookSecret: webhookSecret,
}
}
func GiteaNewClient(giteaURL, token string) *gitea.Client {
client := gitea.NewClient(giteaURL, token)
return client
func (g *Gitea) Repo(user string, repo string) (*Repo, error) {
if g.giteaURL == "" || g.token == "" || g.webhookRepoSecret == "" || g.webhookSecret == "" {
return nil, fmt.Errorf("Gitea config isn't right, some fields are empty.")
}
return &Repo{
user: user,
repo: repo,
cloneAddr: g.cloneAddr,
webhookRepoURL: g.webhookRepoURL,
webhookRepoSecret: g.webhookRepoSecret,
client: gitea.NewClient(g.giteaURL, g.token),
}, nil
}
func GiteaUserID(user string, client *gitea.Client) (int, error) {
// check users exists and return user id
userexist, err := client.SearchUsers(user, 1)
// userID check users exists and return user id
func (r *Repo) userID() (int, error) {
userexist, err := r.client.SearchUsers(r.user, 1)
if err != nil {
return -1, err
}
@ -64,70 +83,64 @@ func GiteaUserID(user string, client *gitea.Client) (int, error) {
return uid, nil
}
func RepoExists(user string, repo string, client *gitea.Client) bool {
// check repo exists.
repoexists, err := client.GetRepo(user, repo)
// Exists checks if the repo exists.
func (r *Repo) Exists() bool {
repoexists, err := r.client.GetRepo(r.user, r.repo)
if err != nil && err.Error() != "404 Not Found" {
return false
}
// (API responses with empty repo if doesn't exist.)
emptyrepo := &gitea.Repository{}
if reflect.DeepEqual(repoexists, emptyrepo) == true {
return false
} else {
return true
}
return !reflect.DeepEqual(repoexists, emptyrepo)
}
// Delete the repo
func (r *Repo) Delete() error {
return r.client.DeleteRepo(r.user, r.repo)
}
func RepoDelete(user string, repo string, client *gitea.Client) error {
// delete repo
err := client.DeleteRepo(user, repo)
// Migrate template repo to new user repo
func (r *Repo) Migrate() error {
uid, err := r.userID()
if err != nil {
log.Printf("Error on delete repo.")
return err
}
return nil
}
func RepoMigrate(cloneAddr string, uid int, repo string, client *gitea.Client) error {
// migrate template repo to new user repo
migrateRepoOption := structs.MigrateRepoOption{CloneAddr: cloneAddr,
migrateRepoOption := structs.MigrateRepoOption{
CloneAddr: r.cloneAddr,
Description: "Basic hugo site repo",
UID: uid,
RepoName: repo,
RepoName: r.repo,
Private: true}
_, err := client.MigrateRepo(migrateRepoOption)
if err != nil {
return err
}
return nil
_, err = r.client.MigrateRepo(migrateRepoOption)
return err
}
func RepoDeployKey(keypub, user, repo string, client *gitea.Client) error {
// put a deploy key on user repo
// DeployKey put a deploy key on the user repo
func (r *Repo) DeployKey(keypub string) error {
deployKeyOption := structs.CreateKeyOption{
Key: keypub,
ReadOnly: true,
Title: "Web deploy key"}
_, err := client.CreateDeployKey(user, repo, deployKeyOption)
_, err := r.client.CreateDeployKey(r.user, r.repo, deployKeyOption)
if err != nil {
return err
}
return nil
}
func RepoCreateWebhook(webhookRepoURL, webhookRepoSecret, user, repo string, client *gitea.Client) error {
// create a webhook on user repo
// CreateWebhook create a webhook on the user repo
func (r *Repo) CreateWebhook() error {
webHookOption := structs.CreateHookOption{
Active: true,
Config: map[string]string{"content_type": "json",
"url": webhookRepoURL,
"secret": webhookRepoSecret},
"url": r.webhookRepoURL,
"secret": r.webhookRepoSecret},
Events: []string{"push"},
Type: "gitea"}
_, err := client.CreateRepoHook(user, repo, webHookOption)
_, err := r.client.CreateRepoHook(r.user, r.repo, webHookOption)
// TODO: <nil> no vale pa na
if err != nil && err.Error() != "<nil>" {
log.Printf("API Error on migrate.")

7
gitea/webhook.go

@ -9,8 +9,7 @@ import (
"strings"
)
func WebhookDeploy(user, repo, keypriv, keypub, webhookURL, webhookSecret string) error {
func (g *Gitea) WebhookDeploy(user, repo, keypriv, keypub string) error {
type WebhookData struct {
USER string
REPO string
@ -19,14 +18,14 @@ func WebhookDeploy(user, repo, keypriv, keypub, webhookURL, webhookSecret string
SECRET string
}
keypriv = strings.Replace(keypriv, "\n", "|", -1) // key to oneline string, no breaks, '|' (pipe) is newline.
whData := WebhookData{user, repo, keypub, keypriv, webhookSecret}
whData := WebhookData{user, repo, keypub, keypriv, g.webhookSecret}
jsonData, err := json.Marshal(whData)
if err != nil && err.Error() != "<nil>" {
log.Printf("Error marshaling data for webhook.")
return err
}
req, err := http.NewRequest("POST", webhookURL, bytes.NewBuffer(jsonData))
req, err := http.NewRequest("POST", g.webhookURL, bytes.NewBuffer(jsonData))
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}

46
server/gitea.go

@ -14,33 +14,18 @@ func (s *server) giteaHandler(w http.ResponseWriter, r *http.Request) {
return
}
var (
giteaURL = s.gitea.GiteaURL
token = s.gitea.Token
cloneAddr = s.gitea.CloneAddr
webhookRepoURL = s.gitea.WebhookRepoURL
webhookRepoSecret = s.gitea.WebhookRepoSecret
webhookURL = s.gitea.WebhookURL
webhookSecret = s.gitea.WebhookSecret
usergitea = response.User
repo = r.FormValue("reponame")
)
// check config vars. giteaURL is empty if any URL fields are empty, init (gitea package) cares about it.
if giteaURL == "" || token == "" || webhookRepoSecret == "" || webhookSecret == "" {
usergitea := response.User
reponame := r.FormValue("reponame")
repo, err := s.gitea.Repo(usergitea, reponame)
if err != nil {
response.execute("ErrorGiteaInit")
log.Printf("Gitea config isn't right, some fields are empty.")
log.Printf("Error loading the repo: %v", err)
return
}
// create client connection to gitea
client := gitea.GiteaNewClient(giteaURL, token)
uid, err := gitea.GiteaUserID(usergitea, client)
// clean existing repo
if gitea.RepoExists(usergitea, repo, client) {
if err := gitea.RepoDelete(usergitea, repo, client); err != nil {
if repo.Exists() {
if err := repo.Delete(); err != nil {
log.Printf("Error deleting gitea repo: %v ", err)
response.execute("Error")
return
@ -48,7 +33,7 @@ func (s *server) giteaHandler(w http.ResponseWriter, r *http.Request) {
}
// create repo from template repo
if err = gitea.RepoMigrate(cloneAddr, uid, repo, client); err != nil {
if err := repo.Migrate(); err != nil {
log.Printf("Error migrating template repo: %v", err)
response.execute("Error")
return
@ -58,7 +43,7 @@ func (s *server) giteaHandler(w http.ResponseWriter, r *http.Request) {
keypriv, keypub, err := gitea.GenerateRSAKeyPair(4096)
if err != nil {
log.Printf("Error generating key pair: ", err)
if err := gitea.RepoDelete(usergitea, repo, client); err != nil {
if err := repo.Delete(); err != nil {
log.Printf("Error deleting gitea repo: %v", err)
}
response.execute("Error")
@ -66,9 +51,9 @@ func (s *server) giteaHandler(w http.ResponseWriter, r *http.Request) {
}
// create deploy key on repo
if err = gitea.RepoDeployKey(keypub, usergitea, repo, client); err != nil {
if err = repo.DeployKey(keypub); err != nil {
log.Printf("Error deploying key on gitea repo: %v", err)
if err := gitea.RepoDelete(usergitea, repo, client); err != nil {
if err := repo.Delete(); err != nil {
log.Printf("Error deleting gitea repo: %v", err)
}
response.execute("Error")
@ -76,9 +61,9 @@ func (s *server) giteaHandler(w http.ResponseWriter, r *http.Request) {
}
// create gitea webhook on repo
if err = gitea.RepoCreateWebhook(webhookRepoURL, webhookRepoSecret, usergitea, repo, client); err != nil {
if err = repo.CreateWebhook(); err != nil {
log.Printf("Error creating webhook on gitea repo: %v", err)
if err := gitea.RepoDelete(usergitea, repo, client); err != nil {
if err := repo.Delete(); err != nil {
log.Printf("Error deleting gitea repo: %v", err)
}
response.execute("Error")
@ -86,9 +71,9 @@ func (s *server) giteaHandler(w http.ResponseWriter, r *http.Request) {
}
// send keys to web host via webhook.
if err = gitea.WebhookDeploy(usergitea, repo, keypriv, keypub, webhookURL, webhookSecret); err != nil {
if err = s.gitea.WebhookDeploy(usergitea, reponame, keypriv, keypub); err != nil {
log.Printf("Error passing keys to webserver host: %v", err)
if err = gitea.RepoDelete(usergitea, repo, client); err != nil {
if err = repo.Delete(); err != nil {
log.Printf("Error deleting gitea repo: %v", err)
}
response.execute("Error")
@ -97,5 +82,4 @@ func (s *server) giteaHandler(w http.ResponseWriter, r *http.Request) {
log.Printf("Gitea repo deployed and keys sent to webhook listener.")
response.execute("RepoDeployed")
}

Loading…
Cancel
Save