Skip to content

Commit

Permalink
[MI-2872]:Fixed issue mattermost#548 on github 'org loack for username'
Browse files Browse the repository at this point in the history
  • Loading branch information
Kshitij-Katiyar committed Mar 13, 2023
1 parent 39a2a70 commit fcd30e0
Showing 1 changed file with 35 additions and 12 deletions.
47 changes: 35 additions & 12 deletions server/plugin/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1203,32 +1203,47 @@ func (p *Plugin) getMilestones(c *UserContext, w http.ResponseWriter, r *http.Re
p.writeJSON(w, allMilestones)
}

func getRepositoryList(c *UserContext, userName string, githubClient *github.Client, allRepos []*github.Repository, opt github.ListOptions) ([]*github.Repository, error) {
for {
repos, resp, err := githubClient.Repositories.List(c.Ctx, userName, &github.RepositoryListOptions{ListOptions: opt})
if err != nil {
return nil, err
}
allRepos = append(allRepos, repos...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}

return allRepos, nil
}

func (p *Plugin) getRepositories(c *UserContext, w http.ResponseWriter, r *http.Request) {
githubClient := p.githubConnectUser(c.Context.Ctx, c.GHInfo)

org := p.getConfiguration().GitHubOrg

var allRepos []*github.Repository
var err error
opt := github.ListOptions{PerPage: 50}

invalidOrgName := false

if org == "" {
for {
repos, resp, err := githubClient.Repositories.List(c.Ctx, "", &github.RepositoryListOptions{ListOptions: opt})
if err != nil {
c.Log.WithError(err).Warnf("Failed to list repositories")
p.writeAPIError(w, &APIErrorResponse{Message: "Failed to fetch repositories", StatusCode: http.StatusInternalServerError})
return
}
allRepos = append(allRepos, repos...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
allRepos, err = getRepositoryList(c, "", githubClient, allRepos, opt)
if err != nil {
c.Log.WithError(err).Warnf("Failed to list repositories")
p.writeAPIError(w, &APIErrorResponse{Message: "Failed to fetch repositories", StatusCode: http.StatusInternalServerError})
}
} else {
for {
repos, resp, err := githubClient.Repositories.ListByOrg(c.Ctx, org, &github.RepositoryListByOrgOptions{Sort: "full_name", ListOptions: opt})
if err != nil {
if resp.StatusCode == 404 {
invalidOrgName = true
break
}
c.Log.WithError(err).Warnf("Failed to list repositories by org")
p.writeAPIError(w, &APIErrorResponse{Message: "Failed to fetch repositories", StatusCode: http.StatusInternalServerError})
return
Expand All @@ -1239,6 +1254,14 @@ func (p *Plugin) getRepositories(c *UserContext, w http.ResponseWriter, r *http.
}
opt.Page = resp.NextPage
}

if invalidOrgName {
allRepos, err = getRepositoryList(c, org, githubClient, allRepos, opt)
if err != nil {
c.Log.WithError(err).Warnf("Failed to list repositories")
p.writeAPIError(w, &APIErrorResponse{Message: "Failed to fetch repositories", StatusCode: http.StatusInternalServerError})
}
}
}

// Only send down fields to client that are needed
Expand Down

0 comments on commit fcd30e0

Please sign in to comment.