From e27d415419fd097a271f2a8fef6f8936435d5b7f Mon Sep 17 00:00:00 2001 From: Pedja Muratovic Date: Thu, 24 Jul 2025 17:17:42 -0400 Subject: [PATCH 01/16] Issue-3634: Api For Social Accounts with example --- example/social_accounts/main.go | 47 ++++++++++++++ github/users_social_accounts.go | 105 ++++++++++++++++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 example/social_accounts/main.go create mode 100644 github/users_social_accounts.go diff --git a/example/social_accounts/main.go b/example/social_accounts/main.go new file mode 100644 index 00000000000..01d9593eecb --- /dev/null +++ b/example/social_accounts/main.go @@ -0,0 +1,47 @@ +// Copyright 2024 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. + +// The social_accounts command demonstrates how to use the social accounts API. +// It lists social accounts for a given username. +package main + +import ( + "context" + "fmt" + + "github.com/google/go-github/v74/github" +) + +// Fetch social accounts for a user. +func fetchSocialAccounts(username string) ([]*github.SocialAccount, error) { + client := github.NewClient(nil) + accounts, _, err := client.Users.ListUserSocialAccounts(context.Background(), username, nil) + return accounts, err +} + +func main() { + var username string + fmt.Print("Enter GitHub username: ") + fmt.Scanf("%s", &username) + + accounts, err := fetchSocialAccounts(username) + if err != nil { + fmt.Printf("Error: %v\n", err) + return + } + + if len(accounts) == 0 { + fmt.Printf("No social accounts found for %s\n", username) + return + } + + fmt.Printf("Social accounts for %s:\n", username) + for i, account := range accounts { + fmt.Printf("%v. Provider: %s, URL: %s\n", + i+1, + *account.Provider, + *account.URL) + } +} diff --git a/github/users_social_accounts.go b/github/users_social_accounts.go new file mode 100644 index 00000000000..6e950450951 --- /dev/null +++ b/github/users_social_accounts.go @@ -0,0 +1,105 @@ +// Copyright 2024 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 github + +import ( + "context" + "fmt" +) + +// SocialAccount represents a social account linked to a user. +type SocialAccount struct { + Provider *string `json:"provider,omitempty"` + URL *string `json:"url,omitempty"` +} + +// ListSocialAccounts lists all social accounts for the authenticated user. +// +// GitHub API docs: https://docs.github.com/rest/users/social-accounts#list-social-accounts-for-the-authenticated-user +// +//meta:operation GET /user/social_accounts +func (s *UsersService) ListSocialAccounts(ctx context.Context, opts *ListOptions) ([]*SocialAccount, *Response, error) { + u := "user/social_accounts" + u, err := addOptions(u, opts) + if err != nil { + return nil, nil, err + } + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + var socialAccounts []*SocialAccount + resp, err := s.client.Do(ctx, req, &socialAccounts) + if err != nil { + return nil, resp, err + } + + return socialAccounts, resp, nil +} + +// AddSocialAccounts adds social accounts for the authenticated user. +// +// GitHub API docs: https://docs.github.com/rest/users/social-accounts#add-social-accounts-for-the-authenticated-user +// +//meta:operation POST /user/social_accounts +func (s *UsersService) AddSocialAccounts(ctx context.Context, accountUrlsToAdd []string) ([]*SocialAccount, *Response, error) { + u := "user/social_accounts" + req, err := s.client.NewRequest("POST", u, accountUrlsToAdd) + if err != nil { + return nil, nil, err + } + + var addedAccounts []*SocialAccount + resp, err := s.client.Do(ctx, req, &addedAccounts) + if err != nil { + return nil, resp, err + } + + return addedAccounts, resp, nil +} + +// DeleteSocialAccounts deletes social accounts for the authenticated user. +// +// GitHub API docs: https://docs.github.com/rest/users/social-accounts#delete-social-accounts-for-the-authenticated-user +// +//meta:operation DELETE /user/social_accounts +func (s *UsersService) DeleteSocialAccounts(ctx context.Context, accountsToDelete []*string) (*Response, error) { + u := "user/social_accounts" + req, err := s.client.NewRequest("DELETE", u, accountsToDelete) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} + +// ListUserSocialAccounts lists all social accounts for a user. +// +// GitHub API docs: https://docs.github.com/rest/users/social-accounts#list-social-accounts-for-a-user +// +//meta:operation GET /users/{username}/social_accounts +func (s *UsersService) ListUserSocialAccounts(ctx context.Context, username string, opts *ListOptions) ([]*SocialAccount, *Response, error) { + u := fmt.Sprintf("users/%v/social_accounts", username) + u, err := addOptions(u, opts) + if err != nil { + return nil, nil, err + } + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + var addedAccounts []*SocialAccount + resp, err := s.client.Do(ctx, req, &addedAccounts) + if err != nil { + return nil, resp, err + } + + return addedAccounts, resp, nil +} From a5ac195325b3678c2a472c3a296e14f68d3dd23f Mon Sep 17 00:00:00 2001 From: Pedja Muratovic Date: Fri, 25 Jul 2025 13:27:14 -0400 Subject: [PATCH 02/16] added first test for fetching social accounts for a user --- github/users_social_accounts_test.go | 51 ++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 github/users_social_accounts_test.go diff --git a/github/users_social_accounts_test.go b/github/users_social_accounts_test.go new file mode 100644 index 00000000000..2e4e2a48502 --- /dev/null +++ b/github/users_social_accounts_test.go @@ -0,0 +1,51 @@ +// Copyright 2025 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 github + +import ( + "context" + "fmt" + "net/http" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestUsersService_ListSocialAccounts(t *testing.T) { + t.Parallel() + + client, mux, _ := setup(t) + + mux.HandleFunc("/user/social_accounts", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{"page": "2"}) + fmt.Fprint(w, `[{ + "provider": "twitter", + "url": "https://twitter.com/github" + }]`) + }) + + opt := &ListOptions{Page: 2} + ctx := context.Background() + accounts, _, err := client.Users.ListSocialAccounts(ctx, opt) + if err != nil { + t.Errorf("Users.ListSocialAccounts returned error: %v", err) + } + + want := []*SocialAccount{{Provider: Ptr("twitter"), URL: Ptr("https://twitter.com/github")}} + if !cmp.Equal(accounts, want) { + t.Errorf("Users.ListSocialAccounts returned %#v, want %#v", accounts, want) + } + + const methodName = "ListSocialAccounts" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Users.ListSocialAccounts(ctx, opt) + if (got != nil) { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} From 5f88c62e3d5a3972f2588c1321b2fabc257f58b8 Mon Sep 17 00:00:00 2001 From: Pedja Muratovic Date: Sun, 27 Jul 2025 17:10:31 -0400 Subject: [PATCH 03/16] updated copyright year --- github/users_social_accounts.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/users_social_accounts.go b/github/users_social_accounts.go index 6e950450951..9fdfb32fb97 100644 --- a/github/users_social_accounts.go +++ b/github/users_social_accounts.go @@ -1,4 +1,4 @@ -// Copyright 2024 The go-github AUTHORS. All rights reserved. +// Copyright 2025 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. From f05afd0ed9bc63b8e3c0d34c6c7b9bcbfb697ccf Mon Sep 17 00:00:00 2001 From: Pedja Muratovic Date: Sun, 27 Jul 2025 17:11:57 -0400 Subject: [PATCH 04/16] updated name for urls input variable --- github/users_social_accounts.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/github/users_social_accounts.go b/github/users_social_accounts.go index 9fdfb32fb97..dda5801d8e4 100644 --- a/github/users_social_accounts.go +++ b/github/users_social_accounts.go @@ -47,9 +47,9 @@ func (s *UsersService) ListSocialAccounts(ctx context.Context, opts *ListOptions // GitHub API docs: https://docs.github.com/rest/users/social-accounts#add-social-accounts-for-the-authenticated-user // //meta:operation POST /user/social_accounts -func (s *UsersService) AddSocialAccounts(ctx context.Context, accountUrlsToAdd []string) ([]*SocialAccount, *Response, error) { +func (s *UsersService) AddSocialAccounts(ctx context.Context, accountURLsToAdd []string) ([]*SocialAccount, *Response, error) { u := "user/social_accounts" - req, err := s.client.NewRequest("POST", u, accountUrlsToAdd) + req, err := s.client.NewRequest("POST", u, accountURLsToAdd) if err != nil { return nil, nil, err } From 623cf137edea92ea17546e8a98c353e2a671b1f6 Mon Sep 17 00:00:00 2001 From: Pedja Muratovic Date: Sun, 27 Jul 2025 17:13:57 -0400 Subject: [PATCH 05/16] fixed DeleteSocialAccounts accountsToDelete input var to be of type []string instead of []*strin --- github/users_social_accounts.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/users_social_accounts.go b/github/users_social_accounts.go index dda5801d8e4..80e1017d2e2 100644 --- a/github/users_social_accounts.go +++ b/github/users_social_accounts.go @@ -68,7 +68,7 @@ func (s *UsersService) AddSocialAccounts(ctx context.Context, accountURLsToAdd [ // GitHub API docs: https://docs.github.com/rest/users/social-accounts#delete-social-accounts-for-the-authenticated-user // //meta:operation DELETE /user/social_accounts -func (s *UsersService) DeleteSocialAccounts(ctx context.Context, accountsToDelete []*string) (*Response, error) { +func (s *UsersService) DeleteSocialAccounts(ctx context.Context, accountsToDelete []string) (*Response, error) { u := "user/social_accounts" req, err := s.client.NewRequest("DELETE", u, accountsToDelete) if err != nil { From 7b7c227bc6bbb474b0aa5f8c8104a6c52b043805 Mon Sep 17 00:00:00 2001 From: Pedja Muratovic Date: Sun, 27 Jul 2025 18:18:53 -0400 Subject: [PATCH 06/16] added tests for AddSocialAccounts --- github/users_social_accounts_test.go | 44 ++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/github/users_social_accounts_test.go b/github/users_social_accounts_test.go index 2e4e2a48502..7f64c2721a5 100644 --- a/github/users_social_accounts_test.go +++ b/github/users_social_accounts_test.go @@ -7,6 +7,7 @@ package github import ( "context" + "encoding/json" "fmt" "net/http" "testing" @@ -49,3 +50,46 @@ func TestUsersService_ListSocialAccounts(t *testing.T) { return resp, err }) } + +func TestUsersService_AddSocialAccounts(t *testing.T) { + t.Parallel() + + client, mux, _ := setup(t) + + input := []string{"https://twitter.com/github"} + + mux.HandleFunc("/user/social_accounts", func(w http.ResponseWriter, r *http.Request) { + var v []string + assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) + + testMethod(t, r, "POST") + if !cmp.Equal(v, input) { + t.Errorf("Request body = %+v, want %+v", v, input) + } + + fmt.Fprint(w, `[{"provider":"twitter","url":"https://twitter.com/github"},{"provider":"facebook","url":"https://facebook.com/github"}]`) + }) + + ctx := context.Background() + accounts, _, err := client.Users.AddSocialAccounts(ctx, input) + if err != nil { + t.Errorf("Users.AddSocialAccounts returned error: %v", err) + } + + want := []*SocialAccount{ + {Provider: Ptr("twitter"), URL: Ptr("https://twitter.com/github")}, + {Provider: Ptr("facebook"), URL: Ptr("https://facebook.com/github")}, + } + if !cmp.Equal(accounts, want) { + t.Errorf("Users.AddSocialAccounts returned %#v, want %#v", accounts, want) + } + + const methodName = "AddSocialAccounts" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Users.AddSocialAccounts(ctx, input) + if (got != nil) { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} From 72f0e6751c4b89037921481402b2e4b4e7d91694 Mon Sep 17 00:00:00 2001 From: Pedja Muratovic Date: Sun, 27 Jul 2025 18:23:03 -0400 Subject: [PATCH 07/16] added test for deleting social accounts --- github/users_social_accounts_test.go | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/github/users_social_accounts_test.go b/github/users_social_accounts_test.go index 7f64c2721a5..2731b33796c 100644 --- a/github/users_social_accounts_test.go +++ b/github/users_social_accounts_test.go @@ -93,3 +93,33 @@ func TestUsersService_AddSocialAccounts(t *testing.T) { return resp, err }) } + + +func TestUsersService_DeleteSocialAccounts(t *testing.T) { + t.Parallel() + + client, mux, _ := setup(t) + + input := []string{"https://twitter.com/github"} + + mux.HandleFunc("/user/social_accounts", func(w http.ResponseWriter, r *http.Request) { + var v []string + assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) + + testMethod(t, r, "DELETE") + if !cmp.Equal(v, input) { + t.Errorf("Request body = %+v, want %+v", v, input) + } + }) + + ctx := context.Background() + _, err := client.Users.DeleteSocialAccounts(ctx, input) + if err != nil { + t.Errorf("Users.DeleteSocialAccounts returned error: %v", err) + } + + const methodName = "DeleteSocialAccounts" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Users.DeleteSocialAccounts(ctx, input) + }) +} \ No newline at end of file From 3698ea22a4653de2e341095495f09a512281fe8a Mon Sep 17 00:00:00 2001 From: Pedja Muratovic Date: Mon, 28 Jul 2025 08:59:52 -0400 Subject: [PATCH 08/16] added test for ListUserSocialAccounts --- github/users_social_accounts_test.go | 39 +++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/github/users_social_accounts_test.go b/github/users_social_accounts_test.go index 2731b33796c..f67dd39bfcb 100644 --- a/github/users_social_accounts_test.go +++ b/github/users_social_accounts_test.go @@ -122,4 +122,41 @@ func TestUsersService_DeleteSocialAccounts(t *testing.T) { testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { return client.Users.DeleteSocialAccounts(ctx, input) }) -} \ No newline at end of file +} + + +func TestUsersService_ListUserSocialAccounts(t *testing.T) { + t.Parallel() + + client, mux, _ := setup(t) + + mux.HandleFunc("/users/u/social_accounts", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{"page": "2"}) + fmt.Fprint(w, `[{ + "provider": "twitter", + "url": "https://twitter.com/github" + }]`) + }) + + opt := &ListOptions{Page: 2} + ctx := context.Background() + accounts, _, err := client.Users.ListUserSocialAccounts(ctx, "u", opt) + if err != nil { + t.Errorf("Users.ListUserSocialAccounts returned error: %v", err) + } + + want := []*SocialAccount{{Provider: Ptr("twitter"), URL: Ptr("https://twitter.com/github")}} + if !cmp.Equal(accounts, want) { + t.Errorf("Users.ListUserSocialAccounts returned %#v, want %#v", accounts, want) + } + + const methodName = "ListUserSocialAccounts" + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Users.ListUserSocialAccounts(ctx, "u", opt) + if (got != nil) { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} From e987f887df07787ac3e09a85e4fa32d66d4a514f Mon Sep 17 00:00:00 2001 From: Pedja Muratovic Date: Mon, 28 Jul 2025 09:15:12 -0400 Subject: [PATCH 09/16] formatted and linted code corrections --- github/users_social_accounts_test.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/github/users_social_accounts_test.go b/github/users_social_accounts_test.go index f67dd39bfcb..11424b1284b 100644 --- a/github/users_social_accounts_test.go +++ b/github/users_social_accounts_test.go @@ -44,7 +44,7 @@ func TestUsersService_ListSocialAccounts(t *testing.T) { const methodName = "ListSocialAccounts" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { got, resp, err := client.Users.ListSocialAccounts(ctx, opt) - if (got != nil) { + if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } return resp, err @@ -87,14 +87,13 @@ func TestUsersService_AddSocialAccounts(t *testing.T) { const methodName = "AddSocialAccounts" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { got, resp, err := client.Users.AddSocialAccounts(ctx, input) - if (got != nil) { + if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } return resp, err }) } - func TestUsersService_DeleteSocialAccounts(t *testing.T) { t.Parallel() @@ -102,7 +101,7 @@ func TestUsersService_DeleteSocialAccounts(t *testing.T) { input := []string{"https://twitter.com/github"} - mux.HandleFunc("/user/social_accounts", func(w http.ResponseWriter, r *http.Request) { + mux.HandleFunc("/user/social_accounts", func(_ http.ResponseWriter, r *http.Request) { var v []string assertNilError(t, json.NewDecoder(r.Body).Decode(&v)) @@ -124,7 +123,6 @@ func TestUsersService_DeleteSocialAccounts(t *testing.T) { }) } - func TestUsersService_ListUserSocialAccounts(t *testing.T) { t.Parallel() @@ -154,7 +152,7 @@ func TestUsersService_ListUserSocialAccounts(t *testing.T) { const methodName = "ListUserSocialAccounts" testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { got, resp, err := client.Users.ListUserSocialAccounts(ctx, "u", opt) - if (got != nil) { + if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } return resp, err From 7c026d1dd97b23c94087ce1ea2975215433987ad Mon Sep 17 00:00:00 2001 From: Pedja Muratovic Date: Mon, 28 Jul 2025 09:20:23 -0400 Subject: [PATCH 10/16] ran generate.sh --- github/github-accessors.go | 16 ++++++++++++++++ github/github-accessors_test.go | 22 ++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/github/github-accessors.go b/github/github-accessors.go index b9d8bcf7e65..127359f1226 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -25734,6 +25734,22 @@ func (s *SignatureVerification) GetVerified() bool { return *s.Verified } +// GetProvider returns the Provider field if it's non-nil, zero value otherwise. +func (s *SocialAccount) GetProvider() string { + if s == nil || s.Provider == nil { + return "" + } + return *s.Provider +} + +// GetURL returns the URL field if it's non-nil, zero value otherwise. +func (s *SocialAccount) GetURL() string { + if s == nil || s.URL == nil { + return "" + } + return *s.URL +} + // GetActor returns the Actor field. func (s *Source) GetActor() *User { if s == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index f6a191ee0f8..50188684d5d 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -33097,6 +33097,28 @@ func TestSignatureVerification_GetVerified(tt *testing.T) { s.GetVerified() } +func TestSocialAccount_GetProvider(tt *testing.T) { + tt.Parallel() + var zeroValue string + s := &SocialAccount{Provider: &zeroValue} + s.GetProvider() + s = &SocialAccount{} + s.GetProvider() + s = nil + s.GetProvider() +} + +func TestSocialAccount_GetURL(tt *testing.T) { + tt.Parallel() + var zeroValue string + s := &SocialAccount{URL: &zeroValue} + s.GetURL() + s = &SocialAccount{} + s.GetURL() + s = nil + s.GetURL() +} + func TestSource_GetActor(tt *testing.T) { tt.Parallel() s := &Source{} From 43c91e24d1e5960e9e3bc9c79837c63bf9e0a7b6 Mon Sep 17 00:00:00 2001 From: Pedja Muratovic Date: Mon, 28 Jul 2025 09:34:45 -0400 Subject: [PATCH 11/16] corrected copyright year --- example/social_accounts/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example/social_accounts/main.go b/example/social_accounts/main.go index 01d9593eecb..9c8ecc33cc3 100644 --- a/example/social_accounts/main.go +++ b/example/social_accounts/main.go @@ -1,4 +1,4 @@ -// Copyright 2024 The go-github AUTHORS. All rights reserved. +// Copyright 2025 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. From 3f5d1b7b016f58f34c007595eaa71976f956e9e5 Mon Sep 17 00:00:00 2001 From: Pedja Muratovic Date: Mon, 28 Jul 2025 09:37:58 -0400 Subject: [PATCH 12/16] applied comments for log formatting --- example/social_accounts/main.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/example/social_accounts/main.go b/example/social_accounts/main.go index 9c8ecc33cc3..7352b3fea2a 100644 --- a/example/social_accounts/main.go +++ b/example/social_accounts/main.go @@ -10,7 +10,7 @@ package main import ( "context" "fmt" - + "log" "github.com/google/go-github/v74/github" ) @@ -28,18 +28,18 @@ func main() { accounts, err := fetchSocialAccounts(username) if err != nil { - fmt.Printf("Error: %v\n", err) + log.Fatalf("Error: %v\n", err) return } if len(accounts) == 0 { - fmt.Printf("No social accounts found for %s\n", username) + fmt.Printf("No social accounts found for %v\n", username) return } fmt.Printf("Social accounts for %s:\n", username) for i, account := range accounts { - fmt.Printf("%v. Provider: %s, URL: %s\n", + fmt.Printf("%v. Provider: %v, URL: %v\n", i+1, *account.Provider, *account.URL) From a8401a5cf9fc2c95c20349e7a2e9e1e4b6b321e5 Mon Sep 17 00:00:00 2001 From: Pedja Muratovic Date: Mon, 28 Jul 2025 10:00:55 -0400 Subject: [PATCH 13/16] corrected lintting issues --- example/social_accounts/main.go | 1 + 1 file changed, 1 insertion(+) diff --git a/example/social_accounts/main.go b/example/social_accounts/main.go index 7352b3fea2a..dde8bd250a6 100644 --- a/example/social_accounts/main.go +++ b/example/social_accounts/main.go @@ -11,6 +11,7 @@ import ( "context" "fmt" "log" + "github.com/google/go-github/v74/github" ) From 928f56b18916753c77d9e2b8df16045b7716dc1c Mon Sep 17 00:00:00 2001 From: Pedja Muratovic Date: Mon, 28 Jul 2025 10:05:21 -0400 Subject: [PATCH 14/16] removed return after log.Fatalf --- example/social_accounts/main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/example/social_accounts/main.go b/example/social_accounts/main.go index dde8bd250a6..f87eb8dd75b 100644 --- a/example/social_accounts/main.go +++ b/example/social_accounts/main.go @@ -30,7 +30,6 @@ func main() { accounts, err := fetchSocialAccounts(username) if err != nil { log.Fatalf("Error: %v\n", err) - return } if len(accounts) == 0 { From c056e96026817b6f4f98ec4a2e258d10d49b158b Mon Sep 17 00:00:00 2001 From: Pedja Muratovic Date: Mon, 28 Jul 2025 10:24:20 -0400 Subject: [PATCH 15/16] updated variable names --- github/users_social_accounts.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/github/users_social_accounts.go b/github/users_social_accounts.go index 80e1017d2e2..30927cd8db1 100644 --- a/github/users_social_accounts.go +++ b/github/users_social_accounts.go @@ -47,20 +47,20 @@ func (s *UsersService) ListSocialAccounts(ctx context.Context, opts *ListOptions // GitHub API docs: https://docs.github.com/rest/users/social-accounts#add-social-accounts-for-the-authenticated-user // //meta:operation POST /user/social_accounts -func (s *UsersService) AddSocialAccounts(ctx context.Context, accountURLsToAdd []string) ([]*SocialAccount, *Response, error) { +func (s *UsersService) AddSocialAccounts(ctx context.Context, accountURLs []string) ([]*SocialAccount, *Response, error) { u := "user/social_accounts" - req, err := s.client.NewRequest("POST", u, accountURLsToAdd) + req, err := s.client.NewRequest("POST", u, accountURLs) if err != nil { return nil, nil, err } - var addedAccounts []*SocialAccount - resp, err := s.client.Do(ctx, req, &addedAccounts) + var accounts []*SocialAccount + resp, err := s.client.Do(ctx, req, &accounts) if err != nil { return nil, resp, err } - return addedAccounts, resp, nil + return accounts, resp, nil } // DeleteSocialAccounts deletes social accounts for the authenticated user. @@ -68,9 +68,9 @@ func (s *UsersService) AddSocialAccounts(ctx context.Context, accountURLsToAdd [ // GitHub API docs: https://docs.github.com/rest/users/social-accounts#delete-social-accounts-for-the-authenticated-user // //meta:operation DELETE /user/social_accounts -func (s *UsersService) DeleteSocialAccounts(ctx context.Context, accountsToDelete []string) (*Response, error) { +func (s *UsersService) DeleteSocialAccounts(ctx context.Context, accountURLs []string) (*Response, error) { u := "user/social_accounts" - req, err := s.client.NewRequest("DELETE", u, accountsToDelete) + req, err := s.client.NewRequest("DELETE", u, accountURLs) if err != nil { return nil, err } From f3b7477e79351ac24a0427f086bdaa60c4f1f4a7 Mon Sep 17 00:00:00 2001 From: Pedja Muratovic Date: Tue, 29 Jul 2025 11:58:29 -0400 Subject: [PATCH 16/16] moved ListUserSocialAccounts example to examples_test.go --- example/social_accounts/main.go | 47 --------------------------------- github/examples_test.go | 16 +++++++++++ 2 files changed, 16 insertions(+), 47 deletions(-) delete mode 100644 example/social_accounts/main.go diff --git a/example/social_accounts/main.go b/example/social_accounts/main.go deleted file mode 100644 index f87eb8dd75b..00000000000 --- a/example/social_accounts/main.go +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright 2025 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. - -// The social_accounts command demonstrates how to use the social accounts API. -// It lists social accounts for a given username. -package main - -import ( - "context" - "fmt" - "log" - - "github.com/google/go-github/v74/github" -) - -// Fetch social accounts for a user. -func fetchSocialAccounts(username string) ([]*github.SocialAccount, error) { - client := github.NewClient(nil) - accounts, _, err := client.Users.ListUserSocialAccounts(context.Background(), username, nil) - return accounts, err -} - -func main() { - var username string - fmt.Print("Enter GitHub username: ") - fmt.Scanf("%s", &username) - - accounts, err := fetchSocialAccounts(username) - if err != nil { - log.Fatalf("Error: %v\n", err) - } - - if len(accounts) == 0 { - fmt.Printf("No social accounts found for %v\n", username) - return - } - - fmt.Printf("Social accounts for %s:\n", username) - for i, account := range accounts { - fmt.Printf("%v. Provider: %v, URL: %v\n", - i+1, - *account.Provider, - *account.URL) - } -} diff --git a/github/examples_test.go b/github/examples_test.go index 525b0f03158..ad9430930de 100644 --- a/github/examples_test.go +++ b/github/examples_test.go @@ -173,3 +173,19 @@ func ExampleTeamsService_ListTeams() { fmt.Printf("Team %q was not found\n", teamName) } + +func ExampleUsersService_ListUserSocialAccounts() { + client := github.NewClient(nil) + ctx := context.Background() + opts := &github.ListOptions{} + for { + accounts, resp, err := client.Users.ListUserSocialAccounts(ctx, "shreyjain13", opts) + if err != nil { + log.Fatalf("Failed to list user social accounts: %v", err) + } + if resp.NextPage == 0 || len(accounts) == 0 { + break + } + opts.Page = resp.NextPage + } +}