Skip to content

Auto-generate interfaces for all services #1832

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions example/embed-interface/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2021 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// This embed-interface example is a copy of the "simple" example
// and its purpose is to demonstrate how embedding an interface
// in a struct makes it easy to mock one or more methods.
package main

import (
"context"
"fmt"

"github.com/google/go-github/v35/github"
)

type myClient struct {
client *github.Client
}

// Fetch all the public organizations' membership of a user.
//
func (m *myClient) fetchOrganizations(ctx context.Context, username string) ([]*github.Organization, error) {
orgs, _, err := m.client.Organizations.List(ctx, username, nil)
return orgs, err
}

func main() {
var username string
fmt.Print("Enter GitHub username: ")
fmt.Scanf("%s", &username)

c := &myClient{
client: github.NewClient(nil),
}

ctx := context.Background()
organizations, err := c.fetchOrganizations(ctx, username)
if err != nil {
fmt.Printf("Error: %v\n", err)
return
}

for i, organization := range organizations {
fmt.Printf("%v. %v\n", i+1, organization.GetLogin())
}
}
50 changes: 50 additions & 0 deletions example/embed-interface/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2021 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package main

import (
"context"
"errors"
"reflect"
"testing"

"github.com/google/go-github/v35/github"
)

type fakeOrgSvc struct {
github.OrganizationsServiceInterface

orgs []*github.Organization
}

func (f *fakeOrgSvc) List(ctx context.Context, org string, opts *github.ListOptions) ([]*github.Organization, *github.Response, error) {
if org != "octocat" {
return nil, nil, errors.New("unexpected org")
}

return f.orgs, nil, nil
}

func TestFetchOrganizations(t *testing.T) {
want := []*github.Organization{
{Name: github.String("octocat")},
}

c := &myClient{
client: github.NewClient(nil),
}
c.client.Organizations = &fakeOrgSvc{orgs: want}

ctx := context.Background()
got, err := c.fetchOrganizations(ctx, "octocat")
if err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(got, want) {
t.Errorf("got = %#v, want = %#v", got, want)
}
}
18 changes: 16 additions & 2 deletions github/apps_marketplace.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ type MarketplacePlanAccount struct {
MarketplacePendingChange *MarketplacePendingChange `json:"marketplace_pending_change,omitempty"`
}

// SetStubbed configures the MarketplaceService Stubbed property.
//
// GitHub API docs: https://docs.github.com/en/developers/github-marketplace/testing-your-app#testing-apis
func (s *MarketplaceService) SetStubbed(stubbed bool) {
s.Stubbed = stubbed
}

// GetStubbed returns the MarketplaceService Stubbed property.
//
// GitHub API docs: https://docs.github.com/en/developers/github-marketplace/testing-your-app#testing-apis
func (s *MarketplaceService) GetStubbed() bool {
return s.Stubbed
}

// ListPlans lists all plans for your Marketplace listing.
//
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/apps#list-plans
Expand Down Expand Up @@ -149,7 +163,7 @@ func (s *MarketplaceService) GetPlanAccountForAccount(ctx context.Context, accou
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/apps/#list-subscriptions-for-the-authenticated-user
func (s *MarketplaceService) ListMarketplacePurchasesForUser(ctx context.Context, opts *ListOptions) ([]*MarketplacePurchase, *Response, error) {
uri := "user/marketplace_purchases"
if s.Stubbed {
if s.GetStubbed() {
uri = "user/marketplace_purchases/stubbed"
}

Expand All @@ -173,7 +187,7 @@ func (s *MarketplaceService) ListMarketplacePurchasesForUser(ctx context.Context

func (s *MarketplaceService) marketplaceURI(endpoint string) string {
url := "marketplace_listing"
if s.Stubbed {
if s.GetStubbed() {
url = "marketplace_listing/stubbed"
}
return url + "/" + endpoint
Expand Down
16 changes: 8 additions & 8 deletions github/apps_marketplace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestMarketplaceService_ListPlans(t *testing.T) {
})

opt := &ListOptions{Page: 1, PerPage: 2}
client.Marketplace.Stubbed = false
client.Marketplace.SetStubbed(false)
ctx := context.Background()
plans, _, err := client.Marketplace.ListPlans(ctx, opt)
if err != nil {
Expand Down Expand Up @@ -59,7 +59,7 @@ func TestMarketplaceService_Stubbed_ListPlans(t *testing.T) {
})

opt := &ListOptions{Page: 1, PerPage: 2}
client.Marketplace.Stubbed = true
client.Marketplace.SetStubbed(true)
ctx := context.Background()
plans, _, err := client.Marketplace.ListPlans(ctx, opt)
if err != nil {
Expand All @@ -82,7 +82,7 @@ func TestMarketplaceService_ListPlanAccountsForPlan(t *testing.T) {
})

opt := &ListOptions{Page: 1, PerPage: 2}
client.Marketplace.Stubbed = false
client.Marketplace.SetStubbed(false)
ctx := context.Background()
accounts, _, err := client.Marketplace.ListPlanAccountsForPlan(ctx, 1, opt)
if err != nil {
Expand Down Expand Up @@ -114,7 +114,7 @@ func TestMarketplaceService_Stubbed_ListPlanAccountsForPlan(t *testing.T) {
})

opt := &ListOptions{Page: 1, PerPage: 2}
client.Marketplace.Stubbed = true
client.Marketplace.SetStubbed(true)
ctx := context.Background()
accounts, _, err := client.Marketplace.ListPlanAccountsForPlan(ctx, 1, opt)
if err != nil {
Expand All @@ -136,7 +136,7 @@ func TestMarketplaceService_GetPlanAccountForAccount(t *testing.T) {
fmt.Fprint(w, `{"id":1, "marketplace_pending_change": {"id": 77}}`)
})

client.Marketplace.Stubbed = false
client.Marketplace.SetStubbed(false)
ctx := context.Background()
account, _, err := client.Marketplace.GetPlanAccountForAccount(ctx, 1)
if err != nil {
Expand Down Expand Up @@ -167,7 +167,7 @@ func TestMarketplaceService_Stubbed_GetPlanAccountForAccount(t *testing.T) {
fmt.Fprint(w, `{"id":1}`)
})

client.Marketplace.Stubbed = true
client.Marketplace.SetStubbed(true)
ctx := context.Background()
account, _, err := client.Marketplace.GetPlanAccountForAccount(ctx, 1)
if err != nil {
Expand All @@ -190,7 +190,7 @@ func TestMarketplaceService_ListMarketplacePurchasesForUser(t *testing.T) {
})

opt := &ListOptions{Page: 1, PerPage: 2}
client.Marketplace.Stubbed = false
client.Marketplace.SetStubbed(false)
ctx := context.Background()
purchases, _, err := client.Marketplace.ListMarketplacePurchasesForUser(ctx, opt)
if err != nil {
Expand Down Expand Up @@ -222,7 +222,7 @@ func TestMarketplaceService_Stubbed_ListMarketplacePurchasesForUser(t *testing.T
})

opt := &ListOptions{Page: 1, PerPage: 2}
client.Marketplace.Stubbed = true
client.Marketplace.SetStubbed(true)
ctx := context.Background()
purchases, _, err := client.Marketplace.ListMarketplacePurchasesForUser(ctx, opt)
if err != nil {
Expand Down
Loading