Skip to content

Commit 8c828ff

Browse files
committed
Only load generated docs list once
instead of for every PR. especially since this list changes so rarely.
1 parent 5f4ff5f commit 8c828ff

File tree

2 files changed

+41
-40
lines changed

2 files changed

+41
-40
lines changed

github/github.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,15 +550,18 @@ func (config *GithubConfig) OpenPR(pr *github.PullRequest, numTries int) error {
550550
func (config *GithubConfig) GetFileContents(file, sha string) (string, error) {
551551
config.analytics.GetContents.Call(config)
552552
getOpts := &github.RepositoryContentGetOptions{Ref: sha}
553+
if len(sha) > 0 {
554+
getOpts.Ref = sha
555+
}
553556
output, _, _, err := config.client.Repositories.GetContents(config.Org, config.Project, file, getOpts)
554557
if err != nil {
555-
err = fmt.Errorf("Unable to get %q at commit %s", file, sha)
558+
err = fmt.Errorf("Unable to get %q at commit %q", file, sha)
556559
// I'm using .V(2) because .generated docs is still not in the repo...
557560
glog.V(2).Infof("%v", err)
558561
return "", err
559562
}
560563
if output == nil {
561-
err = fmt.Errorf("Got empty contents for %q at commit %s", file, sha)
564+
err = fmt.Errorf("Got empty contents for %q at commit %q", file, sha)
562565
glog.Errorf("%v", err)
563566
return "", err
564567
}

mungegithub/pulls/size.go

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,19 @@ import (
2222

2323
github_util "k8s.io/contrib/github"
2424
"k8s.io/contrib/mungegithub/config"
25+
"k8s.io/kubernetes/pkg/util/sets"
2526

2627
"github.com/golang/glog"
2728
"github.com/google/go-github/github"
2829
"github.com/spf13/cobra"
2930
)
3031

31-
type PRSizeMunger struct{}
32+
type PRSizeMunger struct {
33+
genFiles *sets.String
34+
}
3235

3336
func init() {
34-
RegisterMungerOrDie(PRSizeMunger{})
37+
RegisterMungerOrDie(&PRSizeMunger{})
3538
}
3639

3740
func (PRSizeMunger) Name() string { return "size" }
@@ -42,67 +45,62 @@ const labelSizePrefix = "size/"
4245

4346
// getGeneratedFiles returns a list of all automatically generated files in the repo. These include
4447
// docs, deep_copy, and conversions
45-
func getGeneratedFiles(config *config.MungeConfig, c github.RepositoryCommit) []string {
46-
genFiles := []string{
47-
"pkg/api/v1/deep_copy_generated.go",
48-
"pkg/api/deep_copy_generated.go",
49-
"pkg/expapi/v1/deep_copy_generated.go",
50-
"pkg/expapi/deep_copy_generated.go",
51-
"pkg/api/v1/conversion_generated.go",
52-
"pkg/expapi/v1/conversion_generated.go",
53-
"api/swagger-spec/resourceListing.json",
54-
"api/swagger-spec/version.json",
55-
"api/swagger-spec/api.json",
56-
"api/swagger-spec/v1.json",
48+
//
49+
// It would be 'better' to call this for every commit but that takes
50+
// a whole lot of time for almost always the same information, and if
51+
// our results are slightly wrong, who cares? Instead look for the
52+
// generated files once and if someone changed what files are generated
53+
// we'll size slightly wrong. No biggie.
54+
func (s *PRSizeMunger) getGeneratedFiles(config *config.MungeConfig) sets.String {
55+
if s.genFiles != nil {
56+
return *s.genFiles
5757
}
58-
docs, err := config.GetFileContents(".generated_docs", *c.SHA)
58+
files := sets.NewString()
59+
files.Insert("pkg/api/v1/deep_copy_generated.go")
60+
files.Insert("pkg/api/deep_copy_generated.go")
61+
files.Insert("pkg/expapi/v1/deep_copy_generated.go")
62+
files.Insert("pkg/expapi/deep_copy_generated.go")
63+
files.Insert("pkg/api/v1/conversion_generated.go")
64+
files.Insert("pkg/expapi/v1/conversion_generated.go")
65+
files.Insert("api/swagger-spec/resourceListing.json")
66+
files.Insert("api/swagger-spec/version.json")
67+
files.Insert("api/swagger-spec/api.json")
68+
files.Insert("api/swagger-spec/v1.json")
69+
docs, err := config.GetFileContents(".generated_docs", "")
5970
if err != nil {
6071
docs = ""
6172
}
6273
docSlice := strings.Split(docs, "\n")
63-
genFiles = append(genFiles, docSlice...)
74+
files.Insert(docSlice...)
6475

65-
return genFiles
76+
s.genFiles = &files
77+
return *s.genFiles
6678
}
6779

68-
func (PRSizeMunger) MungePullRequest(config *config.MungeConfig, pr *github.PullRequest, issue *github.Issue, commits []github.RepositoryCommit, events []github.IssueEvent) {
80+
func (s *PRSizeMunger) MungePullRequest(config *config.MungeConfig, pr *github.PullRequest, issue *github.Issue, commits []github.RepositoryCommit, events []github.IssueEvent) {
81+
genFiles := s.getGeneratedFiles(config)
82+
6983
if pr.Additions == nil {
7084
glog.Warningf("PR %d has nil Additions", *pr.Number)
7185
return
7286
}
87+
adds := *pr.Additions
7388
if pr.Deletions == nil {
7489
glog.Warningf("PR %d has nil Deletions", *pr.Number)
7590
return
7691
}
77-
78-
adds := *pr.Additions
7992
dels := *pr.Deletions
8093

81-
// It would be 'better' to call this for every commit but that takes
82-
// a whole lot of time for almost always the same information, and if
83-
// our results are slightly wrong, who cares? Instead look for the
84-
// generated files once per PR and if someone changed both what files
85-
// are generated and then undid that change in an intermediate commit
86-
// we might call this PR bigger than we "should."
87-
genFiles := getGeneratedFiles(config, commits[len(commits)-1])
88-
8994
for _, c := range commits {
9095
for _, f := range c.Files {
9196
if strings.HasPrefix(*f.Filename, "Godeps/") {
9297
adds = adds - *f.Additions
9398
dels = dels - *f.Deletions
9499
continue
95100
}
96-
found := false
97-
for _, genFile := range genFiles {
98-
if *f.Filename == genFile {
99-
adds = adds - *f.Additions
100-
dels = dels - *f.Deletions
101-
found = true
102-
break
103-
}
104-
}
105-
if found {
101+
if genFiles.Has(*f.Filename) {
102+
adds = adds - *f.Additions
103+
dels = dels - *f.Deletions
106104
continue
107105
}
108106
}

0 commit comments

Comments
 (0)