Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) harness, fix create branch, PR calls #247

Merged
merged 3 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions scm/driver/harness/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ type gitService struct {
func (s *gitService) CreateBranch(ctx context.Context, repo string, params *scm.ReferenceInput) (*scm.Response, error) {
harnessURI := buildHarnessURI(s.client.account, s.client.organization, s.client.project, repo)
path := fmt.Sprintf("api/v1/repos/%s/branches", harnessURI)
out := new(branch)
return s.client.do(ctx, "GET", path, nil, out)
in := &branchInput{
Name: params.Name,
Target: params.Sha,
}
return s.client.do(ctx, "POST", path, in, nil)
}

func (s *gitService) FindBranch(ctx context.Context, repo, name string) (*scm.Reference, *scm.Response, error) {
Expand Down Expand Up @@ -106,6 +109,10 @@ type (
Sha string `json:"sha"`
Title string `json:"title"`
}
branchInput struct {
Name string `json:"name"`
Target string `json:"target"`
}
branch struct {
Commit struct {
Author struct {
Expand Down
34 changes: 34 additions & 0 deletions scm/driver/harness/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,37 @@ func TestListBranches(t *testing.T) {
t.Log(diff)
}
}

func TestCreateBranch(t *testing.T) {

defer gock.Off()

gock.New(gockOrigin).
Post("/gateway/code/api/v1/repos/px7xd_BFRCi-pfWPYXVjvw/default/codeciintegration/thomas/+/branches").
Reply(200).
Type("application/json").
File("testdata/branch.json")

client, _ := New(gockOrigin, harnessOrg, harnessAccount, harnessProject)
client.Client = &http.Client{
Transport: &transport.Custom{
Before: func(r *http.Request) {
r.Header.Set("x-api-key", harnessPAT)
},
},
}
input := &scm.ReferenceInput{
Name: "test",
Sha: "e8ef0374ca0cee8048e94b28eaf0d9e2e2515a14",
}
result, err := client.Git.CreateBranch(context.Background(), harnessRepo, input)
if err != nil {
t.Error(err)
return
}

if result.Status != 200 {
t.Errorf("Unexpected Results")
}

}
92 changes: 0 additions & 92 deletions scm/driver/harness/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ package harness

import (
"context"
"time"

"github.com/drone/go-scm/scm"
)
Expand All @@ -25,17 +24,14 @@ func (s *issueService) FindComment(ctx context.Context, repo string, index, id i

func (s *issueService) List(ctx context.Context, repo string, opts scm.IssueListOptions) ([]*scm.Issue, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported

}

func (s *issueService) ListComments(ctx context.Context, repo string, index int, opts scm.ListOptions) ([]*scm.Comment, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported

}

func (s *issueService) Create(ctx context.Context, repo string, input *scm.IssueInput) (*scm.Issue, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported

}

func (s *issueService) CreateComment(ctx context.Context, repo string, index int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) {
Expand All @@ -57,91 +53,3 @@ func (s *issueService) Lock(ctx context.Context, repo string, number int) (*scm.
func (s *issueService) Unlock(ctx context.Context, repo string, number int) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}

//
// native data structures
//

type (
// gitea issue response object.
issue struct {
ID int `json:"id"`
Number int `json:"number"`
User user `json:"user"`
Title string `json:"title"`
Body string `json:"body"`
State string `json:"state"`
Labels []string `json:"labels"`
Comments int `json:"comments"`
Created time.Time `json:"created_at"`
Updated time.Time `json:"updated_at"`
PullRequest *struct {
Merged bool `json:"merged"`
MergedAt interface{} `json:"merged_at"`
} `json:"pull_request"`
}

// gitea issue request object.
issueInput struct {
Title string `json:"title"`
Body string `json:"body"`
}

// gitea issue comment response object.
issueComment struct {
ID int `json:"id"`
HTMLURL string `json:"html_url"`
User user `json:"user"`
Body string `json:"body"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}

// gitea issue comment request object.
issueCommentInput struct {
Body string `json:"body"`
}
)

//
// native data structure conversion
//

func convertIssueList(from []*issue) []*scm.Issue {
to := []*scm.Issue{}
for _, v := range from {
to = append(to, convertIssue(v))
}
return to
}

func convertIssue(from *issue) *scm.Issue {
return &scm.Issue{
Number: from.Number,
Title: from.Title,
Body: from.Body,
Link: "", // TODO construct the link to the issue.
Closed: from.State == "closed",
Author: *convertUser(&from.User),
Created: from.Created,
Updated: from.Updated,
}
}

func convertIssueCommentList(from []*issueComment) []*scm.Comment {
to := []*scm.Comment{}
for _, v := range from {
to = append(to, convertIssueComment(v))
}
return to
}

func convertIssueComment(from *issueComment) *scm.Comment {
return &scm.Comment{
ID: from.ID,
Body: from.Body,
Author: *convertUser(&from.User),
Created: from.CreatedAt,
Updated: from.UpdatedAt,
}
}
Loading