diff --git a/github.go b/github.go index d3a9498..c6a66e8 100644 --- a/github.go +++ b/github.go @@ -43,6 +43,10 @@ func NewGitHub(client ApiClient) *GitHub { return &GitHub{client: client} } +// Determine whether the request `content-type` includes a +// server-acceptable mime-type +// +// Failure should yield an HTTP 415 (`http.StatusUnsupportedMediaType`) func HasContentType(r *http.Response, mimetype string) bool { contentType := r.Header.Get("Content-type") if contentType == "" { @@ -74,8 +78,8 @@ type GitTag struct { ZipballURL string `json:"zipball_url,omitempty"` } -func (gh *GitHub) GetGitTagInfo(repository, tag string, ctx context.Context) (*GitTag, error) { - url := fmt.Sprintf("/repos/%s/tags/%s", repository, tag) +func (gh *GitHub) GetGitTagInfo(owner, repo, tag string, ctx context.Context) (*GitTag, error) { + url := fmt.Sprintf("/repos/%s/%s/tags/%s", owner, repo, tag) req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) if err != nil { return nil, err @@ -113,7 +117,8 @@ func (gh *GitHub) GetGitTagInfo(repository, tag string, ctx context.Context) (*G } type CreateReleaseRequest struct { - Repository string `json:"-"` + Owner string `json:"-"` + Repo string `json:"-"` Body string `json:"body,omitempty"` Draft bool `json:"draft,omitempty"` @@ -148,7 +153,7 @@ func (gh *GitHub) CreateRelease(creq *CreateReleaseRequest, ctx context.Context) return nil, err } - url := fmt.Sprintf("/repos/%s/releases", creq.Repository) + url := fmt.Sprintf("/repos/%s/%s/releases", creq.Owner, creq.Repo) req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, &b) if err != nil { return nil, err @@ -201,7 +206,8 @@ func (gh *GitHub) CreateRelease(creq *CreateReleaseRequest, ctx context.Context) } type CreateReleaseArtifactRequest struct { - Repository string `json:"-"` + Owner string `json:"-"` + Repo string `json:"-"` Id int64 `json:"-"` @@ -251,7 +257,7 @@ func (gh *GitHub) CreateReleaseArtifact(creq *CreateReleaseArtifactRequest, ctx w.Close() - url := fmt.Sprintf("/repos/%s/releases/%d/assets", creq.Repository, creq.Id) + url := fmt.Sprintf("/repos/%s/%s/releases/%d/assets", creq.Owner, creq.Repo, creq.Id) req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, &b) if err != nil { return nil, err diff --git a/main.go b/main.go index 94941be..99402ce 100644 --- a/main.go +++ b/main.go @@ -26,7 +26,7 @@ func main() { type ReleaseAction struct { *sdk.Action - repository string + repository Repository version string draft bool @@ -38,13 +38,26 @@ type ReleaseAction struct { artifacts []string } +type Repository []string + +func NewRepository(r string) (Repository, error) { + split := strings.Split(string(r), "/") + if len(split) != 2 { + return nil, fmt.Errorf("repository must be in the format '/'") + } + return split, nil +} +func (r Repository) Owner() string { return r[0] } +func (r Repository) Repo() string { return r[1] } + func (action *ReleaseAction) setup() error { var err error - action.repository = action.GetInput("repository") - if ok, err := regexp.MatchString(`^(\w+)/(\w+)$`, action.repository); !ok || err != nil { - return fmt.Errorf("input 'repository': is empty or does not match /") + r, err := NewRepository(action.GetInput("repository")) + if err != nil { + return fmt.Errorf("input 'repository': %s", err) } + action.repository = r action.version = action.GetInput("version") if len(action.version) == 0 { @@ -109,7 +122,7 @@ func (action *ReleaseAction) run(ctx context.Context) error { // only get the commit info when we are missing a name or a text if len(action.label) == 0 || len(action.notes) == 0 { action.Debugf("Getting git tag information from repository") - tag, err := github.GetGitTagInfo(action.repository, action.version, ctx) + tag, err := github.GetGitTagInfo(action.repository.Owner(), action.repository.Repo(), action.version, ctx) if err != nil { return err } @@ -123,12 +136,13 @@ func (action *ReleaseAction) run(ctx context.Context) error { action.Debugf("Creating release %s", action.version) release, err := github.CreateRelease(&CreateReleaseRequest{ - Repository: action.repository, - TagName: action.version, + Owner: action.repository.Owner(), + Repo: action.repository.Repo(), Name: action.label, Body: action.notes, Draft: action.draft, PreRelease: action.prerelease, + TagName: action.version, }, ctx) if err != nil { return fmt.Errorf("cannot create release for %s: %s", action.version, err) @@ -146,8 +160,9 @@ func (action *ReleaseAction) run(ctx context.Context) error { return } a, err := github.CreateReleaseArtifact(&CreateReleaseArtifactRequest{ - Repository: action.repository, Id: release.Id, + Owner: action.repository.Owner(), + Repo: action.repository.Repo(), Name: f.Name(), // unused? Attachment: f, }, ctx)