Skip to content

Commit

Permalink
fix running incoming webhook on GHE
Browse files Browse the repository at this point in the history
We were not passing the installation URL properly to the endpoint when
detecting the X-GitHub-Enterprise-Host header.

Clarify the documentation about this and add a test for it.

Signed-off-by: Chmouel Boudjnah <[email protected]>
  • Loading branch information
chmouel authored and savitaashture committed Dec 1, 2023
1 parent a105d55 commit c0e0e5d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
10 changes: 10 additions & 0 deletions docs/content/docs/guide/incoming_webhook.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,16 @@ curl -H "Content-Type: application/json" -X POST "https://control.pac.url/incomi

The parameter value of `pull_request_number` will be set to `12345` when using the variable `{{pull_request_number}}` in your PipelineRun.

### Using incoming webhook with GitHub Enterprise application

When using a GitHub application over to a GitHub Enterprise, you will need to
specify the `X-GitHub-Enterprise-Host` header when making the incoming webhook
request. For example when using curl:

```shell
curl -H "X-GitHub-Enterprise-Host: github.example.com" -X POST "https://control.pac.url/incoming?repository=repo&branch=main&secret=very-secure-shared-secret&pipelinerun=target_pipelinerun"
```

### Using incoming webhook with webhook based providers

Webhook based providers (i.e: GitHub Webhook, GitLab, Bitbucket etc..) supports
Expand Down
17 changes: 9 additions & 8 deletions pkg/provider/github/app/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,19 @@ import (

func GetAndUpdateInstallationID(ctx context.Context, req *http.Request, run *params.Run, repo *v1alpha1.Repository, gh *github.Provider, ns string) (string, string, int64, error) {
var (
enterpriseURL, token string
installationID int64
enterpriseHost, token string
installationID int64
)
jwtToken, err := GenerateJWT(ctx, ns, run)
if err != nil {
return "", "", 0, err
}

installationURL := *gh.APIURL + keys.InstallationURL
enterpriseURL = req.Header.Get("X-GitHub-Enterprise-Host")
if enterpriseURL != "" {
installationURL = enterpriseURL + keys.InstallationURL
enterpriseHost = req.Header.Get("X-GitHub-Enterprise-Host")
if enterpriseHost != "" {
// NOTE: Hopefully this works even when the ghe URL is on another host than the api URL
installationURL = "https://" + enterpriseHost + "/api/v3" + keys.InstallationURL
}

res, err := GetReponse(ctx, http.MethodGet, installationURL, jwtToken, run)
Expand All @@ -39,7 +40,7 @@ func GetAndUpdateInstallationID(ctx context.Context, req *http.Request, run *par
}

if res.StatusCode >= 300 {
return "", "", 0, fmt.Errorf("Non-OK HTTP status: %d", res.StatusCode)
return "", "", 0, fmt.Errorf("Non-OK HTTP status while getting installation URL: %s : %d", installationURL, res.StatusCode)
}

defer res.Body.Close()
Expand All @@ -61,7 +62,7 @@ func GetAndUpdateInstallationID(ctx context.Context, req *http.Request, run *par
return "", "", 0, fmt.Errorf("installation ID is nil")
}
if *installationData[i].ID != 0 {
token, err = gh.GetAppToken(ctx, run.Clients.Kube, enterpriseURL, *installationData[i].ID, ns)
token, err = gh.GetAppToken(ctx, run.Clients.Kube, enterpriseHost, *installationData[i].ID, ns)
if err != nil {
return "", "", 0, err
}
Expand All @@ -75,7 +76,7 @@ func GetAndUpdateInstallationID(ctx context.Context, req *http.Request, run *par
break
}
}
return enterpriseURL, token, installationID, nil
return enterpriseHost, token, installationID, nil
}

func listRepos(ctx context.Context, repo *v1alpha1.Repository, gh *github.Provider) (bool, error) {
Expand Down

0 comments on commit c0e0e5d

Please sign in to comment.