Skip to content

Commit

Permalink
Add support for fetching last modification time information of a conf…
Browse files Browse the repository at this point in the history
…ig repostitory
  • Loading branch information
nikhilsbhat committed Sep 12, 2023
1 parent e448c69 commit 0825f02
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 10 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ linters:
- goconst
- maligned
- depguard
- tagalign

issues:
exclude-rules:
Expand Down
58 changes: 56 additions & 2 deletions cmd/config_repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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)

Expand All @@ -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())

Expand Down Expand Up @@ -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
}
9 changes: 5 additions & 4 deletions docs/doc/gocd-cli_configrepo_get-internal.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
9 changes: 5 additions & 4 deletions docs/doc/gocd-cli_pipeline_vsm.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 0825f02

Please sign in to comment.