Skip to content

a8n: Display diff for changesets that exist on the codehost

Created by: mrnugget

This is part of https://github.com/sourcegraph/sourcegraph/issues/6085

The proposed GraphQL schema for displaying diffs for changesets that already exist on codehosts will most likely involve returning a RepositoryComparison for each changeset. See https://github.com/sourcegraph/sourcegraph/pull/5850 and https://github.com/sourcegraph/sourcegraph/pull/6135#issue-330763174


Backend Implementation Details

In order to implement that diff: RepositoryComparison! on the a8n.changesetResolver all that's required is — theoretically — this:

func (r *changesetResolver) Diff(ctx context.Context) (*graphqlbackend.RepositoryComparisonResolver, error) {
	repo, err := r.repoResolver(ctx)
	if err != nil {
		return nil, err
	}

	base := "master"
	head := "master"

	return graphqlbackend.NewRepositoryComparison(ctx, repo, &graphqlbackend.RepositoryComparisonInput{
		Base: &base,
		Head: &head,
	})
}

But: how do we get to base and head? How do we get the git refs?

First answer: we can ask the codehosts.

GitHub

GitHub allows us to query baseRef and headRef for each pull request. But headRef might be null. That's the case when the pull request has been merged.

There's the headRefOid field, which allows us to get to the Git Object ID even if the PR has been merged and/or the commit has been deleted.

Bitbucket SErver

Bitbucket Server pull requests have FromRef and ToRef fields. The API doesn't mention whether the FromRef is nullable or not.

Questions

Do we still want to display a diff when a changeset has been merged?

Because the next question right after "where do we get the headRef if it's been merged?" is: if the PR has been merged, are we sure that still have the commit in our gitserver copy of the repository?

I personally think that the value doesn't lie in displaying merged diff, in which case we still link to the PR on the codehost where the user can presumably still access the (cached) diff.