Skip to content

Commit

Permalink
Fix changelog pr (#26)
Browse files Browse the repository at this point in the history
* Small fixes

(cherry picked from commit 823968ee38758d2efe3a65b45a269aae3899bb4c)

* Refactor
  • Loading branch information
Rick Ducott authored Feb 28, 2019
1 parent 594125e commit 92711e9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 22 deletions.
4 changes: 4 additions & 0 deletions changelogutils/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ func ReadChangelogFile(fs afero.Fs, path string) (*ChangelogFile, error) {
return &changelog, nil
}

func ChangelogDirExists(fs afero.Fs, changelogParentPath string) (bool, error) {
return afero.Exists(fs, filepath.Join(changelogParentPath, ChangelogDirectory))
}

func ComputeChangelogForTag(fs afero.Fs, tag, changelogParentPath string) (*Changelog, error) {
version, err := versionutils.ParseVersion(tag)
if err != nil {
Expand Down
73 changes: 51 additions & 22 deletions docsutils/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ CreateDocsPR("solo-io", "gloo", "gloo", "gloo", "v0.8.2",
"docs/v1/gogoproto",
"docs/v1/google")
*/
func CreateDocsPR(owner, repo, product, project, tag string, paths ...string) error {
func CreateDocsPR(owner, repo, product, project, tag string, apiPaths ...string) error {
ctx := context.TODO()
fs := afero.NewOsFs()
exists, err := afero.Exists(fs, DocsRepo)
Expand All @@ -46,37 +46,41 @@ func CreateDocsPR(owner, repo, product, project, tag string, paths ...string) er
if exists {
return errors.Errorf("Cannot clone because %s already exists", DocsRepo)
}

// setup repo
err = gitCloneDocs()
defer fs.RemoveAll(DocsRepo)
if err != nil {
return errors.Wrapf(err, "Error cloning repo")
}

client, err := githubutils.GetClient(ctx)
if err != nil {
return err
}
changelog, err := changelogutils.ComputeChangelogForTag(fs, tag, "")
// setup branch
branch := repo + "-docs-" + tag + "-" + testutils.RandString(4)
err = gitCheckoutNewBranch(branch)
if err != nil {
return err
return errors.Wrapf(err, "Error checking out branch")
}
markdown := changelogutils.GenerateChangelogMarkdown(changelog)
fmt.Printf(markdown)
err = updateChangelogFile(fs, product, project, markdown, tag)

// update changelog if "changelog" directory exists in this repo
err = updateChangelogIfNecessary(fs, tag, product, project)
if err != nil {
return err
}

branch := repo + "-docs-" + tag + testutils.RandString(4)
err = gitCheckoutNewBranch(branch)
if err != nil {
return errors.Wrapf(err, "Error checking out branch")
}
err = replaceDirectories(product, paths...)
// replaceDirectories("gloo", "docs/v1") updates replaces contents of "solo-docs/gloo/docs/v1" with what's in "docs/v1"
err = replaceApiDirectories(product, apiPaths...)
if err != nil {
return errors.Wrapf(err, "Error removing old docs")
}
err = gitAddAll()

// see if there is something to commit, push and open PR if so
return submitPRIfChanges(ctx, owner, branch, tag, product)

return nil
}

func submitPRIfChanges(ctx context.Context, owner, branch, tag, product string) error {
err := gitAddAll()
if err != nil {
return errors.Wrapf(err, "Error doing git add")
}
Expand All @@ -88,11 +92,15 @@ func CreateDocsPR(owner, repo, product, project, tag string, paths ...string) er
// no diff, exit early cause we're done
return nil
}

err = gitCommit(tag)
if err != nil {
return errors.Wrapf(err, "Error doing git commit")
}
// make sure we can get the client before starting to push
client, err := githubutils.GetClient(ctx)
if err != nil {
return err
}
err = gitPush(branch)
if err != nil {
return errors.Wrapf(err, "Error pushing docs branch")
Expand All @@ -107,13 +115,34 @@ func CreateDocsPR(owner, repo, product, project, tag string, paths ...string) er
Head: &branch,
Base: &base,
}

_, _, err = client.PullRequests.Create(ctx, owner, DocsRepo, &pr)
if err != nil {
return errors.Wrapf(err, "Error creating PR")
}
return nil
}

func updateChangelogIfNecessary(fs afero.Fs, tag, product, project string) error {
exists, err := changelogutils.ChangelogDirExists(fs, "")
if err != nil {
return errors.Wrapf(err, "Error checking for changelog dir")
}
if exists {
changelog, err := changelogutils.ComputeChangelogForTag(fs, tag, "")
if err != nil {
return err
}
markdown := changelogutils.GenerateChangelogMarkdown(changelog)
fmt.Printf(markdown)
err = updateChangelogFile(fs, product, project, markdown, tag)
if err != nil {
return err
}
}
return nil
}

func getChangelogDir(product string) string {
return filepath.Join(DocsRepo, product, "docs", "changelog")
}
Expand All @@ -127,7 +156,7 @@ func getChangelogFile(product, project string) string {
func updateChangelogFile(fs afero.Fs, product, project, markdown, tag string) error {
changelogDir := getChangelogDir(product)
changelogFile := getChangelogFile(product, project)
newContents := fmt.Sprintf("### %s\n\n%s", tag, markdown)
newContents := fmt.Sprintf("### %s\n\n%s\n\n", tag, markdown)
exists, err := afero.Exists(fs, changelogFile)
if err != nil {
return err
Expand Down Expand Up @@ -186,7 +215,7 @@ func gitPush(branch string) error {
return execGit(DocsRepo, "push", "origin", branch)
}

func prepareCmd(dir string, args ...string) *exec.Cmd {
func prepareGitCmd(dir string, args ...string) *exec.Cmd {
cmd := exec.Command("git", args...)
logger.Debugf("git %v", cmd.Args)
cmd.Env = os.Environ()
Expand All @@ -198,7 +227,7 @@ func prepareCmd(dir string, args ...string) *exec.Cmd {
}

func execGit(dir string, args ...string) error {
cmd := prepareCmd(dir, args...)
cmd := prepareGitCmd(dir, args...)
return cmd.Run()
}

Expand All @@ -212,7 +241,7 @@ func execGitWithOutput(dir string, args ...string) (string, error) {
return string(output), nil
}

func replaceDirectories(product string, paths ...string) error {
func replaceApiDirectories(product string, paths ...string) error {
fs := afero.NewOsFs()
for _, path := range paths {
soloDocsPath := filepath.Join(DocsRepo, product, path)
Expand Down

0 comments on commit 92711e9

Please sign in to comment.