|
1 | 1 | package github |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "time" |
| 5 | + |
4 | 6 | "github.com/google/go-github/v82/github" |
5 | 7 | ) |
6 | 8 |
|
@@ -134,8 +136,241 @@ type MinimalProject struct { |
134 | 136 | OwnerType string `json:"owner_type,omitempty"` |
135 | 137 | } |
136 | 138 |
|
| 139 | +// MinimalReactions is the trimmed output type for reaction summaries, dropping the API URL. |
| 140 | +type MinimalReactions struct { |
| 141 | + TotalCount int `json:"total_count"` |
| 142 | + PlusOne int `json:"+1"` |
| 143 | + MinusOne int `json:"-1"` |
| 144 | + Laugh int `json:"laugh"` |
| 145 | + Confused int `json:"confused"` |
| 146 | + Heart int `json:"heart"` |
| 147 | + Hooray int `json:"hooray"` |
| 148 | + Rocket int `json:"rocket"` |
| 149 | + Eyes int `json:"eyes"` |
| 150 | +} |
| 151 | + |
| 152 | +// MinimalIssue is the trimmed output type for issue objects to reduce verbosity. |
| 153 | +type MinimalIssue struct { |
| 154 | + Number int `json:"number"` |
| 155 | + Title string `json:"title"` |
| 156 | + Body string `json:"body,omitempty"` |
| 157 | + State string `json:"state"` |
| 158 | + StateReason string `json:"state_reason,omitempty"` |
| 159 | + Draft bool `json:"draft,omitempty"` |
| 160 | + Locked bool `json:"locked,omitempty"` |
| 161 | + HTMLURL string `json:"html_url"` |
| 162 | + User *MinimalUser `json:"user,omitempty"` |
| 163 | + AuthorAssociation string `json:"author_association,omitempty"` |
| 164 | + Labels []string `json:"labels,omitempty"` |
| 165 | + Assignees []string `json:"assignees,omitempty"` |
| 166 | + Milestone string `json:"milestone,omitempty"` |
| 167 | + Comments int `json:"comments,omitempty"` |
| 168 | + Reactions *MinimalReactions `json:"reactions,omitempty"` |
| 169 | + CreatedAt string `json:"created_at,omitempty"` |
| 170 | + UpdatedAt string `json:"updated_at,omitempty"` |
| 171 | + ClosedAt string `json:"closed_at,omitempty"` |
| 172 | + ClosedBy string `json:"closed_by,omitempty"` |
| 173 | + IssueType string `json:"issue_type,omitempty"` |
| 174 | +} |
| 175 | + |
| 176 | +// MinimalPullRequest is the trimmed output type for pull request objects to reduce verbosity. |
| 177 | +type MinimalPullRequest struct { |
| 178 | + Number int `json:"number"` |
| 179 | + Title string `json:"title"` |
| 180 | + Body string `json:"body,omitempty"` |
| 181 | + State string `json:"state"` |
| 182 | + Draft bool `json:"draft"` |
| 183 | + Merged bool `json:"merged"` |
| 184 | + MergeableState string `json:"mergeable_state,omitempty"` |
| 185 | + HTMLURL string `json:"html_url"` |
| 186 | + User *MinimalUser `json:"user,omitempty"` |
| 187 | + Labels []string `json:"labels,omitempty"` |
| 188 | + Assignees []string `json:"assignees,omitempty"` |
| 189 | + RequestedReviewers []string `json:"requested_reviewers,omitempty"` |
| 190 | + MergedBy string `json:"merged_by,omitempty"` |
| 191 | + Head *MinimalPRBranch `json:"head,omitempty"` |
| 192 | + Base *MinimalPRBranch `json:"base,omitempty"` |
| 193 | + Additions int `json:"additions,omitempty"` |
| 194 | + Deletions int `json:"deletions,omitempty"` |
| 195 | + ChangedFiles int `json:"changed_files,omitempty"` |
| 196 | + Commits int `json:"commits,omitempty"` |
| 197 | + Comments int `json:"comments,omitempty"` |
| 198 | + CreatedAt string `json:"created_at,omitempty"` |
| 199 | + UpdatedAt string `json:"updated_at,omitempty"` |
| 200 | + ClosedAt string `json:"closed_at,omitempty"` |
| 201 | + MergedAt string `json:"merged_at,omitempty"` |
| 202 | + Milestone string `json:"milestone,omitempty"` |
| 203 | +} |
| 204 | + |
| 205 | +// MinimalPRBranch is the trimmed output type for pull request branch references. |
| 206 | +type MinimalPRBranch struct { |
| 207 | + Ref string `json:"ref"` |
| 208 | + SHA string `json:"sha"` |
| 209 | + Repo *MinimalPRBranchRepo `json:"repo,omitempty"` |
| 210 | +} |
| 211 | + |
| 212 | +// MinimalPRBranchRepo is the trimmed repo info nested inside a PR branch. |
| 213 | +type MinimalPRBranchRepo struct { |
| 214 | + FullName string `json:"full_name"` |
| 215 | + Description string `json:"description,omitempty"` |
| 216 | +} |
| 217 | + |
137 | 218 | // Helper functions |
138 | 219 |
|
| 220 | +func convertToMinimalIssue(issue *github.Issue) MinimalIssue { |
| 221 | + m := MinimalIssue{ |
| 222 | + Number: issue.GetNumber(), |
| 223 | + Title: issue.GetTitle(), |
| 224 | + Body: issue.GetBody(), |
| 225 | + State: issue.GetState(), |
| 226 | + StateReason: issue.GetStateReason(), |
| 227 | + Draft: issue.GetDraft(), |
| 228 | + Locked: issue.GetLocked(), |
| 229 | + HTMLURL: issue.GetHTMLURL(), |
| 230 | + User: convertToMinimalUser(issue.GetUser()), |
| 231 | + AuthorAssociation: issue.GetAuthorAssociation(), |
| 232 | + Comments: issue.GetComments(), |
| 233 | + } |
| 234 | + |
| 235 | + if issue.CreatedAt != nil { |
| 236 | + m.CreatedAt = issue.CreatedAt.Format(time.RFC3339) |
| 237 | + } |
| 238 | + if issue.UpdatedAt != nil { |
| 239 | + m.UpdatedAt = issue.UpdatedAt.Format(time.RFC3339) |
| 240 | + } |
| 241 | + if issue.ClosedAt != nil { |
| 242 | + m.ClosedAt = issue.ClosedAt.Format(time.RFC3339) |
| 243 | + } |
| 244 | + |
| 245 | + for _, label := range issue.Labels { |
| 246 | + if label != nil { |
| 247 | + m.Labels = append(m.Labels, label.GetName()) |
| 248 | + } |
| 249 | + } |
| 250 | + |
| 251 | + for _, assignee := range issue.Assignees { |
| 252 | + if assignee != nil { |
| 253 | + m.Assignees = append(m.Assignees, assignee.GetLogin()) |
| 254 | + } |
| 255 | + } |
| 256 | + |
| 257 | + if closedBy := issue.GetClosedBy(); closedBy != nil { |
| 258 | + m.ClosedBy = closedBy.GetLogin() |
| 259 | + } |
| 260 | + |
| 261 | + if milestone := issue.GetMilestone(); milestone != nil { |
| 262 | + m.Milestone = milestone.GetTitle() |
| 263 | + } |
| 264 | + |
| 265 | + if issueType := issue.GetType(); issueType != nil { |
| 266 | + m.IssueType = issueType.GetName() |
| 267 | + } |
| 268 | + |
| 269 | + if r := issue.Reactions; r != nil { |
| 270 | + m.Reactions = &MinimalReactions{ |
| 271 | + TotalCount: r.GetTotalCount(), |
| 272 | + PlusOne: r.GetPlusOne(), |
| 273 | + MinusOne: r.GetMinusOne(), |
| 274 | + Laugh: r.GetLaugh(), |
| 275 | + Confused: r.GetConfused(), |
| 276 | + Heart: r.GetHeart(), |
| 277 | + Hooray: r.GetHooray(), |
| 278 | + Rocket: r.GetRocket(), |
| 279 | + Eyes: r.GetEyes(), |
| 280 | + } |
| 281 | + } |
| 282 | + |
| 283 | + return m |
| 284 | +} |
| 285 | + |
| 286 | +func convertToMinimalPullRequest(pr *github.PullRequest) MinimalPullRequest { |
| 287 | + m := MinimalPullRequest{ |
| 288 | + Number: pr.GetNumber(), |
| 289 | + Title: pr.GetTitle(), |
| 290 | + Body: pr.GetBody(), |
| 291 | + State: pr.GetState(), |
| 292 | + Draft: pr.GetDraft(), |
| 293 | + Merged: pr.GetMerged(), |
| 294 | + MergeableState: pr.GetMergeableState(), |
| 295 | + HTMLURL: pr.GetHTMLURL(), |
| 296 | + User: convertToMinimalUser(pr.GetUser()), |
| 297 | + Additions: pr.GetAdditions(), |
| 298 | + Deletions: pr.GetDeletions(), |
| 299 | + ChangedFiles: pr.GetChangedFiles(), |
| 300 | + Commits: pr.GetCommits(), |
| 301 | + Comments: pr.GetComments(), |
| 302 | + } |
| 303 | + |
| 304 | + if pr.CreatedAt != nil { |
| 305 | + m.CreatedAt = pr.CreatedAt.Format(time.RFC3339) |
| 306 | + } |
| 307 | + if pr.UpdatedAt != nil { |
| 308 | + m.UpdatedAt = pr.UpdatedAt.Format(time.RFC3339) |
| 309 | + } |
| 310 | + if pr.ClosedAt != nil { |
| 311 | + m.ClosedAt = pr.ClosedAt.Format(time.RFC3339) |
| 312 | + } |
| 313 | + if pr.MergedAt != nil { |
| 314 | + m.MergedAt = pr.MergedAt.Format(time.RFC3339) |
| 315 | + } |
| 316 | + |
| 317 | + for _, label := range pr.Labels { |
| 318 | + if label != nil { |
| 319 | + m.Labels = append(m.Labels, label.GetName()) |
| 320 | + } |
| 321 | + } |
| 322 | + |
| 323 | + for _, assignee := range pr.Assignees { |
| 324 | + if assignee != nil { |
| 325 | + m.Assignees = append(m.Assignees, assignee.GetLogin()) |
| 326 | + } |
| 327 | + } |
| 328 | + |
| 329 | + for _, reviewer := range pr.RequestedReviewers { |
| 330 | + if reviewer != nil { |
| 331 | + m.RequestedReviewers = append(m.RequestedReviewers, reviewer.GetLogin()) |
| 332 | + } |
| 333 | + } |
| 334 | + |
| 335 | + if mergedBy := pr.GetMergedBy(); mergedBy != nil { |
| 336 | + m.MergedBy = mergedBy.GetLogin() |
| 337 | + } |
| 338 | + |
| 339 | + if head := pr.Head; head != nil { |
| 340 | + m.Head = convertToMinimalPRBranch(head) |
| 341 | + } |
| 342 | + |
| 343 | + if base := pr.Base; base != nil { |
| 344 | + m.Base = convertToMinimalPRBranch(base) |
| 345 | + } |
| 346 | + |
| 347 | + if milestone := pr.GetMilestone(); milestone != nil { |
| 348 | + m.Milestone = milestone.GetTitle() |
| 349 | + } |
| 350 | + |
| 351 | + return m |
| 352 | +} |
| 353 | + |
| 354 | +func convertToMinimalPRBranch(branch *github.PullRequestBranch) *MinimalPRBranch { |
| 355 | + if branch == nil { |
| 356 | + return nil |
| 357 | + } |
| 358 | + |
| 359 | + b := &MinimalPRBranch{ |
| 360 | + Ref: branch.GetRef(), |
| 361 | + SHA: branch.GetSHA(), |
| 362 | + } |
| 363 | + |
| 364 | + if repo := branch.GetRepo(); repo != nil { |
| 365 | + b.Repo = &MinimalPRBranchRepo{ |
| 366 | + FullName: repo.GetFullName(), |
| 367 | + Description: repo.GetDescription(), |
| 368 | + } |
| 369 | + } |
| 370 | + |
| 371 | + return b |
| 372 | +} |
| 373 | + |
139 | 374 | func convertToMinimalProject(fullProject *github.ProjectV2) *MinimalProject { |
140 | 375 | if fullProject == nil { |
141 | 376 | return nil |
|
0 commit comments