FileViewAdd marks a file as viewed. NOTE: We take the commit SHA from the user to ensure we mark as viewed only what the user actually sees. The downside is that the caller could provide a SHA that never was part of the PR in the first place. We can't block against that with our current data, as the
( ctx context.Context, session *auth.Session, repoRef string, prNum int64, in *FileViewAddInput, )
| 52 | // |
| 53 | //nolint:gocognit // refactor if needed. |
| 54 | func (c *Controller) FileViewAdd( |
| 55 | ctx context.Context, |
| 56 | session *auth.Session, |
| 57 | repoRef string, |
| 58 | prNum int64, |
| 59 | in *FileViewAddInput, |
| 60 | ) (*types.PullReqFileView, error) { |
| 61 | if err := in.Validate(); err != nil { |
| 62 | return nil, err |
| 63 | } |
| 64 | |
| 65 | repo, err := c.getRepoCheckAccess(ctx, session, repoRef, enum.PermissionRepoReview) |
| 66 | if err != nil { |
| 67 | return nil, fmt.Errorf("failed to acquire access to repo: %w", err) |
| 68 | } |
| 69 | |
| 70 | pr, err := c.pullreqStore.FindByNumber(ctx, repo.ID, prNum) |
| 71 | if err != nil { |
| 72 | return nil, fmt.Errorf("failed to find pull request by number: %w", err) |
| 73 | } |
| 74 | |
| 75 | // retrieve file from both provided SHA and mergeBaseSHA to validate user input |
| 76 | |
| 77 | inNode, err := c.git.GetTreeNode(ctx, &git.GetTreeNodeParams{ |
| 78 | ReadParams: git.CreateReadParams(repo), |
| 79 | GitREF: in.CommitSHA, |
| 80 | Path: in.Path, |
| 81 | IncludeLatestCommit: false, |
| 82 | }) |
| 83 | if err != nil && !errors.IsNotFound(err) { |
| 84 | return nil, fmt.Errorf( |
| 85 | "failed to get tree node '%s' for provided sha '%s': %w", |
| 86 | in.Path, |
| 87 | in.CommitSHA, |
| 88 | err, |
| 89 | ) |
| 90 | } |
| 91 | |
| 92 | // ensure provided path actually points to a blob or commit (submodule) |
| 93 | if inNode != nil && |
| 94 | inNode.Node.Type != git.TreeNodeTypeBlob && |
| 95 | inNode.Node.Type != git.TreeNodeTypeCommit { |
| 96 | return nil, usererror.BadRequestf("Provided path '%s' doesn't point to a file.", in.Path) |
| 97 | } |
| 98 | |
| 99 | mergeBaseNode, err := c.git.GetTreeNode(ctx, &git.GetTreeNodeParams{ |
| 100 | ReadParams: git.CreateReadParams(repo), |
| 101 | GitREF: pr.MergeBaseSHA, |
| 102 | Path: in.Path, |
| 103 | IncludeLatestCommit: false, |
| 104 | }) |
| 105 | if err != nil && !errors.IsNotFound(err) { |
| 106 | return nil, fmt.Errorf( |
| 107 | "failed to get tree node '%s' for MergeBaseSHA '%s': %w", |
| 108 | in.Path, |
| 109 | pr.MergeBaseSHA, |
| 110 | err, |
| 111 | ) |
no test coverage detected