Skip to content

Commit

Permalink
Merge pull request #90 from bakito/stash-version
Browse files Browse the repository at this point in the history
retry with event subset for legacy stash versions
  • Loading branch information
bradrydzewski authored Jan 27, 2021
2 parents e5a5af2 + 05d6222 commit 4a12b86
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions scm/driver/stash/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"net/url"
"strconv"
"strings"

"github.com/drone/go-scm/scm"
)
Expand Down Expand Up @@ -209,6 +210,10 @@ func (s *repositoryService) CreateHook(ctx context.Context, repo string, input *
)
out := new(hook)
res, err := s.client.do(ctx, "POST", path, in, out)
if err != nil && isUnknownHookEvent(err) {
downgradeHookInput(in)
res, err = s.client.do(ctx, "POST", path, in, out)
}
return convertHook(out), res, err
}

Expand Down Expand Up @@ -246,6 +251,10 @@ func (s *repositoryService) UpdateHook(ctx context.Context, repo, id string, inp
)
out := new(hook)
res, err := s.client.do(ctx, "PUT", path, in, out)
if err != nil && isUnknownHookEvent(err) {
downgradeHookInput(in)
res, err = s.client.do(ctx, "PUT", path, in, out)
}
return convertHook(out), res, err
}

Expand Down Expand Up @@ -345,6 +354,20 @@ func convertFromHookEvents(from scm.HookEvents) []string {
return events
}

func isUnknownHookEvent(err error) bool {
return strings.Contains(err.Error(), "pr:from_ref_updated is unknown")
}

func downgradeHookInput(in *hookInput) {
var events []string
for _, event := range in.Events {
if event != "pr:from_ref_updated" {
events = append(events, event)
}
}
in.Events = events
}

func convertFromState(from scm.State) string {
switch from {
case scm.StatePending, scm.StateRunning:
Expand Down

0 comments on commit 4a12b86

Please sign in to comment.