Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0ba8027
commit 4c8ac4b
Showing
3 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |