This commit is contained in:
Nukeap committed 2024-04-23 16:49:10 +03:00
commit c0f618a9db
133 files changed
+55621

No files matched your search

+812
View File
@@ -0,0 +1,812 @@
package gitlab
import (
"context"
"fmt"
"net/http"
"strings"
"sync"
"github.com/mattermost/mattermost/server/public/pluginapi/experimental/bot/logger"
"github.com/pkg/errors"
internGitlab "github.com/xanzy/go-gitlab"
"golang.org/x/oauth2"
"golang.org/x/sync/errgroup"
)
const (
stateOpened = "opened"
scopeAll = "all"
perPage = 20
)
type PRDetails struct {
IID int `json:"iid"`
Status *internGitlab.BuildStateValue `json:"status"`
SHA string `json:"sha"`
NumApprovers int `json:"num_approvers"`
ProjectID int `json:"project_id"`
}
type MergeRequest struct {
*internGitlab.MergeRequest
LabelsWithDetails []*internGitlab.Label `json:"labels_with_details,omitempty"`
}
type Issue struct {
*internGitlab.Issue
LabelsWithDetails []*internGitlab.Label `json:"labels_with_details,omitempty"`
}
type LHSContent struct {
AssignedPRs []*MergeRequest `json:"yourAssignedPrs"`
Reviews []*MergeRequest `json:"reviews"`
AssignedIssues []*Issue `json:"yourAssignedIssues"`
Todos []*internGitlab.Todo `json:"todos"`
}
// NewGroupHook creates a webhook associated with a GitLab group
func (g *gitlab) NewGroupHook(ctx context.Context, user *UserInfo, token *oauth2.Token, groupName string, webhookOptions *AddWebhookOptions) (*WebhookInfo, error) {
client, err := g.GitlabConnect(*token)
if err != nil {
return nil, err
}
group, resp, err := client.Groups.GetGroup(groupName, nil, internGitlab.WithContext(ctx))
if respErr := checkResponse(resp); respErr != nil {
return nil, respErr
}
if err != nil {
return nil, err
}
groupHookOptions := internGitlab.AddGroupHookOptions{
URL: &webhookOptions.URL,
ConfidentialNoteEvents: &webhookOptions.ConfidentialNoteEvents,
PushEvents: &webhookOptions.PushEvents,
IssuesEvents: &webhookOptions.IssuesEvents,
ConfidentialIssuesEvents: &webhookOptions.ConfidentialIssuesEvents,
MergeRequestsEvents: &webhookOptions.MergeRequestsEvents,
TagPushEvents: &webhookOptions.TagPushEvents,
NoteEvents: &webhookOptions.NoteEvents,
JobEvents: &webhookOptions.JobEvents,
PipelineEvents: &webhookOptions.PipelineEvents,
WikiPageEvents: &webhookOptions.WikiPageEvents,
EnableSSLVerification: &webhookOptions.EnableSSLVerification,
Token: &webhookOptions.Token,
}
groupHook, resp, err := client.Groups.AddGroupHook(group.ID, &groupHookOptions, internGitlab.WithContext(ctx))
if err != nil {
if resp.StatusCode == http.StatusForbidden {
return nil, ErrForbidden
}
return nil, err
}
groupHookInfo := getGroupHookInfo(groupHook)
return groupHookInfo, nil
}
// NewProjectHook creates a webhook associated with a GitLab project
func (g *gitlab) NewProjectHook(ctx context.Context, user *UserInfo, token *oauth2.Token, projectID interface{}, webhookOptions *AddWebhookOptions) (*WebhookInfo, error) {
client, err := g.GitlabConnect(*token)
if err != nil {
return nil, err
}
projectHookOptions := internGitlab.AddProjectHookOptions{
URL: &webhookOptions.URL,
ConfidentialNoteEvents: &webhookOptions.ConfidentialNoteEvents,
PushEvents: &webhookOptions.PushEvents,
IssuesEvents: &webhookOptions.IssuesEvents,
ConfidentialIssuesEvents: &webhookOptions.ConfidentialIssuesEvents,
MergeRequestsEvents: &webhookOptions.MergeRequestsEvents,
TagPushEvents: &webhookOptions.TagPushEvents,
NoteEvents: &webhookOptions.NoteEvents,
JobEvents: &webhookOptions.JobEvents,
PipelineEvents: &webhookOptions.PipelineEvents,
WikiPageEvents: &webhookOptions.WikiPageEvents,
EnableSSLVerification: &webhookOptions.EnableSSLVerification,
Token: &webhookOptions.Token,
}
projectHook, resp, err := client.Projects.AddProjectHook(projectID, &projectHookOptions, internGitlab.WithContext(ctx))
if respErr := checkResponse(resp); respErr != nil {
return nil, respErr
}
if err != nil {
return nil, err
}
projectHookInfo := getProjectHookInfo(projectHook)
return projectHookInfo, nil
}
// GetGroupHooks gathers all the group level hooks for a GitLab group.
func (g *gitlab) GetGroupHooks(ctx context.Context, user *UserInfo, token *oauth2.Token, owner string) ([]*WebhookInfo, error) {
client, err := g.GitlabConnect(*token)
if err != nil {
return nil, err
}
hooks, resp, err := client.Groups.ListGroupHooks(owner, nil, internGitlab.WithContext(ctx))
if respErr := checkResponse(resp); respErr != nil {
return nil, respErr
}
if err != nil {
return nil, err
}
var webhooks []*WebhookInfo
for _, hook := range hooks {
webhooks = append(webhooks, getGroupHookInfo(hook))
}
return webhooks, nil
}
// String produces a multiline bulleted string for displaying webhook information.
func (w *WebhookInfo) String() string {
var formatedTriggers string
if w.EnableSSLVerification {
formatedTriggers += "SSL Verification Enabled\n"
}
formatedTriggers += "Triggers:\n"
if w.PushEvents {
formatedTriggers += "* Push Events\n"
}
if w.TagPushEvents {
formatedTriggers += "* Tag Push Events\n"
}
if w.NoteEvents {
formatedTriggers += "* Comments\n"
}
if w.ConfidentialNoteEvents {
formatedTriggers += "* Confidential Comments\n"
}
if w.IssuesEvents {
formatedTriggers += "* Issues Events\n"
}
if w.ConfidentialIssuesEvents {
formatedTriggers += "* Confidential Issues Events\n"
}
if w.MergeRequestsEvents {
formatedTriggers += "* Merge Request Events\n"
}
if w.JobEvents {
formatedTriggers += "* Job Events\n"
}
if w.PipelineEvents {
formatedTriggers += "* Pipeline Events\n"
}
if w.WikiPageEvents {
formatedTriggers += "* Wiki Page Events\n"
}
return "\n\n`" + w.URL + "`\n" + formatedTriggers
}
func getProjectHookInfo(hook *internGitlab.ProjectHook) *WebhookInfo {
webhook := &WebhookInfo{
Scope: Project,
URL: hook.URL,
ID: hook.ID,
ConfidentialNoteEvents: hook.ConfidentialNoteEvents,
PushEvents: hook.PushEvents,
IssuesEvents: hook.IssuesEvents,
ConfidentialIssuesEvents: hook.ConfidentialIssuesEvents,
MergeRequestsEvents: hook.MergeRequestsEvents,
TagPushEvents: hook.TagPushEvents,
NoteEvents: hook.NoteEvents,
JobEvents: hook.JobEvents,
PipelineEvents: hook.PipelineEvents,
WikiPageEvents: hook.WikiPageEvents,
EnableSSLVerification: hook.EnableSSLVerification,
CreatedAt: hook.CreatedAt,
}
return webhook
}
func getGroupHookInfo(hook *internGitlab.GroupHook) *WebhookInfo {
webhook := &WebhookInfo{
Scope: Project,
URL: hook.URL,
ID: hook.ID,
ConfidentialNoteEvents: hook.ConfidentialNoteEvents,
PushEvents: hook.PushEvents,
IssuesEvents: hook.IssuesEvents,
ConfidentialIssuesEvents: hook.ConfidentialIssuesEvents,
MergeRequestsEvents: hook.MergeRequestsEvents,
TagPushEvents: hook.TagPushEvents,
NoteEvents: hook.NoteEvents,
JobEvents: hook.JobEvents,
PipelineEvents: hook.PipelineEvents,
WikiPageEvents: hook.WikiPageEvents,
EnableSSLVerification: hook.EnableSSLVerification,
CreatedAt: hook.CreatedAt,
}
return webhook
}
// GetProjectHooks gathers all the project level hooks from a single GitLab project.
func (g *gitlab) GetProjectHooks(ctx context.Context, user *UserInfo, token *oauth2.Token, owner string, repo string) ([]*WebhookInfo, error) {
client, err := g.GitlabConnect(*token)
if err != nil {
return nil, err
}
opt := &internGitlab.ListProjectHooksOptions{
PerPage: perPage,
Page: 1,
}
var projectHooks []*internGitlab.ProjectHook
var hooks []*internGitlab.ProjectHook
var resp *internGitlab.Response
projectPath := fmt.Sprintf("%s/%s", owner, repo)
for {
hooks, resp, err = client.Projects.ListProjectHooks(projectPath, opt)
if err != nil {
return nil, err
}
projectHooks = append(projectHooks, hooks...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
var webhooks []*WebhookInfo
for _, hook := range projectHooks {
webhooks = append(webhooks, getProjectHookInfo(hook))
}
return webhooks, nil
}
func (g *gitlab) GetProject(ctx context.Context, user *UserInfo, token *oauth2.Token, owner, repo string) (*internGitlab.Project, error) {
client, err := g.GitlabConnect(*token)
if err != nil {
return nil, err
}
project, resp, err := client.Projects.GetProject(fmt.Sprintf("%s/%s", owner, repo),
&internGitlab.GetProjectOptions{},
internGitlab.WithContext(ctx),
)
if respErr := checkResponse(resp); respErr != nil {
return nil, respErr
}
if err != nil {
return nil, err
}
return project, nil
}
func (g *gitlab) GetLHSData(ctx context.Context, user *UserInfo, token *oauth2.Token) (*LHSContent, error) {
client, err := g.GitlabConnect(*token)
if err != nil {
return nil, err
}
grp, ctx := errgroup.WithContext(ctx)
var reviews []*MergeRequest
grp.Go(func() error {
reviews, err = g.GetReviews(ctx, user, client)
return err
})
var issues []*Issue
grp.Go(func() error {
issues, err = g.GetYourAssignedIssues(ctx, user, client)
return err
})
var mergeRequests []*MergeRequest
grp.Go(func() error {
mergeRequests, err = g.GetYourAssignedPrs(ctx, user, client)
return err
})
var todos []*internGitlab.Todo
grp.Go(func() error {
todos, err = g.GetToDoList(ctx, user, client)
return err
})
if err := grp.Wait(); err != nil {
return nil, err
}
return &LHSContent{
Reviews: reviews,
AssignedPRs: mergeRequests,
AssignedIssues: issues,
Todos: todos,
}, nil
}
func (g *gitlab) GetReviews(ctx context.Context, user *UserInfo, client *internGitlab.Client) ([]*MergeRequest, error) {
opened := stateOpened
scope := scopeAll
var mrs []*internGitlab.MergeRequest
if g.gitlabGroup == "" {
opt := &internGitlab.ListMergeRequestsOptions{
ReviewerID: internGitlab.ReviewerID(user.GitlabUserID),
State: &opened,
Scope: &scope,
ListOptions: internGitlab.ListOptions{Page: 1, PerPage: perPage},
}
for {
current, resp, err := client.MergeRequests.ListMergeRequests(opt)
if err != nil {
return nil, err
}
mrs = append(mrs, current...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
} else {
opt := &internGitlab.ListGroupMergeRequestsOptions{
ReviewerID: internGitlab.ReviewerID(user.GitlabUserID),
State: &opened,
Scope: &scope,
ListOptions: internGitlab.ListOptions{Page: 1, PerPage: perPage},
}
for {
current, resp, err := client.MergeRequests.ListGroupMergeRequests(g.gitlabGroup, opt)
if err != nil {
return nil, err
}
mrs = append(mrs, current...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
}
mergeRequests := []*MergeRequest{}
for _, mr := range mrs {
if mr.Labels != nil {
labelsWithDetails, err := g.GetLabelDetails(client, mr.ProjectID, mr.Labels)
if err != nil {
return nil, err
}
mergeRequest := &MergeRequest{
MergeRequest: mr,
LabelsWithDetails: labelsWithDetails,
}
mergeRequests = append(mergeRequests, mergeRequest)
}
}
return mergeRequests, nil
}
func (g *gitlab) GetYourAssignedPrs(ctx context.Context, user *UserInfo, client *internGitlab.Client) ([]*MergeRequest, error) {
opened := stateOpened
scope := scopeAll
var mrs []*internGitlab.MergeRequest
if g.gitlabGroup == "" {
opt := &internGitlab.ListMergeRequestsOptions{
AssigneeID: internGitlab.AssigneeID(user.GitlabUserID),
State: &opened,
Scope: &scope,
ListOptions: internGitlab.ListOptions{Page: 1, PerPage: perPage},
}
for {
current, resp, err := client.MergeRequests.ListMergeRequests(opt)
if err != nil {
return nil, err
}
mrs = append(mrs, current...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
} else {
opt := &internGitlab.ListGroupMergeRequestsOptions{
AssigneeID: internGitlab.AssigneeID(user.GitlabUserID),
State: &opened,
Scope: &scope,
ListOptions: internGitlab.ListOptions{Page: 1, PerPage: perPage},
}
for {
current, resp, err := client.MergeRequests.ListGroupMergeRequests(g.gitlabGroup, opt)
if err != nil {
return nil, err
}
mrs = append(mrs, current...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
}
mergeRequests := []*MergeRequest{}
for _, mr := range mrs {
if mr.Labels != nil {
labelsWithDetails, err := g.GetLabelDetails(client, mr.ProjectID, mr.Labels)
if err != nil {
return nil, err
}
mergeRequest := &MergeRequest{
MergeRequest: mr,
LabelsWithDetails: labelsWithDetails,
}
mergeRequests = append(mergeRequests, mergeRequest)
}
}
return mergeRequests, nil
}
func (g *gitlab) GetLabelDetails(client *internGitlab.Client, pid int, labels internGitlab.Labels) ([]*internGitlab.Label, error) {
// Get list of all labels.
labelList, resp, err := client.Labels.ListLabels(pid, nil)
if respErr := checkResponse(resp); respErr != nil {
return nil, respErr
}
if err != nil {
return nil, errors.Wrap(err, "can't get list of labels in GitLab")
}
allLabels := map[string]*internGitlab.Label{}
for _, label := range labelList {
allLabels[label.Name] = label
}
labelsWithDetails := []*internGitlab.Label{}
for _, label := range labels {
if allLabels[label] == nil {
return nil, errors.Wrap(err, "can't get label in GitLab api")
}
labelsWithDetails = append(labelsWithDetails, allLabels[label])
}
return labelsWithDetails, nil
}
func (g *gitlab) GetYourPrDetails(ctx context.Context, log logger.Logger, user *UserInfo, token *oauth2.Token, prList []*PRDetails) ([]*PRDetails, error) {
client, err := g.GitlabConnect(*token)
if err != nil {
return nil, err
}
var result []*PRDetails
var wg sync.WaitGroup
for _, pr := range prList {
wg.Add(1)
go func(pid, iid int, sha string) {
defer wg.Done()
res := g.fetchYourPrDetails(ctx, log, client, pid, iid, sha)
if res != nil {
result = append(result, res)
}
}(pr.ProjectID, pr.IID, pr.SHA)
}
wg.Wait()
return result, nil
}
func (g *gitlab) fetchYourPrDetails(c context.Context, log logger.Logger, client *internGitlab.Client, pid, iid int, sha string) *PRDetails {
var commitDetails *internGitlab.Commit
var approvalDetails *internGitlab.MergeRequestApprovals
var err error
var resp *internGitlab.Response
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
commitDetails, resp, err = client.Commits.GetCommit(pid, sha, internGitlab.WithContext(c))
if respErr := checkResponse(resp); respErr != nil {
log.WithError(respErr).Warnf("Failed to fetch commit details for PR with project_id %d", pid)
return
}
if err != nil {
log.WithError(err).Warnf("Failed to fetch commit details for PR with project_id %d", pid)
return
}
}()
wg.Add(1)
go func() {
defer wg.Done()
approvalDetails, resp, err = client.MergeRequestApprovals.GetConfiguration(pid, iid, internGitlab.WithContext(c))
if respErr := checkResponse(resp); respErr != nil {
log.WithError(respErr).Warnf("Failed to fetch approval details for PR with project_id %d", pid)
return
}
if err != nil {
log.WithError(err).Warnf("Failed to fetch approval details for PR with project_id %d", pid)
return
}
}()
wg.Wait()
if commitDetails != nil && approvalDetails != nil {
return &PRDetails{
ProjectID: pid,
SHA: sha,
Status: commitDetails.Status,
NumApprovers: len(approvalDetails.ApprovedBy),
}
}
return nil
}
func (g *gitlab) GetYourAssignedIssues(ctx context.Context, user *UserInfo, client *internGitlab.Client) ([]*Issue, error) {
opened := stateOpened
scope := scopeAll
var issues []*internGitlab.Issue
if g.gitlabGroup == "" {
opt := &internGitlab.ListIssuesOptions{
AssigneeID: internGitlab.AssigneeID(user.GitlabUserID),
State: &opened,
Scope: &scope,
ListOptions: internGitlab.ListOptions{Page: 1, PerPage: perPage},
}
for {
current, resp, err := client.Issues.ListIssues(opt)
if err != nil {
return nil, err
}
issues = append(issues, current...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
} else {
opt := &internGitlab.ListGroupIssuesOptions{
AssigneeID: internGitlab.AssigneeID(user.GitlabUserID),
State: &opened,
Scope: &scope,
ListOptions: internGitlab.ListOptions{Page: 1, PerPage: perPage},
}
for {
current, resp, err := client.Issues.ListGroupIssues(g.gitlabGroup, opt)
if err != nil {
return nil, err
}
issues = append(issues, current...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
}
var result []*Issue
for _, issue := range issues {
if issue.Labels != nil {
labelsWithDetails, err := g.GetLabelDetails(client, issue.ProjectID, issue.Labels)
if err != nil {
return nil, err
}
issue := &Issue{
Issue: issue,
LabelsWithDetails: labelsWithDetails,
}
result = append(result, issue)
}
}
return result, nil
}
func (g *gitlab) GetToDoList(ctx context.Context, user *UserInfo, client *internGitlab.Client) ([]*internGitlab.Todo, error) {
var todos []*internGitlab.Todo
opt := &internGitlab.ListTodosOptions{
ListOptions: internGitlab.ListOptions{Page: 1, PerPage: perPage},
}
for {
current, resp, err := client.Todos.ListTodos(opt)
if err != nil {
return nil, errors.Wrap(err, "can't list todo in GitLab api")
}
todos = append(todos, current...)
if resp.NextPage == 0 {
break
}
opt.Page = resp.NextPage
}
notifications := make([]*internGitlab.Todo, 0, len(todos))
for _, todo := range todos {
if todo == nil {
continue
}
if todo.Project != nil && g.checkGroup(strings.TrimSuffix(todo.Project.PathWithNamespace, "/"+todo.Project.Path)) != nil {
continue
}
notifications = append(notifications, todo)
}
return notifications, nil
}
func (g *gitlab) ResolveNamespaceAndProject(
ctx context.Context,
userInfo *UserInfo,
token *oauth2.Token,
fullPath string,
allowPrivate bool,
) (owner string, repo string, err error) {
// Initialize client
client, err := g.GitlabConnect(*token)
if err != nil {
return "", "", err
}
fullPath = strings.TrimPrefix(fullPath, g.gitlabURL)
fullPath = strings.Trim(fullPath, "/")
// Search for matching user, group and project concurrently
//
// Note: Calls to Users and Groups could be replaced with a single call to Namespaces.
// However, Namespaces endpoint will not return Group visibility, so we will have to make additional call anyway.
// Making this extra call here should reduce overall latency.
var (
user *internGitlab.User
group *internGitlab.Group
project *internGitlab.Project
)
errGroup, ctx := errgroup.WithContext(ctx)
if strings.Count(fullPath, "/") == 0 { // This request only makes sense for single path component
errGroup.Go(func() error {
users, resp, err := client.Users.ListUsers(&internGitlab.ListUsersOptions{
Username: &fullPath,
},
internGitlab.WithContext(ctx),
)
if respErr := checkResponse(resp); respErr != nil {
return respErr
}
if err != nil {
return errors.Wrap(err, "failed to search users by username")
}
if len(users) == 1 {
user = users[0]
}
return nil
})
}
errGroup.Go(func() error {
gr, resp, err := client.Groups.GetGroup(fullPath, nil, internGitlab.WithContext(ctx))
if err != nil && resp != nil && resp.StatusCode != http.StatusNotFound {
return errors.Wrap(err, "failed to retrieve group by path")
}
group = gr
return nil
})
errGroup.Go(func() error {
p, resp, err := client.Projects.GetProject(fullPath, nil, internGitlab.WithContext(ctx))
if err != nil && resp != nil && resp.StatusCode != http.StatusNotFound {
return errors.Wrap(err, "failed to retrieve project by path")
}
project = p
return nil
})
if err := errGroup.Wait(); err != nil {
return "", "", err
}
// Decide what to return
if user != nil {
return user.Username, "", nil
}
if group != nil {
if !allowPrivate && group.Visibility != internGitlab.PublicVisibility {
return "", "", errors.Wrap(ErrPrivateResource,
"You can't add a private group on this Mattermost instance. Please enable private repositories in the System Console.",
)
}
return group.FullPath, "", nil
}
if project != nil {
if !allowPrivate && project.Visibility != internGitlab.PublicVisibility {
return "", "", errors.Wrap(ErrPrivateResource,
"You can't add a private group on this Mattermost instance. Please enable private repositories in the System Console.",
)
}
return project.Namespace.FullPath, project.Path, nil
}
return "", "", ErrNotFound
}
func (g *gitlab) GetIssueByID(ctx context.Context, user *UserInfo, owner, repo string, issueID int, token *oauth2.Token) (*Issue, error) {
client, err := g.GitlabConnect(*token)
if err != nil {
return nil, err
}
projectPath := fmt.Sprintf("%s/%s", owner, repo)
issue, resp, err := client.Issues.GetIssue(projectPath, issueID)
if respErr := checkResponse(resp); respErr != nil {
return nil, respErr
}
if err != nil {
return nil, errors.Wrap(err, "can't get issue in GitLab api")
}
gitlabIssue := &Issue{
Issue: issue,
}
if issue.Labels != nil {
labelsWithDetails, err := g.GetLabelDetails(client, issue.ProjectID, issue.Labels)
if err != nil {
return nil, err
}
gitlabIssue.LabelsWithDetails = labelsWithDetails
}
return gitlabIssue, nil
}
func (g *gitlab) GetMergeRequestByID(ctx context.Context, user *UserInfo, owner, repo string, mergeRequestID int, token *oauth2.Token) (*MergeRequest, error) {
client, err := g.GitlabConnect(*token)
if err != nil {
return nil, err
}
projectPath := fmt.Sprintf("%s/%s", owner, repo)
mergeRequest, resp, err := client.MergeRequests.GetMergeRequest(projectPath, mergeRequestID, nil)
if respErr := checkResponse(resp); respErr != nil {
return nil, respErr
}
if err != nil {
return nil, errors.Wrap(err, "can't get merge request in GitLab api")
}
gitlabMergeRequest := &MergeRequest{
MergeRequest: mergeRequest,
}
if mergeRequest.Labels != nil {
labelsWithDetails, err := g.GetLabelDetails(client, mergeRequest.ProjectID, mergeRequest.Labels)
if err != nil {
return nil, err
}
gitlabMergeRequest.LabelsWithDetails = labelsWithDetails
}
return gitlabMergeRequest, nil
}
// TriggerProjectPipeline runs a pipeline in a specific project
func (g *gitlab) TriggerProjectPipeline(userInfo *UserInfo, token *oauth2.Token, projectID string, ref string) (*PipelineInfo, error) {
client, err := g.GitlabConnect(*token)
if err != nil {
return &PipelineInfo{}, err
}
pipeline, _, err := client.Pipelines.CreatePipeline(projectID, &internGitlab.CreatePipelineOptions{
Ref: &ref,
})
if err != nil {
return nil, errors.Wrap(err, "failed to run the pipeline")
}
return &PipelineInfo{
PipelineID: pipeline.ID,
Status: pipeline.Status,
Ref: pipeline.Ref,
WebURL: pipeline.WebURL,
SHA: pipeline.SHA,
User: pipeline.User.Name,
}, err
}
+117
View File
@@ -0,0 +1,117 @@
package gitlab
import (
"context"
"net/http"
"strings"
"github.com/mattermost/mattermost/server/public/pluginapi/experimental/bot/logger"
"github.com/pkg/errors"
internGitlab "github.com/xanzy/go-gitlab"
"golang.org/x/oauth2"
)
const Gitlabdotcom = "https://gitlab.com"
// Errors returned by this package.
var (
ErrNotFound = errors.New("not found")
ErrForbidden = errors.New("access forbidden")
ErrPrivateResource = errors.New("private resource")
)
// Gitlab is a client to call GitLab api see New() to build one
type Gitlab interface {
GitlabConnect(token oauth2.Token) (*internGitlab.Client, error)
GetCurrentUser(ctx context.Context, userID string, token oauth2.Token) (*UserInfo, error)
GetIssueByID(ctx context.Context, user *UserInfo, owner, repo string, issueID int, token *oauth2.Token) (*Issue, error)
GetMergeRequestByID(ctx context.Context, user *UserInfo, owner, repo string, mergeRequestID int, token *oauth2.Token) (*MergeRequest, error)
GetUserDetails(ctx context.Context, user *UserInfo, token *oauth2.Token) (*internGitlab.User, error)
GetProject(ctx context.Context, user *UserInfo, token *oauth2.Token, owner, repo string) (*internGitlab.Project, error)
GetYourPrDetails(ctx context.Context, log logger.Logger, user *UserInfo, token *oauth2.Token, prList []*PRDetails) ([]*PRDetails, error)
GetReviews(ctx context.Context, user *UserInfo, client *internGitlab.Client) ([]*MergeRequest, error)
GetYourAssignedPrs(ctx context.Context, user *UserInfo, client *internGitlab.Client) ([]*MergeRequest, error)
GetLHSData(ctx context.Context, user *UserInfo, token *oauth2.Token) (*LHSContent, error)
GetYourAssignedIssues(ctx context.Context, user *UserInfo, client *internGitlab.Client) ([]*Issue, error)
GetToDoList(ctx context.Context, user *UserInfo, client *internGitlab.Client) ([]*internGitlab.Todo, error)
GetProjectHooks(ctx context.Context, user *UserInfo, token *oauth2.Token, owner string, repo string) ([]*WebhookInfo, error)
GetGroupHooks(ctx context.Context, user *UserInfo, token *oauth2.Token, owner string) ([]*WebhookInfo, error)
NewProjectHook(ctx context.Context, user *UserInfo, token *oauth2.Token, projectID interface{}, projectHookOptions *AddWebhookOptions) (*WebhookInfo, error)
NewGroupHook(ctx context.Context, user *UserInfo, token *oauth2.Token, groupName string, groupHookOptions *AddWebhookOptions) (*WebhookInfo, error)
TriggerProjectPipeline(userInfo *UserInfo, token *oauth2.Token, projectID string, ref string) (*PipelineInfo, error)
// ResolveNamespaceAndProject accepts full path to User, Group or namespaced Project and returns corresponding
// namespace and project name.
//
// ErrNotFound will be returned if no resource can be found.
// If allowPrivate is set to false, and resolved group/project is private, ErrPrivateResource will be returned.
ResolveNamespaceAndProject(
ctx context.Context,
userInfo *UserInfo,
token *oauth2.Token,
fullPath string,
allowPrivate bool,
) (namespace string, project string, err error)
}
type gitlab struct {
gitlabURL string
gitlabGroup string
checkGroup func(projectNameWithGroup string) error
}
// Scope identifies the scope of a webhook
type Scope int
const (
// Group is a type for group hooks
Group Scope = iota
// Project is a type for project hooks
Project
)
func (s Scope) String() string {
return [...]string{"group", "project"}[s]
}
// New return a client to call GitLab API
func New(gitlabURL string, gitlabGroup string, checkGroup func(projectNameWithGroup string) error) Gitlab {
if gitlabURL == "" {
gitlabURL = Gitlabdotcom
}
return &gitlab{gitlabURL: gitlabURL, gitlabGroup: gitlabGroup, checkGroup: checkGroup}
}
func (g *gitlab) GitlabConnect(token oauth2.Token) (*internGitlab.Client, error) {
if g.gitlabURL == "" || strings.EqualFold(g.gitlabURL, Gitlabdotcom) {
return internGitlab.NewOAuthClient(token.AccessToken)
}
return internGitlab.NewOAuthClient(token.AccessToken, internGitlab.WithBaseURL(g.gitlabURL))
}
// checkResponse returns known errors based on the http status code.
func checkResponse(resp *internGitlab.Response) error {
if resp == nil {
return nil
}
switch resp.StatusCode {
case http.StatusForbidden:
return ErrForbidden
case http.StatusNotFound:
return ErrNotFound
default:
return nil
}
}
// PrettyError returns an err in a better readable way.
func PrettyError(err error) error {
var errResp *internGitlab.ErrorResponse
if errors.As(err, &errResp) {
return errors.New(errResp.Message)
}
return err
}
+310
View File
@@ -0,0 +1,310 @@
// Code generated by MockGen. DO NOT EDIT.
// Source: github.com/mattermost/mattermost-plugin-gitlab/server/gitlab (interfaces: Gitlab)
// Package mock_gitlab is a generated GoMock package.
package mock_gitlab
import (
context "context"
reflect "reflect"
gomock "github.com/golang/mock/gomock"
gitlab "github.com/mattermost/mattermost-plugin-gitlab/server/gitlab"
logger "github.com/mattermost/mattermost/server/public/pluginapi/experimental/bot/logger"
gitlab0 "github.com/xanzy/go-gitlab"
oauth2 "golang.org/x/oauth2"
)
// MockGitlab is a mock of Gitlab interface.
type MockGitlab struct {
ctrl *gomock.Controller
recorder *MockGitlabMockRecorder
}
// MockGitlabMockRecorder is the mock recorder for MockGitlab.
type MockGitlabMockRecorder struct {
mock *MockGitlab
}
// NewMockGitlab creates a new mock instance.
func NewMockGitlab(ctrl *gomock.Controller) *MockGitlab {
mock := &MockGitlab{ctrl: ctrl}
mock.recorder = &MockGitlabMockRecorder{mock}
return mock
}
// EXPECT returns an object that allows the caller to indicate expected use.
func (m *MockGitlab) EXPECT() *MockGitlabMockRecorder {
return m.recorder
}
// GetCurrentUser mocks base method.
func (m *MockGitlab) GetCurrentUser(arg0 context.Context, arg1 string, arg2 oauth2.Token) (*gitlab.UserInfo, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetCurrentUser", arg0, arg1, arg2)
ret0, _ := ret[0].(*gitlab.UserInfo)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetCurrentUser indicates an expected call of GetCurrentUser.
func (mr *MockGitlabMockRecorder) GetCurrentUser(arg0, arg1, arg2 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetCurrentUser", reflect.TypeOf((*MockGitlab)(nil).GetCurrentUser), arg0, arg1, arg2)
}
// GetGroupHooks mocks base method.
func (m *MockGitlab) GetGroupHooks(arg0 context.Context, arg1 *gitlab.UserInfo, arg2 *oauth2.Token, arg3 string) ([]*gitlab.WebhookInfo, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetGroupHooks", arg0, arg1, arg2, arg3)
ret0, _ := ret[0].([]*gitlab.WebhookInfo)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetGroupHooks indicates an expected call of GetGroupHooks.
func (mr *MockGitlabMockRecorder) GetGroupHooks(arg0, arg1, arg2, arg3 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetGroupHooks", reflect.TypeOf((*MockGitlab)(nil).GetGroupHooks), arg0, arg1, arg2, arg3)
}
// GetIssueByID mocks base method.
func (m *MockGitlab) GetIssueByID(arg0 context.Context, arg1 *gitlab.UserInfo, arg2, arg3 string, arg4 int, arg5 *oauth2.Token) (*gitlab.Issue, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetIssueByID", arg0, arg1, arg2, arg3, arg4, arg5)
ret0, _ := ret[0].(*gitlab.Issue)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetIssueByID indicates an expected call of GetIssueByID.
func (mr *MockGitlabMockRecorder) GetIssueByID(arg0, arg1, arg2, arg3, arg4, arg5 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetIssueByID", reflect.TypeOf((*MockGitlab)(nil).GetIssueByID), arg0, arg1, arg2, arg3, arg4, arg5)
}
// GetLHSData mocks base method.
func (m *MockGitlab) GetLHSData(arg0 context.Context, arg1 *gitlab.UserInfo, arg2 *oauth2.Token) (*gitlab.LHSContent, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetLHSData", arg0, arg1, arg2)
ret0, _ := ret[0].(*gitlab.LHSContent)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetLHSData indicates an expected call of GetLHSData.
func (mr *MockGitlabMockRecorder) GetLHSData(arg0, arg1, arg2 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetLHSData", reflect.TypeOf((*MockGitlab)(nil).GetLHSData), arg0, arg1, arg2)
}
// GetMergeRequestByID mocks base method.
func (m *MockGitlab) GetMergeRequestByID(arg0 context.Context, arg1 *gitlab.UserInfo, arg2, arg3 string, arg4 int, arg5 *oauth2.Token) (*gitlab.MergeRequest, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetMergeRequestByID", arg0, arg1, arg2, arg3, arg4, arg5)
ret0, _ := ret[0].(*gitlab.MergeRequest)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetMergeRequestByID indicates an expected call of GetMergeRequestByID.
func (mr *MockGitlabMockRecorder) GetMergeRequestByID(arg0, arg1, arg2, arg3, arg4, arg5 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetMergeRequestByID", reflect.TypeOf((*MockGitlab)(nil).GetMergeRequestByID), arg0, arg1, arg2, arg3, arg4, arg5)
}
// GetProject mocks base method.
func (m *MockGitlab) GetProject(arg0 context.Context, arg1 *gitlab.UserInfo, arg2 *oauth2.Token, arg3, arg4 string) (*gitlab0.Project, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetProject", arg0, arg1, arg2, arg3, arg4)
ret0, _ := ret[0].(*gitlab0.Project)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetProject indicates an expected call of GetProject.
func (mr *MockGitlabMockRecorder) GetProject(arg0, arg1, arg2, arg3, arg4 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetProject", reflect.TypeOf((*MockGitlab)(nil).GetProject), arg0, arg1, arg2, arg3, arg4)
}
// GetProjectHooks mocks base method.
func (m *MockGitlab) GetProjectHooks(arg0 context.Context, arg1 *gitlab.UserInfo, arg2 *oauth2.Token, arg3, arg4 string) ([]*gitlab.WebhookInfo, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetProjectHooks", arg0, arg1, arg2, arg3, arg4)
ret0, _ := ret[0].([]*gitlab.WebhookInfo)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetProjectHooks indicates an expected call of GetProjectHooks.
func (mr *MockGitlabMockRecorder) GetProjectHooks(arg0, arg1, arg2, arg3, arg4 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetProjectHooks", reflect.TypeOf((*MockGitlab)(nil).GetProjectHooks), arg0, arg1, arg2, arg3, arg4)
}
// GetReviews mocks base method.
func (m *MockGitlab) GetReviews(arg0 context.Context, arg1 *gitlab.UserInfo, arg2 *gitlab0.Client) ([]*gitlab.MergeRequest, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetReviews", arg0, arg1, arg2)
ret0, _ := ret[0].([]*gitlab.MergeRequest)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetReviews indicates an expected call of GetReviews.
func (mr *MockGitlabMockRecorder) GetReviews(arg0, arg1, arg2 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetReviews", reflect.TypeOf((*MockGitlab)(nil).GetReviews), arg0, arg1, arg2)
}
// GetToDoList mocks base method.
func (m *MockGitlab) GetToDoList(arg0 context.Context, arg1 *gitlab.UserInfo, arg2 *gitlab0.Client) ([]*gitlab0.Todo, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetToDoList", arg0, arg1, arg2)
ret0, _ := ret[0].([]*gitlab0.Todo)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetToDoList indicates an expected call of GetToDoList.
func (mr *MockGitlabMockRecorder) GetToDoList(arg0, arg1, arg2 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetToDoList", reflect.TypeOf((*MockGitlab)(nil).GetToDoList), arg0, arg1, arg2)
}
// GetUserDetails mocks base method.
func (m *MockGitlab) GetUserDetails(arg0 context.Context, arg1 *gitlab.UserInfo, arg2 *oauth2.Token) (*gitlab0.User, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetUserDetails", arg0, arg1, arg2)
ret0, _ := ret[0].(*gitlab0.User)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetUserDetails indicates an expected call of GetUserDetails.
func (mr *MockGitlabMockRecorder) GetUserDetails(arg0, arg1, arg2 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetUserDetails", reflect.TypeOf((*MockGitlab)(nil).GetUserDetails), arg0, arg1, arg2)
}
// GetYourAssignedIssues mocks base method.
func (m *MockGitlab) GetYourAssignedIssues(arg0 context.Context, arg1 *gitlab.UserInfo, arg2 *gitlab0.Client) ([]*gitlab.Issue, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetYourAssignedIssues", arg0, arg1, arg2)
ret0, _ := ret[0].([]*gitlab.Issue)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetYourAssignedIssues indicates an expected call of GetYourAssignedIssues.
func (mr *MockGitlabMockRecorder) GetYourAssignedIssues(arg0, arg1, arg2 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetYourAssignedIssues", reflect.TypeOf((*MockGitlab)(nil).GetYourAssignedIssues), arg0, arg1, arg2)
}
// GetYourAssignedPrs mocks base method.
func (m *MockGitlab) GetYourAssignedPrs(arg0 context.Context, arg1 *gitlab.UserInfo, arg2 *gitlab0.Client) ([]*gitlab.MergeRequest, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetYourAssignedPrs", arg0, arg1, arg2)
ret0, _ := ret[0].([]*gitlab.MergeRequest)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetYourAssignedPrs indicates an expected call of GetYourAssignedPrs.
func (mr *MockGitlabMockRecorder) GetYourAssignedPrs(arg0, arg1, arg2 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetYourAssignedPrs", reflect.TypeOf((*MockGitlab)(nil).GetYourAssignedPrs), arg0, arg1, arg2)
}
// GetYourPrDetails mocks base method.
func (m *MockGitlab) GetYourPrDetails(arg0 context.Context, arg1 logger.Logger, arg2 *gitlab.UserInfo, arg3 *oauth2.Token, arg4 []*gitlab.PRDetails) ([]*gitlab.PRDetails, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GetYourPrDetails", arg0, arg1, arg2, arg3, arg4)
ret0, _ := ret[0].([]*gitlab.PRDetails)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GetYourPrDetails indicates an expected call of GetYourPrDetails.
func (mr *MockGitlabMockRecorder) GetYourPrDetails(arg0, arg1, arg2, arg3, arg4 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetYourPrDetails", reflect.TypeOf((*MockGitlab)(nil).GetYourPrDetails), arg0, arg1, arg2, arg3, arg4)
}
// GitlabConnect mocks base method.
func (m *MockGitlab) GitlabConnect(arg0 oauth2.Token) (*gitlab0.Client, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "GitlabConnect", arg0)
ret0, _ := ret[0].(*gitlab0.Client)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// GitlabConnect indicates an expected call of GitlabConnect.
func (mr *MockGitlabMockRecorder) GitlabConnect(arg0 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GitlabConnect", reflect.TypeOf((*MockGitlab)(nil).GitlabConnect), arg0)
}
// NewGroupHook mocks base method.
func (m *MockGitlab) NewGroupHook(arg0 context.Context, arg1 *gitlab.UserInfo, arg2 *oauth2.Token, arg3 string, arg4 *gitlab.AddWebhookOptions) (*gitlab.WebhookInfo, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "NewGroupHook", arg0, arg1, arg2, arg3, arg4)
ret0, _ := ret[0].(*gitlab.WebhookInfo)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// NewGroupHook indicates an expected call of NewGroupHook.
func (mr *MockGitlabMockRecorder) NewGroupHook(arg0, arg1, arg2, arg3, arg4 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewGroupHook", reflect.TypeOf((*MockGitlab)(nil).NewGroupHook), arg0, arg1, arg2, arg3, arg4)
}
// NewProjectHook mocks base method.
func (m *MockGitlab) NewProjectHook(arg0 context.Context, arg1 *gitlab.UserInfo, arg2 *oauth2.Token, arg3 interface{}, arg4 *gitlab.AddWebhookOptions) (*gitlab.WebhookInfo, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "NewProjectHook", arg0, arg1, arg2, arg3, arg4)
ret0, _ := ret[0].(*gitlab.WebhookInfo)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// NewProjectHook indicates an expected call of NewProjectHook.
func (mr *MockGitlabMockRecorder) NewProjectHook(arg0, arg1, arg2, arg3, arg4 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "NewProjectHook", reflect.TypeOf((*MockGitlab)(nil).NewProjectHook), arg0, arg1, arg2, arg3, arg4)
}
// ResolveNamespaceAndProject mocks base method.
func (m *MockGitlab) ResolveNamespaceAndProject(arg0 context.Context, arg1 *gitlab.UserInfo, arg2 *oauth2.Token, arg3 string, arg4 bool) (string, string, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "ResolveNamespaceAndProject", arg0, arg1, arg2, arg3, arg4)
ret0, _ := ret[0].(string)
ret1, _ := ret[1].(string)
ret2, _ := ret[2].(error)
return ret0, ret1, ret2
}
// ResolveNamespaceAndProject indicates an expected call of ResolveNamespaceAndProject.
func (mr *MockGitlabMockRecorder) ResolveNamespaceAndProject(arg0, arg1, arg2, arg3, arg4 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "ResolveNamespaceAndProject", reflect.TypeOf((*MockGitlab)(nil).ResolveNamespaceAndProject), arg0, arg1, arg2, arg3, arg4)
}
// TriggerProjectPipeline mocks base method.
func (m *MockGitlab) TriggerProjectPipeline(arg0 *gitlab.UserInfo, arg1 *oauth2.Token, arg2, arg3 string) (*gitlab.PipelineInfo, error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "TriggerProjectPipeline", arg0, arg1, arg2, arg3)
ret0, _ := ret[0].(*gitlab.PipelineInfo)
ret1, _ := ret[1].(error)
return ret0, ret1
}
// TriggerProjectPipeline indicates an expected call of TriggerProjectPipeline.
func (mr *MockGitlabMockRecorder) TriggerProjectPipeline(arg0, arg1, arg2, arg3 interface{}) *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "TriggerProjectPipeline", reflect.TypeOf((*MockGitlab)(nil).TriggerProjectPipeline), arg0, arg1, arg2, arg3)
}
+22
View File
@@ -0,0 +1,22 @@
package gitlab
// PipelineInfo is the struct describing the status of
// a running pipeline.
type PipelineInfo struct {
PipelineID int
Status string
Ref string
WebURL string
SHA string
User string
}
// PipelineJobInfo is the struct describing the status of
// a running Job which is part of a pipeline
type PipelineJobInfo struct {
JobID int
Status string
Ref string
WebURL string
User string
}
+70
View File
@@ -0,0 +1,70 @@
package gitlab
import (
"context"
"github.com/mattermost/mattermost/server/public/model"
internGitlab "github.com/xanzy/go-gitlab"
"golang.org/x/oauth2"
)
const SettingButtonsTeam = "team"
type UserInfo struct {
UserID string
GitlabUsername string
GitlabUserID int
LastToDoPostAt int64
Settings *UserSettings
AllowedPrivateRepos bool
}
type UserSettings struct {
SidebarButtons string `json:"sidebar_buttons"`
DailyReminder bool `json:"daily_reminder"`
Notifications bool `json:"notifications"`
}
func (g *gitlab) GetCurrentUser(ctx context.Context, userID string, token oauth2.Token) (*UserInfo, error) {
client, err := g.GitlabConnect(token)
if err != nil {
return nil, err
}
gitUser, resp, err := client.Users.CurrentUser(internGitlab.WithContext(ctx))
if respErr := checkResponse(resp); respErr != nil {
return nil, respErr
}
if err != nil {
return nil, err
}
return &UserInfo{
UserID: userID,
GitlabUserID: gitUser.ID,
GitlabUsername: gitUser.Username,
LastToDoPostAt: model.GetMillis(),
Settings: &UserSettings{
SidebarButtons: SettingButtonsTeam,
DailyReminder: true,
Notifications: true,
},
}, nil
}
func (g *gitlab) GetUserDetails(ctx context.Context, user *UserInfo, token *oauth2.Token) (*internGitlab.User, error) {
client, err := g.GitlabConnect(*token)
if err != nil {
return nil, err
}
gitUser, resp, err := client.Users.CurrentUser(internGitlab.WithContext(ctx))
if respErr := checkResponse(resp); respErr != nil {
return nil, respErr
}
if err != nil {
return nil, err
}
return gitUser, nil
}
+41
View File
@@ -0,0 +1,41 @@
package gitlab
import (
"time"
)
// WebhookInfo Provides information about group or project hooks.
type WebhookInfo struct {
ID int
URL string
ConfidentialNoteEvents bool
PushEvents bool
IssuesEvents bool
ConfidentialIssuesEvents bool
MergeRequestsEvents bool
TagPushEvents bool
NoteEvents bool
JobEvents bool
PipelineEvents bool
WikiPageEvents bool
EnableSSLVerification bool
CreatedAt *time.Time
Scope Scope
}
// AddWebhookOptions is a paramater object with options for creating a project or group hook.
type AddWebhookOptions struct {
URL string
ConfidentialNoteEvents bool
PushEvents bool
IssuesEvents bool
ConfidentialIssuesEvents bool
MergeRequestsEvents bool
TagPushEvents bool
NoteEvents bool
JobEvents bool
PipelineEvents bool
WikiPageEvents bool
EnableSSLVerification bool
Token string
}