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

Prompt for unchanged or empty PR description #130

Closed
wants to merge 10 commits into from
Closed
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
12 changes: 12 additions & 0 deletions cmd/create_prs/create_prs.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package create_prs

import (
"fmt"
"github.com/skyscanner/turbolift/internal/prompt"
"os"
"path"
"time"
Expand All @@ -32,6 +34,7 @@ import (
var (
gh github.GitHub = github.NewRealGitHub()
g git.Git = git.NewRealGit()
p prompt.Prompt = prompt.NewRealPrompt()
)

var (
Expand Down Expand Up @@ -68,6 +71,15 @@ func run(c *cobra.Command, _ []string) {
readCampaignActivity.EndWithFailure(err)
return
}
prDescriptionUnchanged, err := campaign.PrDescriptionUnchanged(dir)
Copy link
Collaborator

@rnorth rnorth Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we perhaps rename the function to IsPrDescriptionUnchanged? Unless there is any precedent in other boolean-returning functions?

if err != nil {
logger.Warnf("unable to verify whether PR description has been updated: %s", err)
}
if prDescriptionUnchanged {
if !p.AskConfirm(fmt.Sprintf("It looks like the PR title and/or description has not been updated in %s. Are you sure you want to proceed?", prDescriptionFile)) {
return
}
}
readCampaignActivity.EndWithSuccess()

doneCount := 0
Expand Down
176 changes: 176 additions & 0 deletions cmd/create_prs/create_prs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,105 @@ package create_prs

import (
"bytes"
"github.com/skyscanner/turbolift/internal/campaign"
"github.com/skyscanner/turbolift/internal/git"
"github.com/skyscanner/turbolift/internal/github"
"github.com/skyscanner/turbolift/internal/prompt"
"github.com/skyscanner/turbolift/internal/testsupport"
"github.com/stretchr/testify/assert"
"os"
"path/filepath"
"strings"
"testing"
)

func TestItWarnsIfDescriptionFileTemplateIsUnchanged(t *testing.T) {
fakeGitHub := github.NewAlwaysFailsFakeGitHub()
gh = fakeGitHub
fakeGit := git.NewAlwaysSucceedsFakeGit()
g = fakeGit
fakePrompt := prompt.NewFakePromptNo()
p = fakePrompt

dir := testsupport.PrepareTempCampaign(true, "org/repo1", "org/repo2")
useDefaultPrDescription(filepath.Base(dir))

out, err := runCommand()
assert.NoError(t, err)
assert.NotContains(t, out, "Creating PR in org/repo1")
assert.NotContains(t, out, "Creating PR in org/repo2")
assert.NotContains(t, out, "turbolift create-prs completed")
assert.NotContains(t, out, "2 OK, 0 skipped")

fakePrompt.AssertCalledWith(t, "It looks like the PR title and/or description has not been updated in README.md. Are you sure you want to proceed?")
}

func TestItWarnsIfPrTitleIsUpdatedButNotPrBody(t *testing.T) {
fakeGitHub := github.NewAlwaysFailsFakeGitHub()
gh = fakeGitHub
fakeGit := git.NewAlwaysSucceedsFakeGit()
g = fakeGit
fakePrompt := prompt.NewFakePromptNo()
p = fakePrompt

dir := testsupport.PrepareTempCampaign(true, "org/repo1", "org/repo2")
useDefaultPrBodyOnly(filepath.Base(dir))

out, err := runCommand()
assert.NoError(t, err)
assert.NotContains(t, out, "Creating PR in org/repo1")
assert.NotContains(t, out, "Creating PR in org/repo2")
assert.NotContains(t, out, "turbolift create-prs completed")
assert.NotContains(t, out, "2 OK, 0 skipped")

fakePrompt.AssertCalledWith(t, "It looks like the PR title and/or description has not been updated in README.md. Are you sure you want to proceed?")
}

func TestItWarnsIfPrBodyIsUpdatedButNotPrTitle(t *testing.T) {
fakeGitHub := github.NewAlwaysFailsFakeGitHub()
gh = fakeGitHub
fakeGit := git.NewAlwaysSucceedsFakeGit()
g = fakeGit
fakePrompt := prompt.NewFakePromptNo()
p = fakePrompt

dir := testsupport.PrepareTempCampaign(true, "org/repo1", "org/repo2")
// dir is in the format /var/.../.../turbolift-test-XXXXXX
useDefaultPrTitleOnly(filepath.Base(dir))

out, err := runCommand()
assert.NoError(t, err)
assert.NotContains(t, out, "Creating PR in org/repo1")
assert.NotContains(t, out, "Creating PR in org/repo2")
assert.NotContains(t, out, "turbolift create-prs completed")
assert.NotContains(t, out, "2 OK, 0 skipped")

fakePrompt.AssertCalledWith(t, "It looks like the PR title and/or description has not been updated in README.md. Are you sure you want to proceed?")
}

func TestItWarnsIfDescriptionFileIsEmpty(t *testing.T) {
fakeGitHub := github.NewAlwaysFailsFakeGitHub()
gh = fakeGitHub
fakeGit := git.NewAlwaysSucceedsFakeGit()
g = fakeGit
fakePrompt := prompt.NewFakePromptNo()
p = fakePrompt

customDescriptionFileName := "custom.md"

testsupport.PrepareTempCampaign(true, "org/repo1", "org/repo2")
testsupport.CreateOrUpdatePrDescriptionFile(customDescriptionFileName, "", "")

out, err := runCommandWithAlternativeDescriptionFile(customDescriptionFileName)
assert.NoError(t, err)
assert.NotContains(t, out, "Creating PR in org/repo1")
assert.NotContains(t, out, "Creating PR in org/repo2")
assert.NotContains(t, out, "turbolift create-prs completed")
assert.NotContains(t, out, "2 OK, 0 skipped")

fakePrompt.AssertCalledWith(t, "It looks like the PR title and/or description has not been updated in custom.md. Are you sure you want to proceed?")
}

func TestItLogsCreatePrErrorsButContinuesToTryAll(t *testing.T) {
fakeGitHub := github.NewAlwaysFailsFakeGitHub()
gh = fakeGitHub
Expand Down Expand Up @@ -106,6 +198,31 @@ func TestItLogsCreateDraftPr(t *testing.T) {
})
}

func TestItCreatesPrsFromAlternativeDescriptionFile(t *testing.T) {
fakeGitHub := github.NewAlwaysSucceedsFakeGitHub()
gh = fakeGitHub
fakeGit := git.NewAlwaysSucceedsFakeGit()
g = fakeGit

customDescriptionFileName := "custom.md"

testsupport.PrepareTempCampaign(true, "org/repo1", "org/repo2")
testsupport.CreateOrUpdatePrDescriptionFile(customDescriptionFileName, "custom PR title", "custom PR body")

out, err := runCommandWithAlternativeDescriptionFile(customDescriptionFileName)
assert.NoError(t, err)
assert.Contains(t, out, "Reading campaign data (repos.txt, custom.md)")
assert.Contains(t, out, "Creating PR in org/repo1")
assert.Contains(t, out, "Creating PR in org/repo2")
assert.Contains(t, out, "turbolift create-prs completed")
assert.Contains(t, out, "2 OK, 0 skipped")

fakeGitHub.AssertCalledWith(t, [][]string{
{"work/org/repo1", "custom PR title"},
{"work/org/repo2", "custom PR title"},
})
}

func runCommand() (string, error) {
cmd := NewCreatePRsCmd()
outBuffer := bytes.NewBufferString("")
Expand All @@ -118,6 +235,19 @@ func runCommand() (string, error) {
return outBuffer.String(), nil
}

func runCommandWithAlternativeDescriptionFile(fileName string) (string, error) {
cmd := NewCreatePRsCmd()
prDescriptionFile = fileName
outBuffer := bytes.NewBufferString("")
cmd.SetOut(outBuffer)
err := cmd.Execute()

if err != nil {
return outBuffer.String(), err
}
return outBuffer.String(), nil
}

func runCommandDraft() (string, error) {
cmd := NewCreatePRsCmd()
isDraft = true
Expand All @@ -130,3 +260,49 @@ func runCommandDraft() (string, error) {
}
return outBuffer.String(), nil
}

func useDefaultPrDescription(dirName string) {
err := campaign.ApplyReadMeTemplate("README.md", dirName)
if err != nil {
panic(err)
}
}

func useDefaultPrTitleOnly(dirName string) {
useDefaultPrDescription(dirName)
// append some text to change the pr description body
f, err := os.OpenFile("README.md", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
panic(err)
}
defer func(f *os.File) {
err := f.Close()
if err != nil {
panic(err)
}
}(f)
_, err = f.WriteString("additional pr description")
if err != nil {
panic(err)
}
}

func useDefaultPrBodyOnly(dirName string) {
useDefaultPrDescription(dirName)
// append something to first line to change title
fileName := "README.md"
content, err := os.ReadFile(fileName)
if err != nil {
panic(err)
}

lines := strings.Split(string(content), "\n")
lines[0] += "updated title"

newContent := strings.Join(lines, "\n")

err = os.WriteFile(fileName, []byte(newContent), 0644)
if err != nil {
panic(err)
}
}
63 changes: 6 additions & 57 deletions cmd/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ package init

import (
_ "embed"
"fmt"
"html/template"
"github.com/skyscanner/turbolift/internal/campaign"
"os"
"path/filepath"

Expand All @@ -27,25 +26,7 @@ import (
"github.com/spf13/cobra"
)

var (
campaignName string

//go:embed templates/.gitignore
gitignoreTemplate string

//go:embed templates/.turbolift
turboliftTemplate string

//go:embed templates/README.md
readmeTemplate string

//go:embed templates/repos.txt
reposTemplate string
)

type TemplateVariables struct {
CampaignName string
}
var campaignName string

func NewInitCmd() *cobra.Command {
cmd := &cobra.Command{
Expand Down Expand Up @@ -75,48 +56,16 @@ func run(c *cobra.Command, _ []string) {
}

createFilesActivity := logger.StartActivity("Creating initial files")
data := TemplateVariables{
CampaignName: campaignName,
}

files := map[string]string{
".gitignore": gitignoreTemplate,
".turbolift": turboliftTemplate,
"README.md": readmeTemplate,
"repos.txt": reposTemplate,
}
for filename, templateFile := range files {
err := applyTemplate(filepath.Join(campaignName, filename), templateFile, data)
if err != nil {
createFilesActivity.EndWithFailure(err)
return
}
err = campaign.CreateInitialFiles(campaignName)
if err != nil {
createFilesActivity.EndWithFailure(err)
}

createFilesActivity.EndWithSuccess()

logger.Successf("turbolift init is done - next:\n")
logger.Println("\t1. Run", colors.Cyan("cd ", campaignName))
logger.Println("\t2. Update", colors.Cyan("repos.txt"), "with the names of the repos that need changing (either manually or using a tool to generate a list of repos)")
logger.Println("\t3. Run", colors.Cyan("turbolift clone"))
}

// Applies a given template and data to produce a file with the outputFilename
func applyTemplate(outputFilename string, templateContent string, data interface{}) error {
readme, err := os.Create(outputFilename)
if err != nil {
return fmt.Errorf("unable to open file for output: %w", err)
}

parsedTemplate, err := template.New("").Parse(templateContent)

if err != nil {
return fmt.Errorf("unable to parse template: %w", err)
}

err = parsedTemplate.Execute(readme, data)

if err != nil {
return fmt.Errorf("unable to write templated file: %w", err)
}
return nil
}
Loading
Loading