|
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,138 @@ type MinimalProject struct { |
134 | 136 | OwnerType string `json:"owner_type,omitempty"` |
135 | 137 | } |
136 | 138 |
|
| 139 | +// MinimalPullRequest is the trimmed output type for pull request objects to reduce verbosity. |
| 140 | +type MinimalPullRequest struct { |
| 141 | + Number int `json:"number"` |
| 142 | + Title string `json:"title"` |
| 143 | + Body string `json:"body,omitempty"` |
| 144 | + State string `json:"state"` |
| 145 | + Draft bool `json:"draft"` |
| 146 | + Merged bool `json:"merged"` |
| 147 | + MergeableState string `json:"mergeable_state,omitempty"` |
| 148 | + HTMLURL string `json:"html_url"` |
| 149 | + User *MinimalUser `json:"user,omitempty"` |
| 150 | + Labels []string `json:"labels,omitempty"` |
| 151 | + Assignees []string `json:"assignees,omitempty"` |
| 152 | + RequestedReviewers []string `json:"requested_reviewers,omitempty"` |
| 153 | + MergedBy string `json:"merged_by,omitempty"` |
| 154 | + Head *MinimalPRBranch `json:"head,omitempty"` |
| 155 | + Base *MinimalPRBranch `json:"base,omitempty"` |
| 156 | + Additions int `json:"additions,omitempty"` |
| 157 | + Deletions int `json:"deletions,omitempty"` |
| 158 | + ChangedFiles int `json:"changed_files,omitempty"` |
| 159 | + Commits int `json:"commits,omitempty"` |
| 160 | + Comments int `json:"comments,omitempty"` |
| 161 | + CreatedAt string `json:"created_at,omitempty"` |
| 162 | + UpdatedAt string `json:"updated_at,omitempty"` |
| 163 | + ClosedAt string `json:"closed_at,omitempty"` |
| 164 | + MergedAt string `json:"merged_at,omitempty"` |
| 165 | + Milestone string `json:"milestone,omitempty"` |
| 166 | +} |
| 167 | + |
| 168 | +// MinimalPRBranch is the trimmed output type for pull request branch references. |
| 169 | +type MinimalPRBranch struct { |
| 170 | + Ref string `json:"ref"` |
| 171 | + SHA string `json:"sha"` |
| 172 | + Repo *MinimalPRBranchRepo `json:"repo,omitempty"` |
| 173 | +} |
| 174 | + |
| 175 | +// MinimalPRBranchRepo is the trimmed repo info nested inside a PR branch. |
| 176 | +type MinimalPRBranchRepo struct { |
| 177 | + FullName string `json:"full_name"` |
| 178 | + Description string `json:"description,omitempty"` |
| 179 | +} |
| 180 | + |
137 | 181 | // Helper functions |
138 | 182 |
|
| 183 | +func convertToMinimalPullRequest(pr *github.PullRequest) MinimalPullRequest { |
| 184 | + m := MinimalPullRequest{ |
| 185 | + Number: pr.GetNumber(), |
| 186 | + Title: pr.GetTitle(), |
| 187 | + Body: pr.GetBody(), |
| 188 | + State: pr.GetState(), |
| 189 | + Draft: pr.GetDraft(), |
| 190 | + Merged: pr.GetMerged(), |
| 191 | + MergeableState: pr.GetMergeableState(), |
| 192 | + HTMLURL: pr.GetHTMLURL(), |
| 193 | + User: convertToMinimalUser(pr.GetUser()), |
| 194 | + Additions: pr.GetAdditions(), |
| 195 | + Deletions: pr.GetDeletions(), |
| 196 | + ChangedFiles: pr.GetChangedFiles(), |
| 197 | + Commits: pr.GetCommits(), |
| 198 | + Comments: pr.GetComments(), |
| 199 | + } |
| 200 | + |
| 201 | + if pr.CreatedAt != nil { |
| 202 | + m.CreatedAt = pr.CreatedAt.Format(time.RFC3339) |
| 203 | + } |
| 204 | + if pr.UpdatedAt != nil { |
| 205 | + m.UpdatedAt = pr.UpdatedAt.Format(time.RFC3339) |
| 206 | + } |
| 207 | + if pr.ClosedAt != nil { |
| 208 | + m.ClosedAt = pr.ClosedAt.Format(time.RFC3339) |
| 209 | + } |
| 210 | + if pr.MergedAt != nil { |
| 211 | + m.MergedAt = pr.MergedAt.Format(time.RFC3339) |
| 212 | + } |
| 213 | + |
| 214 | + for _, label := range pr.Labels { |
| 215 | + if label != nil { |
| 216 | + m.Labels = append(m.Labels, label.GetName()) |
| 217 | + } |
| 218 | + } |
| 219 | + |
| 220 | + for _, assignee := range pr.Assignees { |
| 221 | + if assignee != nil { |
| 222 | + m.Assignees = append(m.Assignees, assignee.GetLogin()) |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + for _, reviewer := range pr.RequestedReviewers { |
| 227 | + if reviewer != nil { |
| 228 | + m.RequestedReviewers = append(m.RequestedReviewers, reviewer.GetLogin()) |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + if mergedBy := pr.GetMergedBy(); mergedBy != nil { |
| 233 | + m.MergedBy = mergedBy.GetLogin() |
| 234 | + } |
| 235 | + |
| 236 | + if head := pr.Head; head != nil { |
| 237 | + m.Head = convertToMinimalPRBranch(head) |
| 238 | + } |
| 239 | + |
| 240 | + if base := pr.Base; base != nil { |
| 241 | + m.Base = convertToMinimalPRBranch(base) |
| 242 | + } |
| 243 | + |
| 244 | + if milestone := pr.GetMilestone(); milestone != nil { |
| 245 | + m.Milestone = milestone.GetTitle() |
| 246 | + } |
| 247 | + |
| 248 | + return m |
| 249 | +} |
| 250 | + |
| 251 | +func convertToMinimalPRBranch(branch *github.PullRequestBranch) *MinimalPRBranch { |
| 252 | + if branch == nil { |
| 253 | + return nil |
| 254 | + } |
| 255 | + |
| 256 | + b := &MinimalPRBranch{ |
| 257 | + Ref: branch.GetRef(), |
| 258 | + SHA: branch.GetSHA(), |
| 259 | + } |
| 260 | + |
| 261 | + if repo := branch.GetRepo(); repo != nil { |
| 262 | + b.Repo = &MinimalPRBranchRepo{ |
| 263 | + FullName: repo.GetFullName(), |
| 264 | + Description: repo.GetDescription(), |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + return b |
| 269 | +} |
| 270 | + |
139 | 271 | func convertToMinimalProject(fullProject *github.ProjectV2) *MinimalProject { |
140 | 272 | if fullProject == nil { |
141 | 273 | return nil |
|
0 commit comments