Skip to content

Commit

Permalink
fix: publish pages fix
Browse files Browse the repository at this point in the history
* Corrected the logic flow in publish so that short names are always generated for links.
* Also added ability to set an account name as this allows for users in an org to run the app.
  • Loading branch information
drew-viles authored Dec 6, 2022
1 parent 113692f commit 0d3e3a8
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
1 change: 1 addition & 0 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ var (
// Publish
ghUserFlag string
ghProjectFlag string
ghAccountFlag string
ghTokenFlag string
ghPagesBranchFlag string
)
10 changes: 8 additions & 2 deletions cmd/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ GitHub Pages and view the report there instead in a slightly nicer format.
The website it generates isn't the prettiest right now but it will be improved on over time.`,

Run: func(cmd *cobra.Command, args []string) {
// just setting defaults for account if it's not provided. Presume it's the same as the username.
if viper.GetString("publish.github.account") == "" {
viper.Set("publish.github.account", viper.GetString("publish.github.user"))
}

cloudsConfig := ostack.InitOpenstack()
cloudsConfig.SetOpenstackEnvs()

Expand All @@ -43,7 +48,7 @@ The website it generates isn't the prettiest right now but it will be improved o
}
osClient.OpenstackInit()

pagesGitDir, pagesRepo, err := publish.FetchPagesRepo(viper.GetString("publish.github.user"), viper.GetString("publish.github.token"), viper.GetString("publish.github.project"), viper.GetString("publish.github.pages-branch"))
pagesGitDir, pagesRepo, err := publish.FetchPagesRepo(viper.GetString("publish.github.user"), viper.GetString("publish.github.token"), viper.GetString("publish.github.account"), viper.GetString("publish.github.project"), viper.GetString("publish.github.pages-branch"))
if err != nil {
log.Fatalln(err)
}
Expand Down Expand Up @@ -79,6 +84,7 @@ The website it generates isn't the prettiest right now but it will be improved o

cmd.Flags().StringVar(&ghUserFlag, "github-user", "", "The user for the GitHub project to which the pages will be pushed")
cmd.Flags().StringVar(&ghProjectFlag, "github-project", "", "The GitHub project to which the pages will be pushed")
cmd.Flags().StringVar(&ghAccountFlag, "github-account", "", "The account in which the project is stored. This will default to the user")
cmd.Flags().StringVar(&ghTokenFlag, "github-token", "", "The token for the GitHub project to which the pages will be pushed")
cmd.Flags().StringVar(&ghPagesBranchFlag, "github-pages-branch", "gh-pages", "The branch name for GitHub project to which the pages will be pushed")
cmd.Flags().StringVar(&imageIDFlag, "image-id", "", "The ID of the image to scan")
Expand All @@ -88,11 +94,11 @@ The website it generates isn't the prettiest right now but it will be improved o
bindViper(cmd, "publish.image-id", "image-id")
bindViper(cmd, "publish.github.user", "github-user")
bindViper(cmd, "publish.github.project", "github-project")
bindViper(cmd, "publish.github.account", "github-account")
bindViper(cmd, "publish.github.token", "github-token")
bindViper(cmd, "publish.github.pages-branch", "github-pages-branch")

return cmd

}

// checkErrorPagesWithCleanup takes an error and if it is not nil, will attempt to run a cleanup to ensure no resources are left lying around.
Expand Down
15 changes: 7 additions & 8 deletions cmd/publish/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ import (
var content embed.FS

// FetchPagesRepo pulls the GitHub pages repo locally for modification.
func FetchPagesRepo(ghUser, ghToken, ghProject, ghBranch string) (string, *git.Repository, error) {
pagesRepo := fmt.Sprintf("https://%s:%s@github.com/%s/%s.git", ghUser, ghToken, ghUser, ghProject)
func FetchPagesRepo(ghUser, ghToken, ghAccount, ghProject, ghBranch string) (string, *git.Repository, error) {
pagesRepo := fmt.Sprintf("https://%s:%s@github.com/%s/%s.git", ghUser, ghToken, ghAccount, ghProject)
pagesDir := filepath.Join("/tmp", ghProject)

err := os.MkdirAll(pagesDir, 0755)
Expand Down Expand Up @@ -146,9 +146,6 @@ func ParseReports(reports []string, img *Image) (map[int]constants.Year, error)
}

stripDirPrefix := strings.Split(v, "/")

reportName := stripDirPrefix[len(stripDirPrefix)-1:][0]

fileName := strings.Split(img.CreatedAt, "-")

year, err := strconv.Atoi(fileName[0])
Expand All @@ -172,12 +169,14 @@ func ParseReports(reports []string, img *Image) (map[int]constants.Year, error)
allReports[year] = y
}

reportName := stripDirPrefix[len(stripDirPrefix)-1:][0]
nameSplit := strings.Split(reportName, "-")
shortName := nameSplit[:len(nameSplit)-1]
r.ShortName = strings.Join(shortName, "-")

if allReports[year].Months[monthName].Reports == nil {
m := allReports[year].Months[monthName]
m.Reports = make(map[string]constants.ReportData)
nameSplit := strings.Split(reportName, "-")
shortName := nameSplit[:len(nameSplit)-1]
r.ShortName = strings.Join(shortName, "-")
m.Reports[reportName] = r
allReports[year].Months[monthName] = m
} else {
Expand Down

0 comments on commit 0d3e3a8

Please sign in to comment.