Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: combine titles #19

Merged
merged 6 commits into from
Feb 5, 2024
Merged
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
24 changes: 24 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: "Tests"

on:
push:
branches: [ "main" ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ "main" ]

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: 1.21

- name: Run tests
run: go test -v ./...
4 changes: 1 addition & 3 deletions gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,13 @@ func checkPassingChecks(pr PullRequest) (bool, error) {
return true, nil
}

func checkIfCreatePR(branch string, body string) error {
func checkIfCreatePR(branch string, defaultPRTitle string, body string) error {
create := false
prompt := &survey.Confirm{
Message: "Do you want to submit the combined PR?",
}
survey.AskOne(prompt, &create)

const defaultPRTitle = "Combined dependencies PR"

prTitle := ""
titlePrompt := &survey.Input{
Message: "Do you want to change the PR title?",
Expand Down
13 changes: 7 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
github.com/AlecAivazis/survey/v2 v2.3.7 h1:6I/u8FvytdGsgonrYsVn2t8t4QiRnh6QSTqkkhIiSjQ=
github.com/AlecAivazis/survey/v2 v2.3.7/go.mod h1:xUTIdE4KCOIjsBAE1JYsUPoCqYdZ1reCfTwbto0Fduo=
github.com/MakeNowJust/heredoc v1.0.0 h1:cXCdzVdstXyiTqTvfqk9SDHpKNjxuom+DOlyEeQ4pzQ=
github.com/MakeNowJust/heredoc v1.0.0/go.mod h1:mG5amYoWBHf8vpLOuehzbGGw0EHxpZZ6lCpQ4fNJ8LE=
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2 h1:+vx7roKuyA63nhn5WAunQHLTznkw5W8b1Xc0dNjp83s=
github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDeC1lPdgDeDbhX8XFpy1jqjK0IBG8W5K+xYqA0w=
github.com/cli/go-gh v1.2.1 h1:xFrjejSsgPiwXFP6VYynKWwxLQcNJy3Twbu82ZDlR/o=
Expand All @@ -15,18 +16,18 @@ github.com/creack/pty v1.1.17/go.mod h1:MOBLtS5ELjhRRrroQr9kyvTxUAFNvYEK993ew/Vr
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.14.1 h1:qfhVLaG5s+nCROl1zJsZRxFeYrHLqWroPOQ8BWiNb4w=
github.com/fatih/color v1.14.1/go.mod h1:2oHN61fhTpgcxD3TSWCgKDiH1+x4OiDVVGH8WlgGZGg=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 h1:2VTzZjLZBgl62/EtslCrtky5vbi9dd7HrQPQIx6wqiw=
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542/go.mod h1:Ow0tF8D4Kplbc8s8sSb3V2oUCygFHVp8gC3Dn6U4MNI=
github.com/henvic/httpretty v0.0.6 h1:JdzGzKZBajBfnvlMALXXMVQWxWMF/ofTy8C3/OSUTxs=
github.com/henvic/httpretty v0.0.6/go.mod h1:X38wLjWXHkXT7r2+uK8LjCMne9rsuNaBLJ+5cU2/Pmo=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec h1:qv2VnGeEQHchGaZ/u7lxST/RaJw+cv273q79D81Xbog=
github.com/hinshun/vt10x v0.0.0-20220119200601-820417d04eec/go.mod h1:Q48J4R4DvxnHolD5P8pOtXigYlRuPLGl6moFx3ulM68=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
Expand All @@ -37,8 +38,6 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
Expand All @@ -47,6 +46,7 @@ github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b/go.mod h1:01TrycV0kFyex
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d h1:5PJl274Y63IEHC+7izoQE9x6ikvDFZS2mDVS3drnohI=
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d/go.mod h1:01TrycV0kFyexm33Z7vhZRXopbI8J3TDReVlkTgMUxE=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.12.0 h1:KuQRUE3PgxRFWhq4gHvZtPSLCGDqM5q/cYr1pZ39ytc=
github.com/muesli/termenv v0.12.0/go.mod h1:WCCv32tusQ/EEZ5S8oUIIrC/nIuBcxCVqlN4Xfkv+7A=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand All @@ -56,6 +56,7 @@ github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e h1:BuzhfgfWQbX0dWzYzT1zsORLnHRv3bcRcsaUk0VmXA8=
github.com/thlib/go-timezone-local v0.0.0-20210907160436-ef149e42d28e/go.mod h1:/Tnicc6m/lsJE0irFMA0LfIwTBo4QP7A8IfyIv4zZKI=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand All @@ -81,8 +82,6 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q=
golang.org/x/sys v0.14.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
Expand All @@ -102,7 +101,9 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/h2non/gock.v1 v1.1.2 h1:jBbHXgGBK/AoPVfJh5x4r/WxIrElvbLel8TCZkkZJoY=
gopkg.in/h2non/gock.v1 v1.1.2/go.mod h1:n7UGz/ckNChHiK05rDoiC4MYSunEC/lyaUm2WWaDva0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
45 changes: 20 additions & 25 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,6 @@ func init() {
flag.StringVar(&queryFlag, "query", "", `sets the query used to find combinable PRs. e.g. --query "author:app/dependabot to combine Dependabot PRs`)
flag.BoolVar(&skipPRCheckFlag, "skip-pr-check", false, `if set, will combine matching PRs even if they are not passing checks. Defaults to false when not specified`)
flag.BoolVar(&verboseFlag, "verbose", false, `if set, will print verbose output. Defaults to false when not specified`)

client, err := gh.RESTClient(nil)
if err != nil {
panic(err)
}
ghClient = client

repo, err := gh.CurrentRepository()
if err != nil {
panic(err)
}
fmt.Printf("Current repository is %s/%s\n", repo.Owner(), repo.Name())

currentRepo = repo
}

func main() {
Expand All @@ -69,6 +55,20 @@ func main() {
usage(1, "ERROR: --query is required")
}

client, err := gh.RESTClient(nil)
if err != nil {
panic(err)
}
ghClient = client

repo, err := gh.CurrentRepository()
if err != nil {
panic(err)
}
fmt.Printf("Current repository is %s/%s\n", repo.Owner(), repo.Name())

currentRepo = repo

extensionLogger.Debugf("Dry-run mode: %t\n", dryRunFlag)

selectedPRs, err := fetchAndSelectPRs(interactiveFlag)
Expand Down Expand Up @@ -124,7 +124,9 @@ func main() {
panic(err)
}

branchName := fmt.Sprintf("%s-%s", combinedPRsBranchName, titlesHash(confirmedPRs))
var prTitle = combineTitles(confirmedPRs)

branchName := fmt.Sprintf("%s-%s", combinedPRsBranchName, titlesHash(prTitle))

err = createBranch(branchName, defaultBranch)
if err != nil {
Expand Down Expand Up @@ -162,7 +164,7 @@ func main() {
body += "\n" + relatedIssuesText
}

err = checkIfCreatePR(branchName, body)
err = checkIfCreatePR(branchName, prTitle, body)
if err != nil {
panic(err)
}
Expand All @@ -181,16 +183,9 @@ func defaultBranch() (string, error) {
}

// titlesHash returns a hash of the PR titles
func titlesHash(prs []PullRequest) string {
var titles []string
for _, pr := range prs {
titles = append(titles, pr.Title)
}

decoded := strings.Join(titles, "-")

func titlesHash(prTitle string) string {
h := fnv.New32a()
_, err := h.Write([]byte(decoded))
_, err := h.Write([]byte(prTitle))
if err != nil {
panic(err)
}
Expand Down
35 changes: 35 additions & 0 deletions title.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import "strings"

const (
bumpPrefix string = "chore(deps): bump"
udpatePrefix string = "chore(deps): update"
)

func combineTitles(prs []PullRequest) string {
if len(prs) == 1 {
return prs[0].Title
}

var title string

if strings.HasPrefix(prs[0].Title, bumpPrefix) {
title = bumpPrefix
} else if strings.HasPrefix(prs[0].Title, udpatePrefix) {
title = udpatePrefix
}

for i, pr := range prs {
t := strings.ReplaceAll(pr.Title, bumpPrefix, "")
t = strings.ReplaceAll(t, udpatePrefix, "")
t = strings.TrimSpace(t)

title += " " + t
if i < len(prs)-1 {
title += ","
}
}

return strings.TrimSpace(title)
}
93 changes: 93 additions & 0 deletions title_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package main

import "testing"

func TestCombineTitles(t *testing.T) {
tests := []struct {
name string
prs []PullRequest
want string
}{
{
name: "Dependabot: single PR",
prs: []PullRequest{
{
Title: "chore(deps): update baaz from 3.2.1 to 3.2.2 in /",
},
},
want: "chore(deps): update baaz from 3.2.1 to 3.2.2 in /",
},
{
name: "Non-Dependabot: single PR",
prs: []PullRequest{
{
Title: "feat: implement features",
},
},
want: "feat: implement features",
},
{
name: "Multiple dependabot PRs with bump",
prs: []PullRequest{
{
Title: "chore(deps): bump foo from 1.0.0 to 1.0.1 in /",
},
{
Title: "chore(deps): bump bar from 1.0.0 to 1.0.1 in /modules/bar",
},
{
Title: "chore(deps): update baaz from 3.2.1 to 3.2.2 in /",
},
{
Title: "chore(deps): bump faag from 1.0.0 to 1.0.1 in /foo",
},
},
want: "chore(deps): bump foo from 1.0.0 to 1.0.1 in /, bar from 1.0.0 to 1.0.1 in /modules/bar, baaz from 3.2.1 to 3.2.2 in /, faag from 1.0.0 to 1.0.1 in /foo",
},
{
name: "Multiple dependabot PRs with update",
prs: []PullRequest{
{
Title: "chore(deps): update foo from 1.0.0 to 1.0.1 in /",
},
{
Title: "chore(deps): bump bar from 1.0.0 to 1.0.1 in /modules/bar",
},
{
Title: "chore(deps): bump baaz from 3.2.1 to 3.2.2 in /",
},
{
Title: "chore(deps): bump faag from 1.0.0 to 1.0.1 in /foo",
},
},
want: "chore(deps): update foo from 1.0.0 to 1.0.1 in /, bar from 1.0.0 to 1.0.1 in /modules/bar, baaz from 3.2.1 to 3.2.2 in /, faag from 1.0.0 to 1.0.1 in /foo",
},
{
name: "Multiple PRs with bump",
prs: []PullRequest{
{
Title: "feat 1",
},
{
Title: "feat 2",
},
{
Title: "chore 1",
},
{
Title: "chore 2",
},
},
want: "feat 1, feat 2, chore 1, chore 2",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
title := combineTitles(tt.prs)
if title != tt.want {
t.Errorf("expected %q, got %q", tt.want, title)
}
})
}
}