Skip to content

Commit

Permalink
Merge pull request #1632 from timofurrer/get-pull-mirror
Browse files Browse the repository at this point in the history
Implement function to get pull mirror details from a project
  • Loading branch information
svanharmelen committed Jan 24, 2023
2 parents 5d94d60 + 5d8c3e5 commit 36b86c5
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
42 changes: 41 additions & 1 deletion projects.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"net/http"
"time"

retryablehttp "github.com/hashicorp/go-retryablehttp"
"github.com/hashicorp/go-retryablehttp"
)

// ProjectsService handles communication with the repositories related methods
Expand Down Expand Up @@ -1955,6 +1955,46 @@ func (s *ProjectsService) ChangeAllowedApprovers(pid interface{}, opt *ChangeAll
return pa, resp, err
}

// ProjectPullMirrorDetails represent the details of the configuration pull
// mirror and its update status.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/projects.html#get-a-projects-pull-mirror-details
type ProjectPullMirrorDetails struct {
ID int `json:"id"`
LastError string `json:"last_error"`
LastSuccessfulUpdateAt *time.Time `json:"last_successful_update_at"`
LastUpdateAt *time.Time `json:"last_update_at"`
LastUpdateStartedAt *time.Time `json:"last_update_started_at"`
UpdateStatus string `json:"update_status"`
URL string `json:"url"`
}

// GetProjectPullMirrorDetails returns the pull mirror details.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/projects.html#get-a-projects-pull-mirror-details
func (s *ProjectsService) GetProjectPullMirrorDetails(pid interface{}, options ...RequestOptionFunc) (*ProjectPullMirrorDetails, *Response, error) {
project, err := parseID(pid)
if err != nil {
return nil, nil, err
}
u := fmt.Sprintf("projects/%s/mirror/pull", PathEscape(project))

req, err := s.client.NewRequest(http.MethodGet, u, nil, options)
if err != nil {
return nil, nil, err
}

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

return pmd, resp, err
}

// StartMirroringProject start the pull mirroring process for a project.
//
// GitLab API docs:
Expand Down
39 changes: 39 additions & 0 deletions projects_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,45 @@ func TestCreateProjectApprovalRule(t *testing.T) {
}
}

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

mux.HandleFunc("/api/v4/projects/1/mirror/pull", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{
"id": 101486,
"last_error": null,
"last_successful_update_at": "2020-01-06T17:32:02.823Z",
"last_update_at": "2020-01-06T17:32:02.823Z",
"last_update_started_at": "2020-01-06T17:31:55.864Z",
"update_status": "finished",
"url": "https://*****:*****@gitlab.com/gitlab-org/security/gitlab.git"
}`)
})

pullMirror, _, err := client.Projects.GetProjectPullMirrorDetails(1)
if err != nil {
t.Errorf("Projects.GetProjectPullMirrorDetails returned error: %v", err)
}

wantLastSuccessfulUpdateAtTimestamp := time.Date(2020, 01, 06, 17, 32, 02, 823000000, time.UTC)
wantLastUpdateAtTimestamp := time.Date(2020, 01, 06, 17, 32, 02, 823000000, time.UTC)
wantLastUpdateStartedAtTimestamp := time.Date(2020, 01, 06, 17, 31, 55, 864000000, time.UTC)
want := &ProjectPullMirrorDetails{
ID: 101486,
LastError: "",
LastSuccessfulUpdateAt: &wantLastSuccessfulUpdateAtTimestamp,
LastUpdateAt: &wantLastUpdateAtTimestamp,
LastUpdateStartedAt: &wantLastUpdateStartedAtTimestamp,
UpdateStatus: "finished",
URL: "https://*****:*****@gitlab.com/gitlab-org/security/gitlab.git",
}

if !reflect.DeepEqual(want, pullMirror) {
t.Errorf("Projects.GetProjectPullMirrorDetails returned %+v, want %+v", pullMirror, want)
}
}

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

Expand Down

0 comments on commit 36b86c5

Please sign in to comment.