Skip to content

Commit

Permalink
Implement list gitlab repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
marcofranssen committed Aug 31, 2020
1 parent 0ba8027 commit 4c8ac4b
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 1 deletion.
55 changes: 54 additions & 1 deletion cmd/cmd_gitlab.go
@@ -1,12 +1,16 @@
package cmd

import (
"context"
"fmt"
"io"
"io/ioutil"
"text/tabwriter"

"github.com/urfave/cli/v2"

"github.com/philips-labs/tabia/lib/gitlab"
"github.com/philips-labs/tabia/lib/output"
)

func createGitlab() *cli.Command {
Expand Down Expand Up @@ -38,6 +42,21 @@ func createGitlab() *cli.Command {
Name: "repositories",
Usage: "display insights on repositories",
Action: gitlabRepositories,
Flags: []cli.Flag{
&cli.StringFlag{
Name: "format",
Aliases: []string{"F"},
Usage: "Formats output in the given `FORMAT`",
EnvVars: []string{"TABIA_OUTPUT_FORMAT"},
DefaultText: "",
},
&cli.PathFlag{
Name: "template",
Aliases: []string{"T"},
Usage: "Formats output using the given `TEMPLATE`",
TakesFile: true,
},
},
},
},
}
Expand All @@ -57,11 +76,45 @@ func newGitlabClient(c *cli.Context) (*gitlab.Client, error) {
}

func gitlabRepositories(c *cli.Context) error {
format := c.String("format")

client, err := newGitlabClient(c)
if err != nil {
return err
}
fmt.Fprintln(c.App.Writer, client.BaseURL())
ctx, cancel := context.WithCancel(c.Context)
defer cancel()

repos, err := client.ListRepositories(ctx)
if err != nil {
return err
}

switch format {
case "json":
output.PrintJSON(c.App.Writer, repos)
case "templated":
if !c.IsSet("template") {
return fmt.Errorf("you must specify the path to the template")
}

templateFile := c.Path("template")
tmplContent, err := ioutil.ReadFile(templateFile)
if err != nil {
return err
}
err = output.PrintUsingTemplate(c.App.Writer, tmplContent, repos)
if err != nil {
return err
}
default:
w := tabwriter.NewWriter(c.App.Writer, 3, 0, 2, ' ', tabwriter.TabIndent)
fmt.Fprintln(w, " \tID\tName\tVisibility\tURL")
for i, repo := range repos {
fmt.Fprintf(w, "%04d\t%d\t%s\t%s\t%s\n", i+1, repo.ID, repo.Name, repo.Visibility, repo.URL)
}
w.Flush()
}

return nil
}
68 changes: 68 additions & 0 deletions lib/gitlab/repositories.go
@@ -0,0 +1,68 @@
package gitlab

import (
"context"
"strings"
"time"

"github.com/xanzy/go-gitlab"

"github.com/philips-labs/tabia/lib/shared"
)

type Repository struct {
ID int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description string `json:"description,omitempty"`
URL string `json:"url,omitempty"`
SSHURL string `json:"sshurl,omitempty"`
CreatedAt *time.Time `json:"created_at,omitempty"`
LastActivityAt *time.Time `json:"last_activity_at,omitempty"`
Visibility shared.Visibility `json:"visibility,omitempty"`
}

func (c *Client) ListRepositories(ctx context.Context) ([]Repository, error) {
opt := &gitlab.ListProjectsOptions{
ListOptions: gitlab.ListOptions{
PerPage: 100,
Page: 1,
},
}
var repos []Repository
for {
projects, resp, err := c.Projects.ListProjects(opt, gitlab.WithContext(ctx))
if err != nil {
return repos, err
}
defer resp.Body.Close()
repos = append(repos, Map(projects)...)

if resp.CurrentPage >= resp.TotalPages {
break
}

opt.Page = resp.NextPage
}
return repos, nil
}

func Map(projects []*gitlab.Project) []Repository {
repos := make([]Repository, len(projects))
for i, project := range projects {
repos[i] = Repository{
ID: project.ID,
Name: project.Name,
Description: strings.TrimSpace(project.Description),
URL: project.WebURL,
SSHURL: project.SSHURLToRepo,
CreatedAt: project.CreatedAt,
LastActivityAt: project.LastActivityAt,
Visibility: shared.VisibilityFromText(string(project.Visibility)),
}
}
return repos
}

func boolPointer(b bool) *bool {
return &b
}
22 changes: 22 additions & 0 deletions lib/gitlab/repositories_test.go
@@ -0,0 +1,22 @@
package gitlab_test

import (
"testing"

"github.com/stretchr/testify/assert"
gl "github.com/xanzy/go-gitlab"

"github.com/philips-labs/tabia/lib/gitlab"
)

func TestMap(t *testing.T) {
assert := assert.New(t)

projects := []*gl.Project{
&gl.Project{},
}

repos := gitlab.Map(projects)

assert.Len(repos, len(projects))
}

0 comments on commit 4c8ac4b

Please sign in to comment.