|
@ -13,6 +13,7 @@ import ( |
|
|
"github.com/matrix-org/go-neb/types" |
|
|
"github.com/matrix-org/go-neb/types" |
|
|
"net/http" |
|
|
"net/http" |
|
|
"regexp" |
|
|
"regexp" |
|
|
|
|
|
"sort" |
|
|
"strconv" |
|
|
"strconv" |
|
|
"strings" |
|
|
"strings" |
|
|
) |
|
|
) |
|
@ -221,6 +222,7 @@ func (s *githubService) PostRegister(oldService types.Service) { |
|
|
log.Errorf("PostRegister: %s does not have a github session", s.ClientUserID) |
|
|
log.Errorf("PostRegister: %s does not have a github session", s.ClientUserID) |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if oldService != nil { |
|
|
if oldService != nil { |
|
|
old, ok := oldService.(*githubService) |
|
|
old, ok := oldService.(*githubService) |
|
|
if !ok { |
|
|
if !ok { |
|
@ -228,6 +230,12 @@ func (s *githubService) PostRegister(oldService types.Service) { |
|
|
return |
|
|
return |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Don't spam github webhook requests if we can help it.
|
|
|
|
|
|
if sameRepos(s, old) { |
|
|
|
|
|
log.Print("PostRegister: old and new services have the same repo set. Nooping.") |
|
|
|
|
|
return |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
// TODO: We should be adding webhooks in Register() then removing old hooks in PostRegister()
|
|
|
// TODO: We should be adding webhooks in Register() then removing old hooks in PostRegister()
|
|
|
//
|
|
|
//
|
|
|
// By doing both operations in PostRegister(), if some of the requests fail we can end up in
|
|
|
// By doing both operations in PostRegister(), if some of the requests fail we can end up in
|
|
@ -289,6 +297,37 @@ func modifyWebhooks(s *githubService, cli *github.Client, removeHooks bool) { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func sameRepos(a *githubService, b *githubService) bool { |
|
|
|
|
|
getRepos := func(s *githubService) []string { |
|
|
|
|
|
r := make(map[string]bool) |
|
|
|
|
|
for _, roomConfig := range s.Rooms { |
|
|
|
|
|
for ownerRepo, _ := range roomConfig.Repos { |
|
|
|
|
|
r[ownerRepo] = true |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
var rs []string |
|
|
|
|
|
for k, _ := range r { |
|
|
|
|
|
rs = append(rs, k) |
|
|
|
|
|
} |
|
|
|
|
|
return rs |
|
|
|
|
|
} |
|
|
|
|
|
aRepos := getRepos(a) |
|
|
|
|
|
bRepos := getRepos(b) |
|
|
|
|
|
|
|
|
|
|
|
if len(aRepos) != len(bRepos) { |
|
|
|
|
|
return false |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
sort.Strings(aRepos) |
|
|
|
|
|
sort.Strings(bRepos) |
|
|
|
|
|
for i := 0; i < len(aRepos); i += 1 { |
|
|
|
|
|
if aRepos[i] != bRepos[i] { |
|
|
|
|
|
return false |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
return true |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
func (s *githubService) githubClientFor(userID string, allowUnauth bool) *github.Client { |
|
|
func (s *githubService) githubClientFor(userID string, allowUnauth bool) *github.Client { |
|
|
token, err := getTokenForUser(s.RealmID, userID) |
|
|
token, err := getTokenForUser(s.RealmID, userID) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|