Repository: xanzy/go-gitlab
Pull request #1632 (2023-01-20 09:55:55)

Implement function to get pull mirror details from a project


Patch:
diff --git a/projects.go b/projects.go
index 8741723..e8ff627 100644
--- a/projects.go
+++ b/projects.go
@@ -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
@@ -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:


Test patch:
diff --git a/projects_test.go b/projects_test.go
index 9128b2e..1a60ac1 100644
--- a/projects_test.go
+++ b/projects_test.go
@@ -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)