diff --git a/changelogutils/changelog.go b/changelogutils/changelog.go index dfb94f05..32c86d6b 100644 --- a/changelogutils/changelog.go +++ b/changelogutils/changelog.go @@ -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 { diff --git a/docsutils/cli.go b/docsutils/cli.go index 2a1ceb31..fa26c1a6 100644 --- a/docsutils/cli.go +++ b/docsutils/cli.go @@ -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) @@ -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") } @@ -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") @@ -107,6 +115,7 @@ 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") @@ -114,6 +123,26 @@ func CreateDocsPR(owner, repo, product, project, tag string, paths ...string) er 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") } @@ -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 @@ -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() @@ -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() } @@ -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)