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

Harness move #237

Merged
merged 4 commits into from
Feb 6, 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
3 changes: 3 additions & 0 deletions scm/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ const (
DriverCoding
DriverGitee
DriverAzure
DriverHarness
)

// String returns the string representation of Driver.
Expand All @@ -169,6 +170,8 @@ func (d Driver) String() (s string) {
return "gitee"
case DriverAzure:
return "azure"
case DriverHarness:
return "harness"
default:
return "unknown"
}
Expand Down
234 changes: 234 additions & 0 deletions scm/driver/harness/content.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package harness

import (
"context"
"encoding/base64"
"fmt"
"time"

"github.com/drone/go-scm/scm"
)

type contentService struct {
client *wrapper
}

func (s *contentService) Find(ctx context.Context, repo, path, ref string) (*scm.Content, *scm.Response, error) {
repo = buildHarnessURI(s.client.account, s.client.organization, s.client.project, repo)
endpoint := fmt.Sprintf("api/v1/repos/%s/content/%s?%s&include_commit=true", repo, path, ref)
out := new(fileContent)
res, err := s.client.do(ctx, "GET", endpoint, nil, out)
// decode raw output content
raw, _ := base64.StdEncoding.DecodeString(out.Content.Data)
return &scm.Content{
Path: path,
Sha: out.LatestCommit.Sha,
BlobID: out.Sha,
Data: raw,
}, res, err
}

func (s *contentService) Create(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
repo = buildHarnessURI(s.client.account, s.client.organization, s.client.project, repo)
endpoint := fmt.Sprintf("api/v1/repos/%s/commits", repo)
a := action{
Action: "CREATE",
Path: path,
Payload: string(params.Data),
Encoding: "string",
}
in := editFile{
Branch: params.Branch,
Message: params.Message,
Title: params.Message,
Actions: []action{a},
}

res, err := s.client.do(ctx, "POST", endpoint, in, nil)
return res, err
}

func (s *contentService) Update(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
repo = buildHarnessURI(s.client.account, s.client.organization, s.client.project, repo)
endpoint := fmt.Sprintf("api/v1/repos/%s/commits", repo)
a := action{
Action: "UPDATE",
Path: path,
Payload: string(params.Data),
Encoding: "string",
}
in := editFile{
Branch: params.Branch,
Message: params.Message,
Title: params.Message,
Actions: []action{a},
}

res, err := s.client.do(ctx, "POST", endpoint, in, nil)
return res, err
}

func (s *contentService) Delete(ctx context.Context, repo, path string, params *scm.ContentParams) (*scm.Response, error) {
repo = buildHarnessURI(s.client.account, s.client.organization, s.client.project, repo)
endpoint := fmt.Sprintf("api/v1/repos/%s/commits", repo)
a := action{
Action: "DELETE",
Path: path,
Encoding: "string",
}
in := editFile{
Branch: params.Branch,
Message: params.Message,
Title: params.Message,
Actions: []action{a},
}

res, err := s.client.do(ctx, "POST", endpoint, in, nil)
return res, err
}

func buildHarnessURI(account, organization, project, repo string) (uri string) {
if account != "" {
return fmt.Sprintf("%s/%s/%s/%s/+", account, organization, project, repo)
}
return repo
}

func (s *contentService) List(ctx context.Context, repo, path, ref string, _ scm.ListOptions) ([]*scm.ContentInfo, *scm.Response, error) {
repo = buildHarnessURI(s.client.account, s.client.organization, s.client.project, repo)
endpoint := fmt.Sprintf("api/v1/repos/%s/content/%s?%s&include_commit=true", repo, path, ref)
out := new(contentList)
res, err := s.client.do(ctx, "GET", endpoint, nil, &out)
return convertContentInfoList(out.Content.Entries), res, err
}

type editFile struct {
Actions []action `json:"actions"`
Branch string `json:"branch"`
Message string `json:"message"`
NewBranch string `json:"new_branch"`
Title string `json:"title"`
}

type action struct {
Action string `json:"action"`
Encoding string `json:"encoding"`
Path string `json:"path"`
Payload string `json:"payload"`
Sha string `json:"sha"`
}

type fileContent struct {
Type string `json:"type"`
Sha string `json:"sha"`
Name string `json:"name"`
Path string `json:"path"`
LatestCommit struct {
Sha string `json:"sha"`
Title string `json:"title"`
Message string `json:"message"`
Author struct {
Identity struct {
Name string `json:"name"`
Email string `json:"email"`
} `json:"identity"`
When time.Time `json:"when"`
} `json:"author"`
Committer struct {
Identity struct {
Name string `json:"name"`
Email string `json:"email"`
} `json:"identity"`
When time.Time `json:"when"`
} `json:"committer"`
} `json:"latest_commit"`
Content struct {
Encoding string `json:"encoding"`
Data string `json:"data"`
Size int `json:"size"`
} `json:"content"`
}

type contentList struct {
Type string `json:"type"`
Sha string `json:"sha"`
Name string `json:"name"`
Path string `json:"path"`
LatestCommit struct {
Sha string `json:"sha"`
Title string `json:"title"`
Message string `json:"message"`
Author struct {
Identity struct {
Name string `json:"name"`
Email string `json:"email"`
} `json:"identity"`
When time.Time `json:"when"`
} `json:"author"`
Committer struct {
Identity struct {
Name string `json:"name"`
Email string `json:"email"`
} `json:"identity"`
When time.Time `json:"when"`
} `json:"committer"`
} `json:"latest_commit"`
Content struct {
Entries []fileEntry `json:"entries"`
} `json:"content"`
}

type fileEntry struct {
Type string `json:"type"`
Sha string `json:"sha"`
Name string `json:"name"`
Path string `json:"path"`
LatestCommit struct {
Sha string `json:"sha"`
Title string `json:"title"`
Message string `json:"message"`
Author struct {
Identity struct {
Name string `json:"name"`
Email string `json:"email"`
} `json:"identity"`
When time.Time `json:"when"`
} `json:"author"`
Committer struct {
Identity struct {
Name string `json:"name"`
Email string `json:"email"`
} `json:"identity"`
When time.Time `json:"when"`
} `json:"committer"`
} `json:"latest_commit"`
}

func convertContentInfoList(from []fileEntry) []*scm.ContentInfo {
to := []*scm.ContentInfo{}
for _, v := range from {
to = append(to, convertContentInfo(v))
}
return to
}

func convertContentInfo(from fileEntry) *scm.ContentInfo {
to := &scm.ContentInfo{
Path: from.Path,
Sha: from.LatestCommit.Sha,
BlobID: from.Sha,
}
switch from.Type {
case "file":
to.Kind = scm.ContentKindFile
case "dir":
to.Kind = scm.ContentKindDirectory
default:
to.Kind = scm.ContentKindUnsupported
}
return to
}
Loading