Skip to content

Commit

Permalink
Fix to check if the repo is enabled and don’t make a call to the sett…
Browse files Browse the repository at this point in the history
…ings it isn’t. The API works differently the first time vs if it’s ever been enabled.
  • Loading branch information
josmo committed Feb 23, 2017
1 parent f9770b0 commit 82189c2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
29 changes: 27 additions & 2 deletions remote/bitbucketserver/internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const (
pathHook = "%s/rest/api/1.0/projects/%s/repos/%s/settings/hooks/%s"
pathSource = "%s/projects/%s/repos/%s/browse/%s?at=%s&raw"
hookName = "com.atlassian.stash.plugin.stash-web-post-receive-hooks-plugin:postReceiveHook"
pathHookDetails = "%s/rest/api/1.0/projects/%s/repos/%s/settings/hooks/%s"
pathHookEnabled = "%s/rest/api/1.0/projects/%s/repos/%s/settings/hooks/%s/enabled"
pathHookSettings = "%s/rest/api/1.0/projects/%s/repos/%s/settings/hooks/%s/settings"
pathStatus = "%s/rest/build-status/1.0/commits/%s"
Expand Down Expand Up @@ -131,15 +132,23 @@ func (c *Client) FindFileForRepo(owner string, repo string, fileName string, ref
}

func (c *Client) CreateHook(owner string, name string, callBackLink string) error {
hookSettings, err := c.GetHooks(owner, name)
hookDetails , err := c.GetHookDetails(owner, name)
if err != nil {
return err
}
hooks := hookSettingsToArray(hookSettings)
hooks := make([]string, 0)
if (hookDetails.Enabled) {
hookSettings, err := c.GetHooks(owner, name)
if err != nil {
return err
}
hooks = hookSettingsToArray(hookSettings)

}
if !stringInSlice(callBackLink, hooks) {
hooks = append(hooks, callBackLink)
}

putHookSettings := arrayToHookSettings(hooks)
hookBytes, err := json.Marshal(putHookSettings)
return c.doPut(fmt.Sprintf(pathHookEnabled, c.base, owner, name, hookName), hookBytes)
Expand All @@ -165,6 +174,22 @@ func (c *Client) DeleteHook(owner string, name string, link string) error {
return c.doPut(fmt.Sprintf(pathHookEnabled, c.base, owner, name, hookName), hookBytes)
}

func (c *Client) GetHookDetails(owner string, name string) (*HookPluginDetails, error) {
urlString := fmt.Sprintf(pathHookDetails, c.base, owner, name, hookName)
response, err := c.client.Get(urlString)
if err != nil {
return nil, err
}
defer response.Body.Close()
contents, err := ioutil.ReadAll(response.Body)
hookDetails := HookPluginDetails{}
err = json.Unmarshal(contents, &hookDetails)
if err != nil {
return nil, err
}
return &hookDetails, nil
}

func (c *Client) GetHooks(owner string, name string) (*HookSettings, error) {
urlString := fmt.Sprintf(pathHookSettings, c.base, owner, name, hookName)
response, err := c.client.Get(urlString)
Expand Down
13 changes: 13 additions & 0 deletions remote/bitbucketserver/internal/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,19 @@ type RefChange struct {
Type string `json:"type"`
}

type HookPluginDetails struct {
Details struct {
Key string `json:"key"`
Name string `json:"name"`
Type string `json:"type"`
Description string `json:"description"`
Version string `json:"version"`
ConfigFormKey string `json:"configFormKey"`
} `json:"details"`
Enabled bool `json:"enabled"`
Configured bool `json:"configured"`
}

type HookSettings struct {
HookURL0 string `json:"hook-url-0,omitempty"`
HookURL1 string `json:"hook-url-1,omitempty"`
Expand Down

0 comments on commit 82189c2

Please sign in to comment.