Browse Source

Review comments

pull/20/head
Kegan Dougal 8 years ago
parent
commit
f928b4c84e
  1. 1
      src/github.com/matrix-org/go-neb/database/db.go
  2. 2
      src/github.com/matrix-org/go-neb/database/schema.go
  3. 3
      src/github.com/matrix-org/go-neb/realms/jira/jira.go
  4. 29
      src/github.com/matrix-org/go-neb/services/jira/jira.go

1
src/github.com/matrix-org/go-neb/database/db.go

@ -185,6 +185,7 @@ func (d *ServiceDB) LoadAuthRealm(realmID string) (realm types.AuthRealm, err er
}
// LoadAuthRealmsByType loads all auth realms with the given type from the database.
// The realms are ordered based on their realm ID.
// Returns an empty list if there are no realms with that type.
func (d *ServiceDB) LoadAuthRealmsByType(realmType string) (realms []types.AuthRealm, err error) {
err = runTransaction(d.db, func(txn *sql.Tx) error {

2
src/github.com/matrix-org/go-neb/database/schema.go

@ -263,7 +263,7 @@ func selectRealmTxn(txn *sql.Tx, realmID string) (types.AuthRealm, error) {
}
const selectRealmsByTypeSQL = `
SELECT realm_id, realm_json FROM auth_realms WHERE realm_type = $1
SELECT realm_id, realm_json FROM auth_realms WHERE realm_type = $1 ORDER BY realm_id
`
func selectRealmsByTypeTxn(txn *sql.Tx, realmType string) (realms []types.AuthRealm, err error) {

3
src/github.com/matrix-org/go-neb/realms/jira/jira.go

@ -262,8 +262,7 @@ func (r *JIRARealm) JIRAClient(userID string, allowUnauth bool) (*jira.Client, e
if jsession.AccessSecret == "" || jsession.AccessToken == "" {
if allowUnauth {
// make an unauthenticated client
cli, err = jira.NewClient(nil, r.JIRAEndpoint)
return cli, err
return jira.NewClient(nil, r.JIRAEndpoint)
}
return nil, errors.New("No authenticated session found for " + userID)
}

29
src/github.com/matrix-org/go-neb/services/jira/jira.go

@ -81,9 +81,17 @@ func (s *jiraService) Plugin(roomID string) plugin.Plugin {
},
}
cli, err := r.JIRAClient(userID, false)
if err != nil {
return nil, err
}
i, res, err := cli.Issue.Create(&iss)
if err != nil {
log.WithError(err).Print("Failed to create issue")
log.WithFields(log.Fields{
log.ErrorKey: err,
"user_id": userID,
"project": pkey,
"realm_id": r.ID(),
}).Print("Failed to create issue")
return nil, errors.New("Failed to create issue")
}
if res.StatusCode < 200 || res.StatusCode >= 300 {
@ -123,34 +131,39 @@ func (s *jiraService) projectToRealm(userID, pkey string) (*realms.JIRARealm, er
}
// typecast and move ones which the user has authed with to the front of the queue
var queue []*realms.JIRARealm
var unauthRealms []*realms.JIRARealm
for _, r := range knownRealms {
jrealm, ok := r.(*realms.JIRARealm)
if !ok {
logger.WithField("realm_id", r.ID()).Print(
"Failed to type-cast 'jira' type realm into JIRARealm")
"Failed to type-cast 'jira' type realm into JIRARealm",
)
continue
}
_, err := database.GetServiceDB().LoadAuthSessionByUser(r.ID(), userID)
if err != nil {
if err == sql.ErrNoRows {
// not authed with this JIRA realm; add to back of queue.
queue = append(queue, jrealm)
unauthRealms = append(unauthRealms, jrealm)
} else {
logger.WithError(err).WithField("realm_id", r.ID()).Print(
"Failed to load auth sessions for user")
"Failed to load auth sessions for user",
)
}
continue // this may not have been the match anyway so don't give up!
}
// authed with this JIRA realm; add to front of queue
queue = append([]*realms.JIRARealm{jrealm}, queue...)
queue = append(queue, jrealm)
}
// push unauthed realms to the back
queue = append(queue, unauthRealms...)
for _, jr := range queue {
exists, err := jr.ProjectKeyExists(userID, pkey)
if err != nil {
logger.WithError(err).WithField("realm_id", jr.ID()).Print(
"Failed to check if project key exists on this realm.")
"Failed to check if project key exists on this realm.",
)
continue // may not have been found anyway so keep searching!
}
if exists {

Loading…
Cancel
Save