Fix to check if the repo is enabled and don’t make a call to the settings it isn’t. The API works differently the first time vs if it’s ever been enabled.
This commit is contained in:
parent
f9770b04f2
commit
82189c27fe
2 changed files with 40 additions and 2 deletions
|
@ -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"
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
|
|
|
@ -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"`
|
||||
|
|
Loading…
Reference in a new issue