Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ jobs:
l_max_size: '1000'
fail_if_xl: 'false'
message_if_xl: 'This PR is so big! Please, split it 😊'
files_to_ignore: 'go.mod *.js'
```

> If you want, you can customize all `*_max_size` with the size that fits in your project.
Expand Down
6 changes: 4 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module github.com/friendsofgo/pr-size-labeler
go 1.15

require (
github.com/kelseyhightower/envconfig v1.4.0 // indirect
github.com/posener/goaction v0.1.4
github.com/google/go-github/v31 v31.0.0
github.com/kelseyhightower/envconfig v1.4.0
github.com/posener/goaction v1.1.0
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
)
11 changes: 9 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@ github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+
github.com/goccy/go-yaml v1.4.3/go.mod h1:PsEEJ29nIFZL07P/c8dv4P6rQkVFFXafQee85U+ERHA=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/go-github/v31 v31.0.0 h1:JJUxlP9lFK+ziXKimTCprajMApV1ecWD4NB6CCb0plo=
github.com/google/go-github/v31 v31.0.0/go.mod h1:NQPZol8/1sMoWYGN2yaALIBytu17gAWfhbweiEed3pM=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/hashicorp/errwrap v1.0.0 h1:hLrqtEDnRye3+sgx6z4qVLNuviH3MR5aQ0ykNJa/UYA=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.0.0/go.mod h1:dHtQlpGsu+cZNNAkkCN/P3hoUDHhCYQXV3UM06sGGrk=
github.com/hashicorp/go-multierror v1.1.0 h1:B9UzwGQJehnUY1yNrnwREHc3fGbC2xefo8g4TbElacI=
github.com/hashicorp/go-multierror v1.1.0/go.mod h1:spPvp8C1qA32ftKqdAHm4hHTbPw+vmowP0z+KUhOZdA=
github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=
github.com/kelseyhightower/envconfig v1.4.0/go.mod h1:cccZRl6mQpaq41TPp5QxidR+Sa3axMbJDNb//FQX6Gg=
Expand All @@ -31,8 +34,9 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/autogen v0.0.2/go.mod h1:23ND5WRzjjNM+lOMUvy4WudgDikSK3Sm0rmaXAfnIWo=
github.com/posener/goaction v0.1.4 h1:9psATzCTZWdd6seavDWqkwpCOnu0jn20kNYnt0o5fFQ=
github.com/posener/goaction v0.1.4/go.mod h1:aHgsx8Q6UtebZBWRs8JiSCapjNTfPVqB2Mvi+0REtRQ=
github.com/posener/goaction v1.1.0 h1:XeCafOY/BP69gRPuVWL6GdRjDm+GstTKghwQHeuncKY=
github.com/posener/goaction v1.1.0/go.mod h1:aHgsx8Q6UtebZBWRs8JiSCapjNTfPVqB2Mvi+0REtRQ=
github.com/posener/script v1.1.5 h1:su+9YHNlevT+Hlq2Xul5skh5kYDIBE+x4xu+5mLDT9o=
github.com/posener/script v1.1.5/go.mod h1:Rg3ijooqulo05aGLyGsHoLmIOUzHUVK19WVgrYBPU/E=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand All @@ -43,8 +47,10 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65 h1:+rhAzEzT3f4JtomfC371qB+0Ola2caSKcY69NUBZrRQ=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand All @@ -57,6 +63,7 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/appengine v1.6.5 h1:tycE03LOZYQNhDpS27tcQdAzLCVMaj7QT2SXxebnpCM=
google.golang.org/appengine v1.6.5/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
Expand Down
65 changes: 54 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package main

import (
"context"
"strings"

"github.com/google/go-github/v31/github"
"github.com/kelseyhightower/envconfig"
"github.com/posener/goaction"
"github.com/posener/goaction/actionutil"
"github.com/posener/goaction/log"
"golang.org/x/oauth2"
)

func main() {
Expand All @@ -27,14 +29,28 @@ func main() {
log.Fatalf("Required configuration is missing: %s", err)
}

ctx := context.Background()

ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.GitHubToken},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)

event, err := goaction.GetPullRequest()
if err != nil {
log.Fatalf("Error happened while getting event info: %s", err)
}

prSize := GetPrSize(config, event.PullRequest)
filesToIgnore := strings.Fields(config.FilesToIgnore)

files, _, err := client.PullRequests.ListFiles(ctx, *event.Repo.Owner.Name, *event.Repo.Name, *event.PullRequest.Number, nil)
if err != nil {
log.Fatalf("Error happened while getting files info: %s", err)
}

prSize := GetPrSize(config, files, filesToIgnore)

ctx := context.Background()
gh := actionutil.NewClientWithToken(ctx, config.GitHubToken)

_, _, err = gh.IssuesAddLabelsToIssue(ctx, goaction.PrNum(), []string{string(prSize)})
Expand Down Expand Up @@ -62,8 +78,18 @@ func main() {
log.Debugf("Pull request successfully labeled")
}

func GetPrSize(config Config, pr *github.PullRequest) PrSize {
totalModifications := pr.GetAdditions() + pr.GetDeletions()
func GetPrSize(config Config, files []*github.CommitFile, filesToIgnore []string) PrSize {
var totalModifications int

checkIgnorableFiles := len(filesToIgnore) > 0

for _, file := range files {
if checkIgnorableFiles && isIgnorable(file.GetFilename(), filesToIgnore) {
continue
}

totalModifications += file.GetChanges()
}

switch {
case totalModifications < config.XsMaxSize:
Expand All @@ -79,6 +105,22 @@ func GetPrSize(config Config, pr *github.PullRequest) PrSize {
}
}

func isIgnorable(filename string, filesToIgnore []string) bool {
for _, fti := range filesToIgnore {
if strings.Contains(fti, "*") {
ext := strings.Split(fti, "*")[1]
if strings.Contains(filename, ext) {
return true
}
}
if fti == filename {
return true
}
}

return false
}

type PrSize string

const (
Expand All @@ -91,11 +133,12 @@ const (

// Config is the data structure used to define the action settings.
type Config struct {
GitHubToken string `envconfig:"GITHUB_TOKEN" required:"true"`
XsMaxSize int `envconfig:"XS_MAX_SIZE" default:"10"`
SMaxSize int `envconfig:"S_MAX_SIZE" default:"100"`
MMaxSize int `envconfig:"M_MAX_SIZE" default:"500"`
LMaxSize int `envconfig:"L_MAX_SIZE" default:"1000"`
FailIfXL bool `envconfig:"FAIL_IF_XL" default:"false"`
MessageIfXL string `envconfig:"MESSAGE_IF_XL" default:""`
GitHubToken string `envconfig:"GITHUB_TOKEN" required:"true"`
XsMaxSize int `envconfig:"XS_MAX_SIZE" default:"10"`
SMaxSize int `envconfig:"S_MAX_SIZE" default:"100"`
MMaxSize int `envconfig:"M_MAX_SIZE" default:"500"`
LMaxSize int `envconfig:"L_MAX_SIZE" default:"1000"`
FailIfXL bool `envconfig:"FAIL_IF_XL" default:"false"`
MessageIfXL string `envconfig:"MESSAGE_IF_XL" default:""`
FilesToIgnore string `envconfig:"FILES_TO_IGNORE" default:""`
}