feat: [CODE-4834]: support for upstream in divergences api (#4791)
* ee5bdf add fork ID to repo core * fbf480 support for upstream in divergences api
This commit is contained in:
parent
828f426ee5
commit
4eaafb5460
|
|
@ -395,16 +395,11 @@ func (c *Controller) fetchUpstreamObjects(
|
|||
repoForkCore *types.RepositoryCore,
|
||||
getSHA func(params git.ReadParams) (sha.SHA, error),
|
||||
) (sha.SHA, *types.RepositoryCore, error) {
|
||||
repoFork, err := c.repoStore.Find(ctx, repoForkCore.ID)
|
||||
if err != nil {
|
||||
return sha.None, nil, fmt.Errorf("failed to find fork repo: %w", err)
|
||||
}
|
||||
|
||||
if repoFork.ForkID == 0 {
|
||||
if repoForkCore.ForkID == 0 {
|
||||
return sha.None, nil, errors.InvalidArgument("Repository is not a fork.")
|
||||
}
|
||||
|
||||
repoUpstreamCore, err := c.repoFinder.FindByID(ctx, repoFork.ForkID)
|
||||
repoUpstreamCore, err := c.repoFinder.FindByID(ctx, repoForkCore.ForkID)
|
||||
if err != nil {
|
||||
return sha.None, nil, fmt.Errorf("failed to find upstream repo: %w", err)
|
||||
}
|
||||
|
|
@ -445,6 +440,57 @@ func (c *Controller) fetchUpstreamObjects(
|
|||
return upstreamSHA, repoUpstreamCore, nil
|
||||
}
|
||||
|
||||
func (c *Controller) fetchCommitDivergenceObjectsFromUpstream(
|
||||
ctx context.Context,
|
||||
session *auth.Session,
|
||||
repo *types.RepositoryCore,
|
||||
div *git.CommitDivergenceRequest,
|
||||
) error {
|
||||
dot, err := makeDotRange(div.To, div.From, true)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to make dot range: %w", err)
|
||||
}
|
||||
|
||||
err = c.fetchDotRangeObjectsFromUpstream(ctx, session, repo, &dot)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch dot range objects: %w", err)
|
||||
}
|
||||
|
||||
div.To = dot.BaseRef
|
||||
div.From = dot.HeadRef
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Controller) fetchDotRangeObjectsFromUpstream(
|
||||
ctx context.Context,
|
||||
session *auth.Session,
|
||||
repoForkCore *types.RepositoryCore,
|
||||
dotRange *DotRange,
|
||||
) error {
|
||||
if dotRange.BaseUpstream {
|
||||
refSHA, _, err := c.fetchUpstreamRevision(ctx, session, repoForkCore, dotRange.BaseRef)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch upstream objects: %w", err)
|
||||
}
|
||||
|
||||
dotRange.BaseUpstream = false
|
||||
dotRange.BaseRef = refSHA.String()
|
||||
}
|
||||
|
||||
if dotRange.HeadUpstream {
|
||||
refSHA, _, err := c.fetchUpstreamRevision(ctx, session, repoForkCore, dotRange.HeadRef)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch upstream objects: %w", err)
|
||||
}
|
||||
|
||||
dotRange.HeadUpstream = false
|
||||
dotRange.HeadRef = refSHA.String()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
const dotRangeUpstreamMarker = "upstream:"
|
||||
|
||||
type DotRange struct {
|
||||
|
|
|
|||
|
|
@ -159,32 +159,3 @@ func (c *Controller) Diff(
|
|||
|
||||
return reader, nil
|
||||
}
|
||||
|
||||
func (c *Controller) fetchDotRangeObjectsFromUpstream(
|
||||
ctx context.Context,
|
||||
session *auth.Session,
|
||||
repoForkCore *types.RepositoryCore,
|
||||
dotRange *DotRange,
|
||||
) error {
|
||||
if dotRange.BaseUpstream {
|
||||
refSHA, _, err := c.fetchUpstreamRevision(ctx, session, repoForkCore, dotRange.BaseRef)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch upstream objects: %w", err)
|
||||
}
|
||||
|
||||
dotRange.BaseUpstream = false
|
||||
dotRange.BaseRef = refSHA.String()
|
||||
}
|
||||
|
||||
if dotRange.HeadUpstream {
|
||||
refSHA, _, err := c.fetchUpstreamRevision(ctx, session, repoForkCore, dotRange.HeadRef)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to fetch upstream objects: %w", err)
|
||||
}
|
||||
|
||||
dotRange.HeadUpstream = false
|
||||
dotRange.HeadRef = refSHA.String()
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@ package repo
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/harness/gitness/app/api/request"
|
||||
"github.com/harness/gitness/app/api/usererror"
|
||||
|
|
@ -75,6 +76,11 @@ func (c *Controller) GetCommitDivergences(ctx context.Context,
|
|||
if len(options.Requests[i].To) == 0 {
|
||||
options.Requests[i].To = repo.DefaultBranch
|
||||
}
|
||||
|
||||
err = c.fetchCommitDivergenceObjectsFromUpstream(ctx, session, repo, &options.Requests[i])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to fetch object from upstream: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: We should cache the responses as times can reach multiple seconds
|
||||
|
|
|
|||
|
|
@ -98,6 +98,8 @@ func (c *Controller) PurgeNoAuth(
|
|||
return fmt.Errorf("failed to delete repo from db: %w", err)
|
||||
}
|
||||
|
||||
c.repoFinder.MarkChanged(ctx, repo.Core())
|
||||
|
||||
if err := c.DeleteGitRepository(ctx, session, repo.GitUID); err != nil {
|
||||
log.Ctx(ctx).Err(err).Msg("failed to remove git repository")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@ type RepositoryCore struct {
|
|||
Path string `json:"path" yaml:"path"`
|
||||
GitUID string `json:"-" yaml:"-"`
|
||||
DefaultBranch string `json:"default_branch" yaml:"default_branch"`
|
||||
ForkID int64 `json:"fork_id" yaml:"fork_id"`
|
||||
State enum.RepoState `json:"-" yaml:"-"`
|
||||
Type enum.RepoType `json:"type,omitempty" yaml:"type,omitempty"`
|
||||
}
|
||||
|
|
@ -88,6 +89,7 @@ func (r *Repository) Core() *RepositoryCore {
|
|||
Identifier: r.Identifier,
|
||||
Path: r.Path,
|
||||
GitUID: r.GitUID,
|
||||
ForkID: r.ForkID,
|
||||
DefaultBranch: r.DefaultBranch,
|
||||
State: r.State,
|
||||
Type: r.Type,
|
||||
|
|
|
|||
Loading…
Reference in New Issue