resolveChatDiffReference builds the diff reference from the cached status stored in the database. The git branch and remote origin are populated by the workspace agent during git operations (via the gitaskpass flow), so no SSH into the workspace is needed here. nolint:revive // Boolean indicates wh
( ctx context.Context, chat database.Chat, found bool, status database.ChatDiffStatus, )
| 4098 | // |
| 4099 | //nolint:revive // Boolean indicates whether diff status was found. |
| 4100 | func (api *API) resolveChatDiffReference( |
| 4101 | ctx context.Context, |
| 4102 | chat database.Chat, |
| 4103 | found bool, |
| 4104 | status database.ChatDiffStatus, |
| 4105 | ) (chatDiffReference, error) { |
| 4106 | reference := chatDiffReference{} |
| 4107 | if !found { |
| 4108 | return reference, nil |
| 4109 | } |
| 4110 | |
| 4111 | reference.PullRequestURL = strings.TrimSpace(status.Url.String) |
| 4112 | |
| 4113 | // Build the repository ref from the stored git branch/origin |
| 4114 | // that the agent reported. |
| 4115 | reference.RepositoryRef = api.buildChatRepositoryRefFromStatus(ctx, status) |
| 4116 | |
| 4117 | // If we have a repo ref with a branch, try to resolve the |
| 4118 | // current open PR. This picks up new PRs after the previous |
| 4119 | // one was closed. |
| 4120 | if reference.RepositoryRef != nil && reference.RepositoryRef.Owner != "" { |
| 4121 | gp := api.resolveGitProvider(ctx, reference.RepositoryRef.RemoteOrigin) |
| 4122 | if gp != nil { |
| 4123 | token, err := api.resolveChatGitAccessToken(ctx, chat.OwnerID, reference.RepositoryRef.RemoteOrigin) |
| 4124 | if token == nil || errors.Is(err, gitsync.ErrNoTokenAvailable) { |
| 4125 | // No token available yet. |
| 4126 | return reference, nil |
| 4127 | } else if err != nil { |
| 4128 | return chatDiffReference{}, xerrors.Errorf("resolve git access token: %w", err) |
| 4129 | } |
| 4130 | prRef, lookupErr := gp.ResolveBranchPullRequest(ctx, *token, gitprovider.BranchRef{ |
| 4131 | Owner: reference.RepositoryRef.Owner, |
| 4132 | Repo: reference.RepositoryRef.Repo, |
| 4133 | Branch: reference.RepositoryRef.Branch, |
| 4134 | }) |
| 4135 | if lookupErr != nil { |
| 4136 | api.Logger.Debug(ctx, "failed to resolve pull request from repository reference", |
| 4137 | slog.F("chat_id", chat.ID), |
| 4138 | slog.F("provider", reference.RepositoryRef.Provider), |
| 4139 | slog.F("remote_origin", reference.RepositoryRef.RemoteOrigin), |
| 4140 | slog.F("branch", reference.RepositoryRef.Branch), |
| 4141 | slog.Error(lookupErr), |
| 4142 | ) |
| 4143 | } else if prRef != nil { |
| 4144 | reference.PullRequestURL = gp.BuildPullRequestURL(*prRef) |
| 4145 | } |
| 4146 | reference.PullRequestURL = gp.NormalizePullRequestURL(reference.PullRequestURL) |
| 4147 | } |
| 4148 | } |
| 4149 | |
| 4150 | // If we have a PR URL but no repo ref (e.g. the agent hasn't |
| 4151 | // reported branch/origin yet), derive a partial ref from the |
| 4152 | // PR URL so the caller can still show provider/owner/repo. |
| 4153 | if reference.RepositoryRef == nil && reference.PullRequestURL != "" { |
| 4154 | for _, extAuth := range api.ExternalAuthConfigs { |
| 4155 | gp, err := extAuth.Git(api.HTTPClient) |
| 4156 | if err != nil || gp == nil { |
| 4157 | continue |
no test coverage detected