refreshOne processes a single row using an already-resolved provider and token.
( ctx context.Context, provider gitprovider.Provider, token string, row database.ChatDiffStatus, )
| 257 | // refreshOne processes a single row using an already-resolved |
| 258 | // provider and token. |
| 259 | func (r *Refresher) refreshOne( |
| 260 | ctx context.Context, |
| 261 | provider gitprovider.Provider, |
| 262 | token string, |
| 263 | row database.ChatDiffStatus, |
| 264 | ) (*database.UpsertChatDiffStatusParams, error) { |
| 265 | var ref gitprovider.PRRef |
| 266 | var prURL string |
| 267 | |
| 268 | if row.Url.Valid && row.Url.String != "" { |
| 269 | // Row already has a PR URL — parse it directly. |
| 270 | parsed, ok := provider.ParsePullRequestURL(row.Url.String) |
| 271 | if !ok { |
| 272 | return nil, xerrors.Errorf("parse pull request URL %q", row.Url.String) |
| 273 | } |
| 274 | ref = parsed |
| 275 | prURL = row.Url.String |
| 276 | } else { |
| 277 | // No PR URL — resolve owner/repo from the remote origin, |
| 278 | // then look up the open PR for this branch. |
| 279 | owner, repo, _, ok := provider.ParseRepositoryOrigin(row.GitRemoteOrigin) |
| 280 | if !ok { |
| 281 | return nil, xerrors.Errorf("parse repository origin %q", row.GitRemoteOrigin) |
| 282 | } |
| 283 | |
| 284 | resolved, err := provider.ResolveBranchPullRequest(ctx, token, gitprovider.BranchRef{ |
| 285 | Owner: owner, |
| 286 | Repo: repo, |
| 287 | Branch: row.GitBranch, |
| 288 | }) |
| 289 | if err != nil { |
| 290 | return nil, xerrors.Errorf("resolve branch pull request: %w", err) |
| 291 | } |
| 292 | if resolved == nil { |
| 293 | // No PR exists yet for this branch. |
| 294 | return nil, nil |
| 295 | } |
| 296 | ref = *resolved |
| 297 | prURL = provider.BuildPullRequestURL(ref) |
| 298 | } |
| 299 | |
| 300 | status, err := provider.FetchPullRequestStatus(ctx, token, ref) |
| 301 | if err != nil { |
| 302 | return nil, xerrors.Errorf("fetch pull request status: %w", err) |
| 303 | } |
| 304 | |
| 305 | now := r.clock.Now().UTC() |
| 306 | params := &database.UpsertChatDiffStatusParams{ |
| 307 | ChatID: row.ChatID, |
| 308 | Url: sql.NullString{String: prURL, Valid: prURL != ""}, |
| 309 | PullRequestState: sql.NullString{ |
| 310 | String: string(status.State), |
| 311 | Valid: status.State != "", |
| 312 | }, |
| 313 | PullRequestTitle: status.Title, |
| 314 | PullRequestDraft: status.Draft, |
| 315 | ChangesRequested: status.ChangesRequested, |
| 316 | Additions: status.DiffStats.Additions, |
no test coverage detected