-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add GitLab PR generator for applicationset
Signed-off-by: Ivo Verberk <ivo.verberk@gmail.com>
- Loading branch information
Showing
11 changed files
with
630 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
applicationset/services/pull_request/fixtures/gitlab_mr_list_response.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
[ | ||
{ | ||
"id": 35385049, | ||
"iid": 15442, | ||
"project_id": 278964, | ||
"title": "Draft: Use structured logging for DB load balancer", | ||
"description": "", | ||
"state": "opened", | ||
"created_at": "2019-08-20T10:58:54.413Z", | ||
"updated_at": "2019-08-20T12:01:49.849Z", | ||
"merged_by": null, | ||
"merged_at": null, | ||
"closed_by": null, | ||
"closed_at": null, | ||
"target_branch": "master", | ||
"source_branch": "use-structured-logging-for-db-load-balancer", | ||
"user_notes_count": 1, | ||
"upvotes": 0, | ||
"downvotes": 0, | ||
"assignee": { | ||
"id": 4088036, | ||
"name": "Hordur Freyr Yngvason", | ||
"username": "hfyngvason", | ||
"state": "active", | ||
"avatar_url": "https://assets.gitlab-static.net/uploads/-/system/user/avatar/4088036/avatar.png", | ||
"web_url": "https://gitlab.com/hfyngvason" | ||
}, | ||
"author": { | ||
"id": 4088036, | ||
"name": "Hordur Freyr Yngvason", | ||
"username": "hfyngvason", | ||
"state": "active", | ||
"avatar_url": "https://assets.gitlab-static.net/uploads/-/system/user/avatar/4088036/avatar.png", | ||
"web_url": "https://gitlab.com/hfyngvason" | ||
}, | ||
"assignees": [ | ||
{ | ||
"id": 4088036, | ||
"name": "Hordur Freyr Yngvason", | ||
"username": "hfyngvason", | ||
"state": "active", | ||
"avatar_url": "https://assets.gitlab-static.net/uploads/-/system/user/avatar/4088036/avatar.png", | ||
"web_url": "https://gitlab.com/hfyngvason" | ||
} | ||
], | ||
"reviewers": [ | ||
{ | ||
"id": 2535118, | ||
"name": "Thong Kuah", | ||
"username": "tkuah", | ||
"state": "active", | ||
"avatar_url": "https://secure.gravatar.com/avatar/f7b51bdd49a4914d29504d7ff4c3f7b9?s=80&d=identicon", | ||
"web_url": "https://gitlab.com/tkuah" | ||
} | ||
], | ||
"source_project_id": 278964, | ||
"target_project_id": 278964, | ||
"labels": [ | ||
"backend", | ||
"backstage", | ||
"database", | ||
"database::review pending", | ||
"group::autodevops and kubernetes" | ||
], | ||
"work_in_progress": true, | ||
"milestone": null, | ||
"merge_when_pipeline_succeeds": false, | ||
"merge_status": "can_be_merged", | ||
"sha": "2fc4e8b972ff3208ec63b6143e34ad67ff343ad7", | ||
"merge_commit_sha": null, | ||
"discussion_locked": null, | ||
"should_remove_source_branch": null, | ||
"force_remove_source_branch": true, | ||
"reference": "!15442", | ||
"web_url": "https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/15442", | ||
"time_stats": { | ||
"time_estimate": 0, | ||
"total_time_spent": 0, | ||
"human_time_estimate": null, | ||
"human_total_time_spent": null | ||
}, | ||
"squash": true, | ||
"task_completion_status": { | ||
"count": 12, | ||
"completed_count": 0 | ||
}, | ||
"approvals_before_merge": 1 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package pull_request | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
gitlab "github.com/xanzy/go-gitlab" | ||
) | ||
|
||
type GitLabService struct { | ||
client *gitlab.Client | ||
project string | ||
labels []string | ||
} | ||
|
||
var _ PullRequestService = (*GitLabService)(nil) | ||
|
||
func NewGitLabService(ctx context.Context, token, url, project string, labels []string) (PullRequestService, error) { | ||
var clientOptionFns []gitlab.ClientOptionFunc | ||
|
||
// Set a custom Gitlab base URL if one is provided | ||
if url != "" { | ||
clientOptionFns = append(clientOptionFns, gitlab.WithBaseURL(url)) | ||
} | ||
|
||
if token == "" { | ||
token = os.Getenv("GITLAB_TOKEN") | ||
} | ||
|
||
client, err := gitlab.NewClient(token, clientOptionFns...) | ||
if err != nil { | ||
return nil, fmt.Errorf("error creating Gitlab client: %v", err) | ||
} | ||
|
||
return &GitLabService{ | ||
client: client, | ||
project: project, | ||
labels: labels, | ||
}, nil | ||
} | ||
|
||
func (g *GitLabService) List(ctx context.Context) ([]*PullRequest, error) { | ||
|
||
// Filter the merge requests on labels, if they are specified. | ||
var labels *gitlab.Labels | ||
if len(g.labels) > 0 { | ||
labels = (*gitlab.Labels)(&g.labels) | ||
} | ||
|
||
opts := &gitlab.ListProjectMergeRequestsOptions{ | ||
ListOptions: gitlab.ListOptions{ | ||
PerPage: 100, | ||
}, | ||
Labels: labels, | ||
} | ||
|
||
pullRequests := []*PullRequest{} | ||
for { | ||
mrs, resp, err := g.client.MergeRequests.ListProjectMergeRequests(g.project, opts) | ||
if err != nil { | ||
return nil, fmt.Errorf("error listing merge requests for project '%s': %v", g.project, err) | ||
} | ||
for _, mr := range mrs { | ||
pullRequests = append(pullRequests, &PullRequest{ | ||
Number: mr.IID, | ||
Branch: mr.SourceBranch, | ||
HeadSHA: mr.SHA, | ||
}) | ||
} | ||
if resp.NextPage == 0 { | ||
break | ||
} | ||
opts.Page = resp.NextPage | ||
} | ||
return pullRequests, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package pull_request | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func writeMRListResponse(t *testing.T, w io.Writer) { | ||
f, err := os.Open("fixtures/gitlab_mr_list_response.json") | ||
if err != nil { | ||
t.Fatalf("error opening fixture file: %v", err) | ||
} | ||
|
||
if _, err = io.Copy(w, f); err != nil { | ||
t.Fatalf("error writing response: %v", err) | ||
} | ||
} | ||
|
||
func TestGitLabServiceCustomBaseURL(t *testing.T) { | ||
mux := http.NewServeMux() | ||
server := httptest.NewServer(mux) | ||
defer server.Close() | ||
|
||
path := "/api/v4/projects/278964/merge_requests" | ||
|
||
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, path+"?per_page=100", r.URL.RequestURI()) | ||
writeMRListResponse(t, w) | ||
}) | ||
|
||
svc, err := NewGitLabService(context.Background(), "", server.URL, "278964", nil) | ||
assert.NoError(t, err) | ||
|
||
_, err = svc.List(context.Background()) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestGitLabServiceToken(t *testing.T) { | ||
mux := http.NewServeMux() | ||
server := httptest.NewServer(mux) | ||
defer server.Close() | ||
|
||
path := "/api/v4/projects/278964/merge_requests" | ||
|
||
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, "token-123", r.Header.Get("Private-Token")) | ||
writeMRListResponse(t, w) | ||
}) | ||
|
||
svc, err := NewGitLabService(context.Background(), "token-123", server.URL, "278964", nil) | ||
assert.NoError(t, err) | ||
|
||
_, err = svc.List(context.Background()) | ||
assert.NoError(t, err) | ||
} | ||
|
||
func TestList(t *testing.T) { | ||
mux := http.NewServeMux() | ||
server := httptest.NewServer(mux) | ||
defer server.Close() | ||
|
||
path := "/api/v4/projects/278964/merge_requests" | ||
|
||
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, path+"?per_page=100", r.URL.RequestURI()) | ||
writeMRListResponse(t, w) | ||
}) | ||
|
||
svc, err := NewGitLabService(context.Background(), "", server.URL, "278964", []string{}) | ||
assert.NoError(t, err) | ||
|
||
prs, err := svc.List(context.Background()) | ||
assert.NoError(t, err) | ||
assert.Len(t, prs, 1) | ||
assert.Equal(t, prs[0].Number, 15442) | ||
assert.Equal(t, prs[0].Branch, "use-structured-logging-for-db-load-balancer") | ||
assert.Equal(t, prs[0].HeadSHA, "2fc4e8b972ff3208ec63b6143e34ad67ff343ad7") | ||
} | ||
|
||
func TestListWithLabels(t *testing.T) { | ||
mux := http.NewServeMux() | ||
server := httptest.NewServer(mux) | ||
defer server.Close() | ||
|
||
path := "/api/v4/projects/278964/merge_requests" | ||
|
||
mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { | ||
assert.Equal(t, path+"?labels=feature%2Cready&per_page=100", r.URL.RequestURI()) | ||
writeMRListResponse(t, w) | ||
}) | ||
|
||
svc, err := NewGitLabService(context.Background(), "", server.URL, "278964", []string{"feature", "ready"}) | ||
assert.NoError(t, err) | ||
|
||
_, err = svc.List(context.Background()) | ||
assert.NoError(t, err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.