Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Commit

Permalink
Implement missing bits in CampaignSpec/ChangesetSpec resolvers (#12247)
Browse files Browse the repository at this point in the history
* Fix intendation of query in test

* Implement CampaignSpec.ViewerCanAdminister

* Add safety-check to ChangesetSpecDescription.Diff method

* Clean up repository permissions test

* Implement rest of ChangesetSpec and CampaignSpec resolvers

* Add options to CountChangesetSpecsOpts

* Implement changesetSpecConnectionResolver

* Reduce test data after reducing test cases

* Change type checks on ChangesetSpecDescription

* Query __typename in test to make sure we get the right response

* Use same repo query pattern in ChangesetResolver as in changesetSpecResolver

* Implement the after param for changesetSpecs connection

* Check for permissions in applyCampaign and moveCampaign

* Add a test for ChangesetSpec.Description.Diff

* Remove isHidden filter

* Do not yield ChangesetSpec if repo is (soft-)deleted
  • Loading branch information
mrnugget committed Jul 20, 2020
1 parent de2e8c6 commit 4f856d1
Show file tree
Hide file tree
Showing 21 changed files with 1,271 additions and 409 deletions.
4 changes: 2 additions & 2 deletions cmd/frontend/graphqlbackend/campaigns.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ type CampaignSpecResolver interface {

PreviewURL() (string, error)

ViewerCanAdminister() bool
ViewerCanAdminister(context.Context) (bool, error)
}

type CampaignDescriptionResolver interface {
Expand All @@ -114,7 +114,7 @@ type ChangesetSpecConnectionResolver interface {
type ChangesetSpecResolver interface {
ID() graphql.ID

Type() campaigns.ChangesetSpecType
Type() campaigns.ChangesetSpecDescriptionType

ExpiresAt() *DateTime

Expand Down
6 changes: 6 additions & 0 deletions enterprise/internal/campaigns/resolvers/apitest/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ type CampaignSpec struct {

ChangesetSpecs ChangesetSpecConnection

ViewerCanAdminister bool

CreatedAt graphqlbackend.DateTime
ExpiresAt *graphqlbackend.DateTime
}
Expand Down Expand Up @@ -235,6 +237,10 @@ type ChangesetSpecDescription struct {
Commits []GitCommitDescription

Published bool

Diff struct {
FileDiffs FileDiffs
}
}

type GitCommitDescription struct {
Expand Down
52 changes: 15 additions & 37 deletions enterprise/internal/campaigns/resolvers/campaign_spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package resolvers

import (
"context"
"strconv"

"github.com/graph-gophers/graphql-go"
"github.com/graph-gophers/graphql-go/relay"
"github.com/sourcegraph/sourcegraph/cmd/frontend/graphqlbackend"
"github.com/sourcegraph/sourcegraph/cmd/frontend/graphqlbackend/graphqlutil"
ee "github.com/sourcegraph/sourcegraph/enterprise/internal/campaigns"
"github.com/sourcegraph/sourcegraph/internal/campaigns"
"github.com/sourcegraph/sourcegraph/internal/errcode"
Expand Down Expand Up @@ -46,23 +46,22 @@ func (r *campaignSpecResolver) ParsedInput() (graphqlbackend.JSONValue, error) {
}

func (r *campaignSpecResolver) ChangesetSpecs(ctx context.Context, args *graphqlbackend.ChangesetSpecsConnectionArgs) (graphqlbackend.ChangesetSpecConnectionResolver, error) {
opts := ee.ListChangesetSpecsOpts{Limit: -1, CampaignSpecID: r.campaignSpec.ID}
cs, _, err := r.store.ListChangesetSpecs(ctx, opts)
if err != nil {
return nil, err
opts := ee.ListChangesetSpecsOpts{CampaignSpecID: r.campaignSpec.ID}
if args.First != nil {
opts.Limit = int(*args.First)
}

resolvers := make([]graphqlbackend.ChangesetSpecResolver, 0, len(cs))
for _, c := range cs {
resolvers = append(resolvers, &changesetSpecResolver{
store: r.store,
httpFactory: r.httpFactory,
changesetSpec: c,
})
if args.After != nil {
id, err := strconv.Atoi(*args.After)
if err != nil {
return nil, err
}
opts.Cursor = int64(id)
}

return &changesetSpecConnectionResolver{
resolvers: resolvers,
store: r.store,
httpFactory: r.httpFactory,
opts: opts,
}, nil
}

Expand Down Expand Up @@ -97,7 +96,6 @@ func (r *campaignSpecResolver) Namespace(ctx context.Context) (*graphqlbackend.N
}

func (r *campaignSpecResolver) PreviewURL() (string, error) {
// TODO: this needs to take the namespace into account
return "/campaigns/new?spec=" + string(r.ID()), nil
}

Expand All @@ -109,9 +107,8 @@ func (r *campaignSpecResolver) ExpiresAt() *graphqlbackend.DateTime {
return &graphqlbackend.DateTime{Time: r.campaignSpec.ExpiresAt()}
}

func (r *campaignSpecResolver) ViewerCanAdminister() bool {
// TODO: Implement.
return true
func (r *campaignSpecResolver) ViewerCanAdminister(ctx context.Context) (bool, error) {
return checkSiteAdminOrSameUser(ctx, r.campaignSpec.UserID)
}

type campaignDescriptionResolver struct {
Expand All @@ -125,22 +122,3 @@ func (r *campaignDescriptionResolver) Name() string {
func (r *campaignDescriptionResolver) Description() string {
return r.description
}

type changesetSpecConnectionResolver struct {
resolvers []graphqlbackend.ChangesetSpecResolver
}

func (r *changesetSpecConnectionResolver) TotalCount(ctx context.Context) (int32, error) {
// TODO: Implement.
return int32(len(r.resolvers)), nil
}

func (r *changesetSpecConnectionResolver) PageInfo(ctx context.Context) (*graphqlutil.PageInfo, error) {
// TODO: Implement.
return &graphqlutil.PageInfo{}, nil
}

func (r *changesetSpecConnectionResolver) Nodes(ctx context.Context) ([]graphqlbackend.ChangesetSpecResolver, error) {
// TODO: Implement.
return r.resolvers, nil
}
85 changes: 79 additions & 6 deletions enterprise/internal/campaigns/resolvers/campaign_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ package resolvers

import (
"context"
"database/sql"
"encoding/json"
"testing"
"time"

"github.com/google/go-cmp/cmp"
"github.com/sourcegraph/sourcegraph/cmd/frontend/backend"
"github.com/sourcegraph/sourcegraph/cmd/frontend/graphqlbackend"
"github.com/sourcegraph/sourcegraph/cmd/repo-updater/repos"
ee "github.com/sourcegraph/sourcegraph/enterprise/internal/campaigns"
"github.com/sourcegraph/sourcegraph/enterprise/internal/campaigns/resolvers/apitest"
ct "github.com/sourcegraph/sourcegraph/enterprise/internal/campaigns/testing"
Expand All @@ -27,6 +29,14 @@ func TestCampaignSpecResolver(t *testing.T) {

store := ee.NewStore(dbconn.Global)

reposStore := repos.NewDBStore(dbconn.Global, sql.TxOptions{})

repo := newGitHubTestRepo("github.com/sourcegraph/sourcegraph", 1)
if err := reposStore.UpsertRepos(ctx, repo); err != nil {
t.Fatal(err)
}
repoID := graphqlbackend.MarshalRepositoryID(repo.ID)

userID := insertTestUser(t, dbconn.Global, "campaign-spec-by-id", false)

spec, err := campaigns.NewCampaignSpecFromRaw(ct.TestRawCampaignSpec)
Expand All @@ -35,11 +45,22 @@ func TestCampaignSpecResolver(t *testing.T) {
}
spec.UserID = userID
spec.NamespaceUserID = userID

if err := store.CreateCampaignSpec(ctx, spec); err != nil {
t.Fatal(err)
}

changesetSpec, err := campaigns.NewChangesetSpecFromRaw(ct.NewRawChangesetSpecGitBranch(repoID, "deadb33f"))
if err != nil {
t.Fatal(err)
}
changesetSpec.CampaignSpecID = spec.ID
changesetSpec.UserID = userID
changesetSpec.RepoID = repo.ID

if err := store.CreateChangesetSpec(ctx, changesetSpec); err != nil {
t.Fatal(err)
}

s, err := graphqlbackend.NewSchema(&Resolver{store: store}, nil, nil)
if err != nil {
t.Fatal(err)
Expand All @@ -66,11 +87,29 @@ func TestCampaignSpecResolver(t *testing.T) {
OriginalInput: spec.RawSpec,
ParsedInput: graphqlbackend.JSONValue{Value: unmarshaled},

PreviewURL: "/campaigns/new?spec=" + apiID,
Namespace: apitest.UserOrg{ID: userApiID, DatabaseID: userID},
Creator: apitest.User{ID: userApiID, DatabaseID: userID},
CreatedAt: graphqlbackend.DateTime{Time: spec.CreatedAt.Truncate(time.Second)},
ExpiresAt: &graphqlbackend.DateTime{Time: spec.ExpiresAt().Truncate(time.Second)},
PreviewURL: "/campaigns/new?spec=" + apiID,
Namespace: apitest.UserOrg{ID: userApiID, DatabaseID: userID},
Creator: apitest.User{ID: userApiID, DatabaseID: userID},
ViewerCanAdminister: true,

CreatedAt: graphqlbackend.DateTime{Time: spec.CreatedAt.Truncate(time.Second)},
ExpiresAt: &graphqlbackend.DateTime{Time: spec.ExpiresAt().Truncate(time.Second)},

ChangesetSpecs: apitest.ChangesetSpecConnection{
TotalCount: 1,
Nodes: []apitest.ChangesetSpec{
{
ID: string(marshalChangesetSpecRandID(changesetSpec.RandID)),
Typename: "VisibleChangesetSpec",
Description: apitest.ChangesetSpecDescription{
BaseRepository: apitest.Repository{
ID: string(repoID),
Name: repo.Name,
},
},
},
},
},
}

if diff := cmp.Diff(want, response.Node); diff != "" {
Expand Down Expand Up @@ -98,9 +137,43 @@ query($campaignSpec: ID!) {
}
previewURL
viewerCanAdminister
createdAt
expiresAt
changesetSpecs(first: 100) {
totalCount
nodes {
__typename
type
... on HiddenChangesetSpec {
id
}
... on VisibleChangesetSpec {
id
description {
... on ExistingChangesetReference {
baseRepository {
id
name
}
}
... on GitBranchChangesetDescription {
baseRepository {
id
name
}
}
}
}
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion enterprise/internal/campaigns/resolvers/campaigns.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (r *campaignResolver) Author(ctx context.Context) (*graphqlbackend.UserReso
}

func (r *campaignResolver) ViewerCanAdminister(ctx context.Context) (bool, error) {
return currentUserCanAdministerCampaign(ctx, r.Campaign)
return checkSiteAdminOrSameUser(ctx, r.Campaign.AuthorID)
}

func (r *campaignResolver) URL(ctx context.Context) (string, error) {
Expand Down
3 changes: 1 addition & 2 deletions enterprise/internal/campaigns/resolvers/changeset_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,10 @@ func (r *changesetEventResolver) CreatedAt() graphqlbackend.DateTime {

func (r *changesetEventResolver) Changeset(ctx context.Context) (graphqlbackend.ExternalChangesetResolver, error) {
return &changesetResolver{
// Just to be explicit we always return external changesets here.
isHidden: false,
store: r.store,
httpFactory: r.httpFactory,
changeset: r.changeset,
repoCtx: ctx,
}, nil
}

Expand Down
Loading

0 comments on commit 4f856d1

Please sign in to comment.