Repository: xanzy/go-gitlab
Pull request #731 (2019-11-04 16:44:29)

Add date fields to PipelineInfo
The title says it all.

I personally need these fields to know which pipelines have recently been updated simply by calling `Pipelines.ListProjectPipelines`. Otherwise I would need to fetch each build individually to check for these dates.

Thanks


Patch:
diff --git a/environments.go b/environments.go
index 397c2d2..ee77307 100644
--- a/environments.go
+++ b/environments.go
@@ -35,9 +35,7 @@ type Environment struct {
 	ID             int         `json:"id"`
 	Name           string      `json:"name"`
 	Slug           string      `json:"slug"`
-	State          string      `json:"state"`
 	ExternalURL    string      `json:"external_url"`
-	Project        *Project    `json:"project"`
 	LastDeployment *Deployment `json:"last_deployment"`
 }
 
diff --git a/pipelines.go b/pipelines.go
index 972a5b6..071e5e5 100644
--- a/pipelines.go
+++ b/pipelines.go
@@ -83,11 +83,13 @@ func (p Pipeline) String() string {
 // PipelineInfo shows the basic entities of a pipeline, mostly used as fields
 // on other assets, like Commit.
 type PipelineInfo struct {
-	ID     int    `json:"id"`
-	Status string `json:"status"`
-	Ref    string `json:"ref"`
-	SHA    string `json:"sha"`
-	WebURL string `json:"web_url"`
+	ID        int        `json:"id"`
+	Status    string     `json:"status"`
+	Ref       string     `json:"ref"`
+	SHA       string     `json:"sha"`
+	WebURL    string     `json:"web_url"`
+	UpdatedAt *time.Time `json:"updated_at"`
+	CreatedAt *time.Time `json:"created_at"`
 }
 
 func (p PipelineInfo) String() string {


Test patch:
diff --git a/commits_test.go b/commits_test.go
index 3800b37..fbb77a8 100644
--- a/commits_test.go
+++ b/commits_test.go
@@ -5,6 +5,7 @@ import (
 	"net/http"
 	"reflect"
 	"testing"
+	"time"
 
 	"github.com/stretchr/testify/assert"
 )
@@ -25,6 +26,8 @@ func TestGetCommit(t *testing.T) {
 		t.Fatalf("Commits.GetCommit returned error: %v, response: %v", err, resp)
 	}
 
+	updatedAt := time.Date(2019, 11, 4, 15, 39, 03, 935000000, time.UTC)
+	createdAt := time.Date(2019, 11, 4, 15, 38, 53, 154000000, time.UTC)
 	want := &Commit{
 		ID:             "6104942438c14ec7bd21c6cd5bd995272b3faff6",
 		ShortID:        "6104942438c",
@@ -38,11 +41,13 @@ func TestGetCommit(t *testing.T) {
 		Stats:          &CommitStats{Additions: 15, Deletions: 10, Total: 25},
 		Status:         BuildState(Running),
 		LastPipeline: &PipelineInfo{
-			ID:     8,
-			Ref:    "master",
-			SHA:    "2dc6aa325a317eda67812f05600bdf0fcdc70ab0",
-			Status: "created",
-			WebURL: "https://gitlab.com/gitlab-org/gitlab-ce/pipelines/54268416",
+			ID:        8,
+			Ref:       "master",
+			SHA:       "2dc6aa325a317eda67812f05600bdf0fcdc70ab0",
+			Status:    "created",
+			WebURL:    "https://gitlab.com/gitlab-org/gitlab-ce/pipelines/54268416",
+			UpdatedAt: &updatedAt,
+			CreatedAt: &createdAt,
 		},
 		ProjectID: 13083,
 	}
@@ -108,6 +113,8 @@ func TestRevertCommit_NoOptions(t *testing.T) {
 		t.Fatalf("Commits.RevertCommit returned error: %v, response: %v", err, resp)
 	}
 
+	updatedAt := time.Date(2019, 11, 4, 15, 39, 03, 935000000, time.UTC)
+	createdAt := time.Date(2019, 11, 4, 15, 38, 53, 154000000, time.UTC)
 	want := &Commit{
 		ID:             "6104942438c14ec7bd21c6cd5bd995272b3faff6",
 		ShortID:        "6104942438c",
@@ -121,11 +128,13 @@ func TestRevertCommit_NoOptions(t *testing.T) {
 		Stats:          &CommitStats{Additions: 15, Deletions: 10, Total: 25},
 		Status:         BuildState(Running),
 		LastPipeline: &PipelineInfo{
-			ID:     8,
-			Ref:    "master",
-			SHA:    "2dc6aa325a317eda67812f05600bdf0fcdc70ab0",
-			Status: "created",
-			WebURL: "https://gitlab.com/gitlab-org/gitlab-ce/pipelines/54268416",
+			ID:        8,
+			Ref:       "master",
+			SHA:       "2dc6aa325a317eda67812f05600bdf0fcdc70ab0",
+			Status:    "created",
+			WebURL:    "https://gitlab.com/gitlab-org/gitlab-ce/pipelines/54268416",
+			UpdatedAt: &updatedAt,
+			CreatedAt: &createdAt,
 		},
 		ProjectID: 13083,
 	}
@@ -150,6 +159,8 @@ func TestRevertCommit_WithOptions(t *testing.T) {
 		t.Fatalf("Commits.RevertCommit returned error: %v, response: %v", err, resp)
 	}
 
+	updatedAt := time.Date(2019, 11, 4, 15, 39, 03, 935000000, time.UTC)
+	createdAt := time.Date(2019, 11, 4, 15, 38, 53, 154000000, time.UTC)
 	want := &Commit{
 		ID:             "6104942438c14ec7bd21c6cd5bd995272b3faff6",
 		ShortID:        "6104942438c",
@@ -163,11 +174,13 @@ func TestRevertCommit_WithOptions(t *testing.T) {
 		Stats:          &CommitStats{Additions: 15, Deletions: 10, Total: 25},
 		Status:         BuildState(Running),
 		LastPipeline: &PipelineInfo{
-			ID:     8,
-			Ref:    "master",
-			SHA:    "2dc6aa325a317eda67812f05600bdf0fcdc70ab0",
-			Status: "created",
-			WebURL: "https://gitlab.com/gitlab-org/gitlab-ce/pipelines/54268416",
+			ID:        8,
+			Ref:       "master",
+			SHA:       "2dc6aa325a317eda67812f05600bdf0fcdc70ab0",
+			Status:    "created",
+			WebURL:    "https://gitlab.com/gitlab-org/gitlab-ce/pipelines/54268416",
+			UpdatedAt: &updatedAt,
+			CreatedAt: &createdAt,
 		},
 		ProjectID: 13083,
 	}
diff --git a/environments_test.go b/environments_test.go
index 0b42c01..281c43a 100644
--- a/environments_test.go
+++ b/environments_test.go
@@ -1,14 +1,11 @@
 package gitlab
 
 import (
-	"encoding/json"
 	"fmt"
 	"log"
 	"net/http"
 	"reflect"
 	"testing"
-
-	"github.com/stretchr/testify/assert"
 )
 
 func TestListEnvironments(t *testing.T) {
@@ -121,56 +118,3 @@ func TestStopEnvironment(t *testing.T) {
 		log.Fatal(err)
 	}
 }
-
-func TestUnmarshal(t *testing.T) {
-	jsonObject := `
-    {
-        "id": 10,
-        "name": "production",
-        "slug": "production",
-        "external_url": "https://example.com",
-        "project": {
-            "id": 1,
-            "description": "",
-            "name": "Awesome Project",
-            "name_with_namespace": "FooBar Group / Awesome Project",
-            "path": "awesome-project",
-            "path_with_namespace": "foobar-group/awesome-project",
-            "created_at": "2017-09-30T11:10:08.476-04:00",
-            "default_branch": "develop",
-            "tag_list": [],
-            "ssh_url_to_repo": "git@example.gitlab.com:foobar-group/api.git",
-            "http_url_to_repo": "https://example.gitlab.com/foobar-group/api.git",
-            "web_url": "https://example.gitlab.com/foobar-group/api",
-            "readme_url": null,
-            "avatar_url": null,
-            "star_count": 0,
-            "forks_count": 1,
-            "last_activity_at": "2019-11-03T22:22:46.564-05:00",
-            "namespace": {
-                "id": 15,
-                "name": "FooBar Group",
-                "path": "foobar-group",
-                "kind": "group",
-                "full_path": "foobar-group",
-                "parent_id": null,
-                "avatar_url": null,
-                "web_url": "https://example.gitlab.com/groups/foobar-group"
-            }
-        },
-        "state": "available"
-    }`
-
-	var env Environment
-	err := json.Unmarshal([]byte(jsonObject), &env)
-
-	if assert.NoError(t, err) {
-		assert.Equal(t, 10, env.ID)
-		assert.Equal(t, "production", env.Name)
-		assert.Equal(t, "https://example.com", env.ExternalURL)
-		assert.Equal(t, "available", env.State)
-		if assert.NotNil(t, env.Project) {
-			assert.Equal(t, "Awesome Project", env.Project.Name)
-		}
-	}
-}
\ No newline at end of file
diff --git a/merge_requests_test.go b/merge_requests_test.go
index a7baaf5..9bc39bf 100644
--- a/merge_requests_test.go
+++ b/merge_requests_test.go
@@ -38,17 +38,19 @@ var (
 		"group::knowledge",
 		"missed:12.1",
 	}
-	pipelineBasic = PipelineInfo{
-		ID:     77056819,
-		SHA:    "8e0b45049b6253b8984cde9241830d2851168142",
-		Ref:    "delete-designs-v2",
-		Status: "success",
-		WebURL: "https://gitlab.com/gitlab-org/gitlab-ee/pipelines/77056819",
-	}
 	pipelineCreation = time.Date(2019, 8, 19, 9, 50, 58, 157000000, time.UTC)
+	pipelineUpdate   = time.Date(2019, 8, 19, 19, 22, 29, 647000000, time.UTC)
+	pipelineBasic    = PipelineInfo{
+		ID:        77056819,
+		SHA:       "8e0b45049b6253b8984cde9241830d2851168142",
+		Ref:       "delete-designs-v2",
+		Status:    "success",
+		WebURL:    "https://gitlab.com/gitlab-org/gitlab-ee/pipelines/77056819",
+		CreatedAt: &pipelineCreation,
+		UpdatedAt: &pipelineUpdate,
+	}
 	pipelineStarted  = time.Date(2019, 8, 19, 9, 51, 6, 545000000, time.UTC)
 	pipelineFinished = time.Date(2019, 8, 19, 19, 22, 29, 632000000, time.UTC)
-	pipelineUpdate   = time.Date(2019, 8, 19, 19, 22, 29, 647000000, time.UTC)
 	pipelineDetailed = Pipeline{
 		ID:         77056819,
 		SHA:        "8e0b45049b6253b8984cde9241830d2851168142",
diff --git a/testdata/get_commit.json b/testdata/get_commit.json
index 79be3c4..ec96cdc 100644
--- a/testdata/get_commit.json
+++ b/testdata/get_commit.json
@@ -15,7 +15,9 @@
         "ref": "master",
         "sha": "2dc6aa325a317eda67812f05600bdf0fcdc70ab0",
         "status": "created",
-        "web_url": "https://gitlab.com/gitlab-org/gitlab-ce/pipelines/54268416"
+        "web_url": "https://gitlab.com/gitlab-org/gitlab-ce/pipelines/54268416",
+        "created_at": "2019-11-04T15:38:53.154Z",
+        "updated_at": "2019-11-04T15:39:03.935Z"
     },
     "stats": {
         "additions": 15,
diff --git a/testdata/get_merge_request.json b/testdata/get_merge_request.json
index eccb04c..2434efa 100644
--- a/testdata/get_merge_request.json
+++ b/testdata/get_merge_request.json
@@ -100,7 +100,9 @@
     "sha": "8e0b45049b6253b8984cde9241830d2851168142",
     "ref": "delete-designs-v2",
     "status": "success",
-    "web_url": "https://gitlab.com/gitlab-org/gitlab-ee/pipelines/77056819"
+    "web_url": "https://gitlab.com/gitlab-org/gitlab-ee/pipelines/77056819",
+    "created_at": "2019-08-19T09:50:58.157Z",
+    "updated_at": "2019-08-19T19:22:29.647Z"
   },
   "head_pipeline": {
     "id": 77056819,