-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for exporting project data
- Loading branch information
Showing
17 changed files
with
1,417 additions
and
265 deletions.
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
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,99 @@ | ||
package gitlab | ||
|
||
import ( | ||
"context" | ||
|
||
gitlab "github.com/xanzy/go-gitlab" | ||
|
||
"github.com/cluttrdev/gitlab-exporter/protobuf/typespb" | ||
) | ||
|
||
type ListProjectOptions = gitlab.ListProjectsOptions | ||
|
||
func (c *Client) ListProjects(ctx context.Context, opt ListProjectOptions) ([]*typespb.Project, error) { | ||
var projects []*typespb.Project | ||
|
||
for { | ||
ps, resp, err := c.client.Projects.ListProjects(&opt, gitlab.WithContext(ctx)) | ||
if err != nil { | ||
return projects, err | ||
} | ||
|
||
for _, p := range ps { | ||
projects = append(projects, convertProject(p)) | ||
} | ||
|
||
if resp.NextPage == 0 { | ||
break | ||
} | ||
opt.Page = resp.NextPage | ||
} | ||
|
||
return projects, nil | ||
} | ||
|
||
func (c *Client) GetProject(ctx context.Context, id int64) (*typespb.Project, error) { | ||
opt := gitlab.GetProjectOptions{ | ||
Statistics: ptr(true), | ||
} | ||
|
||
p, _, err := c.client.Projects.GetProject(int(id), &opt) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return convertProject(p), nil | ||
} | ||
|
||
func convertProject(p *gitlab.Project) *typespb.Project { | ||
return &typespb.Project{ | ||
Id: int64(p.ID), | ||
Name: p.Name, | ||
NameWithNamespace: p.NameWithNamespace, | ||
Path: p.Path, | ||
PathWithNamespace: p.PathWithNamespace, | ||
Description: p.Description, | ||
|
||
CreatedAt: convertTime(p.CreatedAt), | ||
LastActivityAt: convertTime(p.LastActivityAt), | ||
|
||
DefaultBranch: p.DefaultBranch, | ||
WebUrl: p.WebURL, | ||
|
||
Namespace: convertProjectNamespace(p.Namespace), | ||
Statistics: convertProjectStatistics(p), | ||
} | ||
} | ||
|
||
func convertProjectNamespace(n *gitlab.ProjectNamespace) *typespb.ProjectNamespace { | ||
return &typespb.ProjectNamespace{ | ||
Id: int64(n.ID), | ||
Name: n.Name, | ||
Kind: n.Kind, | ||
Path: n.Path, | ||
FullPath: n.FullPath, | ||
ParentId: int64(n.ParentID), | ||
|
||
AvatarUrl: n.AvatarURL, | ||
WebUrl: n.WebURL, | ||
} | ||
} | ||
|
||
func convertProjectStatistics(p *gitlab.Project) *typespb.ProjectStatistics { | ||
s := &typespb.ProjectStatistics{ | ||
ForksCount: int64(p.ForksCount), | ||
StarsCount: int64(p.StarCount), | ||
} | ||
if p.Statistics != nil { | ||
s.CommitCount = p.Statistics.CommitCount | ||
s.StorageSize = p.Statistics.StorageSize | ||
s.RepositorySize = p.Statistics.RepositorySize | ||
s.WikiSize = p.Statistics.WikiSize | ||
s.LfsObjectsSize = p.Statistics.LFSObjectsSize | ||
s.JobArtifactsSize = p.Statistics.JobArtifactsSize | ||
s.PackagesSize = p.Statistics.PackagesSize | ||
s.SnippetsSize = p.Statistics.SnippetsSize | ||
s.UploadsSize = p.Statistics.UploadsSize | ||
} | ||
return s | ||
} |
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,26 @@ | ||
package tasks | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/cluttrdev/gitlab-exporter/internal/exporter" | ||
"github.com/cluttrdev/gitlab-exporter/internal/gitlab" | ||
"github.com/cluttrdev/gitlab-exporter/protobuf/typespb" | ||
) | ||
|
||
type ExportProjectOptions struct { | ||
ProjectID int64 | ||
} | ||
|
||
func ExportProject(ctx context.Context, glab *gitlab.Client, exp *exporter.Exporter, opt ExportProjectOptions) error { | ||
p, err := glab.GetProject(ctx, opt.ProjectID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := exp.ExportProjects(ctx, []*typespb.Project{p}); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.