Skip to content

Commit

Permalink
Merge pull request #1942 from heidiberry/main
Browse files Browse the repository at this point in the history
Add more GitHub import API support
  • Loading branch information
svanharmelen authored May 22, 2024
2 parents 1e1b4a2 + afc1073 commit 14dafd2
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 6 deletions.
70 changes: 70 additions & 0 deletions import.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,73 @@ func (s *ImportService) ImportRepositoryFromGitHub(opt *ImportRepositoryFromGitH

return gi, resp, nil
}

// CancelledGitHubImport represents the response when canceling
// an import from GitHub.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/import.html#cancel-github-project-import
type CancelledGitHubImport struct {
ID int `json:"id"`
Name string `json:"name"`
FullPath string `json:"full_path"`
FullName string `json:"full_name"`
ImportSource string `json:"import_source"`
ImportStatus string `json:"import_status"`
HumanImportStatusName string `json:"human_import_status_name"`
ProviderLink string `json:"provider_link"`
}

func (s CancelledGitHubImport) String() string {
return Stringify(s)
}

// CancelGitHubProjectImportOptions represents the available
// CancelGitHubProjectImport() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/import.html#cancel-github-project-import
type CancelGitHubProjectImportOptions struct {
ProjectID *int `url:"project_id,omitempty" json:"project_id,omitempty"`
}

// Cancel an import of a repository from GitHub.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/import.html#cancel-github-project-import
func (s *ImportService) CancelGitHubProjectImport(opt *CancelGitHubProjectImportOptions, options ...RequestOptionFunc) (*CancelledGitHubImport, *Response, error) {
req, err := s.client.NewRequest(http.MethodPost, "import/github/cancel", opt, options)
if err != nil {
return nil, nil, err
}

cgi := new(CancelledGitHubImport)
resp, err := s.client.Do(req, cgi)
if err != nil {
return nil, resp, err
}

return cgi, resp, nil
}

// ImportGitHubGistsIntoGitLabSnippetsOptions represents the available
// ImportGitHubGistsIntoGitLabSnippets() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/import.html#import-github-gists-into-gitlab-snippets
type ImportGitHubGistsIntoGitLabSnippetsOptions struct {
PersonalAccessToken *string `url:"personal_access_token,omitempty" json:"personal_access_token,omitempty"`
}

// Import personal GitHub Gists into personal GitLab Snippets.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/import.html#import-github-gists-into-gitlab-snippets
func (s *ImportService) ImportGitHubGistsIntoGitLabSnippets(opt *ImportGitHubGistsIntoGitLabSnippetsOptions, options ...RequestOptionFunc) (*Response, error) {
req, err := s.client.NewRequest(http.MethodPost, "import/github/gists", opt, options)
if err != nil {
return nil, err
}

return s.client.Do(req, nil)
}
77 changes: 71 additions & 6 deletions import_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package gitlab
import (
"fmt"
"net/http"
"reflect"
"testing"

"github.com/stretchr/testify/require"
)

func TestImportService_ImportRepositoryFromGitHub(t *testing.T) {
Expand Down Expand Up @@ -47,12 +48,76 @@ func TestImportService_ImportRepositoryFromGitHub(t *testing.T) {
TargetNamespace: Ptr("root"),
}

gi, _, err := client.Import.ImportRepositoryFromGitHub(opt)
if err != nil {
t.Errorf("Import.ImportRepositoryFromGitHub returned error: %v", err)
gi, resp, err := client.Import.ImportRepositoryFromGitHub(opt)
require.NoError(t, err)
require.NotNil(t, resp)
require.Equal(t, want, gi)

gi, resp, err = client.Import.ImportRepositoryFromGitHub(opt, errorOption)
require.EqualError(t, err, "RequestOptionFunc returns an error")
require.Nil(t, resp)
require.Nil(t, gi)
}

func TestImportService_CancelGitHubProjectImport(t *testing.T) {
mux, client := setup(t)

mux.HandleFunc("/api/v4/import/github/cancel", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
fmt.Fprintf(w, `
{
"id": 27,
"name": "my-repo",
"full_path": "/root/my-repo",
"full_name": "Administrator / my-repo",
"import_source": "my-github/repo",
"import_status": "scheduled",
"human_import_status_name": "scheduled",
"provider_link": "/my-github/repo"
}
`)
})

want := &CancelledGitHubImport{
ID: 27,
Name: "my-repo",
FullPath: "/root/my-repo",
FullName: "Administrator / my-repo",
ImportSource: "my-github/repo",
ImportStatus: "scheduled",
HumanImportStatusName: "scheduled",
ProviderLink: "/my-github/repo",
}

if !reflect.DeepEqual(want, gi) {
t.Errorf("Import.ImportRepositoryFromGitHub return %+v, want %+v", gi, want)
opt := &CancelGitHubProjectImportOptions{
ProjectID: Ptr(27),
}

cgi, resp, err := client.Import.CancelGitHubProjectImport(opt)
require.NoError(t, err)
require.NotNil(t, resp)
require.Equal(t, want, cgi)

cgi, resp, err = client.Import.CancelGitHubProjectImport(opt, errorOption)
require.EqualError(t, err, "RequestOptionFunc returns an error")
require.Nil(t, resp)
require.Nil(t, cgi)
}

func TestImportService_ImportGitHubGistsIntoGitLabSnippets(t *testing.T) {
mux, client := setup(t)

mux.HandleFunc("/api/v4/import/github/gists", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
})

opt := &ImportGitHubGistsIntoGitLabSnippetsOptions{PersonalAccessToken: Ptr("token")}

resp, err := client.Import.ImportGitHubGistsIntoGitLabSnippets(opt)
require.NoError(t, err)
require.NotNil(t, resp)

resp, err = client.Import.ImportGitHubGistsIntoGitLabSnippets(opt, errorOption)
require.EqualError(t, err, "RequestOptionFunc returns an error")
require.Nil(t, resp)
}

0 comments on commit 14dafd2

Please sign in to comment.