Skip to content

Commit

Permalink
Merge pull request #10 from opensourceways/develop/v0
Browse files Browse the repository at this point in the history
add some openapi, update unit test and add orchestration field interface
  • Loading branch information
GeorgeCao-hw authored Dec 14, 2024
2 parents cfacd3b + ea3b52a commit 8ebd404
Show file tree
Hide file tree
Showing 30 changed files with 815 additions and 125 deletions.
20 changes: 15 additions & 5 deletions openapi/common_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@ import (
"time"
)

type timestamp time.Time
type Timestamp time.Time

// UnmarshalJSON implements the json.Unmarshaler interface.
// Time is expected in RFC3339 or Unix format.
func (t *timestamp) UnmarshalJSON(data []byte) error {
func (t *Timestamp) UnmarshalJSON(data []byte) error {
if len(data) == 2 {
*t = timestamp(time.Time{})
*t = Timestamp(time.Time{})
return nil
}

t1, err := time.Parse(`"`+time.RFC3339+`"`, string(data))
*t = timestamp(t1)
*t = Timestamp(t1)
return err
}

func (t *timestamp) MarshalJSON() ([]byte, error) {
func (t *Timestamp) MarshalJSON() ([]byte, error) {
b := make([]byte, 0, len(time.RFC3339)+2)
b = append(b, '"')
if !time.Time(*t).IsZero() {
Expand All @@ -41,3 +41,13 @@ func (t *timestamp) MarshalJSON() ([]byte, error) {
b = append(b, '"')
return b, nil
}

func (t *Timestamp) ToString() *string {
b := make([]byte, 0, len(time.RFC3339))
if !time.Time(*t).IsZero() {
b = time.Time(*t).AppendFormat(b, time.RFC3339)
s := string(b)
return &s
}
return nil
}
43 changes: 43 additions & 0 deletions openapi/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,47 @@ func TestNewRequestError(t *testing.T) {
assert.Equal(t, false, ok)
assert.Equal(t, ([]*RepositoryCommit)(nil), result14)
assert.Equal(t, msg1, err.Error())

result15, ok, err := client.Issues.GetIssueLabels(context.Background(), owner, "15423", "1")
assert.Equal(t, false, ok)
assert.Equal(t, ([]*Label)(nil), result15)
assert.Equal(t, msg1, err.Error())

result16, ok, err := client.PullRequests.GetPullRequestChangeFiles(context.Background(), owner, repo, "1")
assert.Equal(t, false, ok)
assert.Equal(t, ([]*CommitFile)(nil), result16)
assert.Equal(t, msg1, err.Error())

result17, ok, err := client.PullRequests.ListPullRequestOperationLogs(context.Background(), owner, repo, "1", "asc", "1")
assert.Equal(t, false, ok)
assert.Equal(t, ([]*PullRequestOperationLog)(nil), result17)
assert.Equal(t, msg1, err.Error())

result18, ok, err := client.PullRequests.MergePullRequest(context.Background(), owner, repo, "1", "merge")
assert.Equal(t, false, ok)
assert.Equal(t, (*PullRequestMergedResult)(nil), result18)
assert.Equal(t, msg1, err.Error())

result19, ok, err := client.PullRequests.ListPullRequestComments(context.Background(), owner, repo, "1", "1", "pr_comment")
assert.Equal(t, false, ok)
assert.Equal(t, ([]*PullRequestComment)(nil), result19)
assert.Equal(t, msg1, err.Error())

ok, err = client.PullRequests.UpdatePullRequestComment(context.Background(), owner, repo, "11234", "1fasd")
assert.Equal(t, false, ok)
assert.Equal(t, msg1, err.Error())

ok, err = client.PullRequests.DeletePullRequestComment(context.Background(), owner, repo, "11234")
assert.Equal(t, false, ok)
assert.Equal(t, msg1, err.Error())

result20, ok, err := client.PullRequests.GetLabelsOfPullRequest(context.Background(), owner, repo, "1")
assert.Equal(t, false, ok)
assert.Equal(t, ([]*Label)(nil), result20)
assert.Equal(t, msg1, err.Error())

result21, ok, err := client.Repository.GetRepoContentByPath(context.Background(), owner, repo, "1.txt", "main")
assert.Equal(t, false, ok)
assert.Equal(t, (*RepositoryContent)(nil), result21)
assert.Equal(t, msg1, err.Error())
}
5 changes: 2 additions & 3 deletions openapi/issues.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"context"
"fmt"
"net/http"
"net/url"
)

// UpdateIssue 更新Issue
Expand All @@ -39,8 +38,8 @@ func (s *IssuesService) UpdateIssue(ctx context.Context, owner, number string, i
//
// api Docs: https://docs.gitcode.com/docs/openapi/repos/issues/#7-%e8%8e%b7%e5%8f%96-issue-%e5%85%b3%e8%81%94%e7%9a%84-pull-requests
func (s *IssuesService) ListIssueLinkingPullRequests(ctx context.Context, owner, repo, number string) ([]*PullRequest, bool, error) {
urlStr := fmt.Sprintf("repos/%s/issues/%s/pull_requests", owner, number)
req, err := newRequest(s.api, http.MethodGet, urlStr, &url.Values{"repo": []string{repo}}, RequestHandler{t: Query})
urlStr := fmt.Sprintf("repos/%s/%s/issues/%s/pull_requests", owner, repo, number)
req, err := newRequest(s.api, http.MethodGet, urlStr, nil)
if err != nil {
return nil, false, err
}
Expand Down
16 changes: 16 additions & 0 deletions openapi/issues_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,19 @@ func (s *IssuesService) RemoveLabelsFromIssue(ctx context.Context, owner, repo,
resp, err := s.api.Do(ctx, req, nil)
return successModified(resp), err
}

// GetIssueLabels 获取企业某个Issue所有标签
//
// api Docs: https://docs.gitcode.com/docs/openapi/repos/issues/#8%e8%8e%b7%e5%8f%96%e4%bc%81%e4%b8%9a%e6%9f%90%e4%b8%aaissue%e6%89%80%e6%9c%89%e6%a0%87%e7%ad%be
func (s *IssuesService) GetIssueLabels(ctx context.Context, owner, issueID, page string) ([]*Label, bool, error) {
urlStr := fmt.Sprintf("enterprises/%s/issues/%s/labels", owner, issueID)
req, err := newRequest(s.api, http.MethodGet, urlStr,
&url.Values{"page": []string{page}, "per_page": []string{"100"}})
if err != nil {
return nil, false, err
}

var havingLabels []*Label
resp, err := s.api.Do(ctx, req, &havingLabels)
return havingLabels, successGetData(resp), err
}
26 changes: 26 additions & 0 deletions openapi/issues_labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,29 @@ func TestRemoveLabelsFromIssue(t *testing.T) {
assert.Equal(t, true, ok)

}

func TestGetIssueLabels(t *testing.T) {

client, mux, _ := mockServer(t)

var labels []*Label
_ = readTestdata(t, issuesTestDataDir+"issues_having_labels.json", &labels)

mux.HandleFunc("/enterprises/"+owner+"/issues/5342/labels", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(headerContentTypeName, headerContentTypeJsonValue)
err := json.NewEncoder(w).Encode(labels)
if err != nil {
t.Errorf("Issues.GetIssueLabels mock response data error: %v", err)
}
})

result, ok, err := client.Issues.GetIssueLabels(context.Background(), owner, "5342", "1")
if err != nil {
t.Errorf("Issues.GetIssueLabels returned error: %v", err)
}
assert.Equal(t, true, ok)
for i := range labels {
assert.Equal(t, *labels[i], *result[i])
}

}
12 changes: 6 additions & 6 deletions openapi/issues_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ type Issue struct {
Assignee *User `json:"assignee,omitempty"`
Repository *Repository `json:"repository,omitempty"`
Labels []*Label `json:"labels,omitempty"`
CreatedAt *timestamp `json:"created_at,omitempty"`
UpdatedAt *timestamp `json:"updated_at,omitempty"`
ClosedAt *timestamp `json:"finished_at,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
UpdatedAt *Timestamp `json:"updated_at,omitempty"`
ClosedAt *Timestamp `json:"finished_at,omitempty"`
ClosedBy *User `json:"closed_by,omitempty"`

PullRequestLinks *PullRequestLinks `json:"pull_request,omitempty"`
Expand All @@ -64,7 +64,7 @@ type PullRequestLinks struct {
HTMLURL *string `json:"html_url,omitempty"`
DiffURL *string `json:"diff_url,omitempty"`
PatchURL *string `json:"patch_url,omitempty"`
MergedAt *timestamp `json:"merged_at,omitempty"`
MergedAt *Timestamp `json:"merged_at,omitempty"`
}

type IssueRequest struct {
Expand All @@ -84,7 +84,7 @@ type IssueComment struct {
ID *json.Number `json:"id,omitempty"`
Body *string `json:"body,omitempty"`
User *User `json:"user,omitempty"`
CreatedAt *timestamp `json:"created_at,omitempty"`
UpdatedAt *timestamp `json:"updated_at,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
UpdatedAt *Timestamp `json:"updated_at,omitempty"`
Target *Issue `json:"target,omitempty"`
}
2 changes: 1 addition & 1 deletion openapi/issues_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func TestListIssueLinkingPullRequests(t *testing.T) {
prs := new([]*PullRequest)
_ = readTestdata(t, issuesTestDataDir+"issues_linking_prs.json", prs)

mux.HandleFunc(prefixUrlPath+owner+"/issues/1/pull_requests", func(w http.ResponseWriter, r *http.Request) {
mux.HandleFunc(prefixUrlPath+owner+"/"+repo+"/issues/1/pull_requests", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(headerContentTypeName, headerContentTypeJsonValue)
err := json.NewEncoder(w).Encode(prs)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions openapi/org_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ type Organization struct {
PrivateRepos *int `json:"private_repos,omitempty"`
Followers *int `json:"followers,omitempty"`
FollowCount *int `json:"follow_count,omitempty"`
CreatedAt *timestamp `json:"created_at,omitempty"`
UpdatedAt *timestamp `json:"updated_at,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
UpdatedAt *Timestamp `json:"updated_at,omitempty"`
Members *string `json:"members,omitempty"`
Public *bool `json:"public,omitempty"`
Type *string `json:"type,omitempty"`
Expand Down
49 changes: 49 additions & 0 deletions openapi/pull_requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"fmt"
"net/http"
"net/url"
)

// GetPullRequest 获取单个Pull Requests
Expand Down Expand Up @@ -78,3 +79,51 @@ func (s *PullRequestsService) ListPullRequestCommits(ctx context.Context, owner,
resp, err := s.api.Do(ctx, req, &commitList)
return commitList, successGetData(resp), err
}

// GetPullRequestChangeFiles Pull Request Commit文件列表
//
// api Docs: https://docs.gitcode.com/docs/openapi/repos/pulls/#5-pull-request-commit%e6%96%87%e4%bb%b6%e5%88%97%e8%a1%a8
func (s *PullRequestsService) GetPullRequestChangeFiles(ctx context.Context, owner, repo, number string) ([]*CommitFile, bool, error) {
urlStr := fmt.Sprintf("repos/%s/%s/pulls/%s/files", owner, repo, number)
req, err := newRequest(s.api, http.MethodGet, urlStr, nil)
if err != nil {
return nil, false, err
}

var changes []*CommitFile
resp, err := s.api.Do(ctx, req, &changes)
return changes, successGetData(resp), err
}

// MergePullRequest 合并Pull Request
//
// api Docs: https://docs.gitcode.com/docs/openapi/repos/pulls/#2-%e5%90%88%e5%b9%b6pull-request
func (s *PullRequestsService) MergePullRequest(ctx context.Context, owner, repo, number, mergeMethod string) (*PullRequestMergedResult, bool, error) {
urlStr := fmt.Sprintf("repos/%s/%s/pulls/%s/merge", owner, repo, number)
req, err := newRequest(s.api, http.MethodPut, urlStr, &PullRequestRequestMerge{
Method: mergeMethod,
})
if err != nil {
return nil, false, err
}

pr := new(PullRequestMergedResult)
resp, err := s.api.Do(ctx, req, pr)
return pr, successCreated(resp), err
}

// ListPullRequestOperationLogs 获取某个Pull Request的操作日志
//
// api Docs: https://docs.gitcode.com/docs/openapi/repos/pulls/#15-%e8%8e%b7%e5%8f%96%e6%9f%90%e4%b8%aapull-request%e7%9a%84%e6%93%8d%e4%bd%9c%e6%97%a5%e5%bf%97
func (s *PullRequestsService) ListPullRequestOperationLogs(ctx context.Context, owner, repo, number, sort, page string) ([]*PullRequestOperationLog, bool, error) {
urlStr := fmt.Sprintf("repos/%s/%s/pulls/%s/operate_logs", owner, repo, number)
req, err := newRequest(s.api, http.MethodGet, urlStr,
&url.Values{"page": []string{page}, "per_page": []string{"100"}, "sort": []string{sort}}, RequestHandler{t: Query})
if err != nil {
return nil, false, err
}

var logs []*PullRequestOperationLog
resp, err := s.api.Do(ctx, req, &logs)
return logs, successGetData(resp), err
}
56 changes: 56 additions & 0 deletions openapi/pull_requests_comments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,59 @@ func TestCreatePullRequestComment(t *testing.T) {
d2, _ := json.Marshal(*got)
assert.Equal(t, d1, d2)
}

func TestListPullRequestComments(t *testing.T) {

client, mux, _ := mockServer(t)

want := new([]*PullRequestComment)
_ = readTestdata(t, prTestDataDir+"pull_requests_comments.json", want)

mux.HandleFunc(prefixUrlPath+owner+"/"+repo+"/pulls/127/comments", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(headerContentTypeName, headerContentTypeJsonValue)
_ = json.NewEncoder(w).Encode(want)
})

ctx := context.Background()
got, ok, err := client.PullRequests.ListPullRequestComments(ctx, owner, repo, "127", "2", "pr_comment")
assert.Equal(t, nil, err)
assert.Equal(t, true, ok)

for i := range *want {
d1, _ := json.Marshal(*(*want)[i])
d2, _ := json.Marshal(*got[i])
assert.Equal(t, d1, d2)
}

assert.Equal(t, "2024-12-11T14:38:33+08:00", *got[0].UpdatedAt.ToString())
}

func TestUpdatePullRequestComment(t *testing.T) {

client, mux, _ := mockServer(t)

mux.HandleFunc(prefixUrlPath+owner+"/"+repo+"/pulls/comments/1274612", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(headerContentTypeName, headerContentTypeJsonValue)
w.WriteHeader(http.StatusOK)
})

ctx := context.Background()
ok, err := client.PullRequests.UpdatePullRequestComment(ctx, owner, repo, "1274612", "gfgfds")
assert.Equal(t, nil, err)
assert.Equal(t, true, ok)
}

func TestDeletePullRequestComment(t *testing.T) {

client, mux, _ := mockServer(t)

mux.HandleFunc(prefixUrlPath+owner+"/"+repo+"/pulls/comments/64345", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(headerContentTypeName, headerContentTypeJsonValue)
w.WriteHeader(http.StatusOK)
})

ctx := context.Background()
ok, err := client.PullRequests.DeletePullRequestComment(ctx, owner, repo, "64345")
assert.Equal(t, nil, err)
assert.Equal(t, true, ok)
}
15 changes: 15 additions & 0 deletions openapi/pull_requests_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,18 @@ func (s *PullRequestsService) RemoveLabelsFromPullRequest(ctx context.Context, o
resp, err := s.api.Do(ctx, req, nil)
return successModified(resp), err
}

// GetLabelsOfPullRequest 获取某个 Pull Request 的所有标签
//
// api Docs: https://docs.gitcode.com/docs/openapi/repos/pulls/#16-%e8%8e%b7%e5%8f%96%e6%9f%90%e4%b8%aa-pull-request-%e7%9a%84%e6%89%80%e6%9c%89%e6%a0%87%e7%ad%be
func (s *PullRequestsService) GetLabelsOfPullRequest(ctx context.Context, owner, repo, number string) ([]*Label, bool, error) {
urlStr := fmt.Sprintf("repos/%s/%s/pulls/%s/labels", owner, repo, number)
req, err := newRequest(s.api, http.MethodGet, urlStr, nil)
if err != nil {
return nil, false, err
}

var labels []*Label
resp, err := s.api.Do(ctx, req, &labels)
return labels, successGetData(resp), err
}
26 changes: 26 additions & 0 deletions openapi/pull_requests_labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,29 @@ func TestRemoveLabelsFromPullRequest(t *testing.T) {
assert.Equal(t, nil, err)
assert.Equal(t, true, ok)
}

func TestGetLabelsOfPullRequest(t *testing.T) {

client, mux, _ := mockServer(t)

var labels []*Label
_ = readTestdata(t, prTestDataDir+"pull_requests_add_labels.json", &labels)

mux.HandleFunc(prefixUrlPath+owner+"/"+repo+"/pulls/4432/labels", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set(headerContentTypeName, headerContentTypeJsonValue)
err := json.NewEncoder(w).Encode(labels)
if err != nil {
t.Errorf("PR.GetLabelsOfPullRequest mock response data error: %v", err)
}
})

result, ok, err := client.PullRequests.GetLabelsOfPullRequest(context.Background(), owner, repo, "4432")
if err != nil {
t.Errorf("PR.GetLabelsOfPullRequest returned error: %v", err)
}
assert.Equal(t, true, ok)
for i := range labels {
assert.Equal(t, *labels[i], *result[i])
}

}
Loading

0 comments on commit 8ebd404

Please sign in to comment.