Skip to content

Commit

Permalink
FTR-99: Refactor and add friendly doc generation output (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
frasdav authored Nov 7, 2023
1 parent 733e049 commit 92e03d4
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 99 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/frontierdigital/ranger
go 1.20

require (
github.com/frontierdigital/utils v0.0.7
github.com/frontierdigital/utils v0.0.8
github.com/google/uuid v1.1.2
github.com/libgit2/git2go/v34 v34.0.0
github.com/otiai10/copy v1.9.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7
github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/frankban/quicktest v1.14.3 h1:FJKSZTDHjyhriyC81FLQ0LY93eSai0ZyR/ZIkd3ZUKE=
github.com/frontierdigital/utils v0.0.7 h1:nNhFoM0YOF2OhuwVerbsHxHp0QMuOeztT9gs/Ui9VKA=
github.com/frontierdigital/utils v0.0.7/go.mod h1:iz+lbB9iGA9ZsVbber/tPosKC9HFTgKTDP8/UC6XxBY=
github.com/frontierdigital/utils v0.0.8 h1:zIvqu8KLX8yfzEu0qEaAO9+/cu7Da5p0zZdhcG21Ze4=
github.com/frontierdigital/utils v0.0.8/go.mod h1:iz+lbB9iGA9ZsVbber/tPosKC9HFTgKTDP8/UC6XxBY=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
Expand Down
77 changes: 38 additions & 39 deletions pkg/cmd/app/generate/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import (
"github.com/frontierdigital/ranger/pkg/core"
rfile "github.com/frontierdigital/ranger/pkg/util/file"
rtime "github.com/frontierdigital/ranger/pkg/util/time"
egit "github.com/frontierdigital/utils/git/external_git"
igit "github.com/frontierdigital/utils/git"
git "github.com/frontierdigital/utils/git/external_git"
"github.com/frontierdigital/utils/output"
)

Expand All @@ -24,8 +25,7 @@ type Workload struct {
Build string
}

func publish(ado *core.AzureDevOps) error {
repo := egit.NewGit(ado.WikiRepo.LocalPath)
func publish(ado *core.AzureDevOps, repoName string, repo interface{ igit.Git }) error {
hasChanges, err := repo.HasChanges()
if err != nil {
return err
Expand All @@ -38,14 +38,14 @@ func publish(ado *core.AzureDevOps) error {
}

us := rtime.GetUnixTimestamp()
branchName := "generate-docs-" + us
branchName := fmt.Sprintf("ranger/update/%s", us)

err = repo.Checkout(branchName, true)
if err != nil {
return err
}

commitMessage := "Initial Commit"
commitMessage := "Update docs."
_, err = repo.Commit(commitMessage)
if err != nil {
return err
Expand All @@ -56,9 +56,7 @@ func publish(ado *core.AzureDevOps) error {
return err
}

output.PrintlnfInfo("Pushed.")

prId, err := ado.CreatePullRequest(branchName, "Update docs "+us)
prId, err := ado.CreatePullRequest(repoName, branchName, "main", "Update docs")
if err != nil {
return err
}
Expand All @@ -68,7 +66,7 @@ func publish(ado *core.AzureDevOps) error {
return err
}

err = ado.SetPullRequestAutoComplete(prId, identityId)
err = ado.SetPullRequestAutoComplete(repoName, prId, identityId)
if err != nil {
return err
}
Expand All @@ -82,94 +80,95 @@ func createWorkLoadPages(workloads *[]core.Workload, localPath string) error {

err := rfile.Clear(orderPath)
if err != nil {
return errors.New("Could not reset order file")
return errors.New("could not reset order file")
}

for _, w := range *workloads {
fullPath := filepath.Join(localPath, "workloads", fmt.Sprintf("%s.md", w.Name))

err := rfile.CreateOrUpdate(fullPath, w.Readme, false)
if err != nil {
return errors.New("Could not create or update page")
return errors.New("could not create or update page")
}

err = rfile.CreateOrUpdate(orderPath, fmt.Sprintln(w.Name), true)
if err != nil {
return errors.New("Could not create or update orderfile")
return errors.New("could not create or update orderfile")
}
}

return nil
}

type WorkloadIndex struct {
Workloads []core.Workload
}

func createWorkloadIndex(workloads *[]core.Workload, localPath string) error {
wl := WorkloadIndex{
wl := core.WorkloadIndex{
Workloads: *workloads,
}
tmpl, err := template.New("workloadTemplate").Parse(workloadTemplate)
if err != nil {
return err
}
var f *os.File
f, err = os.Create(filepath.Join(localPath, "workloads.md"))
if err != nil {
panic(err)
}
err = tmpl.Execute(f, wl)

file, err := os.Create(filepath.Join(localPath, "workloads.md"))
if err != nil {
panic(err)
return err
}
err = f.Close()
defer file.Close()

err = tmpl.Execute(file, wl)
if err != nil {
panic(err)
return err
}

return nil
}

func (*Workload) GetTemplate() string {
return workloadTemplate
}

func GenerateDocs(config *core.Config, projectName string, organisationName string, repoName string, feedName string) error {
func GenerateDocs(config *core.Config, projectName string, organisationName string, wikiName string, feedName string) error {
ado := &core.AzureDevOps{
OrganisationName: organisationName,
ProjectName: projectName,
PAT: config.ADO.PAT,
WorkloadFeedName: feedName,
WikiRepoName: repoName,
}

workloads, err := ado.GetWorkloadInfo()
err := ado.CreateWikiIfNotExists(wikiName, config.Git.UserName, config.Git.UserEmail)
if err != nil {
return err
}

output.PrintlnfInfo("Created wiki '%s' (%s)", wikiName, ado.WikiRemoteUrl)

wikiRepo, err := git.NewClonedGit(ado.WikiRepoRemoteUrl, "x-oauth-basic", config.ADO.PAT, config.Git.UserEmail, config.Git.UserName)
if err != nil {
return err
}
defer os.RemoveAll(wikiRepo.GetRepositoryPath())

err = ado.CreateWikiIfNotExists(config.Git.UserName, config.Git.UserEmail)
workloads, err := ado.GetWorkloadInfo()
if err != nil {
return err
}

err = createWorkLoadPages(workloads, ado.WikiRepo.LocalPath)
output.PrintlnfInfo("Fetched workload info from feed '%s' (https://dev.azure.com/%s/%s/_artifacts/feed/%s)", feedName, organisationName, projectName, feedName)

err = createWorkLoadPages(workloads, wikiRepo.GetRepositoryPath())
if err != nil {
return err
}

err = createWorkloadIndex(workloads, ado.WikiRepo.LocalPath)
err = createWorkloadIndex(workloads, wikiRepo.GetRepositoryPath())
if err != nil {
return err
}

output.Println(ado.WikiRepo.LocalPath)
output.PrintlnInfo("Generated workload index and pages")

err = publish(ado)
err = publish(ado, wikiName, wikiRepo)
if err != nil {
return errors.New("Could not create or automerge PR")
return errors.New("could not create or automerge PR")
}

output.PrintlnfInfo("Published Wiki '%s' (%s)", wikiName, ado.WikiRemoteUrl)

return nil
}
16 changes: 2 additions & 14 deletions pkg/cmd/app/promote/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,11 @@ func PromoteSet(config *core.Config, projectName string, organisationName string
nextEnvironmentSetRepoName := fmt.Sprintf("%s-%s-set", nextEnvironment, sourceManifest.Set)
nextEnvironmentSetRepoUrl := fmt.Sprintf("https://dev.azure.com/%s/%s/_git/%s", organisationName, projectName, nextEnvironmentSetRepoName)

nextEnvironmentSetRepoPath, err := os.MkdirTemp("", "")
if err != nil {
return err
}
nextEnvironmentSetRepo := git.NewGit(nextEnvironmentSetRepoPath)
err = nextEnvironmentSetRepo.CloneOverHttp(nextEnvironmentSetRepoUrl, config.ADO.PAT, "x-oauth-basic")
if err != nil {
return err
}
err = nextEnvironmentSetRepo.SetConfig("user.email", config.Git.UserEmail)
if err != nil {
return err
}
err = nextEnvironmentSetRepo.SetConfig("user.name", config.Git.UserName)
nextEnvironmentSetRepo, err := git.NewClonedGit(nextEnvironmentSetRepoUrl, "x-oauth-basic", config.ADO.PAT, config.Git.UserEmail, config.Git.UserName)
if err != nil {
return err
}
defer os.RemoveAll(nextEnvironmentSetRepo.GetRepositoryPath())

output.PrintfInfo("Cloned target environment set repository '%s' (https://dev.azure.com/%s/%s/_git/%s)", nextEnvironmentSetRepoName, organisationName, projectName, nextEnvironmentSetRepoName)

Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/cli/generate/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func NewCmdGenerateDocs(config *core.Config) *cobra.Command {

cmd.Flags().StringVarP(&projectName, "project-name", "p", "", "Project name")
cmd.Flags().StringVarP(&orgName, "organisation-name", "o", "", "Organisation name")
cmd.Flags().StringVarP(&wikiName, "wiki-name", "w", "", "Repository name the stores the wiki docs")
cmd.Flags().StringVarP(&feedName, "feed-name", "f", "", "ADO artifact feed name")
cmd.Flags().StringVarP(&wikiName, "wiki-name", "w", "", "Wiki name")
cmd.Flags().StringVarP(&feedName, "feed-name", "f", "", "Artifact feed name")

return cmd
}
25 changes: 13 additions & 12 deletions pkg/core/azuredevops.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,43 @@ import (
"github.com/google/uuid"
)

func (ado *AzureDevOps) CreatePullRequest(branchName string, message string) (*int, error) {
func (ado *AzureDevOps) CreatePullRequest(repositoryName string, sourceBranchName string, targetBranchName string, title string) (*int, error) {
azureDevOps := azuredevops.NewAzureDevOps(ado.OrganisationName, ado.PAT)

pr, err := azureDevOps.CreatePullRequest(ado.ProjectName, ado.WikiRepoName, fmt.Sprintf("refs/heads/%s", branchName), "refs/heads/main", message)
pr, err := azureDevOps.CreatePullRequest(ado.ProjectName, repositoryName, fmt.Sprintf("refs/heads/%s", sourceBranchName), fmt.Sprintf("refs/heads/%s", targetBranchName), title)
if err != nil {
return nil, err
}

return pr.PullRequestId, nil
}

func (ado *AzureDevOps) SetPullRequestAutoComplete(pullRequestId *int, identityId *uuid.UUID) error {
func (ado *AzureDevOps) SetPullRequestAutoComplete(repositoryName string, pullRequestId *int, identityId *uuid.UUID) error {
azureDevOps := azuredevops.NewAzureDevOps(ado.OrganisationName, ado.PAT)
return azureDevOps.SetPullRequestAutoComplete(ado.ProjectName, ado.WikiRepoName, *pullRequestId, identityId)
return azureDevOps.SetPullRequestAutoComplete(ado.ProjectName, repositoryName, *pullRequestId, identityId)
}

func (ado *AzureDevOps) CreateWikiIfNotExists(gitUserName string, gitUserEmail string) error {
func (ado *AzureDevOps) CreateWikiIfNotExists(wikiName string, gitUserName string, gitUserEmail string) error {
azureDevOps := azuredevops.NewAzureDevOps(ado.OrganisationName, ado.PAT)
localPath, err := azureDevOps.CreateWikiIfNotExists(ado.ProjectName, ado.WikiRepoName, gitUserEmail, gitUserName, ado.PAT)

wiki, repo, err := azureDevOps.CreateWikiIfNotExists(ado.ProjectName, wikiName, gitUserEmail, gitUserName)
if err != nil {
return err
}
ado.WikiRepo = &GitRepository{
LocalPath: *localPath,
UserName: gitUserName,
UserEmail: gitUserEmail,
}
ado.WikiRemoteUrl = *wiki.RemoteUrl
ado.WikiRepoRemoteUrl = *repo.RemoteUrl

return nil
}

func (ado *AzureDevOps) GetIdentityId() (*uuid.UUID, error) {
azureDevOps := azuredevops.NewAzureDevOps(ado.OrganisationName, ado.PAT)

identityId, err := azureDevOps.GetIdentityId()
if err != nil {
return nil, err
}

return identityId, nil
}

Expand All @@ -58,7 +59,7 @@ func (ado *AzureDevOps) GetWorkloadInfo() (*[]Workload, error) {

for _, p := range *packages {
if len(*p.Versions) > 0 {
c, _ := azureDevOps.GetFileContent(ado.ProjectName, *p.Name, *(*p.Versions)[0].Version)
c, _ := azureDevOps.GetFileContent(ado.ProjectName, *p.Name, *(*p.Versions)[0].Version, "README.md")
workloads = append(workloads, Workload{
Name: *p.Name,
Version: *(*p.Versions)[0].Version,
Expand Down
1 change: 1 addition & 0 deletions pkg/core/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func (m *Manifest) Save() error {
if err != nil {
return err
}
defer file.Close()

_, err = fmt.Fprint(file, builder.String())
if err != nil {
Expand Down
22 changes: 10 additions & 12 deletions pkg/core/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ type ADOConfig struct {
}

type AzureDevOps struct {
OrganisationName string
ProjectName string
PAT string
WorkloadFeedName string
WikiRepoName string
WikiRepo *GitRepository
OrganisationName string
ProjectName string
PAT string
WikiRemoteUrl string
WikiRepoRemoteUrl string
WorkloadFeedName string
}

type Config struct {
Expand All @@ -32,12 +32,6 @@ type GitConfig struct {
UserName string `mapstructure:"UserName"`
}

type GitRepository struct {
LocalPath string
UserName string
UserEmail string
}

type Manifest struct {
Version int64 `yaml:"version"`
Environment string `yaml:"environment"`
Expand All @@ -54,6 +48,10 @@ type Workload struct {
Instances []*WorkloadInstance
}

type WorkloadIndex struct {
Workloads []Workload
}

type WorkloadInstance struct {
ExtraParameters []ExtraParameter `yaml:"extraParameters"`
Name string `yaml:"name"`
Expand Down
14 changes: 12 additions & 2 deletions pkg/core/workload_result.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,22 @@ func (d *WorkloadResult) PrintResult() {
elasped = d.FinishTime.Sub(*d.QueueTime)
}

var queueTime string
if d.QueueTime != nil {
queueTime = d.QueueTime.Format(time.RFC1123)
}

var finishTime string
if d.QueueTime != nil {
finishTime = d.FinishTime.Format(time.RFC1123)
}

builder := &strings.Builder{}
builder.WriteString(fmt.Sprintf("%s\n", str.Repeat("-", 78)))
builder.WriteString(fmt.Sprintf("Result | %s\n", result))
builder.WriteString(fmt.Sprintf("Link | %s\n", d.Link))
builder.WriteString(fmt.Sprintf("Queued | %s\n", d.QueueTime.Format(time.RFC1123)))
builder.WriteString(fmt.Sprintf("Finished | %s\n", d.FinishTime.Format(time.RFC1123)))
builder.WriteString(fmt.Sprintf("Queued | %s\n", queueTime))
builder.WriteString(fmt.Sprintf("Finished | %s\n", finishTime))
builder.WriteString(fmt.Sprintf("Elapsed | %s\n", elasped))
builder.WriteString(fmt.Sprintf("%s\n", strings.Repeat("=", 78)))
output.Println(builder.String())
Expand Down
Loading

0 comments on commit 92e03d4

Please sign in to comment.