Skip to content

Commit

Permalink
Merge pull request #14 from Chia-Network/check-for-bots
Browse files Browse the repository at this point in the history
Perform the check for users listed in the labelskipmap when building …
  • Loading branch information
pmaslana authored May 31, 2024
2 parents 1b83c16 + 4d7d8df commit 6bf7f5c
Show file tree
Hide file tree
Showing 13 changed files with 61 additions and 42 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ jobs:
matrix:
mode:
- name: label-prs
- name: notify-stale-pending-prs
- name: notify-pending-prs
- name: notify-stale-prs
steps:
- uses: actions/checkout@v4

Expand Down
2 changes: 1 addition & 1 deletion cmd/labelPRs.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var labelPRsCmd = &cobra.Command{
loopDuration := viper.GetDuration("loop-time")
for {
log.Println("Labeling Pull Requests")
err = label.PullRequests(client, cfg.InternalTeam, cfg.LabelConfig)
err = label.PullRequests(client, cfg)
if err != nil {
log.Fatalln(err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/notifyPendingCI.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var notifyPendingCICmd = &cobra.Command{
var listPendingPRs []string
for {
log.Println("Checking for community PRs that are waiting for CI to run")
listPendingPRs, err = github2.CheckForPendingCI(client, cfg.InternalTeam, cfg.CheckStalePending)
listPendingPRs, err = github2.CheckForPendingCI(client, cfg)
if err != nil {
log.Printf("The following error occurred while obtaining a list of pending PRs: %s", err)
time.Sleep(loopDuration)
Expand Down
2 changes: 1 addition & 1 deletion cmd/notifyStale.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ var notifyStaleCmd = &cobra.Command{
var listPendingPRs []string
for {
log.Println("Checking for community PRs that have no update in the last 7 days")
_, err = github2.CheckStalePRs(client, cfg.InternalTeam, cfg.CheckStalePending)
_, err = github2.CheckStalePRs(client, cfg)
if err != nil {
log.Printf("The following error occurred while obtaining a list of stale PRs: %s", err)
time.Sleep(loopDuration)
Expand Down
22 changes: 8 additions & 14 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,22 @@ package config

// Config defines the config for all aspects of the bot
type Config struct {
GithubToken string `yaml:"github_token"`
InternalTeam string `yaml:"internal_team"`
LabelConfig `yaml:",inline"`
CheckStalePending `yaml:",inline"`
GithubToken string `yaml:"github_token"`
InternalTeam string `yaml:"internal_team"`
SkipUsers []string `yaml:"skip_users"`
SkipUsersMap map[string]bool
LabelConfig `yaml:",inline"`
CheckRepos []CheckRepo `yaml:"check_repos"`
}

// LabelConfig is the configuration options specific to labeling PRs
type LabelConfig struct {
LabelInternal string `yaml:"label_internal"`
LabelExternal string `yaml:"label_external"`
LabelCheckRepos []CheckRepo `yaml:"label_check_repos"`
LabelSkipUsers []string `yaml:"label_skip_users"`
LabelSkipMap map[string]bool
LabelInternal string `yaml:"label_internal"`
LabelExternal string `yaml:"label_external"`
}

// CheckRepo is config settings when checking a repo
type CheckRepo struct {
Name string `yaml:"name"`
MinimumNumber int `yaml:"minimum_number"`
}

// CheckStalePending are config settings when checking a repo
type CheckStalePending struct {
CheckStalePending []CheckRepo `yaml:"check_stale_pending_repos"`
}
6 changes: 3 additions & 3 deletions internal/config/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ func LoadConfig(path string) (*Config, error) {
return nil, err
}

config.LabelSkipMap = map[string]bool{}
for _, user := range config.LabelSkipUsers {
config.LabelSkipMap[user] = true
config.SkipUsersMap = map[string]bool{}
for _, user := range config.SkipUsers {
config.SkipUsersMap[user] = true
}

return config, nil
Expand Down
8 changes: 4 additions & 4 deletions internal/github/checkPendingCI.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ import (
)

// CheckForPendingCI returns a list of PR URLs that are ready for CI to run but haven't started yet.
func CheckForPendingCI(githubClient *github.Client, internalTeam string, cfg config.CheckStalePending) ([]string, error) {
teamMembers, _ := GetTeamMemberList(githubClient, internalTeam)
func CheckForPendingCI(githubClient *github.Client, cfg *config.Config) ([]string, error) {
teamMembers, _ := GetTeamMemberList(githubClient, cfg.InternalTeam)
var pendingPRs []string

for _, fullRepo := range cfg.CheckStalePending {
for _, fullRepo := range cfg.CheckRepos {
log.Println("Checking repository:", fullRepo.Name)
parts := strings.Split(fullRepo.Name, "/")
if len(parts) != 2 {
Expand All @@ -28,7 +28,7 @@ func CheckForPendingCI(githubClient *github.Client, internalTeam string, cfg con
owner, repo := parts[0], parts[1]

// Fetch community PRs using the FindCommunityPRs function
communityPRs, err := FindCommunityPRs(cfg.CheckStalePending, teamMembers, githubClient)
communityPRs, err := FindCommunityPRs(cfg, teamMembers, githubClient)
if err != nil {
return nil, err
}
Expand Down
6 changes: 3 additions & 3 deletions internal/github/checkStalePRs.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import (
)

// CheckStalePRs will return a list of PR URLs that have not been updated in the last 7 days by internal team members.
func CheckStalePRs(githubClient *github.Client, internalTeam string, cfg config.CheckStalePending) ([]string, error) {
func CheckStalePRs(githubClient *github.Client, cfg *config.Config) ([]string, error) {
var stalePRUrls []string
cutoffDate := time.Now().Add(7 * 24 * time.Hour) // 7 days ago
teamMembers, err := GetTeamMemberList(githubClient, internalTeam)
teamMembers, err := GetTeamMemberList(githubClient, cfg.InternalTeam)
if err != nil {
return nil, err
}
communityPRs, err := FindCommunityPRs(cfg.CheckStalePending, teamMembers, githubClient)
communityPRs, err := FindCommunityPRs(cfg, teamMembers, githubClient)
if err != nil {
return nil, err
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// FindCommunityPRs obtains PRs based on provided filters
func FindCommunityPRs(repos []config.CheckRepo, teamMembers map[string]bool, githubClient *github.Client) ([]*github.PullRequest, error) {
func FindCommunityPRs(cfg *config.Config, teamMembers map[string]bool, githubClient *github.Client) ([]*github.PullRequest, error) {
var finalPRs []*github.PullRequest
opts := &github.PullRequestListOptions{
State: "open",
Expand All @@ -24,7 +24,7 @@ func FindCommunityPRs(repos []config.CheckRepo, teamMembers map[string]bool, git
},
}

for _, fullRepo := range repos {
for _, fullRepo := range cfg.CheckRepos {
log.Println("Checking repository:", fullRepo.Name)
parts := strings.Split(fullRepo.Name, "/")
if len(parts) != 2 {
Expand All @@ -46,7 +46,7 @@ func FindCommunityPRs(repos []config.CheckRepo, teamMembers map[string]bool, git
continue
}
user := *pullRequest.User.Login
if !teamMembers[user] {
if !teamMembers[user] || !cfg.SkipUsersMap[user] {
finalPRs = append(finalPRs, pullRequest)
}
}
Expand Down
11 changes: 4 additions & 7 deletions internal/label/pullrequests.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,26 @@ import (
)

// PullRequests applies internal or community labels to pull requests
func PullRequests(githubClient *github.Client, internalTeam string, cfg config.LabelConfig) error {
teamMembers, err := github2.GetTeamMemberList(githubClient, internalTeam)
func PullRequests(githubClient *github.Client, cfg *config.Config) error {
teamMembers, err := github2.GetTeamMemberList(githubClient, cfg.InternalTeam)
if err != nil {
return fmt.Errorf("error getting team members: %w", err) // Properly handle and return error if team member list fetch fails
}

pullRequests, err := github2.FindCommunityPRs(cfg.LabelCheckRepos, teamMembers, githubClient)
pullRequests, err := github2.FindCommunityPRs(cfg, teamMembers, githubClient)
if err != nil {
return fmt.Errorf("error finding community PRs: %w", err) // Handle error from finding community PRs
}

for _, pullRequest := range pullRequests {
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
Expand Down
4 changes: 2 additions & 2 deletions k8s/label-prs.yml.j2
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ secretFile:
internal_team: "{{ INTERNAL_TEAM_NAME }}"
label_internal: ""
label_external: "community-pr"
label_check_repos:
check_repos:
- name: "Chia-Network/chia-blockchain"
minimum_number: 17788
- name: "Chia-Network/chia-blockchain-gui"
Expand All @@ -26,6 +26,6 @@ secretFile:
minimum_number: 533
- name: "Chia-Network/chialisp-web"
minimum_number: 263
label_skip_users:
skip_users:
- "dependabot[bot]"
- "github-actions[bot]"
25 changes: 25 additions & 0 deletions k8s/notify-pending-prs.yml.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
replicaCount: 1
image:
repository: ghcr.io/chia-network/github-bot
tag: {{ DOCKER_TAG }}

deployment:
args:
- notify-stale
- --loop

# Creates a secret with the following values, and mounts as a file into the main deployment container
secretFile:
mountPath: "/config"
stringValues:
config.yml: |
github_token: "{{ BOT_GITHUB_TOKEN }}"
internal_team: "{{ INTERNAL_TEAM_NAME }}"
check_repos:
- name: "Chia-Network/chia-blockchain"
minimum_number: 17788
- name: "Chia-Network/chia-blockchain-gui"
minimum_number: 2300
skip_users:
- "dependabot[bot]"
- "github-actions[bot]"
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ image:
deployment:
args:
- notify-pendingci
- notify-stale
- --loop

# Creates a secret with the following values, and mounts as a file into the main deployment container
Expand All @@ -16,8 +15,11 @@ secretFile:
config.yml: |
github_token: "{{ BOT_GITHUB_TOKEN }}"
internal_team: "{{ INTERNAL_TEAM_NAME }}"
check_stale_pending_repos:
check_repos:
- name: "Chia-Network/chia-blockchain"
minimum_number: 17788
- name: "Chia-Network/chia-blockchain-gui"
minimum_number: 2300
skip_users:
- "dependabot[bot]"
- "github-actions[bot]"

0 comments on commit 6bf7f5c

Please sign in to comment.