Created
September 4, 2023 15:03
-
-
Save dmytrogajewski/94876ee320e5fdd363479691485f32df to your computer and use it in GitHub Desktop.
Pull stars from other user
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"log" | |
"strings" | |
"github.com/google/go-github/v39/github" | |
"github.com/manifoldco/promptui" | |
"golang.org/x/oauth2" | |
) | |
func main() { | |
// Get the GitHub personal access token from the user. | |
token := getGitHubToken() | |
// Authenticate with GitHub using the personal access token. | |
client := authenticateGitHub(token) | |
// Get the GitHub profile link from the user. | |
profileLink := getGitHubProfileLink() | |
// Extract the username from the profile link. | |
username := extractGitHubUsername(profileLink) | |
// Get the list of starred repositories for the given GitHub user. | |
starredRepos, err := getStarredRepositories(client, username) | |
if err != nil { | |
log.Fatalf("Error getting starred repositories: %v", err) | |
} | |
// Star each repository for the authenticated user. | |
starredCount := starRepositories(client, starredRepos) | |
// Print the results. | |
fmt.Printf("Starred %d repositories.\n", starredCount) | |
} | |
func getGitHubToken() string { | |
prompt := promptui.Prompt{ | |
Label: "Enter your GitHub personal access token:", | |
Mask: '*', | |
} | |
result, err := prompt.Run() | |
if err != nil { | |
log.Fatalf("Prompt failed: %v", err) | |
} | |
return result | |
} | |
func authenticateGitHub(token string) *github.Client { | |
ctx := context.Background() | |
ts := oauth2.StaticTokenSource( | |
&oauth2.Token{AccessToken: token}, | |
) | |
tc := oauth2.NewClient(ctx, ts) | |
client := github.NewClient(tc) | |
return client | |
} | |
func getGitHubProfileLink() string { | |
prompt := promptui.Prompt{ | |
Label: "Enter the GitHub profile link:", | |
} | |
result, err := prompt.Run() | |
if err != nil { | |
log.Fatalf("Prompt failed: %v", err) | |
} | |
return result | |
} | |
func extractGitHubUsername(profileLink string) string { | |
parts := strings.Split(profileLink, "/") | |
if len(parts) < 2 { | |
log.Fatalf("Invalid GitHub profile link") | |
} | |
return parts[len(parts)-1] | |
} | |
func getStarredRepositories(client *github.Client, username string) ([]*github.StarredRepository, error) { | |
ctx := context.Background() | |
opt := &github.ActivityListStarredOptions{ListOptions: github.ListOptions{PerPage: 100}} | |
var allRepos []*github.StarredRepository | |
for { | |
repos, resp, err := client.Activity.ListStarred(ctx, username, opt) | |
if err != nil { | |
return nil, err | |
} | |
allRepos = append(allRepos, repos...) | |
if resp.NextPage == 0 { | |
break | |
} | |
opt.Page = resp.NextPage | |
} | |
return allRepos, nil | |
} | |
func starRepositories(client *github.Client, list []*github.StarredRepository) int { | |
ctx := context.Background() | |
starredCount := 0 | |
for _, item := range list { | |
_, err := client.Activity.Star(ctx, *item.Repository.Owner.Login, *item.Repository.Name) | |
if err != nil { | |
fmt.Printf("Error starring repository %s: %v\n", *item.Repository.FullName, err) | |
} else { | |
fmt.Printf("Starred repository: %s\n", *item.Repository.FullName) | |
starredCount++ | |
} | |
} | |
return starredCount | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment