Add support for migrating from Gitlab (#9084)
* First stab at a Gitlab migrations interface. * Modify JS to show migration for Gitlab * Properly strip out #gitlab tag from repo name * Working Gitlab migrations! Still need to figure out how to hide tokens/etc from showing up in opts.CloneAddr * Try #2 at trying to hide credentials. CloneAddr was being used as OriginalURL. Now passing OriginalURL through from the form and saving it. * Add go-gitlab dependency * Vendor go-gitlab * Use gitlab.BasicAuthClient Correct CloneURL. This should be functioning! Previous commits fixed "Migrated from" from including the migration credentials. * Replaced repoPath with repoID globally. RepoID is grabbed in NewGitlabDownloader * Logging touchup * Properly set private repo status. Properly set milestone deadline time. Consistently use Gitlab username for 'Name'. * Add go-gitlab vendor cache * Fix PR migrations: - Count of issues is kept to set a non-conflicting PR.ID - Bool is used to tell whether to fetch Issue or PR comments * Ensure merged PRs are closed and set with the proper time * Remove copyright and some commented code * Rip out '#gitlab' based self-hosted Gitlab support * Hide given credentials for migrated repos. CloneAddr was being saved as OriginalURL. Now passing OriginalURL through from the form and saving it in it's place * Use asset.URL directly, no point in parsing. Opened PRs should fall through to false. * Fix importing Milestones. Allow importing using Personal Tokens or anonymous access. * Fix Gitlab Milestone migration if DueDate isn't set * Empty Milestone due dates properly return nil, not zero time * Add GITLAB_READ_TOKEN to drone unit-test step * Add working gitlab_test.go. A Personal Access Token, given in env variable GITLAB_READ_TOKEN is required to run the test. * Fix linting issues * Add modified JS files * Remove pre-build JS files * Only merged PRs are marged as merged/closed * Test topics * Skip test if gitlab is inaccessible * Grab personal token from username, not password. Matches Github migration implementation * Add SetContext() to GitlabDownloader. * Checking Updated field in Issues. * Actually fetch Issue Updated time from Gitlab * Add Gitlab migration GetReviews() stub * Fix Patch and Clone URLs * check Updated too * fix mod * make vendor with go1.14 Co-authored-by: techknowlogick <techknowlogick@gitea.io> Co-authored-by: 6543 <6543@obermui.de> Co-authored-by: Lauris BH <lauris@nix.lv> Co-authored-by: Lunny Xiao <xiaolunwen@gmail.com>mj
parent
41f05588ed
commit
5c092eb0ef
@ -0,0 +1,539 @@
|
||||
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/modules/migrations/base"
|
||||
"code.gitea.io/gitea/modules/structs"
|
||||
|
||||
"github.com/xanzy/go-gitlab"
|
||||
)
|
||||
|
||||
var (
|
||||
_ base.Downloader = &GitlabDownloader{}
|
||||
_ base.DownloaderFactory = &GitlabDownloaderFactory{}
|
||||
)
|
||||
|
||||
func init() {
|
||||
RegisterDownloaderFactory(&GitlabDownloaderFactory{})
|
||||
}
|
||||
|
||||
// GitlabDownloaderFactory defines a gitlab downloader factory
|
||||
type GitlabDownloaderFactory struct {
|
||||
}
|
||||
|
||||
// Match returns ture if the migration remote URL matched this downloader factory
|
||||
func (f *GitlabDownloaderFactory) Match(opts base.MigrateOptions) (bool, error) {
|
||||
var matched bool
|
||||
|
||||
u, err := url.Parse(opts.CloneAddr)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if strings.EqualFold(u.Host, "gitlab.com") && opts.AuthUsername != "" {
|
||||
matched = true
|
||||
}
|
||||
|
||||
return matched, nil
|
||||
}
|
||||
|
||||
// New returns a Downloader related to this factory according MigrateOptions
|
||||
func (f *GitlabDownloaderFactory) New(opts base.MigrateOptions) (base.Downloader, error) {
|
||||
u, err := url.Parse(opts.CloneAddr)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
baseURL := u.Scheme + "://" + u.Host
|
||||
repoNameSpace := strings.TrimPrefix(u.Path, "/")
|
||||
|
||||
log.Trace("Create gitlab downloader. BaseURL: %s RepoName: %s", baseURL, repoNameSpace)
|
||||
|
||||
return NewGitlabDownloader(baseURL, repoNameSpace, opts.AuthUsername, opts.AuthPassword), nil
|
||||
}
|
||||
|
||||
// GitServiceType returns the type of git service
|
||||
func (f *GitlabDownloaderFactory) GitServiceType() structs.GitServiceType {
|
||||
return structs.GitlabService
|
||||
}
|
||||
|
||||
// GitlabDownloader implements a Downloader interface to get repository informations
|
||||
// from gitlab via go-gitlab
|
||||
// - issueCount is incremented in GetIssues() to ensure PR and Issue numbers do not overlap,
|
||||
// because Gitlab has individual Issue and Pull Request numbers.
|
||||
// - issueSeen, working alongside issueCount, is checked in GetComments() to see whether we
|
||||
// need to fetch the Issue or PR comments, as Gitlab stores them separately.
|
||||
type GitlabDownloader struct {
|
||||
ctx context.Context
|
||||
client *gitlab.Client
|
||||
repoID int
|
||||
repoName string
|
||||
issueCount int64
|
||||
fetchPRcomments bool
|
||||
}
|
||||
|
||||
// NewGitlabDownloader creates a gitlab Downloader via gitlab API
|
||||
// Use either a username/password, personal token entered into the username field, or anonymous/public access
|
||||
// Note: Public access only allows very basic access
|
||||
func NewGitlabDownloader(baseURL, repoPath, username, password string) *GitlabDownloader {
|
||||
var client *http.Client
|
||||
var gitlabClient *gitlab.Client
|
||||
var err error
|
||||
if username != "" {
|
||||
if password == "" {
|
||||
gitlabClient = gitlab.NewClient(client, username)
|
||||
} else {
|
||||
gitlabClient, err = gitlab.NewBasicAuthClient(client, baseURL, username, password)
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
log.Trace("Error logging into gitlab: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Grab and store project/repo ID here, due to issues using the URL escaped path
|
||||
gr, _, err := gitlabClient.Projects.GetProject(repoPath, nil, nil)
|
||||
if err != nil {
|
||||
log.Trace("Error retrieving project: %v", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
if gr == nil {
|
||||
log.Trace("Error getting project, project is nil")
|
||||
return nil
|
||||
}
|
||||
|
||||
return &GitlabDownloader{
|
||||
ctx: context.Background(),
|
||||
client: gitlabClient,
|
||||
repoID: gr.ID,
|
||||
repoName: gr.Name,
|
||||
}
|
||||
}
|
||||
|
||||
// SetContext set context
|
||||
func (g *GitlabDownloader) SetContext(ctx context.Context) {
|
||||
g.ctx = ctx
|
||||
}
|
||||
|
||||
// GetRepoInfo returns a repository information
|
||||
func (g *GitlabDownloader) GetRepoInfo() (*base.Repository, error) {
|
||||
if g == nil {
|
||||
return nil, errors.New("error: GitlabDownloader is nil")
|
||||
}
|
||||
|
||||
gr, _, err := g.client.Projects.GetProject(g.repoID, nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var private bool
|
||||
switch gr.Visibility {
|
||||
case gitlab.InternalVisibility:
|
||||
private = true
|
||||
case gitlab.PrivateVisibility:
|
||||
private = true
|
||||
}
|
||||
|
||||
var owner string
|
||||
if gr.Owner == nil {
|
||||
log.Trace("gr.Owner is nil, trying to get owner from Namespace")
|
||||
if gr.Namespace != nil && gr.Namespace.Kind == "user" {
|
||||
owner = gr.Namespace.Path
|
||||
}
|
||||
} else {
|
||||
owner = gr.Owner.Username
|
||||
}
|
||||
|
||||
// convert gitlab repo to stand Repo
|
||||
return &base.Repository{
|
||||
Owner: owner,
|
||||
Name: gr.Name,
|
||||
IsPrivate: private,
|
||||
Description: gr.Description,
|
||||
OriginalURL: gr.WebURL,
|
||||
CloneURL: gr.HTTPURLToRepo,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// GetTopics return gitlab topics
|
||||
func (g *GitlabDownloader) GetTopics() ([]string, error) {
|
||||
if g == nil {
|
||||
return nil, errors.New("error: GitlabDownloader is nil")
|
||||
}
|
||||
|
||||
gr, _, err := g.client.Projects.GetProject(g.repoID, nil, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return gr.TagList, err
|
||||
}
|
||||
|
||||
// GetMilestones returns milestones
|
||||
func (g *GitlabDownloader) GetMilestones() ([]*base.Milestone, error) {
|
||||
if g == nil {
|
||||
return nil, errors.New("error: GitlabDownloader is nil")
|
||||
}
|
||||
var perPage = 100
|
||||
var state = "all"
|
||||
var milestones = make([]*base.Milestone, 0, perPage)
|
||||
for i := 1; ; i++ {
|
||||
ms, _, err := g.client.Milestones.ListMilestones(g.repoID, &gitlab.ListMilestonesOptions{
|
||||
State: &state,
|
||||
ListOptions: gitlab.ListOptions{
|
||||
Page: i,
|
||||
PerPage: perPage,
|
||||
}}, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, m := range ms {
|
||||
var desc string
|
||||
if m.Description != "" {
|
||||
desc = m.Description
|
||||
}
|
||||
var state = "open"
|
||||
var closedAt *time.Time
|
||||
if m.State != "" {
|
||||
state = m.State
|
||||
if state == "closed" {
|
||||
closedAt = m.UpdatedAt
|
||||
}
|
||||
}
|
||||
|
||||
var deadline *time.Time
|
||||
if m.DueDate != nil {
|
||||
deadlineParsed, err := time.Parse("2006-01-02", m.DueDate.String())
|
||||
if err != nil {
|
||||
log.Trace("Error parsing Milestone DueDate time")
|
||||
deadline = nil
|
||||
} else {
|
||||
deadline = &deadlineParsed
|
||||
}
|
||||
}
|
||||
|
||||
milestones = append(milestones, &base.Milestone{
|
||||
Title: m.Title,
|
||||
Description: desc,
|
||||
Deadline: deadline,
|
||||
State: state,
|
||||
Created: *m.CreatedAt,
|
||||
Updated: m.UpdatedAt,
|
||||
Closed: closedAt,
|
||||
})
|
||||
}
|
||||
if len(ms) < perPage {
|
||||
break
|
||||
}
|
||||
}
|
||||
return milestones, nil
|
||||
}
|
||||
|
||||
// GetLabels returns labels
|
||||
func (g *GitlabDownloader) GetLabels() ([]*base.Label, error) {
|
||||
if g == nil {
|
||||
return nil, errors.New("error: GitlabDownloader is nil")
|
||||
}
|
||||
var perPage = 100
|
||||
var labels = make([]*base.Label, 0, perPage)
|
||||
for i := 1; ; i++ {
|
||||
ls, _, err := g.client.Labels.ListLabels(g.repoID, &gitlab.ListLabelsOptions{
|
||||
Page: i,
|
||||
PerPage: perPage,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, label := range ls {
|
||||
baseLabel := &base.Label{
|
||||
Name: label.Name,
|
||||
Color: strings.TrimLeft(label.Color, "#)"),
|
||||
Description: label.Description,
|
||||
}
|
||||
labels = append(labels, baseLabel)
|
||||
}
|
||||
if len(ls) < perPage {
|
||||
break
|
||||
}
|
||||
}
|
||||
return labels, nil
|
||||
}
|
||||
|
||||
func (g *GitlabDownloader) convertGitlabRelease(rel *gitlab.Release) *base.Release {
|
||||
|
||||
r := &base.Release{
|
||||
TagName: rel.TagName,
|
||||
TargetCommitish: rel.Commit.ID,
|
||||
Name: rel.Name,
|
||||
Body: rel.Description,
|
||||
Created: *rel.CreatedAt,
|
||||
PublisherID: int64(rel.Author.ID),
|
||||
PublisherName: rel.Author.Username,
|
||||
}
|
||||
|
||||
for k, asset := range rel.Assets.Links {
|
||||
r.Assets = append(r.Assets, base.ReleaseAsset{
|
||||
URL: asset.URL,
|
||||
Name: asset.Name,
|
||||
ContentType: &rel.Assets.Sources[k].Format,
|
||||
})
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
// GetReleases returns releases
|
||||
func (g *GitlabDownloader) GetReleases() ([]*base.Release, error) {
|
||||
var perPage = 100
|
||||
var releases = make([]*base.Release, 0, perPage)
|
||||
for i := 1; ; i++ {
|
||||
ls, _, err := g.client.Releases.ListReleases(g.repoID, &gitlab.ListReleasesOptions{
|
||||
Page: i,
|
||||
PerPage: perPage,
|
||||
}, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, release := range ls {
|
||||
releases = append(releases, g.convertGitlabRelease(release))
|
||||
}
|
||||
if len(ls) < perPage {
|
||||
break
|
||||
}
|
||||
}
|
||||
return releases, nil
|
||||
}
|
||||
|
||||
// GetIssues returns issues according start and limit
|
||||
// Note: issue label description and colors are not supported by the go-gitlab library at this time
|
||||
// TODO: figure out how to transfer issue reactions
|
||||
func (g *GitlabDownloader) GetIssues(page, perPage int) ([]*base.Issue, bool, error) {
|
||||
state := "all"
|
||||
sort := "asc"
|
||||
|
||||
opt := &gitlab.ListProjectIssuesOptions{
|
||||
State: &state,
|
||||
Sort: &sort,
|
||||
ListOptions: gitlab.ListOptions{
|
||||
PerPage: perPage,
|
||||
Page: page,
|
||||
},
|
||||
}
|
||||
|
||||
var allIssues = make([]*base.Issue, 0, perPage)
|
||||
|
||||
issues, _, err := g.client.Issues.ListProjectIssues(g.repoID, opt, nil)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf("error while listing issues: %v", err)
|
||||
}
|
||||
for _, issue := range issues {
|
||||
|
||||
var labels = make([]*base.Label, 0, len(issue.Labels))
|
||||
for _, l := range issue.Labels {
|
||||
labels = append(labels, &base.Label{
|
||||
Name: l,
|
||||
})
|
||||
}
|
||||
|
||||
var milestone string
|
||||
if issue.Milestone != nil {
|
||||
milestone = issue.Milestone.Title
|
||||
}
|
||||
|
||||
allIssues = append(allIssues, &base.Issue{
|
||||
Title: issue.Title,
|
||||
Number: int64(issue.IID),
|
||||
PosterID: int64(issue.Author.ID),
|
||||
PosterName: issue.Author.Username,
|
||||
Content: issue.Description,
|
||||
Milestone: milestone,
|
||||
State: issue.State,
|
||||
Created: *issue.CreatedAt,
|
||||
Labels: labels,
|
||||
Closed: issue.ClosedAt,
|
||||
IsLocked: issue.DiscussionLocked,
|
||||
Updated: *issue.UpdatedAt,
|
||||
})
|
||||
|
||||
// increment issueCount, to be used in GetPullRequests()
|
||||
g.issueCount++
|
||||
}
|
||||
|
||||
return allIssues, len(issues) < perPage, nil
|
||||
}
|
||||
|
||||
// GetComments returns comments according issueNumber
|
||||
func (g *GitlabDownloader) GetComments(issueNumber int64) ([]*base.Comment, error) {
|
||||
var allComments = make([]*base.Comment, 0, 100)
|
||||
|
||||
var page = 1
|
||||
var realIssueNumber int64
|
||||
|
||||
for {
|
||||
var comments []*gitlab.Discussion
|
||||
var resp *gitlab.Response
|
||||
var err error
|
||||
// fetchPRcomments decides whether to fetch Issue or PR comments
|
||||
if !g.fetchPRcomments {
|
||||
realIssueNumber = issueNumber
|
||||
comments, resp, err = g.client.Discussions.ListIssueDiscussions(g.repoID, int(realIssueNumber), &gitlab.ListIssueDiscussionsOptions{
|
||||
Page: page,
|
||||
PerPage: 100,
|
||||
}, nil)
|
||||
} else {
|
||||
// If this is a PR, we need to figure out the Gitlab/original PR ID to be passed below
|
||||
realIssueNumber = issueNumber - g.issueCount
|
||||
comments, resp, err = g.client.Discussions.ListMergeRequestDiscussions(g.repoID, int(realIssueNumber), &gitlab.ListMergeRequestDiscussionsOptions{
|
||||
Page: page,
|
||||
PerPage: 100,
|
||||
}, nil)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error while listing comments: %v %v", g.repoID, err)
|
||||
}
|
||||
for _, comment := range comments {
|
||||
// Flatten comment threads
|
||||
if !comment.IndividualNote {
|
||||
for _, note := range comment.Notes {
|
||||
allComments = append(allComments, &base.Comment{
|
||||
IssueIndex: realIssueNumber,
|
||||
PosterID: int64(note.Author.ID),
|
||||
PosterName: note.Author.Username,
|
||||
PosterEmail: note.Author.Email,
|
||||
Content: note.Body,
|
||||
Created: *note.CreatedAt,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
c := comment.Notes[0]
|
||||
allComments = append(allComments, &base.Comment{
|
||||
IssueIndex: realIssueNumber,
|
||||
PosterID: int64(c.Author.ID),
|
||||
PosterName: c.Author.Username,
|
||||
PosterEmail: c.Author.Email,
|
||||
Content: c.Body,
|
||||
Created: *c.CreatedAt,
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
if resp.NextPage == 0 {
|
||||
break
|
||||
}
|
||||
page = resp.NextPage
|
||||
}
|
||||
return allComments, nil
|
||||
}
|
||||
|
||||
// GetPullRequests returns pull requests according page and perPage
|
||||
func (g *GitlabDownloader) GetPullRequests(page, perPage int) ([]*base.PullRequest, error) {
|
||||
|
||||
opt := &gitlab.ListProjectMergeRequestsOptions{
|
||||
ListOptions: gitlab.ListOptions{
|
||||
PerPage: perPage,
|
||||
Page: page,
|
||||
},
|
||||
}
|
||||
|
||||
// Set fetchPRcomments to true here, so PR comments are fetched instead of Issue comments
|
||||
g.fetchPRcomments = true
|
||||
|
||||
var allPRs = make([]*base.PullRequest, 0, perPage)
|
||||
|
||||
prs, _, err := g.client.MergeRequests.ListProjectMergeRequests(g.repoID, opt, nil)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error while listing merge requests: %v", err)
|
||||
}
|
||||
for _, pr := range prs {
|
||||
|
||||
var labels = make([]*base.Label, 0, len(pr.Labels))
|
||||
for _, l := range pr.Labels {
|
||||
labels = append(labels, &base.Label{
|
||||
Name: l,
|
||||
})
|
||||
}
|
||||
|
||||
var merged bool
|
||||
if pr.State == "merged" {
|
||||
merged = true
|
||||
pr.State = "closed"
|
||||
}
|
||||
|
||||
var mergeTime = pr.MergedAt
|
||||
if merged && pr.MergedAt == nil {
|
||||
mergeTime = pr.UpdatedAt
|
||||
}
|
||||
|
||||
var closeTime = pr.ClosedAt
|
||||
if merged && pr.ClosedAt == nil {
|
||||
closeTime = pr.UpdatedAt
|
||||
}
|
||||
|
||||
var locked bool
|
||||
if pr.State == "locked" {
|
||||
locked = true
|
||||
}
|
||||
|
||||
var milestone string
|
||||
if pr.Milestone != nil {
|
||||
milestone = pr.Milestone.Title
|
||||
}
|
||||
|
||||
// Add the PR ID to the Issue Count because PR and Issues share ID space in Gitea
|
||||
newPRnumber := g.issueCount + int64(pr.IID)
|
||||
|
||||
allPRs = append(allPRs, &base.PullRequest{
|
||||
Title: pr.Title,
|
||||
Number: int64(newPRnumber),
|
||||
PosterName: pr.Author.Username,
|
||||
PosterID: int64(pr.Author.ID),
|
||||
Content: pr.Description,
|
||||
Milestone: milestone,
|
||||
State: pr.State,
|
||||
Created: *pr.CreatedAt,
|
||||
Closed: closeTime,
|
||||
Labels: labels,
|
||||
Merged: merged,
|
||||
MergeCommitSHA: pr.MergeCommitSHA,
|
||||
MergedTime: mergeTime,
|
||||
IsLocked: locked,
|
||||
Head: base.PullRequestBranch{
|
||||
Ref: pr.SourceBranch,
|
||||
SHA: pr.SHA,
|
||||
RepoName: g.repoName,
|
||||
OwnerName: pr.Author.Username,
|
||||
CloneURL: pr.WebURL,
|
||||
},
|
||||
Base: base.PullRequestBranch{
|
||||
Ref: pr.TargetBranch,
|
||||
SHA: pr.DiffRefs.BaseSha,
|
||||
RepoName: g.repoName,
|
||||
OwnerName: pr.Author.Username,
|
||||
},
|
||||
PatchURL: pr.WebURL + ".patch",
|
||||
})
|
||||
}
|
||||
|
||||
return allPRs, nil
|
||||
}
|
||||
|
||||
// GetReviews returns pull requests review
|
||||
func (g *GitlabDownloader) GetReviews(pullRequestNumber int64) ([]*base.Review, error) {
|
||||
|
||||
return nil, nil
|
||||
}
|
@ -0,0 +1,246 @@
|
||||
// Copyright 2019 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"code.gitea.io/gitea/modules/migrations/base"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGitlabDownloadRepo(t *testing.T) {
|
||||
// Skip tests if Gitlab token is not found
|
||||
gitlabPersonalAccessToken := os.Getenv("GITLAB_READ_TOKEN")
|
||||
if gitlabPersonalAccessToken == "" {
|
||||
t.Skip("skipped test because GITLAB_READ_TOKEN was not in the environment")
|
||||
}
|
||||
|
||||
resp, err := http.Get("https://gitlab.com/gitea/test_repo")
|
||||
if err != nil || resp.StatusCode != 200 {
|
||||
t.Skipf("Can't access test repo, skipping %s", t.Name())
|
||||
}
|
||||
|
||||
downloader := NewGitlabDownloader("https://gitlab.com", "gitea/test_repo", gitlabPersonalAccessToken, "")
|
||||
if downloader == nil {
|
||||
t.Fatal("NewGitlabDownloader is nil")
|
||||
}
|
||||
repo, err := downloader.GetRepoInfo()
|
||||
assert.NoError(t, err)
|
||||
// Repo Owner is blank in Gitlab Group repos
|
||||
assert.EqualValues(t, &base.Repository{
|
||||
Name: "test_repo",
|
||||
Owner: "",
|
||||
Description: "Test repository for testing migration from gitlab to gitea",
|
||||
CloneURL: "https://gitlab.com/gitea/test_repo.git",
|
||||
OriginalURL: "https://gitlab.com/gitea/test_repo",
|
||||
}, repo)
|
||||
|
||||
topics, err := downloader.GetTopics()
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, len(topics) == 2)
|
||||
assert.EqualValues(t, []string{"migration", "test"}, topics)
|
||||
|
||||
milestones, err := downloader.GetMilestones()
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, len(milestones) >= 2)
|
||||
|
||||
for _, milestone := range milestones {
|
||||
switch milestone.Title {
|
||||
case "1.0":
|
||||
assertMilestoneEqual(t, "", "1.0",
|
||||
"",
|
||||
"2019-11-28 08:42:30.301 +0000 UTC",
|
||||
"2019-11-28 15:57:52.401 +0000 UTC",
|
||||
"",
|
||||
"closed", milestone)
|
||||
case "1.1.0":
|
||||
assertMilestoneEqual(t, "", "1.1.0",
|
||||
"",
|
||||
"2019-11-28 08:42:44.575 +0000 UTC",
|
||||
"2019-11-28 08:42:44.575 +0000 UTC",
|
||||
"",
|
||||
"active", milestone)
|
||||
}
|
||||
}
|
||||
|
||||
labels, err := downloader.GetLabels()
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, len(labels) >= 9)
|
||||
for _, l := range labels {
|
||||
switch l.Name {
|
||||
case "bug":
|
||||
assertLabelEqual(t, "bug", "d9534f", "", l)
|
||||
case "documentation":
|
||||
assertLabelEqual(t, "documentation", "f0ad4e", "", l)
|
||||
case "confirmed":
|
||||
assertLabelEqual(t, "confirmed", "d9534f", "", l)
|
||||
case "enhancement":
|
||||
assertLabelEqual(t, "enhancement", "5cb85c", "", l)
|
||||
case "critical":
|
||||
assertLabelEqual(t, "critical", "d9534f", "", l)
|
||||
case "discussion":
|
||||
assertLabelEqual(t, "discussion", "428bca", "", l)
|
||||
case "suggestion":
|
||||
assertLabelEqual(t, "suggestion", "428bca", "", l)
|
||||
case "support":
|
||||
assertLabelEqual(t, "support", "f0ad4e", "", l)
|
||||
case "duplicate":
|
||||
assertLabelEqual(t, "duplicate", "7F8C8D", "", l)
|
||||
}
|
||||
}
|
||||
|
||||
releases, err := downloader.GetReleases()
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, []*base.Release{
|
||||
{
|
||||
TagName: "v0.9.99",
|
||||
TargetCommitish: "0720a3ec57c1f843568298117b874319e7deee75",
|
||||
Name: "First Release",
|
||||
Body: "A test release",
|
||||
Created: time.Date(2019, 11, 28, 9, 9, 48, 840000000, time.UTC),
|
||||
PublisherID: 1241334,
|
||||
PublisherName: "lafriks",
|
||||
},
|
||||
}, releases[len(releases)-1:])
|
||||
|
||||
// downloader.GetIssues()
|
||||
issues, isEnd, err := downloader.GetIssues(1, 2)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 2, len(issues))
|
||||
assert.False(t, isEnd)
|
||||
|
||||
var (
|
||||
closed1 = time.Date(2019, 11, 28, 8, 46, 23, 275000000, time.UTC)
|
||||
closed2 = time.Date(2019, 11, 28, 8, 45, 44, 959000000, time.UTC)
|
||||
)
|
||||
assert.EqualValues(t, []*base.Issue{
|
||||
{
|
||||
Number: 1,
|
||||
Title: "Please add an animated gif icon to the merge button",
|
||||
Content: "I just want the merge button to hurt my eyes a little. :stuck_out_tongue_closed_eyes:",
|
||||
Milestone: "1.0.0",
|
||||
PosterID: 1241334,
|
||||
PosterName: "lafriks",
|
||||
State: "closed",
|
||||
Created: time.Date(2019, 11, 28, 8, 43, 35, 459000000, time.UTC),
|
||||
Updated: time.Date(2019, 11, 28, 8, 46, 23, 275000000, time.UTC),
|
||||
Labels: []*base.Label{
|
||||
{
|
||||
Name: "bug",
|
||||
},
|
||||
{
|
||||
Name: "discussion",
|
||||
},
|
||||
},
|
||||
Reactions: nil,
|
||||
Closed: &closed1,
|
||||
},
|
||||
{
|
||||
Number: 2,
|
||||
Title: "Test issue",
|
||||
Content: "This is test issue 2, do not touch!",
|
||||
Milestone: "1.1.0",
|
||||
PosterID: 1241334,
|
||||
PosterName: "lafriks",
|
||||
State: "closed",
|
||||
Created: time.Date(2019, 11, 28, 8, 44, 46, 277000000, time.UTC),
|
||||
Updated: time.Date(2019, 11, 28, 8, 45, 44, 987000000, time.UTC),
|
||||
Labels: []*base.Label{
|
||||
{
|
||||
Name: "duplicate",
|
||||
},
|
||||
},
|
||||
Reactions: nil,
|
||||
Closed: &closed2,
|
||||
},
|
||||
}, issues)
|
||||
|
||||
// downloader.GetComments()
|
||||
comments, err := downloader.GetComments(2)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 4, len(comments))
|
||||
assert.EqualValues(t, []*base.Comment{
|
||||
{
|
||||
IssueIndex: 2,
|
||||
PosterID: 1241334,
|
||||
PosterName: "lafriks",
|
||||
Created: time.Date(2019, 11, 28, 8, 44, 52, 501000000, time.UTC),
|
||||
Updated: time.Date(2019, 11, 28, 8, 44, 52, 501000000, time.UTC),
|
||||
Content: "This is a comment",
|
||||
Reactions: nil,
|
||||
},
|
||||
{
|
||||
IssueIndex: 2,
|
||||
PosterID: 1241334,
|
||||
PosterName: "lafriks",
|
||||
Created: time.Date(2019, 11, 28, 8, 45, 2, 329000000, time.UTC),
|
||||
Content: "changed milestone to %2",
|
||||
Reactions: nil,
|
||||
},
|
||||
{
|
||||
IssueIndex: 2,
|
||||
PosterID: 1241334,
|
||||
PosterName: "lafriks",
|
||||
Created: time.Date(2019, 11, 28, 8, 45, 45, 7000000, time.UTC),
|
||||
Content: "closed",
|
||||
Reactions: nil,
|
||||
},
|
||||
{
|
||||
IssueIndex: 2,
|
||||
PosterID: 1241334,
|
||||
PosterName: "lafriks",
|
||||
Created: time.Date(2019, 11, 28, 8, 45, 53, 501000000, time.UTC),
|
||||
Content: "A second comment",
|
||||
Reactions: nil,
|
||||
},
|
||||
}, comments[:4])
|
||||
|
||||
// downloader.GetPullRequests()
|
||||
prs, err := downloader.GetPullRequests(1, 1)
|
||||
assert.NoError(t, err)
|
||||
assert.EqualValues(t, 1, len(prs))
|
||||
|
||||
assert.EqualValues(t, []*base.PullRequest{
|
||||
{
|
||||
Number: 4,
|
||||
Title: "Test branch",
|
||||
Content: "do not merge this PR",
|
||||
Milestone: "1.0.0",
|
||||
PosterID: 1241334,
|
||||
PosterName: "lafriks",
|
||||
State: "opened",
|
||||
Created: time.Date(2019, 11, 28, 15, 56, 54, 104000000, time.UTC),
|
||||
Updated: time.Date(2019, 11, 28, 15, 56, 54, 104000000, time.UTC),
|
||||
Labels: []*base.Label{
|
||||
{
|
||||
Name: "bug",
|
||||
},
|
||||
},
|
||||
PatchURL: "https://gitlab.com/gitea/test_repo/-/merge_requests/2.patch",
|
||||
Head: base.PullRequestBranch{
|
||||
Ref: "feat/test",
|
||||
CloneURL: "https://gitlab.com/gitea/test_repo/-/merge_requests/2",
|
||||
SHA: "9f733b96b98a4175276edf6a2e1231489c3bdd23",
|
||||
RepoName: "test_repo",
|
||||
OwnerName: "lafriks",
|
||||
},
|
||||
Base: base.PullRequestBranch{
|
||||
Ref: "master",
|
||||
SHA: "",
|
||||
OwnerName: "lafriks",
|
||||
RepoName: "test_repo",
|
||||
},
|
||||
Closed: nil,
|
||||
Merged: false,
|
||||
MergedTime: nil,
|
||||
MergeCommitSHA: "",
|
||||
},
|
||||
}, prs)
|
||||
}
|
@ -0,0 +1,28 @@
|
||||
# Compiled Object files, Static and Dynamic libs (Shared Objects)
|
||||
*.o
|
||||
*.a
|
||||
*.so
|
||||
|
||||
# Folders
|
||||
_obj
|
||||
_test
|
||||
|
||||
# Architecture specific extensions/prefixes
|
||||
*.[568vq]
|
||||
[568vq].out
|
||||
|
||||
*.cgo1.go
|
||||
*.cgo2.c
|
||||
_cgo_defun.c
|
||||
_cgo_gotypes.go
|
||||
_cgo_export.*
|
||||
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
*.test
|
||||
*.prof
|
||||
|
||||
# IDE specific files and folders
|
||||
.idea
|
||||
*.iml
|
@ -0,0 +1,28 @@
|
||||
language: go
|
||||
|
||||
go:
|
||||
- 1.10.x
|
||||
- 1.11.x
|
||||
- 1.12.x
|
||||
- 1.13.x
|
||||
- master
|
||||
|
||||
stages:
|
||||
- lint
|
||||
- test
|
||||
|
||||
jobs:
|
||||
include:
|
||||
- stage: lint
|
||||
script:
|
||||
- go get golang.org/x/lint/golint
|
||||
- golint -set_exit_status
|
||||
- go vet -v
|
||||
- stage: test
|
||||
script:
|
||||
- go test -v
|
||||
|
||||
matrix:
|
||||
allow_failures:
|
||||
- go: master
|
||||
fast_finish: true
|
@ -0,0 +1,27 @@
|
||||
go-github CHANGELOG
|
||||
===================
|
||||
|
||||
0.6.0
|
||||
-----
|
||||
- Add support for the V4 Gitlab API. This means the older V3 API is no longer fully supported
|
||||
with this version. If you still need that version, please use the `f-api-v3` branch.
|
||||
|
||||
0.4.0
|
||||
-----
|
||||
- Add support to use [`sudo`](https://docs.gitlab.com/ce/api/README.html#sudo) for all API calls.
|
||||
- Add support for the Notification Settings API.
|
||||
- Add support for the Time Tracking API.
|
||||
- Make sure that the error response correctly outputs any returned errors.
|
||||
- And a reasonable number of smaller enhanchements and bugfixes.
|
||||
|
||||
0.3.0
|
||||
-----
|
||||
- Moved the tags related API calls to their own service, following the Gitlab API structure.
|
||||
|
||||
0.2.0
|
||||
-----
|
||||
- Convert all Option structs to use pointers for their fields.
|
||||
|
||||
0.1.0
|
||||
-----
|
||||
- Initial release.
|
@ -0,0 +1,202 @@
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
|
||||
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||
|
||||
1. Definitions.
|
||||
|
||||
"License" shall mean the terms and conditions for use, reproduction,
|
||||
and distribution as defined by Sections 1 through 9 of this document.
|
||||
|
||||
"Licensor" shall mean the copyright owner or entity authorized by
|
||||
the copyright owner that is granting the License.
|
||||
|
||||
"Legal Entity" shall mean the union of the acting entity and all
|
||||
other entities that control, are controlled by, or are under common
|
||||
control with that entity. For the purposes of this definition,
|
||||
"control" means (i) the power, direct or indirect, to cause the
|
||||
direction or management of such entity, whether by contract or
|
||||
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||
|
||||
"You" (or "Your") shall mean an individual or Legal Entity
|
||||
exercising permissions granted by this License.
|
||||
|
||||
"Source" form shall mean the preferred form for making modifications,
|
||||
including but not limited to software source code, documentation
|
||||
source, and configuration files.
|
||||
|
||||
"Object" form shall mean any form resulting from mechanical
|
||||
transformation or translation of a Source form, including but
|
||||
not limited to compiled object code, generated documentation,
|
||||
and conversions to other media types.
|
||||
|
||||
"Work" shall mean the work of authorship, whether in Source or
|
||||
Object form, made available under the License, as indicated by a
|
||||
copyright notice that is included in or attached to the work
|
||||
(an example is provided in the Appendix below).
|
||||
|
||||
"Derivative Works" shall mean any work, whether in Source or Object
|
||||
form, that is based on (or derived from) the Work and for which the
|
||||
editorial revisions, annotations, elaborations, or other modifications
|
||||
represent, as a whole, an original work of authorship. For the purposes
|
||||
of this License, Derivative Works shall not include works that remain
|
||||
separable from, or merely link (or bind by name) to the interfaces of,
|
||||
the Work and Derivative Works thereof.
|
||||
|
||||
"Contribution" shall mean any work of authorship, including
|
||||
the original version of the Work and any modifications or additions
|
||||
to that Work or Derivative Works thereof, that is intentionally
|
||||
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||
or by an individual or Legal Entity authorized to submit on behalf of
|
||||
the copyright owner. For the purposes of this definition, "submitted"
|
||||
means any form of electronic, verbal, or written communication sent
|
||||
to the Licensor or its representatives, including but not limited to
|
||||
communication on electronic mailing lists, source code control systems,
|
||||
and issue tracking systems that are managed by, or on behalf of, the
|
||||
Licensor for the purpose of discussing and improving the Work, but
|
||||
excluding communication that is conspicuously marked or otherwise
|
||||
designated in writing by the copyright owner as "Not a Contribution."
|
||||
|
||||
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||
on behalf of whom a Contribution has been received by Licensor and
|
||||
subsequently incorporated within the Work.
|
||||
|
||||
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
copyright license to reproduce, prepare Derivative Works of,
|
||||
publicly display, publicly perform, sublicense, and distribute the
|
||||
Work and such Derivative Works in Source or Object form.
|
||||
|
||||
3. Grant of Patent License. Subject to the terms and conditions of
|
||||
this License, each Contributor hereby grants to You a perpetual,
|
||||
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||
(except as stated in this section) patent license to make, have made,
|
||||
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||
where such license applies only to those patent claims licensable
|
||||
by such Contributor that are necessarily infringed by their
|
||||
Contribution(s) alone or by combination of their Contribution(s)
|
||||
with the Work to which such Contribution(s) was submitted. If You
|
||||
institute patent litigation against any entity (including a
|
||||
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||
or a Contribution incorporated within the Work constitutes direct
|
||||
or contributory patent infringement, then any patent licenses
|
||||
granted to You under this License for that Work shall terminate
|
||||
as of the date such litigation is filed.
|
||||
|
||||
4. Redistribution. You may reproduce and distribute copies of the
|
||||
Work or Derivative Works thereof in any medium, with or without
|
||||
modifications, and in Source or Object form, provided that You
|
||||
meet the following conditions:
|
||||
|
||||
(a) You must give any other recipients of the Work or
|
||||
Derivative Works a copy of this License; and
|
||||
|
||||
(b) You must cause any modified files to carry prominent notices
|
||||
stating that You changed the files; and
|
||||
|
||||
(c) You must retain, in the Source form of any Derivative Works
|
||||
that You distribute, all copyright, patent, trademark, and
|
||||
attribution notices from the Source form of the Work,
|
||||
excluding those notices that do not pertain to any part of
|
||||
the Derivative Works; and
|
||||
|
||||
(d) If the Work includes a "NOTICE" text file as part of its
|
||||
distribution, then any Derivative Works that You distribute must
|
||||
include a readable copy of the attribution notices contained
|
||||
within such NOTICE file, excluding those notices that do not
|
||||
pertain to any part of the Derivative Works, in at least one
|
||||
of the following places: within a NOTICE text file distributed
|
||||
as part of the Derivative Works; within the Source form or
|
||||
documentation, if provided along with the Derivative Works; or,
|
||||
within a display generated by the Derivative Works, if and
|
||||
wherever such third-party notices normally appear. The contents
|
||||
of the NOTICE file are for informational purposes only and
|
||||
do not modify the License. You may add Your own attribution
|
||||
notices within Derivative Works that You distribute, alongside
|
||||
or as an addendum to the NOTICE text from the Work, provided
|
||||
that such additional attribution notices cannot be construed
|
||||
as modifying the License.
|
||||
|
||||
You may add Your own copyright statement to Your modifications and
|
||||
may provide additional or different license terms and conditions
|
||||
for use, reproduction, or distribution of Your modifications, or
|
||||
for any such Derivative Works as a whole, provided Your use,
|
||||
reproduction, and distribution of the Work otherwise complies with
|
||||
the conditions stated in this License.
|
||||
|
||||
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||
any Contribution intentionally submitted for inclusion in the Work
|
||||
by You to the Licensor shall be under the terms and conditions of
|
||||
this License, without any additional terms or conditions.
|
||||
Notwithstanding the above, nothing herein shall supersede or modify
|
||||
the terms of any separate license agreement you may have executed
|
||||
with Licensor regarding such Contributions.
|
||||
|
||||
6. Trademarks. This License does not grant permission to use the trade
|
||||
names, trademarks, service marks, or product names of the Licensor,
|
||||
except as required for reasonable and customary use in describing the
|
||||
origin of the Work and reproducing the content of the NOTICE file.
|
||||
|
||||
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||
agreed to in writing, Licensor provides the Work (and each
|
||||
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||
implied, including, without limitation, any warranties or conditions
|
||||
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||
appropriateness of using or redistributing the Work and assume any
|
||||
risks associated with Your exercise of permissions under this License.
|
||||
|
||||
8. Limitation of Liability. In no event and under no legal theory,
|
||||
whether in tort (including negligence), contract, or otherwise,
|
||||
unless required by applicable law (such as deliberate and grossly
|
||||
negligent acts) or agreed to in writing, shall any Contributor be
|
||||
liable to You for damages, including any direct, indirect, special,
|
||||
incidental, or consequential damages of any character arising as a
|
||||
result of this License or out of the use or inability to use the
|
||||
Work (including but not limited to damages for loss of goodwill,
|
||||
work stoppage, computer failure or malfunction, or any and all
|
||||
other commercial damages or losses), even if such Contributor
|
||||
has been advised of the possibility of such damages.
|
||||
|
||||
9. Accepting Warranty or Additional Liability. While redistributing
|
||||
the Work or Derivative Works thereof, You may choose to offer,
|
||||
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||
or other liability obligations and/or rights consistent with this
|
||||
License. However, in accepting such obligations, You may act only
|
||||
on Your own behalf and on Your sole responsibility, not on behalf
|
||||
of any other Contributor, and only if You agree to indemnify,
|
||||
defend, and hold each Contributor harmless for any liability
|
||||
incurred by, or claims asserted against, such Contributor by reason
|
||||
of your accepting any such warranty or additional liability.
|
||||
|
||||
END OF TERMS AND CONDITIONS
|
||||
|
||||
APPENDIX: How to apply the Apache License to your work.
|
||||
|
||||
To apply the Apache License to your work, attach the following
|
||||
boilerplate notice, with the fields enclosed by brackets "{}"
|
||||
replaced with your own identifying information. (Don't include
|
||||
the brackets!) The text should be enclosed in the appropriate
|
||||
comment syntax for the file format. We also recommend that a
|
||||
file or class name and description of purpose be included on the
|
||||
same "printed page" as the copyright notice for easier
|
||||
identification within third-party archives.
|
||||
|
||||
Copyright {yyyy} {name of copyright owner}
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
@ -0,0 +1,173 @@
|
||||
# go-gitlab
|
||||
|
||||
A GitLab API client enabling Go programs to interact with GitLab in a simple and uniform way
|
||||
|
||||
[](https://travis-ci.org/xanzy/go-gitlab)
|
||||
[](https://github.com/xanzy/go-gitlab/blob/master/LICENSE)
|
||||
[](https://sourcegraph.com/github.com/xanzy/go-gitlab?badge)
|
||||
[](https://godoc.org/github.com/xanzy/go-gitlab)
|
||||
[](https://goreportcard.com/report/github.com/xanzy/go-gitlab)
|
||||
[](https://github.com/xanzy/go-gitlab/issues)
|
||||
|
||||
## NOTE
|
||||
|
||||
Release v0.6.0 (released on 25-08-2017) no longer supports the older V3 Gitlab API. If
|
||||
you need V3 support, please use the `f-api-v3` branch. This release contains some backwards
|
||||
incompatible changes that were needed to fully support the V4 Gitlab API.
|
||||
|
||||
## Coverage
|
||||
|
||||
This API client package covers most of the existing Gitlab API calls and is updated regularly
|
||||
to add new and/or missing endpoints. Currently the following services are supported:
|
||||
|
||||
- [x] Award Emojis
|
||||
- [x] Branches
|
||||
- [x] Broadcast Messages
|
||||
- [x] Commits
|
||||
- [x] Container Registry
|
||||
- [x] Custom Attributes
|
||||
- [x] Deploy Keys
|
||||
- [x] Deployments
|
||||
- [ ] Discussions (threaded comments)
|
||||
- [x] Environments
|
||||
- [ ] Epic Issues
|
||||
- [ ] Epics
|
||||
- [x] Events
|
||||
- [x] Feature Flags
|
||||
- [ ] Geo Nodes
|
||||
- [x] GitLab CI Config Templates
|
||||
- [x] Gitignores Templates
|
||||
- [x] Group Access Requests
|
||||
- [x] Group Issue Boards
|
||||
- [x] Group Members
|
||||
- [x] Group Milestones
|
||||
- [x] Group-Level Variables
|
||||
- [x] Groups
|
||||
- [x] Issue Boards
|
||||
- [x] Issues
|
||||
- [x] Jobs
|
||||
- [x] Keys
|
||||
- [x] Labels
|
||||
- [x] License
|
||||
- [x] Merge Request Approvals
|
||||
- [x] Merge Requests
|
||||
- [x] Namespaces
|
||||
- [x] Notes (comments)
|
||||
- [x] Notification Settings
|
||||
- [x] Open Source License Templates
|
||||
- [x] Pages Domains
|
||||
- [x] Pipeline Schedules
|
||||
- [x] Pipeline Triggers
|
||||
- [x] Pipelines
|
||||
- [x] Project Access Requests
|
||||
- [x] Project Badges
|
||||
- [x] Project Clusters
|
||||
- [x] Project Import/export
|
||||
- [x] Project Members
|
||||
- [x] Project Milestones
|
||||
- [x] Project Snippets
|
||||
- [x] Project-Level Variables
|
||||
- [x] Projects (including setting Webhooks)
|
||||
- [x] Protected Branches
|
||||
- [x] Protected Tags
|
||||
- [x] Repositories
|
||||
- [x] Repository Files
|
||||
- [x] Runners
|
||||
- [x] Search
|
||||
- [x] Services
|
||||
- [x] Settings
|
||||
- [x] Sidekiq Metrics
|
||||
- [x] System Hooks
|
||||
- [x] Tags
|
||||
- [x] Todos
|
||||
- [x] Users
|
||||
- [x] Validate CI Configuration
|
||||
- [x] Version
|
||||
- [x] Wikis
|
||||
|
||||
## Usage
|
||||
|
||||
```go
|
||||
import "github.com/xanzy/go-gitlab"
|
||||
```
|
||||
|
||||
Construct a new GitLab client, then use the various services on the client to
|
||||
access different parts of the GitLab API. For example, to list all
|
||||
users:
|
||||
|
||||
```go
|
||||
git := gitlab.NewClient(nil, "yourtokengoeshere")
|
||||
//git.SetBaseURL("https://git.mydomain.com/api/v4")
|
||||
users, _, err := git.Users.ListUsers(&gitlab.ListUsersOptions{})
|
||||
```
|
||||
|
||||
Some API methods have optional parameters that can be passed. For example,
|
||||
to list all projects for user "svanharmelen":
|
||||
|
||||
```go
|
||||
git := gitlab.NewClient(nil)
|
||||
opt := &ListProjectsOptions{Search: gitlab.String("svanharmelen")}
|
||||
projects, _, err := git.Projects.ListProjects(opt)
|
||||
```
|
||||
|
||||
### Examples
|
||||
|
||||
The [examples](https://github.com/xanzy/go-gitlab/tree/master/examples) directory
|
||||
contains a couple for clear examples, of which one is partially listed here as well:
|
||||
|
||||
```go
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
|
||||
"github.com/xanzy/go-gitlab"
|
||||
)
|
||||
|
||||
func main() {
|
||||
git := gitlab.NewClient(nil, "yourtokengoeshere")
|
||||
|
||||
// Create new project
|
||||
p := &gitlab.CreateProjectOptions{
|
||||
Name: gitlab.String("My Project"),
|
||||
Description: gitlab.String("Just a test project to play with"),
|
||||
MergeRequestsEnabled: gitlab.Bool(true),
|
||||
SnippetsEnabled: gitlab.Bool(true),
|
||||
Visibility: gitlab.Visibility(gitlab.PublicVisibility),
|
||||
}
|
||||
project, _, err := git.Projects.CreateProject(p)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Add a new snippet
|
||||
s := &gitlab.CreateProjectSnippetOptions{
|
||||
Title: gitlab.String("Dummy Snippet"),
|
||||
FileName: gitlab.String("snippet.go"),
|
||||
Code: gitlab.String("package main...."),
|
||||
Visibility: gitlab.Visibility(gitlab.PublicVisibility),
|
||||
}
|
||||
_, _, err = git.ProjectSnippets.CreateSnippet(project.ID, s)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
For complete usage of go-gitlab, see the full [package docs](https://godoc.org/github.com/xanzy/go-gitlab).
|
||||
|
||||
## ToDo
|
||||
|
||||
- The biggest thing this package still needs is tests :disappointed:
|
||||
|
||||
## Issues
|
||||
|
||||
- If you have an issue: report it on the [issue tracker](https://github.com/xanzy/go-gitlab/issues)
|
||||
|
||||
## Author
|
||||
|
||||
Sander van Harmelen (<sander@xanzy.io>)
|
||||
|
||||
## License
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at <http://www.apache.org/licenses/LICENSE-2.0>
|
@ -0,0 +1,236 @@
|
||||
package gitlab
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
// AccessRequest represents a access request for a group or project.
|
||||
//
|
||||
// GitLab API docs:
|
||||
// https://docs.gitlab.com/ce/api/access_requests.html
|
||||
type AccessRequest struct {
|
||||
ID int `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Name string `json:"name"`
|
||||
State string `json:"state"`
|
||||
CreatedAt *time.Time `json:"created_at"`
|
||||
RequestedAt *time.Time `json:"requested_at"`
|
||||
AccessLevel AccessLevelValue `json:"access_level"`
|
||||
}
|
||||
|
||||
// AccessRequestsService handles communication with the project/group
|
||||
// access requests related methods of the GitLab API.
|
||||
//
|
||||
// GitLab API docs: https://docs.gitlab.com/ce/api/access_requests.html
|
||||
type AccessRequestsService struct {
|
||||
client *Client
|
||||
}
|
||||
|
||||
// ListAccessRequestsOptions represents the available
|
||||
// ListProjectAccessRequests() or ListGroupAccessRequests() options.
|
||||
//
|
||||
// GitLab API docs:
|
||||
// https://docs.gitlab.com/ce/api/access_requests.html#list-access-requests-for-a-group-or-project
|
||||
type ListAccessRequestsOptions ListOptions
|
||||
|
||||
// ListProjectAccessRequests gets a list of access requests
|
||||
// viewable by the authenticated user.
|
||||
//
|
||||
// GitLab API docs:
|
||||
// https://docs.gitlab.com/ce/api/access_requests.html#list-access-requests-for-a-group-or-project
|
||||
func (s *AccessRequestsService) ListProjectAccessRequests(pid interface{}, opt *ListAccessRequestsOptions, options ...OptionFunc) ([]*AccessRequest, *Response, error) {
|
||||
project, err := parseID(pid)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
u := fmt.Sprintf("projects/%s/access_requests", pathEscape(project))
|
||||
|
||||
req, err := s.client.NewRequest("GET", u, opt, options)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var ars []*AccessRequest
|
||||
resp, err := s.client.Do(req, &ars)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return ars, resp, err
|
||||
}
|
||||
|
||||
// ListGroupAccessRequests gets a list of access requests
|
||||
// viewable by the authenticated user.
|
||||
//
|
||||
// GitLab API docs:
|
||||
// https://docs.gitlab.com/ce/api/access_requests.html#list-access-requests-for-a-group-or-project
|
||||
func (s *AccessRequestsService) ListGroupAccessRequests(gid interface{}, opt *ListAccessRequestsOptions, options ...OptionFunc) ([]*AccessRequest, *Response, error) {
|
||||
group, err := parseID(gid)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
u := fmt.Sprintf("groups/%s/access_requests", pathEscape(group))
|
||||
|
||||
req, err := s.client.NewRequest("GET", u, opt, options)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
var ars []*AccessRequest
|
||||
resp, err := s.client.Do(req, &ars)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return ars, resp, err
|
||||
}
|
||||
|
||||
// RequestProjectAccess requests access for the authenticated user
|
||||
// to a group or project.
|
||||
//
|
||||
// GitLab API docs:
|
||||
// https://docs.gitlab.com/ce/api/access_requests.html#request-access-to-a-group-or-project
|
||||
func (s *AccessRequestsService) RequestProjectAccess(pid interface{}, options ...OptionFunc) (*AccessRequest, *Response, error) {
|
||||
project, err := parseID(pid)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
u := fmt.Sprintf("projects/%s/access_requests", pathEscape(project))
|
||||
|
||||
req, err := s.client.NewRequest("POST", u, nil, options)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
ar := new(AccessRequest)
|
||||
resp, err := s.client.Do(req, ar)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return ar, resp, err
|
||||
}
|
||||
|
||||
// RequestGroupAccess requests access for the authenticated user
|
||||
// to a group or project.
|
||||
//
|
||||
// GitLab API docs:
|
||||
// https://docs.gitlab.com/ce/api/access_requests.html#request-access-to-a-group-or-project
|
||||
func (s *AccessRequestsService) RequestGroupAccess(gid interface{}, options ...OptionFunc) (*AccessRequest, *Response, error) {
|
||||
group, err := parseID(gid)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
u := fmt.Sprintf("groups/%s/access_requests", pathEscape(group))
|
||||
|
||||
req, err := s.client.NewRequest("POST", u, nil, options)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
ar := new(AccessRequest)
|
||||
resp, err := s.client.Do(req, ar)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return ar, resp, err
|
||||
}
|
||||
|
||||
// ApproveAccessRequestOptions represents the available
|
||||
// ApproveProjectAccessRequest() and ApproveGroupAccessRequest() options.
|
||||
//
|
||||
// GitLab API docs:
|
||||
// https://docs.gitlab.com/ce/api/access_requests.html#approve-an-access-request
|
||||
type ApproveAccessRequestOptions struct {
|
||||
AccessLevel *AccessLevelValue `url:"access_level,omitempty" json:"access_level,omitempty"`
|
||||
}
|
||||
|
||||
// ApproveProjectAccessRequest approves an access request for the given user.
|
||||
//
|
||||
// GitLab API docs:
|
||||
// https://docs.gitlab.com/ce/api/access_requests.html#approve-an-access-request
|
||||
func (s *AccessRequestsService) ApproveProjectAccessRequest(pid interface{}, user int, opt *ApproveAccessRequestOptions, options ...OptionFunc) (*AccessRequest, *Response, error) {
|
||||
project, err := parseID(pid)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
u := fmt.Sprintf("projects/%s/access_requests/%d/approve", pathEscape(project), user)
|
||||
|
||||
req, err := s.client.NewRequest("PUT", u, opt, options)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
ar := new(AccessRequest)
|
||||
resp, err := s.client.Do(req, ar)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return ar, resp, err
|
||||
}
|
||||
|
||||
// ApproveGroupAccessRequest approves an access request for the given user.
|
||||
//
|
||||
// GitLab API docs:
|
||||
// https://docs.gitlab.com/ce/api/access_requests.html#approve-an-access-request
|
||||
func (s *AccessRequestsService) ApproveGroupAccessRequest(gid interface{}, user int, opt *ApproveAccessRequestOptions, options ...OptionFunc) (*AccessRequest, *Response, error) {
|
||||
group, err := parseID(gid)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
u := fmt.Sprintf("groups/%s/access_requests/%d/approve", pathEscape(group), user)
|
||||
|
||||
req, err := s.client.NewRequest("PUT", u, opt, options)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
ar := new(AccessRequest)
|
||||
resp, err := s.client.Do(req, ar)
|
||||
if err != nil {
|
||||
return nil, resp, err
|
||||
}
|
||||
|
||||
return ar, resp, err
|
||||
}
|
||||
|
||||
// DenyProjectAccessRequest denies an access request for the given user.
|
||||
//
|
||||
// GitLab API docs:
|
||||
// https://docs.gitlab.com/ce/api/access_requests.html#deny-an-access-request
|
||||
func ( |