mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-04-26 13:34:45 +00:00
Merge pull request #1949 from josmo/commit-hook-fix-stash
Fix to check if the repo is enabled and don’t make a call to the sett…
This commit is contained in:
commit
4c036b7838
2 changed files with 40 additions and 10 deletions
|
@ -23,6 +23,7 @@ const (
|
||||||
pathHook = "%s/rest/api/1.0/projects/%s/repos/%s/settings/hooks/%s"
|
pathHook = "%s/rest/api/1.0/projects/%s/repos/%s/settings/hooks/%s"
|
||||||
pathSource = "%s/projects/%s/repos/%s/browse/%s?at=%s&raw"
|
pathSource = "%s/projects/%s/repos/%s/browse/%s?at=%s&raw"
|
||||||
hookName = "com.atlassian.stash.plugin.stash-web-post-receive-hooks-plugin:postReceiveHook"
|
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"
|
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"
|
pathHookSettings = "%s/rest/api/1.0/projects/%s/repos/%s/settings/hooks/%s/settings"
|
||||||
pathStatus = "%s/rest/build-status/1.0/commits/%s"
|
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 {
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
hooks := hookSettingsToArray(hookSettings)
|
var hooks []string
|
||||||
|
if hookDetails.Enabled {
|
||||||
|
hookSettings, err := c.GetHooks(owner, name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
hooks = hookSettingsToArray(hookSettings)
|
||||||
|
|
||||||
|
}
|
||||||
if !stringInSlice(callBackLink, hooks) {
|
if !stringInSlice(callBackLink, hooks) {
|
||||||
hooks = append(hooks, callBackLink)
|
hooks = append(hooks, callBackLink)
|
||||||
}
|
}
|
||||||
|
|
||||||
putHookSettings := arrayToHookSettings(hooks)
|
putHookSettings := arrayToHookSettings(hooks)
|
||||||
hookBytes, err := json.Marshal(putHookSettings)
|
hookBytes, err := json.Marshal(putHookSettings)
|
||||||
return c.doPut(fmt.Sprintf(pathHookEnabled, c.base, owner, name, hookName), hookBytes)
|
return c.doPut(fmt.Sprintf(pathHookEnabled, c.base, owner, name, hookName), hookBytes)
|
||||||
|
@ -165,6 +174,18 @@ func (c *Client) DeleteHook(owner string, name string, link string) error {
|
||||||
return c.doPut(fmt.Sprintf(pathHookEnabled, c.base, owner, name, hookName), hookBytes)
|
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()
|
||||||
|
hookDetails := HookPluginDetails{}
|
||||||
|
err = json.NewDecoder(response.Body).Decode(&hookDetails)
|
||||||
|
return &hookDetails, err
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Client) GetHooks(owner string, name string) (*HookSettings, error) {
|
func (c *Client) GetHooks(owner string, name string) (*HookSettings, error) {
|
||||||
urlString := fmt.Sprintf(pathHookSettings, c.base, owner, name, hookName)
|
urlString := fmt.Sprintf(pathHookSettings, c.base, owner, name, hookName)
|
||||||
response, err := c.client.Get(urlString)
|
response, err := c.client.Get(urlString)
|
||||||
|
@ -172,13 +193,9 @@ func (c *Client) GetHooks(owner string, name string) (*HookSettings, error) {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer response.Body.Close()
|
defer response.Body.Close()
|
||||||
contents, err := ioutil.ReadAll(response.Body)
|
|
||||||
hookSettings := HookSettings{}
|
hookSettings := HookSettings{}
|
||||||
err = json.Unmarshal(contents, &hookSettings)
|
err = json.NewDecoder(response.Body).Decode(&hookSettings)
|
||||||
if err != nil {
|
return &hookSettings, err
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return &hookSettings, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO: make these as as general do with the action
|
//TODO: make these as as general do with the action
|
||||||
|
@ -252,7 +269,7 @@ func (c *Client) paginatedRepos(start int) ([]*Repo, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func filter(vs []string, f func(string) bool) []string {
|
func filter(vs []string, f func(string) bool) []string {
|
||||||
vsf := make([]string, 0)
|
var vsf []string
|
||||||
for _, v := range vs {
|
for _, v := range vs {
|
||||||
if f(v) {
|
if f(v) {
|
||||||
vsf = append(vsf, v)
|
vsf = append(vsf, v)
|
||||||
|
@ -316,7 +333,7 @@ func arrayToHookSettings(hooks []string) HookSettings {
|
||||||
}
|
}
|
||||||
|
|
||||||
func hookSettingsToArray(hookSettings *HookSettings) []string {
|
func hookSettingsToArray(hookSettings *HookSettings) []string {
|
||||||
hooks := make([]string, 0)
|
var hooks []string
|
||||||
|
|
||||||
if hookSettings.HookURL0 != "" {
|
if hookSettings.HookURL0 != "" {
|
||||||
hooks = append(hooks, hookSettings.HookURL0)
|
hooks = append(hooks, hookSettings.HookURL0)
|
||||||
|
|
|
@ -170,6 +170,19 @@ type RefChange struct {
|
||||||
Type string `json:"type"`
|
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 {
|
type HookSettings struct {
|
||||||
HookURL0 string `json:"hook-url-0,omitempty"`
|
HookURL0 string `json:"hook-url-0,omitempty"`
|
||||||
HookURL1 string `json:"hook-url-1,omitempty"`
|
HookURL1 string `json:"hook-url-1,omitempty"`
|
||||||
|
|
Loading…
Reference in a new issue