-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split out code from label/pullrequests.go into helper functions
- Loading branch information
Showing
7 changed files
with
99 additions
and
200 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package cmd | ||
|
||
// For future use |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,47 @@ | ||
package cmd | ||
|
||
import ( | ||
"log" | ||
"time" | ||
|
||
"github.com/google/go-github/v60/github" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
|
||
"github.com/chia-network/github-bot/internal/config" | ||
"github.com/chia-network/github-bot/internal/label" | ||
) | ||
|
||
// labelPRsCmd represents the labelPRs command | ||
var sendKeybaseMessage = &cobra.Command{ | ||
Use: "sendKeybaseMessage", | ||
Short: "Sends a message to Keybase informing users of stale community PRs", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
cfg, err := config.LoadConfig(viper.GetString("config")) | ||
if err != nil { | ||
log.Fatalf("error loading config: %s\n", err.Error()) | ||
} | ||
client := github.NewClient(nil).WithAuthToken(cfg.GithubToken) | ||
|
||
loop := viper.GetBool("loop") | ||
loopDuration := viper.GetDuration("loop-time") | ||
for { | ||
log.Println("Labeling Pull Requests") | ||
err = label.PullRequests(client, cfg.InternalTeam, cfg.LabelConfig) | ||
if err != nil { | ||
log.Fatalln(err.Error()) | ||
} | ||
|
||
if !loop { | ||
break | ||
} | ||
|
||
log.Printf("Waiting %s for next iteration\n", loopDuration.String()) | ||
time.Sleep(loopDuration) | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(sendKeybaseMessage) | ||
} | ||
//import ( | ||
// "log" | ||
// "time" | ||
// | ||
// "github.com/google/go-github/v60/github" | ||
// "github.com/spf13/cobra" | ||
// "github.com/spf13/viper" | ||
// | ||
// "github.com/chia-network/github-bot/internal/config" | ||
// "github.com/chia-network/github-bot/internal/label" | ||
//) | ||
// | ||
//labelPRsCmd represents the labelPRs command | ||
//var sendKeybaseMessage = &cobra.Command{ | ||
// Use: "sendKeybaseMessage", | ||
// Short: "Sends a message to Keybase informing users of stale community PRs", | ||
// Run: func(cmd *cobra.Command, args []string) { | ||
// cfg, err := config.LoadConfig(viper.GetString("config")) | ||
// if err != nil { | ||
// log.Fatalf("error loading config: %s\n", err.Error()) | ||
// } | ||
// client := github.NewClient(nil).WithAuthToken(cfg.GithubToken) | ||
// | ||
// loop := viper.GetBool("loop") | ||
// loopDuration := viper.GetDuration("loop-time") | ||
// for { | ||
// log.Println("Labeling Pull Requests") | ||
// err = label.PullRequests(client, cfg.InternalTeam, cfg.LabelConfig) | ||
// if err != nil { | ||
// log.Fatalln(err.Error()) | ||
// } | ||
// | ||
// if !loop { | ||
// break | ||
// } | ||
// | ||
// log.Printf("Waiting %s for next iteration\n", loopDuration.String()) | ||
// time.Sleep(loopDuration) | ||
// } | ||
// }, | ||
//} | ||
// | ||
//func init() { | ||
// rootCmd.AddCommand(sendKeybaseMessage) | ||
//} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,44 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/google/go-github/v60/github" | ||
) | ||
|
||
// GetTeamMemList obtains a list of teammembers | ||
func GetTeamMemList(githubClient *github.Client, internalTeam string) (map[string]bool, error) { | ||
teamMembers := make(map[string]bool) | ||
|
||
teamParts := strings.Split(internalTeam, "/") | ||
if len(teamParts) != 2 { | ||
return nil, fmt.Errorf("invalid team name - must contain org and team : %s", internalTeam) | ||
} | ||
|
||
teamOpts := &github.TeamListTeamMembersOptions{ | ||
Role: "all", | ||
ListOptions: github.ListOptions{ | ||
Page: 0, | ||
PerPage: 100, | ||
}, | ||
} | ||
|
||
for { | ||
teamOpts.ListOptions.Page++ | ||
members, resp, err := githubClient.Teams.ListTeamMembersBySlug(context.TODO(), teamParts[0], teamParts[1], teamOpts) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting team %s: %w", internalTeam, err) | ||
} | ||
|
||
for _, member := range members { | ||
teamMembers[*member.Login] = true | ||
} | ||
|
||
if resp.NextPage == 0 { | ||
break | ||
} | ||
} | ||
return teamMembers, nil | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,124 +1,3 @@ | ||
package sendMessage | ||
package message | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
||
"github.com/google/go-github/v60/github" | ||
|
||
"github.com/chia-network/github-bot/internal/config" | ||
) | ||
|
||
// PullRequests applies internal or community labels to pull requests | ||
// Internal is determined by checking if the PR author is a member of the specified internalTeam | ||
func PullRequests(githubClient *github.Client, internalTeam string, cfg config.LabelConfig) error { | ||
teamMembers := map[string]bool{} | ||
|
||
teamParts := strings.Split(internalTeam, "/") | ||
if len(teamParts) != 2 { | ||
return fmt.Errorf("invalid team name - must contain org and team : %s", internalTeam) | ||
} | ||
|
||
teamOpts := &github.TeamListTeamMembersOptions{ | ||
Role: "all", | ||
ListOptions: github.ListOptions{ | ||
Page: 0, | ||
PerPage: 100, | ||
}, | ||
} | ||
|
||
for { | ||
teamOpts.ListOptions.Page++ | ||
members, resp, err := githubClient.Teams.ListTeamMembersBySlug(context.TODO(), teamParts[0], teamParts[1], teamOpts) | ||
if err != nil { | ||
return fmt.Errorf("error getting team %s: %w", internalTeam, err) | ||
} | ||
|
||
for _, member := range members { | ||
teamMembers[*member.Login] = true | ||
} | ||
|
||
if resp.NextPage == 0 { | ||
break | ||
} | ||
} | ||
|
||
for _, fullRepo := range cfg.LabelCheckRepos { | ||
log.Println("checking repos") | ||
parts := strings.Split(fullRepo.Name, "/") | ||
if len(parts) != 2 { | ||
return fmt.Errorf("invalid repository name - must contain owner and repository: %s", fullRepo.Name) | ||
} | ||
opts := &github.PullRequestListOptions{ | ||
State: "open", | ||
Sort: "created", | ||
Direction: "desc", | ||
ListOptions: github.ListOptions{ | ||
Page: 0, | ||
PerPage: 100, | ||
}, | ||
} | ||
for { | ||
lowestNumber := 0 | ||
opts.ListOptions.Page++ | ||
owner := parts[0] | ||
repo := parts[1] | ||
pullRequests, resp, err := githubClient.PullRequests.List(context.TODO(), owner, repo, opts) | ||
if err != nil { | ||
return fmt.Errorf("error listing pull requests: %w", err) | ||
} | ||
|
||
for _, pullRequest := range pullRequests { | ||
lowestNumber = *pullRequest.Number | ||
if *pullRequest.Number < fullRepo.MinimumNumber { | ||
// Break, not continue, since our order ensures PR numbers are getting smaller | ||
break | ||
} | ||
if *pullRequest.Draft { | ||
continue | ||
} | ||
user := *pullRequest.User.Login | ||
if cfg.LabelSkipMap[user] { | ||
continue | ||
} | ||
var label string | ||
if teamMembers[user] { | ||
label = cfg.LabelInternal | ||
} else { | ||
label = cfg.LabelExternal | ||
} | ||
|
||
if label != "" { | ||
log.Printf("Pull Request %d by %s will be labeled %s\n", *pullRequest.Number, user, label) | ||
hasLabel := false | ||
for _, existingLabel := range pullRequest.Labels { | ||
if *existingLabel.Name == label { | ||
log.Println(" Already labeled, skipping...") | ||
hasLabel = true | ||
break | ||
} | ||
} | ||
|
||
if !hasLabel { | ||
allLabels := []string{label} | ||
for _, labelP := range pullRequest.Labels { | ||
allLabels = append(allLabels, *labelP.Name) | ||
} | ||
_, _, err := githubClient.Issues.AddLabelsToIssue(context.TODO(), owner, repo, *pullRequest.Number, allLabels) | ||
if err != nil { | ||
return fmt.Errorf("error adding labels to pull request %d: %w", *pullRequest.Number, err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
if resp.NextPage == 0 || lowestNumber <= fullRepo.MinimumNumber { | ||
break | ||
} | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
// For Future Use |