github.com/jancarloviray/community@v0.41.1-0.20170124221257-33a66c87cf2f/core/section/github/model.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 "sort" 16 "strings" 17 "time" 18 19 "github.com/documize/community/core/log" 20 21 gogithub "github.com/google/go-github/github" 22 ) 23 24 type githubRender struct { 25 Config githubConfig `json:"config"` 26 List []githubBranch `json:"list"` 27 RepoCount int `json:"repoCount"` 28 ShowList bool `json:"showList"` 29 ShowIssueNumbers bool `json:"showIssueNumbers"` 30 BranchCommits []githubCommit `json:"branchCommits"` 31 HasCommits bool `json:"hasCommits"` 32 CommitCount int `json:"commitCount"` 33 Issues []githubIssue `json:"issues"` 34 HasIssues bool `json:"hasIssues"` 35 SharedLabels []githubSharedLabel `json:"sharedLabels"` 36 HasSharedLabels bool `json:"hasSharedLabels"` 37 OpenIssues int `json:"openIssues"` 38 ClosedIssues int `json:"closedIssues"` 39 Limit int `json:"limit"` 40 Milestones []githubMilestone `json:"milestones"` 41 HasMilestones bool `json:"hasMilestones"` 42 OpenMS int `json:"openMS"` 43 ClosedMS int `json:"closedMS"` 44 OpenPRs int `json:"openPRs"` 45 ClosedPRs int `json:"closedPRs"` 46 AuthorStats []githubAuthorStats `json:"authorStats"` 47 HasAuthorStats bool `json:"hasAuthorStats"` 48 NumContributors int `json:"numContributors"` 49 } 50 51 type report struct { 52 refresh func(*githubRender, *githubConfig, *gogithub.Client) error 53 render func(*githubRender, *githubConfig) error 54 template string 55 } 56 57 var reports = make(map[string]report) 58 59 type githubOwner struct { 60 ID string `json:"id"` 61 Name string `json:"name"` 62 } 63 64 type githubBranch struct { 65 ID string `json:"id"` 66 Owner string `json:"owner"` 67 Repo string `json:"repo"` 68 Name string `json:"name"` 69 Included bool `json:"included"` 70 URL string `json:"url"` 71 Color string `json:"color,omitempty"` 72 Comma bool `json:"comma"` 73 Private bool `json:"private"` 74 } 75 76 type githubLabel struct { 77 ID string `json:"id"` 78 Owner string `json:"owner"` 79 Repo string `json:"repo"` 80 Name string `json:"name"` 81 Included bool `json:"included"` 82 URL string `json:"url"` 83 Color string `json:"color,omitempty"` 84 } 85 86 type githubConfig struct { 87 Token string `json:"-"` // NOTE very important that the secret Token is not leaked to the client side, so "-" 88 UserID string `json:"userId"` 89 PageID string `json:"pageId"` 90 Owner string `json:"owner_name"` 91 BranchSince string `json:"branchSince,omitempty"` 92 SincePtr *time.Time `json:"-"` 93 Since string `json:"-"` 94 BranchLines int `json:"branchLines,omitempty,string"` 95 OwnerInfo githubOwner `json:"owner"` 96 ClientID string `json:"clientId"` 97 CallbackURL string `json:"callbackUrl"` 98 Lists []githubBranch `json:"lists,omitempty"` 99 ReportOrder []string `json:"-"` 100 DateMessage string `json:"-"` 101 UserNames map[string]string `json:"UserNames"` 102 ShowMilestones bool `json:"showMilestones,omitempty"` 103 ShowIssues bool `json:"showIssues,omitempty"` 104 ShowCommits bool `json:"showCommits,omitempty"` 105 } 106 107 func (c *githubConfig) Clean() { 108 c.Owner = c.OwnerInfo.Name 109 if len(c.BranchSince) >= len("yyyy/mm/dd hh:ss") { 110 var since time.Time 111 tt := []byte("yyyy-mm-ddThh:mm:00Z") 112 for _, i := range []int{0, 1, 2, 3, 5, 6, 8, 9, 11, 12, 14, 15} { 113 tt[i] = c.BranchSince[i] 114 } 115 err := since.UnmarshalText(tt) 116 if err != nil { 117 log.ErrorString("Date unmarshall '" + c.BranchSince + "'->'" + string(tt) + "' error: " + err.Error()) 118 } else { 119 c.SincePtr = &since 120 } 121 } 122 if c.SincePtr == nil { 123 c.DateMessage = " (the last 7 days)" 124 since := time.Now().AddDate(0, 0, -7) 125 c.SincePtr = &since 126 } else { 127 c.DateMessage = "" 128 } 129 c.Since = (*c.SincePtr).Format(issuesTimeFormat) 130 131 c.ReportOrder = []string{tagSummaryData} 132 133 if c.ShowMilestones { 134 c.ReportOrder = append(c.ReportOrder, tagMilestonesData) 135 } 136 if c.ShowIssues { 137 c.ReportOrder = append(c.ReportOrder, tagIssuesData) 138 } 139 if c.ShowCommits { 140 c.ReportOrder = append(c.ReportOrder, tagCommitsData) 141 } 142 143 c.BranchLines = 100 // overide any existing value with maximum allowable in one call 144 145 sort.Sort(branchesToSort(c.Lists)) // get the configured branches in a sensible order for display 146 147 lastItem := 0 148 for i := range c.Lists { 149 c.Lists[i].Comma = true 150 if c.Lists[i].Included { 151 lastItem = i 152 } 153 } 154 if lastItem < len(c.Lists) { 155 c.Lists[lastItem].Comma = false 156 } 157 158 if c.UserNames == nil { 159 c.UserNames = make(map[string]string) 160 } 161 162 } 163 164 type githubCallbackT struct { 165 AccessToken string `json:"access_token"` 166 } 167 168 func repoName(branchName string) string { 169 bits := strings.Split(branchName, "/") 170 if len(bits) != 2 { 171 return branchName + "?repo" 172 } 173 pieces := strings.Split(bits[1], ":") 174 if len(pieces) == 0 { 175 return branchName + "?repo:?branch" 176 } 177 return pieces[0] 178 } 179 180 func getUserName(client *gogithub.Client, config *githubConfig, login string) (fullName string) { 181 an := login 182 if content, found := config.UserNames[login]; found { 183 if len(content) > 0 { 184 an = content 185 } 186 } else { 187 usr, _, err := client.Users.Get(login) 188 if err == nil { 189 if usr.Name != nil { 190 if len(*usr.Name) > 0 { 191 config.UserNames[login] = *usr.Name 192 an = *usr.Name 193 } 194 } 195 } else { 196 config.UserNames[login] = login // don't look again for a missing name 197 } 198 } 199 return an 200 }