Skip to content

Commit b89f5d1

Browse files
author
Alvaro Muñoz
committed
Fix info command
1 parent 79ec676 commit b89f5d1

File tree

2 files changed

+41
-8
lines changed

2 files changed

+41
-8
lines changed

cmd/info.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"log"
77
"os"
88
"path/filepath"
9+
"strings"
910

1011
"github.com/GitHubSecurityLab/gh-qldb/utils"
1112
"github.com/spf13/cobra"
@@ -30,23 +31,42 @@ func init() {
3031
}
3132

3233
func info(nwo string, language string) {
33-
dir := filepath.Join(utils.GetPath(nwo), language)
34-
files, err := os.ReadDir(dir)
34+
dir := utils.GetPath(nwo)
35+
entries, err := os.ReadDir(dir)
3536
if err != nil {
3637
log.Fatal(err)
3738
}
3839
var pathList []map[string]string
39-
for _, f := range files {
40-
filename := f.Name()
41-
sha := filename[:len(filename)-len(filepath.Ext(filename))]
42-
commitSha, committedDate, err := utils.GetCommitInfo(nwo, sha)
40+
for _, e := range entries {
41+
entryName := e.Name()
42+
var name string
43+
if filepath.Ext(entryName) == ".zip" {
44+
// remove the .zip extension if it exists
45+
name = entryName[:len(entryName)-len(filepath.Ext(entryName))]
46+
} else if e.IsDir() {
47+
name = e.Name()
48+
} else {
49+
continue
50+
}
51+
52+
// split the name by the "-". first element is the language, second is the short commit sha
53+
nameSplit := strings.Split(name, "-")
54+
if len(nameSplit) != 2 {
55+
log.Fatal(fmt.Errorf("invalid database name: %s", name))
56+
}
57+
if nameSplit[0] != language {
58+
continue
59+
}
60+
shortSha := nameSplit[1]
61+
62+
commitSha, committedDate, err := utils.GetCommitInfo2(nwo, shortSha)
4363
if err != nil {
4464
log.Fatal(err)
4565
}
4666
pathList = append(pathList, map[string]string{
4767
"commitSha": commitSha,
4868
"committedDate": committedDate,
49-
"path": filepath.Join(dir, filename),
69+
"path": filepath.Join(dir, entryName),
5070
})
5171
}
5272
if jsonFlag {

utils/utils.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ func ZipDirectory(zipFileName string, directoryToZip string) error {
148148
return err
149149
}
150150
if !info.IsDir() {
151-
return errors.New("directoryToZip is not a directory")
151+
return errors.New(directoryToZip + " is not a directory")
152152
}
153153

154154
// Create a new zip file
@@ -244,3 +244,16 @@ func GetCommitInfo(nwo string, commitSha string) (string, string, error) {
244244
}
245245
return string(query.Repository.Object.Commit.AbbreviatedOid), query.Repository.Object.Commit.CommittedDate.Format("2006-01-02T15:04:05"), nil
246246
}
247+
248+
func GetCommitInfo2(nwo string, commitSha string) (string, string, error) {
249+
restClient, err := gh.RESTClient(nil)
250+
response := make(map[string]interface{})
251+
err = restClient.Get(fmt.Sprintf("repos/%s/commits/%s", nwo, commitSha), &response)
252+
if err != nil {
253+
log.Fatal(err)
254+
}
255+
commitMap := response["commit"].(map[string]interface{})
256+
committerMap := commitMap["committer"].(map[string]interface{})
257+
dateString := committerMap["date"].(string)
258+
return commitSha, dateString, nil
259+
}

0 commit comments

Comments
 (0)