Skip to content

Commit e28faee

Browse files
fixed getRepoByOrg and setDefaultRepo command
1 parent 2ee9ee5 commit e28faee

File tree

2 files changed

+51
-27
lines changed

2 files changed

+51
-27
lines changed

server/plugin/api.go

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,44 +1441,54 @@ func (p *Plugin) getReposByOrg(c *UserContext, w http.ResponseWriter, r *http.Re
14411441

14421442
opt := github.ListOptions{PerPage: 50}
14431443

1444-
org := r.URL.Query().Get("organization")
1444+
orgString := r.URL.Query().Get("organization")
14451445

1446-
if org == "" {
1446+
if orgString == "" {
14471447
c.Log.Warnf("Organization query param is empty")
14481448
p.writeAPIError(w, &APIErrorResponse{Message: "Organization query is empty, must include organization name ", StatusCode: http.StatusBadRequest})
14491449
return
14501450
}
14511451

1452+
orgList := strings.Split(orgString, ",")
14521453
var allRepos []*github.Repository
1453-
var err error
1454-
var statusCode int
14551454

1456-
// If an organization is the username of an authenticated user then return repos where the authenticated user is the owner
1457-
if org == c.GHInfo.GitHubUsername {
1458-
allRepos, err = p.getRepositoryList(c.Ctx, c.GHInfo, "", githubClient, opt)
1459-
if err != nil {
1460-
c.Log.WithError(err).Errorf("Failed to list repositories")
1461-
p.writeAPIError(w, &APIErrorResponse{Message: "Failed to fetch repositories", StatusCode: http.StatusInternalServerError})
1462-
return
1455+
for _, org := range orgList {
1456+
org = strings.TrimSpace(org)
1457+
if org == "" {
1458+
continue
14631459
}
1464-
} else {
1465-
allRepos, statusCode, err = p.getRepositoryListByOrg(c.Ctx, c.GHInfo, org, githubClient, opt)
1466-
if err != nil {
1467-
if statusCode == http.StatusNotFound {
1468-
allRepos, err = p.getRepositoryList(c.Ctx, c.GHInfo, org, githubClient, opt)
1469-
if err != nil {
1470-
c.Log.WithError(err).Errorf("Failed to list repositories")
1471-
p.writeAPIError(w, &APIErrorResponse{Message: "Failed to fetch repositories", StatusCode: http.StatusInternalServerError})
1472-
return
1460+
1461+
var repos []*github.Repository
1462+
var err error
1463+
var statusCode int
1464+
1465+
// If an organization is the username of an authenticated user then return repos where the authenticated user is the owner
1466+
if org == c.GHInfo.GitHubUsername {
1467+
repos, err = p.getRepositoryList(c.Ctx, c.GHInfo, "", githubClient, opt)
1468+
if err != nil {
1469+
c.Log.WithError(err).Errorf("Failed to list repositories for user %s", org)
1470+
continue
1471+
}
1472+
} else {
1473+
repos, statusCode, err = p.getRepositoryListByOrg(c.Ctx, c.GHInfo, org, githubClient, opt)
1474+
if err != nil {
1475+
if statusCode == http.StatusNotFound {
1476+
repos, err = p.getRepositoryList(c.Ctx, c.GHInfo, org, githubClient, opt)
1477+
if err != nil {
1478+
c.Log.WithError(err).Errorf("Failed to list repositories for org/user %s", org)
1479+
continue
1480+
}
1481+
} else {
1482+
c.Log.WithError(err).Warnf("Failed to list repositories for org %s", org)
1483+
continue
14731484
}
1474-
} else {
1475-
c.Log.WithError(err).Warnf("Failed to list repositories")
1476-
p.writeAPIError(w, &APIErrorResponse{Message: "Failed to fetch repositories", StatusCode: statusCode})
1477-
return
14781485
}
14791486
}
1487+
1488+
allRepos = append(allRepos, repos...)
14801489
}
1481-
// Only send repositories which are part of the requested organization
1490+
1491+
// Only send repositories which are part of the requested organization(s)
14821492
type RepositoryResponse struct {
14831493
Name string `json:"name,omitempty"`
14841494
FullName string `json:"full_name,omitempty"`

server/plugin/command.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -842,8 +842,8 @@ func (p *Plugin) handleSetDefaultRepo(args *model.CommandArgs, parameters []stri
842842
owner = strings.ToLower(owner)
843843
repo = strings.ToLower(repo)
844844

845-
if config.GitHubOrg != "" && strings.ToLower(config.GitHubOrg) != owner {
846-
return fmt.Sprintf("Repository is not part of the locked Github organization. Locked Github organization: %s", config.GitHubOrg)
845+
if config.GitHubOrg != "" && !p.isRepoInLockedOrgs(config.GitHubOrg, owner) {
846+
return fmt.Sprintf("Repository is not part of the locked Github organization. Locked Github organizations: %s", config.GitHubOrg)
847847
}
848848

849849
ctx := context.Background()
@@ -910,6 +910,20 @@ func (p *Plugin) handleUnSetDefaultRepo(args *model.CommandArgs, userInfo *GitHu
910910
return "The default repository has been unset successfully"
911911
}
912912

913+
func (p *Plugin) isRepoInLockedOrgs(configuredOrgs, owner string) bool {
914+
if configuredOrgs == "" {
915+
return true
916+
}
917+
918+
orgs := strings.Split(configuredOrgs, ",")
919+
for _, org := range orgs {
920+
if strings.ToLower(strings.TrimSpace(org)) == strings.ToLower(owner) {
921+
return true
922+
}
923+
}
924+
return false
925+
}
926+
913927
func (p *Plugin) handleSetup(_ *plugin.Context, args *model.CommandArgs, parameters []string) string {
914928
userID := args.UserId
915929
isSysAdmin, err := p.isAuthorizedSysAdmin(userID)

0 commit comments

Comments
 (0)