github.com/google/go-github/v49@v49.1.0/test/integration/activity_test.go (about) 1 // Copyright 2014 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 //go:build integration 7 // +build integration 8 9 package integration 10 11 import ( 12 "context" 13 "testing" 14 15 "github.com/google/go-github/v49/github" 16 ) 17 18 const ( 19 owner = "google" 20 repo = "go-github" 21 ) 22 23 func TestActivity_Starring(t *testing.T) { 24 stargazers, _, err := client.Activity.ListStargazers(context.Background(), owner, repo, nil) 25 if err != nil { 26 t.Fatalf("Activity.ListStargazers returned error: %v", err) 27 } 28 29 if len(stargazers) == 0 { 30 t.Errorf("Activity.ListStargazers(%q, %q) returned no stargazers", owner, repo) 31 } 32 33 // the rest of the tests requires auth 34 if !checkAuth("TestActivity_Starring") { 35 return 36 } 37 38 // first, check if already starred the target repository 39 star, _, err := client.Activity.IsStarred(context.Background(), owner, repo) 40 if err != nil { 41 t.Fatalf("Activity.IsStarred returned error: %v", err) 42 } 43 if star { 44 t.Fatalf("Already starring %v/%v. Please manually unstar it first.", owner, repo) 45 } 46 47 // star the target repository 48 _, err = client.Activity.Star(context.Background(), owner, repo) 49 if err != nil { 50 t.Fatalf("Activity.Star returned error: %v", err) 51 } 52 53 // check again and verify starred 54 star, _, err = client.Activity.IsStarred(context.Background(), owner, repo) 55 if err != nil { 56 t.Fatalf("Activity.IsStarred returned error: %v", err) 57 } 58 if !star { 59 t.Fatalf("Not starred %v/%v after starring it.", owner, repo) 60 } 61 62 // unstar 63 _, err = client.Activity.Unstar(context.Background(), owner, repo) 64 if err != nil { 65 t.Fatalf("Activity.Unstar returned error: %v", err) 66 } 67 68 // check again and verify not watching 69 star, _, err = client.Activity.IsStarred(context.Background(), owner, repo) 70 if err != nil { 71 t.Fatalf("Activity.IsStarred returned error: %v", err) 72 } 73 if star { 74 t.Fatalf("Still starred %v/%v after unstarring it.", owner, repo) 75 } 76 } 77 78 func deleteSubscription(t *testing.T) { 79 // delete subscription 80 _, err := client.Activity.DeleteRepositorySubscription(context.Background(), owner, repo) 81 if err != nil { 82 t.Fatalf("Activity.DeleteRepositorySubscription returned error: %v", err) 83 } 84 85 // check again and verify not watching 86 sub, _, err := client.Activity.GetRepositorySubscription(context.Background(), owner, repo) 87 if err != nil { 88 t.Fatalf("Activity.GetRepositorySubscription returned error: %v", err) 89 } 90 if sub != nil { 91 t.Fatalf("Still watching %v/%v after deleting subscription.", owner, repo) 92 } 93 } 94 95 func createSubscription(t *testing.T) { 96 // watch the target repository 97 sub := &github.Subscription{Subscribed: github.Bool(true)} 98 _, _, err := client.Activity.SetRepositorySubscription(context.Background(), owner, repo, sub) 99 if err != nil { 100 t.Fatalf("Activity.SetRepositorySubscription returned error: %v", err) 101 } 102 103 // check again and verify watching 104 sub, _, err = client.Activity.GetRepositorySubscription(context.Background(), owner, repo) 105 if err != nil { 106 t.Fatalf("Activity.GetRepositorySubscription returned error: %v", err) 107 } 108 if sub == nil || !*sub.Subscribed { 109 t.Fatalf("Not watching %v/%v after setting subscription.", owner, repo) 110 } 111 } 112 113 func TestActivity_Watching(t *testing.T) { 114 watchers, _, err := client.Activity.ListWatchers(context.Background(), owner, repo, nil) 115 if err != nil { 116 t.Fatalf("Activity.ListWatchers returned error: %v", err) 117 } 118 119 if len(watchers) == 0 { 120 t.Errorf("Activity.ListWatchers(%q, %q) returned no watchers", owner, repo) 121 } 122 123 // the rest of the tests requires auth 124 if !checkAuth("TestActivity_Watching") { 125 return 126 } 127 128 // first, check if already watching the target repository 129 sub, _, err := client.Activity.GetRepositorySubscription(context.Background(), owner, repo) 130 if err != nil { 131 t.Fatalf("Activity.GetRepositorySubscription returned error: %v", err) 132 } 133 134 switch { 135 case sub != nil: // If already subscribing, delete then recreate subscription. 136 deleteSubscription(t) 137 createSubscription(t) 138 case sub == nil: // Otherwise, create subscription and then delete it. 139 createSubscription(t) 140 deleteSubscription(t) 141 } 142 }