Skip to content

Commit

Permalink
Merge pull request #67 from takirala/tga/add-labels-support
Browse files Browse the repository at this point in the history
Support fetching labels from pull requests in github,gitlab & gitea
  • Loading branch information
bradrydzewski authored Aug 19, 2020
2 parents 14a5bd7 + 1ccff59 commit 565d225
Show file tree
Hide file tree
Showing 10 changed files with 77 additions and 5 deletions.
12 changes: 12 additions & 0 deletions scm/driver/gitea/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ type pr struct {
Merged bool `json:"merged"`
Created time.Time `json:"created_at"`
Updated time.Time `json:"updated_at"`
Labels []struct {
Name string `json:"name"`
Color string `json:"color"`
} `json:"labels"`
}

type reference struct {
Expand Down Expand Up @@ -123,6 +127,13 @@ func convertPullRequests(src []*pr) []*scm.PullRequest {
}

func convertPullRequest(src *pr) *scm.PullRequest {
var labels []scm.Label
for _, label := range src.Labels {
labels = append(labels, scm.Label{
Name: label.Name,
Color: label.Color,
})
}
return &scm.PullRequest{
Number: src.Number,
Title: src.Title,
Expand All @@ -138,6 +149,7 @@ func convertPullRequest(src *pr) *scm.PullRequest {
Merged: src.Merged,
Created: src.Created,
Updated: src.Updated,
Labels: labels,
}
}

Expand Down
12 changes: 12 additions & 0 deletions scm/driver/github/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ type pr struct {
MergedAt null.String `json:"merged_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Labels []struct {
Name string `json:"name"`
Color string `json:"color"`
} `json:"labels"`
}

type prInput struct {
Expand Down Expand Up @@ -123,6 +127,13 @@ func convertPullRequestList(from []*pr) []*scm.PullRequest {
}

func convertPullRequest(from *pr) *scm.PullRequest {
var labels []scm.Label
for _, label := range from.Labels {
labels = append(labels, scm.Label{
Name: label.Name,
Color: label.Color,
})
}
return &scm.PullRequest{
Number: from.Number,
Title: from.Title,
Expand Down Expand Up @@ -151,6 +162,7 @@ func convertPullRequest(from *pr) *scm.PullRequest {
},
Created: from.CreatedAt,
Updated: from.UpdatedAt,
Labels: labels,
}
}

Expand Down
8 changes: 8 additions & 0 deletions scm/driver/github/testdata/webhooks/pr_labeled.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@
"node_id": "MDU6TGFiZWw2MzA2MzQ4MA==",
"url": "https://api.github.com/repos/bradrydzewski/drone-test-go/labels/bug",
"name": "bug",
"color": "fc2927",
"default": true
},
{
"id": 63063480,
"node_id": "MDU6TGFiZWw2MzA2MzQ4MA==",
"url": "https://api.github.com/repos/bradrydzewski/drone-test-go/labels/bug",
"name": "documentation",
"color": "fc2929",
"default": true
}
Expand Down
12 changes: 11 additions & 1 deletion scm/driver/github/testdata/webhooks/pr_labeled.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,17 @@
"Avatar": "https://avatars1.githubusercontent.com/u/817538?v=4"
},
"Created": "2018-06-22T23:54:09Z",
"Updated": "2018-06-25T19:05:03Z"
"Updated": "2018-06-25T19:05:03Z",
"labels": [
{
"name": "bug",
"color": "fc2927"
},
{
"name": "documentation",
"color": "fc2929"
}
]
},
"Sender": {
"Login": "bradrydzewski",
Expand Down
8 changes: 8 additions & 0 deletions scm/driver/gitlab/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ type pr struct {
Created time.Time `json:"created_at"`
Updated time.Time `json:"updated_at"`
Closed time.Time
Labels []string `json:"labels"`
}

type changes struct {
Expand All @@ -132,6 +133,12 @@ func convertPullRequestList(from []*pr) []*scm.PullRequest {
}

func convertPullRequest(from *pr) *scm.PullRequest {
var labels []scm.Label
for _, label := range from.Labels {
labels = append(labels, scm.Label{
Name: label,
})
}
return &scm.PullRequest{
Number: from.Number,
Title: from.Title,
Expand All @@ -150,6 +157,7 @@ func convertPullRequest(from *pr) *scm.PullRequest {
},
Created: from.Created,
Updated: from.Updated,
Labels: labels,
}
}

Expand Down
2 changes: 1 addition & 1 deletion scm/driver/gitlab/testdata/merge.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"assignee": null,
"source_project_id": 32732,
"target_project_id": 32732,
"labels": [],
"labels": ["bug", "documentation"],
"work_in_progress": false,
"milestone": null,
"merge_when_pipeline_succeeds": false,
Expand Down
10 changes: 9 additions & 1 deletion scm/driver/gitlab/testdata/merge.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,13 @@
"Avatar": "https://secure.gravatar.com/avatar/b5bf44866b4eeafa2d8114bfe15da02f?s=80\u0026d=identicon"
},
"Created": "2015-12-18T18:29:53.563Z",
"Updated": "2015-12-18T18:30:22.522Z"
"Updated": "2015-12-18T18:30:22.522Z",
"Labels": [
{
"name": "bug"
},
{
"name": "documentation"
}
]
}
2 changes: 1 addition & 1 deletion scm/driver/gitlab/testdata/merges.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"assignee": null,
"source_project_id": 32732,
"target_project_id": 32732,
"labels": [],
"labels": ["bug", "documentation"],
"work_in_progress": false,
"milestone": null,
"merge_when_pipeline_succeeds": false,
Expand Down
10 changes: 9 additions & 1 deletion scm/driver/gitlab/testdata/merges.json.golden
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
"Avatar": "https://secure.gravatar.com/avatar/b5bf44866b4eeafa2d8114bfe15da02f?s=80\u0026d=identicon"
},
"Created": "2015-12-18T18:29:53.563Z",
"Updated": "2015-12-18T18:30:22.522Z"
"Updated": "2015-12-18T18:30:22.522Z",
"Labels": [
{
"name": "bug"
},
{
"name": "documentation"
}
]
}
]
6 changes: 6 additions & 0 deletions scm/pr.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type (
Author User
Created time.Time
Updated time.Time
Labels []Label
}

// PullRequestInput provides the input fields required for creating a pull request.
Expand Down Expand Up @@ -55,6 +56,11 @@ type (
Deleted bool
}

Label struct {
Name string
Color string
}

// PullRequestService provides access to pull request resources.
PullRequestService interface {
// Find returns the repository pull request by number.
Expand Down

0 comments on commit 565d225

Please sign in to comment.