github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/core/section/github/lists.go (about) 1 // Copyright 2016 Documize Inc. <legal@documize.com>. All rights reserved. 2 // 3 // This software (Documize Community Edition) is licensed under 4 // GNU AGPL v3 http://www.gnu.org/licenses/agpl-3.0.en.html 5 // 6 // You can operate outside the AGPL restrictions by purchasing 7 // Documize Enterprise Edition and obtaining a commercial license 8 // by contacting <sales@documize.com>. 9 // 10 // https://documize.com 11 12 package github 13 14 import ( 15 "fmt" 16 "net/http" 17 18 "github.com/documize/community/core/log" 19 "github.com/documize/community/core/section/provider" 20 21 gogithub "github.com/google/go-github/github" 22 ) 23 24 func listFailed(method string, config githubConfig, client *gogithub.Client, w http.ResponseWriter) (failed bool) { 25 switch method { // which list to choose? 26 27 case "owners": 28 29 me, _, err := client.Users.Get("") 30 if err != nil { 31 log.Error("github get user details:", err) 32 provider.WriteError(w, "github", err) 33 return 34 } 35 36 orgs, _, err := client.Organizations.List("", nil) 37 if err != nil { 38 log.Error("github get user's organisations:", err) 39 provider.WriteError(w, "github", err) 40 return 41 } 42 43 owners := make([]githubOwner, 1+len(orgs)) 44 owners[0] = githubOwner{ID: *me.Login, Name: *me.Login} 45 for ko, vo := range orgs { 46 id := 1 + ko 47 owners[id].ID = *vo.Login 48 owners[id].Name = *vo.Login 49 } 50 51 owners = sortOwners(owners) 52 53 provider.WriteJSON(w, owners) 54 55 case "orgrepos": 56 57 var render []githubBranch 58 if config.Owner != "" { 59 60 me, _, err := client.Users.Get("") 61 if err != nil { 62 log.Error("github get user details:", err) 63 provider.WriteError(w, "github", err) 64 return 65 } 66 67 var repos []*gogithub.Repository 68 if config.Owner == *me.Login { 69 repos, _, err = client.Repositories.List(config.Owner, nil) 70 } else { 71 opt := &gogithub.RepositoryListByOrgOptions{ 72 ListOptions: gogithub.ListOptions{PerPage: 100}, 73 } 74 repos, _, err = client.Repositories.ListByOrg(config.Owner, opt) 75 } 76 if err != nil { 77 log.Error("github get user/org repositories:", err) 78 provider.WriteError(w, "github", err) 79 return 80 } 81 for _, vr := range repos { 82 render = append(render, 83 githubBranch{ 84 Name: "master", 85 ID: fmt.Sprintf("%s:%s", config.Owner, *vr.Name), 86 Owner: config.Owner, 87 Repo: *vr.Name, 88 Private: *vr.Private, 89 Included: false, 90 URL: *vr.HTMLURL, 91 }) 92 } 93 } 94 render = sortBranches(render) 95 96 provider.WriteJSON(w, render) 97 98 case "content": 99 100 provider.WriteJSON(w, refreshReportData(&config, client)) 101 102 default: 103 return true // failed to get a list 104 } 105 return 106 }