From 0825f024381c9eaab6459541bc4956e430c2a955 Mon Sep 17 00:00:00 2001 From: nikhilsbhat Date: Tue, 12 Sep 2023 12:02:47 +0530 Subject: [PATCH] Add support for fetching last modification time information of a config repostitory --- .golangci.yml | 1 + cmd/config_repos.go | 58 +++++++++++++++++++- docs/doc/gocd-cli_configrepo_get-internal.md | 9 +-- docs/doc/gocd-cli_pipeline_vsm.md | 9 +-- 4 files changed, 67 insertions(+), 10 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index a77bfe2..c540557 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -21,6 +21,7 @@ linters: - goconst - maligned - depguard + - tagalign issues: exclude-rules: diff --git a/cmd/config_repos.go b/cmd/config_repos.go index fe9fbfd..bd85687 100644 --- a/cmd/config_repos.go +++ b/cmd/config_repos.go @@ -3,7 +3,9 @@ package cmd import ( "encoding/json" "fmt" + "log" "strings" + "time" "github.com/nikhilsbhat/gocd-cli/pkg/errors" "github.com/nikhilsbhat/gocd-cli/pkg/render" @@ -92,8 +94,17 @@ func getConfigReposCommand() *cobra.Command { } func getFailedConfigReposCommand() *cobra.Command { - var configRepoNames bool - var configRepoFailed bool + var ( + configRepoNames bool + configRepoFailed bool + getLastModified bool + ) + + type configRepoLastModified struct { + LastModified float64 `json:"lastModified,omitempty" yaml:"lastModified,omitempty"` + Name string `json:"name,omitempty" yaml:"name,omitempty"` + URL string `json:"url,omitempty" yaml:"url,omitempty"` + } configGetCommand := &cobra.Command{ Use: "get-internal", @@ -125,6 +136,25 @@ Do not use this command unless you know what you are doing with it`, repos = response } + if getLastModified { + configRepo := make([]configRepoLastModified, 0) + for _, cfgRepo := range response { + modificationTime := cfgRepo.ConfigRepoParseInfo.LatestParsedModification["modified_time"] + if modificationTime != nil { + modifiedDate := modificationTime.(string) + configRepo = append(configRepo, configRepoLastModified{ + LastModified: lastUpdatedCommit(modifiedDate), + Name: cfgRepo.ID, + URL: cfgRepo.Material.Attributes.URL, + }) + } else { + cliLogger.Debugf("looks like config repo '%s' was never parsed, check the status of it", cfgRepo.ID) + } + } + + return cliRenderer.Render(configRepo) + } + if len(jsonQuery) != 0 { cliLogger.Debugf(queryEnabledMessage, jsonQuery) @@ -146,6 +176,9 @@ Do not use this command unless you know what you are doing with it`, "list of config repo name those are failing") configGetCommand.PersistentFlags().BoolVarP(&configRepoFailed, "failed", "", false, "when enabled, fetches only the failed config repositories") + configGetCommand.PersistentFlags().BoolVarP(&getLastModified, "last-modified", "", false, + "list config repo with last modified in number of days") + configGetCommand.MarkFlagsMutuallyExclusive("last-modified", "names") configGetCommand.SetUsageTemplate(getUsageTemplate()) @@ -472,3 +505,24 @@ func (cfg *goCdPlugin) getPluginID() string { return cfg.pluginID } + +func lastUpdatedCommit(date string) float64 { + const hoursInADay = 24 + + loc, err := time.LoadLocation("Asia/Kolkata") + if err != nil { + log.Fatalln(err) + } + + tm, err := time.ParseInLocation(time.RFC3339, date, loc) + if err != nil { + log.Fatalln(err) + } + parsedTime := tm.In(loc) + + timeNow := time.Now().In(loc) + + diff := timeNow.Sub(parsedTime).Hours() / hoursInADay + + return diff +} diff --git a/docs/doc/gocd-cli_configrepo_get-internal.md b/docs/doc/gocd-cli_configrepo_get-internal.md index c23089a..8ca537d 100644 --- a/docs/doc/gocd-cli_configrepo_get-internal.md +++ b/docs/doc/gocd-cli_configrepo_get-internal.md @@ -10,9 +10,10 @@ gocd-cli configrepo get-internal [flags] ### Options ``` - --failed when enabled, fetches only the failed config repositories - -h, --help help for get-internal - --names list of config repo name those are failing + --failed when enabled, fetches only the failed config repositories + -h, --help help for get-internal + --last-modified list config repo with last modified in number of days + --names list of config repo name those are failing ``` ### Options inherited from parent commands @@ -38,4 +39,4 @@ gocd-cli configrepo get-internal [flags] * [gocd-cli configrepo](gocd-cli_configrepo.md) - Command to operate on configrepo present in GoCD [https://api.gocd.org/current/#config-repo] -###### Auto generated by spf13/cobra on 11-Sep-2023 +###### Auto generated by spf13/cobra on 12-Sep-2023 diff --git a/docs/doc/gocd-cli_pipeline_vsm.md b/docs/doc/gocd-cli_pipeline_vsm.md index 8fb0d9b..f9f2479 100644 --- a/docs/doc/gocd-cli_pipeline_vsm.md +++ b/docs/doc/gocd-cli_pipeline_vsm.md @@ -15,9 +15,10 @@ gocd-cli pipeline get sample-pipeline --query "[*] | name eq sample-group" ### Options ``` - --down-stream when enabled, will fetch all downstream pipelines of a specified pipeline. - -h, --help help for vsm - --up-stream when enabled, will fetch all upstream pipelines of a specified pipeline. (NOTE: flag up-stream is still in experimental phase) + --down-stream when enabled, will fetch all downstream pipelines of a specified pipeline. + -h, --help help for vsm + --pipeline strings name of the pipeline for which the VSM has to be retrieved + --up-stream when enabled, will fetch all upstream pipelines of a specified pipeline. (NOTE: flag up-stream is still in experimental phase) ``` ### Options inherited from parent commands @@ -43,4 +44,4 @@ gocd-cli pipeline get sample-pipeline --query "[*] | name eq sample-group" * [gocd-cli pipeline](gocd-cli_pipeline.md) - Command to operate on pipelines present in GoCD -###### Auto generated by spf13/cobra on 11-Sep-2023 +###### Auto generated by spf13/cobra on 12-Sep-2023