forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
so we can create/find/list releases
- Loading branch information
Showing
15 changed files
with
652 additions
and
2 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
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
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
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,98 @@ | ||
package fake | ||
|
||
import ( | ||
"context" | ||
"strconv" | ||
|
||
"github.com/jenkins-x/go-scm/scm" | ||
) | ||
|
||
type releaseService struct { | ||
client *wrapper | ||
data *Data | ||
} | ||
|
||
func (r *releaseService) Find(_ context.Context, repo string, number int) (*scm.Release, *scm.Response, error) { | ||
release := r.releaseMap(repo)[number] | ||
if release == nil { | ||
return nil, nil, scm.ErrNotFound | ||
} | ||
return release, nil, nil | ||
} | ||
|
||
func (r *releaseService) releaseMap(repo string) map[int]*scm.Release { | ||
if r.data.Releases == nil { | ||
r.data.Releases = map[string]map[int]*scm.Release{} | ||
} | ||
releaseMap := r.data.Releases[repo] | ||
if releaseMap == nil { | ||
releaseMap = map[int]*scm.Release{} | ||
r.data.Releases[repo] = releaseMap | ||
} | ||
return releaseMap | ||
} | ||
|
||
func (r *releaseService) FindByTag(_ context.Context, repo string, tag string) (*scm.Release, *scm.Response, error) { | ||
for _, rel := range r.releaseMap(repo) { | ||
if rel.Tag == tag { | ||
return rel, nil, nil | ||
} | ||
} | ||
return nil, nil, scm.ErrNotFound | ||
} | ||
|
||
func (r *releaseService) List(_ context.Context, repo string, options scm.ReleaseListOptions) ([]*scm.Release, *scm.Response, error) { | ||
var answer []*scm.Release | ||
for _, rel := range r.releaseMap(repo) { | ||
answer = append(answer, rel) | ||
} | ||
if len(answer) == 0 { | ||
return nil, nil, scm.ErrNotFound | ||
} | ||
return answer, nil, nil | ||
} | ||
|
||
func (r *releaseService) Create(_ context.Context, repo string, input *scm.ReleaseInput) (*scm.Release, *scm.Response, error) { | ||
m := r.releaseMap(repo) | ||
id := len(m) | ||
release := &scm.Release{ | ||
ID: id, | ||
Title: input.Tag, | ||
Description: input.Tag, | ||
Link: "https://fake.git/" + repo + "/releases/release/" + strconv.Itoa(id), | ||
Tag: input.Tag, | ||
Commitish: input.Commitish, | ||
Draft: input.Draft, | ||
Prerelease: input.Prerelease, | ||
} | ||
m[id] = release | ||
return release, nil, nil | ||
} | ||
|
||
func (r *releaseService) Update(_ context.Context, repo string, number int, input *scm.ReleaseInput) (*scm.Release, *scm.Response, error) { | ||
rel := r.releaseMap(repo)[number] | ||
if rel == nil { | ||
return nil, nil, scm.ErrNotFound | ||
} | ||
if input.Commitish != "" { | ||
rel.Commitish = input.Commitish | ||
} | ||
if input.Description != "" { | ||
rel.Description = input.Description | ||
} | ||
if input.Tag != "" { | ||
rel.Tag = input.Tag | ||
} | ||
if input.Title != "" { | ||
rel.Title = input.Title | ||
} | ||
rel.Draft = input.Draft | ||
rel.Prerelease = input.Prerelease | ||
return nil, nil, nil | ||
} | ||
|
||
func (r *releaseService) Delete(_ context.Context, repo string, number int) (*scm.Response, error) { | ||
m := r.releaseMap(repo) | ||
delete(m, number) | ||
return nil, 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
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,116 @@ | ||
package github | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/jenkins-x/go-scm/scm" | ||
) | ||
|
||
type releaseService struct { | ||
client *wrapper | ||
} | ||
|
||
type release struct { | ||
ID int `json:"id"` | ||
Title string `json:"name"` | ||
Description string `json:"description"` | ||
Link string `json:"html_url,omitempty"` | ||
Tag string `json:"tag_name,omitempty"` | ||
Commitish string `json:"target_commitish,omitempty"` | ||
Draft bool `json:"draft"` | ||
Prerelease bool `json:"prerelease"` | ||
} | ||
|
||
type releaseInput struct { | ||
Title string `json:"name"` | ||
Description string `json:"description"` | ||
Tag string `json:"tag_name"` | ||
Commitish string `json:"target_commitish"` | ||
Draft bool `json:"draft"` | ||
Prerelease bool `json:"prerelease"` | ||
} | ||
|
||
func (s *releaseService) Find(ctx context.Context, repo string, id int) (*scm.Release, *scm.Response, error) { | ||
path := fmt.Sprintf("repos/%s/releases/%d", repo, id) | ||
out := new(release) | ||
res, err := s.client.do(ctx, "GET", path, nil, out) | ||
return convertRelease(out), res, err | ||
} | ||
|
||
func (s *releaseService) FindByTag(ctx context.Context, repo string, tag string) (*scm.Release, *scm.Response, error) { | ||
path := fmt.Sprintf("repos/%s/releases/tags/%s", repo, tag) | ||
out := new(release) | ||
res, err := s.client.do(ctx, "GET", path, nil, out) | ||
return convertRelease(out), res, err | ||
} | ||
|
||
func (s *releaseService) List(ctx context.Context, repo string, opts scm.ReleaseListOptions) ([]*scm.Release, *scm.Response, error) { | ||
path := fmt.Sprintf("repos/%s/releases?%s", repo, encodeReleaseListOptions(opts)) | ||
out := []*release{} | ||
res, err := s.client.do(ctx, "GET", path, nil, &out) | ||
return convertReleaseList(out), res, err | ||
} | ||
|
||
func (s *releaseService) Create(ctx context.Context, repo string, input *scm.ReleaseInput) (*scm.Release, *scm.Response, error) { | ||
path := fmt.Sprintf("repos/%s/releases", repo) | ||
in := &releaseInput{ | ||
Title: input.Title, | ||
Commitish: input.Commitish, | ||
Description: input.Description, | ||
Draft: input.Draft, | ||
Prerelease: input.Prerelease, | ||
Tag: input.Tag, | ||
} | ||
out := new(release) | ||
res, err := s.client.do(ctx, "POST", path, in, out) | ||
return convertRelease(out), res, err | ||
} | ||
|
||
func (s *releaseService) Delete(ctx context.Context, repo string, id int) (*scm.Response, error) { | ||
path := fmt.Sprintf("repos/%s/releases/%d", repo, id) | ||
return s.client.do(ctx, "DELETE", path, nil, nil) | ||
} | ||
|
||
func (s *releaseService) Update(ctx context.Context, repo string, id int, input *scm.ReleaseInput) (*scm.Release, *scm.Response, error) { | ||
path := fmt.Sprintf("repos/%s/releases/%d", repo, id) | ||
in := &releaseInput{} | ||
if input.Title != "" { | ||
in.Title = input.Title | ||
} | ||
if input.Description != "" { | ||
in.Description = input.Description | ||
} | ||
if input.Commitish != "" { | ||
in.Commitish = input.Commitish | ||
} | ||
if input.Tag != "" { | ||
in.Tag = input.Tag | ||
} | ||
in.Draft = input.Draft | ||
in.Prerelease = input.Prerelease | ||
out := new(release) | ||
res, err := s.client.do(ctx, "PATCH", path, in, out) | ||
return convertRelease(out), res, err | ||
} | ||
|
||
func convertReleaseList(from []*release) []*scm.Release { | ||
var to []*scm.Release | ||
for _, m := range from { | ||
to = append(to, convertRelease(m)) | ||
} | ||
return to | ||
} | ||
|
||
func convertRelease(from *release) *scm.Release { | ||
return &scm.Release{ | ||
ID: from.ID, | ||
Title: from.Title, | ||
Description: from.Description, | ||
Link: from.Link, | ||
Tag: from.Tag, | ||
Commitish: from.Commitish, | ||
Draft: from.Draft, | ||
Prerelease: from.Prerelease, | ||
} | ||
} |
Oops, something went wrong.