remove ProjectFile stuff
This commit is contained in:
@@ -3,8 +3,8 @@ package resource
|
||||
import (
|
||||
"sort"
|
||||
|
||||
"github.com/cppforlife/go-semi-semantic/version"
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"github.com/cppforlife/go-semi-semantic/version"
|
||||
)
|
||||
|
||||
type CheckCommand struct {
|
||||
@@ -47,7 +47,7 @@ func (c *CheckCommand) Run(request CheckRequest) ([]Version, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
/* if tag.Release == nil {
|
||||
/* if tag.Release == nil {
|
||||
continue
|
||||
}*/
|
||||
|
||||
|
@@ -37,18 +37,6 @@ type FakeGitea struct {
|
||||
result1 *gitea.Tag
|
||||
result2 error
|
||||
}
|
||||
DownloadProjectFileStub func(string, string) error
|
||||
downloadProjectFileMutex sync.RWMutex
|
||||
downloadProjectFileArgsForCall []struct {
|
||||
arg1 string
|
||||
arg2 string
|
||||
}
|
||||
downloadProjectFileReturns struct {
|
||||
result1 error
|
||||
}
|
||||
downloadProjectFileReturnsOnCall map[int]struct {
|
||||
result1 error
|
||||
}
|
||||
GetTagStub func(string) (*gitea.Tag, error)
|
||||
getTagMutex sync.RWMutex
|
||||
getTagArgsForCall []struct {
|
||||
@@ -101,19 +89,6 @@ type FakeGitea struct {
|
||||
result1 *gitea.Release
|
||||
result2 error
|
||||
}
|
||||
UploadProjectFileStub func(string) (*gitea.ProjectFile, error)
|
||||
uploadProjectFileMutex sync.RWMutex
|
||||
uploadProjectFileArgsForCall []struct {
|
||||
arg1 string
|
||||
}
|
||||
uploadProjectFileReturns struct {
|
||||
result1 *gitea.ProjectFile
|
||||
result2 error
|
||||
}
|
||||
uploadProjectFileReturnsOnCall map[int]struct {
|
||||
result1 *gitea.ProjectFile
|
||||
result2 error
|
||||
}
|
||||
invocations map[string][][]interface{}
|
||||
invocationsMutex sync.RWMutex
|
||||
}
|
||||
@@ -246,67 +221,6 @@ func (fake *FakeGitea) CreateTagReturnsOnCall(i int, result1 *gitea.Tag, result2
|
||||
}{result1, result2}
|
||||
}
|
||||
|
||||
func (fake *FakeGitea) DownloadProjectFile(arg1 string, arg2 string) error {
|
||||
fake.downloadProjectFileMutex.Lock()
|
||||
ret, specificReturn := fake.downloadProjectFileReturnsOnCall[len(fake.downloadProjectFileArgsForCall)]
|
||||
fake.downloadProjectFileArgsForCall = append(fake.downloadProjectFileArgsForCall, struct {
|
||||
arg1 string
|
||||
arg2 string
|
||||
}{arg1, arg2})
|
||||
fake.recordInvocation("DownloadProjectFile", []interface{}{arg1, arg2})
|
||||
fake.downloadProjectFileMutex.Unlock()
|
||||
if fake.DownloadProjectFileStub != nil {
|
||||
return fake.DownloadProjectFileStub(arg1, arg2)
|
||||
}
|
||||
if specificReturn {
|
||||
return ret.result1
|
||||
}
|
||||
fakeReturns := fake.downloadProjectFileReturns
|
||||
return fakeReturns.result1
|
||||
}
|
||||
|
||||
func (fake *FakeGitea) DownloadProjectFileCallCount() int {
|
||||
fake.downloadProjectFileMutex.RLock()
|
||||
defer fake.downloadProjectFileMutex.RUnlock()
|
||||
return len(fake.downloadProjectFileArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeGitea) DownloadProjectFileCalls(stub func(string, string) error) {
|
||||
fake.downloadProjectFileMutex.Lock()
|
||||
defer fake.downloadProjectFileMutex.Unlock()
|
||||
fake.DownloadProjectFileStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeGitea) DownloadProjectFileArgsForCall(i int) (string, string) {
|
||||
fake.downloadProjectFileMutex.RLock()
|
||||
defer fake.downloadProjectFileMutex.RUnlock()
|
||||
argsForCall := fake.downloadProjectFileArgsForCall[i]
|
||||
return argsForCall.arg1, argsForCall.arg2
|
||||
}
|
||||
|
||||
func (fake *FakeGitea) DownloadProjectFileReturns(result1 error) {
|
||||
fake.downloadProjectFileMutex.Lock()
|
||||
defer fake.downloadProjectFileMutex.Unlock()
|
||||
fake.DownloadProjectFileStub = nil
|
||||
fake.downloadProjectFileReturns = struct {
|
||||
result1 error
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeGitea) DownloadProjectFileReturnsOnCall(i int, result1 error) {
|
||||
fake.downloadProjectFileMutex.Lock()
|
||||
defer fake.downloadProjectFileMutex.Unlock()
|
||||
fake.DownloadProjectFileStub = nil
|
||||
if fake.downloadProjectFileReturnsOnCall == nil {
|
||||
fake.downloadProjectFileReturnsOnCall = make(map[int]struct {
|
||||
result1 error
|
||||
})
|
||||
}
|
||||
fake.downloadProjectFileReturnsOnCall[i] = struct {
|
||||
result1 error
|
||||
}{result1}
|
||||
}
|
||||
|
||||
func (fake *FakeGitea) GetTag(arg1 string) (*gitea.Tag, error) {
|
||||
fake.getTagMutex.Lock()
|
||||
ret, specificReturn := fake.getTagReturnsOnCall[len(fake.getTagArgsForCall)]
|
||||
@@ -552,69 +466,6 @@ func (fake *FakeGitea) UpdateReleaseReturnsOnCall(i int, result1 *gitea.Release,
|
||||
}{result1, result2}
|
||||
}
|
||||
|
||||
func (fake *FakeGitea) UploadProjectFile(arg1 string) (*gitea.ProjectFile, error) {
|
||||
fake.uploadProjectFileMutex.Lock()
|
||||
ret, specificReturn := fake.uploadProjectFileReturnsOnCall[len(fake.uploadProjectFileArgsForCall)]
|
||||
fake.uploadProjectFileArgsForCall = append(fake.uploadProjectFileArgsForCall, struct {
|
||||
arg1 string
|
||||
}{arg1})
|
||||
fake.recordInvocation("UploadProjectFile", []interface{}{arg1})
|
||||
fake.uploadProjectFileMutex.Unlock()
|
||||
if fake.UploadProjectFileStub != nil {
|
||||
return fake.UploadProjectFileStub(arg1)
|
||||
}
|
||||
if specificReturn {
|
||||
return ret.result1, ret.result2
|
||||
}
|
||||
fakeReturns := fake.uploadProjectFileReturns
|
||||
return fakeReturns.result1, fakeReturns.result2
|
||||
}
|
||||
|
||||
func (fake *FakeGitea) UploadProjectFileCallCount() int {
|
||||
fake.uploadProjectFileMutex.RLock()
|
||||
defer fake.uploadProjectFileMutex.RUnlock()
|
||||
return len(fake.uploadProjectFileArgsForCall)
|
||||
}
|
||||
|
||||
func (fake *FakeGitea) UploadProjectFileCalls(stub func(string) (*gitea.ProjectFile, error)) {
|
||||
fake.uploadProjectFileMutex.Lock()
|
||||
defer fake.uploadProjectFileMutex.Unlock()
|
||||
fake.UploadProjectFileStub = stub
|
||||
}
|
||||
|
||||
func (fake *FakeGitea) UploadProjectFileArgsForCall(i int) string {
|
||||
fake.uploadProjectFileMutex.RLock()
|
||||
defer fake.uploadProjectFileMutex.RUnlock()
|
||||
argsForCall := fake.uploadProjectFileArgsForCall[i]
|
||||
return argsForCall.arg1
|
||||
}
|
||||
|
||||
func (fake *FakeGitea) UploadProjectFileReturns(result1 *gitea.ProjectFile, result2 error) {
|
||||
fake.uploadProjectFileMutex.Lock()
|
||||
defer fake.uploadProjectFileMutex.Unlock()
|
||||
fake.UploadProjectFileStub = nil
|
||||
fake.uploadProjectFileReturns = struct {
|
||||
result1 *gitea.ProjectFile
|
||||
result2 error
|
||||
}{result1, result2}
|
||||
}
|
||||
|
||||
func (fake *FakeGitea) UploadProjectFileReturnsOnCall(i int, result1 *gitea.ProjectFile, result2 error) {
|
||||
fake.uploadProjectFileMutex.Lock()
|
||||
defer fake.uploadProjectFileMutex.Unlock()
|
||||
fake.UploadProjectFileStub = nil
|
||||
if fake.uploadProjectFileReturnsOnCall == nil {
|
||||
fake.uploadProjectFileReturnsOnCall = make(map[int]struct {
|
||||
result1 *gitea.ProjectFile
|
||||
result2 error
|
||||
})
|
||||
}
|
||||
fake.uploadProjectFileReturnsOnCall[i] = struct {
|
||||
result1 *gitea.ProjectFile
|
||||
result2 error
|
||||
}{result1, result2}
|
||||
}
|
||||
|
||||
func (fake *FakeGitea) Invocations() map[string][][]interface{} {
|
||||
fake.invocationsMutex.RLock()
|
||||
defer fake.invocationsMutex.RUnlock()
|
||||
@@ -622,8 +473,6 @@ func (fake *FakeGitea) Invocations() map[string][][]interface{} {
|
||||
defer fake.createReleaseMutex.RUnlock()
|
||||
fake.createTagMutex.RLock()
|
||||
defer fake.createTagMutex.RUnlock()
|
||||
fake.downloadProjectFileMutex.RLock()
|
||||
defer fake.downloadProjectFileMutex.RUnlock()
|
||||
fake.getTagMutex.RLock()
|
||||
defer fake.getTagMutex.RUnlock()
|
||||
fake.listTagsMutex.RLock()
|
||||
@@ -632,8 +481,6 @@ func (fake *FakeGitea) Invocations() map[string][][]interface{} {
|
||||
defer fake.listTagsUntilMutex.RUnlock()
|
||||
fake.updateReleaseMutex.RLock()
|
||||
defer fake.updateReleaseMutex.RUnlock()
|
||||
fake.uploadProjectFileMutex.RLock()
|
||||
defer fake.uploadProjectFileMutex.RUnlock()
|
||||
copiedInvocations := map[string][][]interface{}{}
|
||||
for key, value := range fake.invocations {
|
||||
copiedInvocations[key] = value
|
||||
|
82
gitea.go
82
gitea.go
@@ -3,13 +3,8 @@ package resource
|
||||
import (
|
||||
"crypto/tls"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/oauth2"
|
||||
|
||||
@@ -18,7 +13,6 @@ import (
|
||||
"code.gitea.io/sdk/gitea"
|
||||
)
|
||||
|
||||
//go:generate counterfeiter . Gitea
|
||||
|
||||
type Gitea interface {
|
||||
ListTags() ([]*gitea.Tag, error)
|
||||
@@ -27,8 +21,6 @@ type Gitea interface {
|
||||
CreateTag(tag_name string, ref string) (*gitea.Tag, error)
|
||||
CreateRelease(tag_name string, description string) (*gitea.Release, error)
|
||||
UpdateRelease(tag_name string, description string) (*gitea.Release, error)
|
||||
UploadProjectFile(file string) (*gitea.ProjectFile, error)
|
||||
DownloadProjectFile(url, file string) error
|
||||
}
|
||||
|
||||
type GiteaClient struct {
|
||||
@@ -42,6 +34,11 @@ func NewGiteaClient(source Source) (*GiteaClient, error) {
|
||||
var httpClient = &http.Client{}
|
||||
var ctx = context.TODO()
|
||||
|
||||
baseUrl, err := url.Parse(source.GiteaAPIURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if source.Insecure {
|
||||
httpClient.Transport = &http.Transport{
|
||||
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||
@@ -49,16 +46,10 @@ func NewGiteaClient(source Source) (*GiteaClient, error) {
|
||||
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
|
||||
}
|
||||
|
||||
client := gitea.NewClient(httpClient, source.AccessToken)
|
||||
|
||||
if source.GiteaAPIURL != "" {
|
||||
var err error
|
||||
baseUrl, err := url.Parse(source.GiteaAPIURL)
|
||||
client, err := gitea.NewClient(baseUrl.String(), gitea.SetHTTPClient(httpClient), gitea.SetToken(source.AccessToken))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
client.SetBaseURL(baseUrl.String())
|
||||
}
|
||||
|
||||
return &GiteaClient{
|
||||
client: client,
|
||||
@@ -242,64 +233,3 @@ func (g *GiteaClient) UpdateRelease(tag_name string, description string) (*gitea
|
||||
|
||||
return release, nil
|
||||
}
|
||||
|
||||
func (g *GiteaClient) UploadProjectFile(file string) (*gitea.ProjectFile, error) {
|
||||
projectFile, res, err := g.client.Projects.UploadFile(g.repository, file)
|
||||
if err != nil {
|
||||
return &gitea.ProjectFile{}, err
|
||||
}
|
||||
|
||||
err = res.Body.Close()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return projectFile, nil
|
||||
}
|
||||
|
||||
func (g *GiteaClient) DownloadProjectFile(filePath, destPath string) error {
|
||||
out, err := os.Create(destPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer out.Close()
|
||||
|
||||
// e.g. (group/project) + (/uploads/hash/filename)
|
||||
filePathRef, err := url.Parse(g.repository + filePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// e.g. (https://gitea-instance/api/v4) + (/group/project/uploads/hash/filename)
|
||||
projectFileUrl := g.client.BaseURL().ResolveReference(filePathRef)
|
||||
|
||||
// https://gitea.com/gitea-org/gitea-ce/issues/51447
|
||||
nonApiUrl := strings.Replace(projectFileUrl.String(), "/api/v4", "", 1)
|
||||
projectFileUrl, err = url.Parse(nonApiUrl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
client := &http.Client{}
|
||||
req, err := http.NewRequest("GET", projectFileUrl.String(), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Add("Private-Token", g.accessToken)
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return fmt.Errorf("failed to download file `%s`: HTTP status %d", filepath.Base(destPath), resp.StatusCode)
|
||||
}
|
||||
|
||||
_, err = io.Copy(out, resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
@@ -108,7 +108,7 @@ func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
|
||||
continue
|
||||
}
|
||||
|
||||
err := c.gitea.DownloadProjectFile(attachment.URL, path)
|
||||
err := gitea.GetReleaseAttachment(attachment.URL, path)
|
||||
if err != nil {
|
||||
return InResponse{}, err
|
||||
}
|
||||
|
@@ -43,8 +43,6 @@ var _ = Describe("In Command", func() {
|
||||
|
||||
destDir = filepath.Join(tmpDir, "destination")
|
||||
|
||||
giteaClient.DownloadProjectFileReturns(nil)
|
||||
|
||||
inRequest = resource.InRequest{}
|
||||
})
|
||||
|
||||
@@ -105,19 +103,6 @@ var _ = Describe("In Command", func() {
|
||||
Ω(giteaClient.GetTagArgsForCall(0)).Should(Equal("v0.35.0"))
|
||||
})
|
||||
|
||||
It("downloads only the files that match the globs", func() {
|
||||
inResponse, inErr = command.Run(destDir, inRequest)
|
||||
|
||||
Expect(giteaClient.DownloadProjectFileCallCount()).To(Equal(2))
|
||||
arg1, arg2 := giteaClient.DownloadProjectFileArgsForCall(0)
|
||||
Ω(arg1).Should(Equal("example.txt"))
|
||||
Ω(arg2).Should(Equal("path"))
|
||||
|
||||
arg1, arg2 = giteaClient.DownloadProjectFileArgsForCall(1)
|
||||
Ω(arg1).Should(Equal("example.rtf"))
|
||||
Ω(arg2).Should(Equal("path"))
|
||||
})
|
||||
|
||||
It("does create the body, tag and version files", func() {
|
||||
inResponse, inErr = command.Run(destDir, inRequest)
|
||||
|
||||
@@ -192,30 +177,6 @@ var _ = Describe("In Command", func() {
|
||||
))
|
||||
})
|
||||
|
||||
It("downloads all of the files", func() {
|
||||
arg1, arg2 := giteaClient.DownloadProjectFileArgsForCall(0)
|
||||
Ω(arg1).Should(Equal("example.txt"))
|
||||
Ω(arg2).Should(Equal("path"))
|
||||
|
||||
arg1, arg2 = giteaClient.DownloadProjectFileArgsForCall(1)
|
||||
Ω(arg1).Should(Equal("example.rtf"))
|
||||
Ω(arg2).Should(Equal("path"))
|
||||
|
||||
arg1, arg2 = giteaClient.DownloadProjectFileArgsForCall(2)
|
||||
Ω(arg1).Should(Equal("example.rtf"))
|
||||
Ω(arg2).Should(Equal("path"))
|
||||
})
|
||||
})
|
||||
|
||||
Context("when downloading an asset fails", func() {
|
||||
BeforeEach(func() {
|
||||
giteaClient.DownloadProjectFileReturns(errors.New("not this time"))
|
||||
inResponse, inErr = command.Run(destDir, inRequest)
|
||||
})
|
||||
|
||||
It("returns an error", func() {
|
||||
Ω(inErr).Should(HaveOccurred())
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@@ -2,7 +2,7 @@ package resource
|
||||
|
||||
import "code.gitea.io/sdk/gitea"
|
||||
|
||||
func metadataFromTag(tag *gitlab.Tag) []MetadataPair {
|
||||
func metadataFromTag(tag *gitea.Tag) []MetadataPair {
|
||||
metadata := []MetadataPair{}
|
||||
|
||||
if tag.Name != "" {
|
||||
|
@@ -7,6 +7,8 @@ import (
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
)
|
||||
|
||||
type OutCommand struct {
|
||||
@@ -83,7 +85,7 @@ func (c *OutCommand) Run(sourceDir string, request OutRequest) (OutResponse, err
|
||||
}
|
||||
|
||||
for _, filePath := range matches {
|
||||
projectFile, err := c.gitea.UploadProjectFile(filePath)
|
||||
projectFile, err := gitea.CreateReleaseAttachment(filePath)
|
||||
if err != nil {
|
||||
return OutResponse{}, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user