added logging when login or hook fails. helps troubleshoot common issues
This commit is contained in:
parent
ab3df4bd65
commit
eda3cfbe4b
4 changed files with 50 additions and 21 deletions
|
@ -61,6 +61,9 @@ func (db *DB) GetBuildLast(repo string) (*common.Build, error) {
|
|||
build := &common.Build{}
|
||||
err := db.View(func(t *bolt.Tx) error {
|
||||
raw := t.Bucket(bucketBuildSeq).Get(key)
|
||||
if raw == nil {
|
||||
return ErrKeyNotFound
|
||||
}
|
||||
num := binary.LittleEndian.Uint32(raw)
|
||||
key = []byte(repo + "/" + strconv.FormatUint(uint64(num), 10))
|
||||
return get(t, bucketBuild, key, build)
|
||||
|
|
|
@ -3,6 +3,7 @@ package server
|
|||
import (
|
||||
"strings"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/drone/drone/common"
|
||||
// "github.com/bradrydzewski/drone/worker"
|
||||
"github.com/gin-gonic/gin"
|
||||
|
@ -19,6 +20,7 @@ func PostHook(c *gin.Context) {
|
|||
|
||||
hook, err := remote.Hook(c.Request)
|
||||
if err != nil {
|
||||
log.Errorf("failure to parse hook. %s", err)
|
||||
c.Fail(400, err)
|
||||
return
|
||||
}
|
||||
|
@ -27,6 +29,7 @@ func PostHook(c *gin.Context) {
|
|||
return
|
||||
}
|
||||
if hook.Repo == nil {
|
||||
log.Errorf("failure to ascertain repo from hook.")
|
||||
c.Writer.WriteHeader(400)
|
||||
return
|
||||
}
|
||||
|
@ -34,23 +37,36 @@ func PostHook(c *gin.Context) {
|
|||
// a build may be skipped if the text [CI SKIP]
|
||||
// is found inside the commit message
|
||||
if hook.Commit != nil && strings.Contains(hook.Commit.Message, "[CI SKIP]") {
|
||||
log.Infof("ignoring hook. [ci skip] found for %s")
|
||||
c.Writer.WriteHeader(204)
|
||||
return
|
||||
}
|
||||
|
||||
repo, err := store.GetRepo(hook.Repo.FullName)
|
||||
if err != nil {
|
||||
log.Errorf("failure to find repo %s from hook. %s", hook.Repo.FullName, err)
|
||||
c.Fail(404, err)
|
||||
return
|
||||
}
|
||||
|
||||
if repo.Disabled || repo.User == nil || (repo.DisablePR && hook.PullRequest != nil) {
|
||||
switch {
|
||||
case repo.Disabled:
|
||||
log.Infof("ignoring hook. repo %s is disabled.", repo.FullName)
|
||||
c.Writer.WriteHeader(204)
|
||||
return
|
||||
case repo.User == nil:
|
||||
log.Warnf("ignoring hook. repo %s has no owner.", repo.FullName)
|
||||
c.Writer.WriteHeader(204)
|
||||
return
|
||||
case repo.DisablePR && hook.PullRequest != nil:
|
||||
log.Warnf("ignoring hook. repo %s is disabled for pull requests.", repo.FullName)
|
||||
c.Writer.WriteHeader(204)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := store.GetUser(repo.User.Login)
|
||||
if err != nil {
|
||||
log.Errorf("failure to find repo owner %s. %s", repo.User.Login, err)
|
||||
c.Fail(500, err)
|
||||
return
|
||||
}
|
||||
|
@ -63,6 +79,7 @@ func PostHook(c *gin.Context) {
|
|||
// featch the .drone.yml file from the database
|
||||
_, err = remote.Script(user, repo, build)
|
||||
if err != nil {
|
||||
log.Errorf("failure to get .drone.yml for %s. %s", repo.FullName, err)
|
||||
c.Fail(404, err)
|
||||
return
|
||||
}
|
||||
|
|
|
@ -2,11 +2,11 @@ package server
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
log "github.com/Sirupsen/logrus"
|
||||
"github.com/drone/drone/common"
|
||||
"github.com/drone/drone/common/gravatar"
|
||||
"github.com/drone/drone/common/httputil"
|
||||
|
@ -52,15 +52,20 @@ func GetLogin(c *gin.Context) {
|
|||
login := ToUser(c)
|
||||
u, err := store.GetUser(login.Login)
|
||||
if err != nil {
|
||||
count, err := store.GetUserCount()
|
||||
if err != nil {
|
||||
log.Errorf("cannot register %s. %s", login.Login, err)
|
||||
c.Redirect(303, "/login#error=internal_error")
|
||||
return
|
||||
}
|
||||
|
||||
// if self-registration is disabled we should
|
||||
// return a notAuthorized error. the only exception
|
||||
// is if no users exist yet in the system we'll proceed.
|
||||
if !settings.Service.Open {
|
||||
count, err := store.GetUserCount()
|
||||
if err != nil || count != 0 {
|
||||
c.String(400, "Unable to create account. Registration is closed")
|
||||
return
|
||||
}
|
||||
if !settings.Service.Open && count != 0 {
|
||||
log.Errorf("cannot register %s. registration closed", login.Login)
|
||||
c.Redirect(303, "/login#error=access_denied")
|
||||
return
|
||||
}
|
||||
|
||||
// create the user account
|
||||
|
@ -74,15 +79,14 @@ func GetLogin(c *gin.Context) {
|
|||
|
||||
// insert the user into the database
|
||||
if err := store.InsertUser(u); err != nil {
|
||||
log.Println(err)
|
||||
c.Fail(400, err)
|
||||
log.Errorf("cannot insert %s. %s", login.Login, err)
|
||||
c.Redirect(303, "/login#error=internal_error")
|
||||
return
|
||||
}
|
||||
|
||||
// // if this is the first user, they
|
||||
// // should be an admin.
|
||||
//if u.ID == 1 {
|
||||
if u.Login == "bradrydzewski" {
|
||||
// if this is the first user, they
|
||||
// should be an admin.
|
||||
if count == 0 {
|
||||
u.Admin = true
|
||||
}
|
||||
}
|
||||
|
@ -96,15 +100,15 @@ func GetLogin(c *gin.Context) {
|
|||
u.Gravatar = gravatar.Generate(u.Email)
|
||||
|
||||
if err := store.UpdateUser(u); err != nil {
|
||||
log.Println(err)
|
||||
c.Fail(400, err)
|
||||
log.Errorf("cannot update %s. %s", u.Login, err)
|
||||
c.Redirect(303, "/login#error=internal_error")
|
||||
return
|
||||
}
|
||||
|
||||
token, err := session.GenerateToken(c.Request, u)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
c.Fail(400, err)
|
||||
log.Errorf("cannot create token for %s. %s", u.Login, err)
|
||||
c.Redirect(303, "/login#error=internal_error")
|
||||
return
|
||||
}
|
||||
c.Redirect(303, "/#access_token="+token)
|
||||
|
@ -130,6 +134,7 @@ func getLoginOauth2(c *gin.Context) {
|
|||
var code = c.Request.FormValue("code")
|
||||
//var state = c.Request.FormValue("state")
|
||||
if len(code) == 0 {
|
||||
// TODO this should be a random number, verified by a cookie
|
||||
c.Redirect(303, config.AuthCodeURL("random"))
|
||||
return
|
||||
}
|
||||
|
@ -138,14 +143,16 @@ func getLoginOauth2(c *gin.Context) {
|
|||
var trans = &oauth2.Transport{Config: config}
|
||||
var token, err = trans.Exchange(code)
|
||||
if err != nil {
|
||||
c.Fail(400, err)
|
||||
log.Errorf("cannot get access_token. %s", err)
|
||||
c.Redirect(303, "/login#error=token_exchange")
|
||||
return
|
||||
}
|
||||
|
||||
// get user account
|
||||
user, err := remote.Login(token.AccessToken, token.RefreshToken)
|
||||
if err != nil {
|
||||
c.Fail(404, err)
|
||||
log.Errorf("cannot get user with access_token. %s", err)
|
||||
c.Redirect(303, "/login#error=user_not_found")
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -172,7 +179,8 @@ func getLoginBasic(c *gin.Context) {
|
|||
// get user account
|
||||
user, err := remote.Login(username, password)
|
||||
if err != nil {
|
||||
c.Fail(404, err)
|
||||
log.Errorf("invalid username or password for %s. %s", username, err)
|
||||
c.Redirect(303, "/login#error=invalid_credentials")
|
||||
return
|
||||
}
|
||||
|
||||
|
|
|
@ -212,6 +212,7 @@ func PostRepo(c *gin.Context) {
|
|||
c.Fail(500, err)
|
||||
return
|
||||
}
|
||||
println(link)
|
||||
|
||||
// persist the repository
|
||||
err = store.InsertRepo(user, r)
|
||||
|
|
Loading…
Reference in a new issue