Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GITHUB_BASE_URL env var for job links #1771

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions backend/controllers/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ import (
"encoding/json"
"errors"
"fmt"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"path"
"reflect"
"strconv"
"strings"

"github.com/diggerhq/digger/backend/ci_backends"
"github.com/diggerhq/digger/backend/locking"
"github.com/diggerhq/digger/backend/segment"
Expand All @@ -17,15 +27,6 @@ import (
orchestrator_scheduler "github.com/diggerhq/digger/libs/scheduler"
"github.com/google/uuid"
"gorm.io/gorm"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"path"
"reflect"
"strconv"
"strings"

"github.com/diggerhq/digger/backend/middleware"
"github.com/diggerhq/digger/backend/models"
Expand Down Expand Up @@ -187,7 +188,7 @@ func GithubAppSetup(c *gin.Context) {
},
}

githubHostname := getGithubHostname()
githubHostname := utils.GetGithubHostname()
url := &url.URL{
Scheme: "https",
Host: githubHostname,
Expand All @@ -209,14 +210,6 @@ func GithubAppSetup(c *gin.Context) {
c.HTML(http.StatusOK, "github_setup.tmpl", gin.H{"Target": url.String(), "Manifest": string(jsonManifest)})
}

func getGithubHostname() string {
githubHostname := os.Getenv("DIGGER_GITHUB_HOSTNAME")
if githubHostname == "" {
githubHostname = "github.com"
}
return githubHostname
}

// GithubSetupExchangeCode handles the user coming back from creating their app
// A code query parameter is exchanged for this app's ID, key, and webhook_secret
// Implements https://developer.github.com/apps/building-github-apps/creating-github-apps-from-a-manifest/#implementing-the-github-app-manifest-flow
Expand Down Expand Up @@ -1248,7 +1241,7 @@ func validateGithubCallback(githubClientProvider utils.GithubClientProvider, cli
}
httpClient := http.Client{}

githubHostname := getGithubHostname()
githubHostname := utils.GetGithubHostname()
reqURL := fmt.Sprintf("https://%v/login/oauth/access_token?client_id=%s&client_secret=%s&code=%s", githubHostname, clientId, clientSecret, code)
req, err := http.NewRequest(http.MethodPost, reqURL, nil)
if err != nil {
Expand Down
21 changes: 15 additions & 6 deletions backend/utils/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import (
"context"
"encoding/base64"
"fmt"
"log"
net "net/http"
"os"
"strings"
"time"

"github.com/bradleyfalzon/ghinstallation/v2"
"github.com/diggerhq/digger/backend/models"
"github.com/diggerhq/digger/libs/ci"
Expand All @@ -13,11 +19,6 @@ import (
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/google/go-github/v61/github"
"log"
net "net/http"
"os"
"strings"
"time"
)

func createTempDir() string {
Expand Down Expand Up @@ -211,6 +212,14 @@ func SetPRStatusForJobs(prService ci.PullRequestService, prNumber int, jobs []sc
return nil
}

func GetGithubHostname() string {
githubHostname := os.Getenv("DIGGER_GITHUB_HOSTNAME")
if githubHostname == "" {
githubHostname = "github.com"
}
return githubHostname
}

func GetWorkflowIdAndUrlFromDiggerJobId(client *github.Client, repoOwner string, repoName string, diggerJobID string) (int64, string, error) {
timeFilter := time.Now().Add(-5 * time.Minute)
runs, _, err := client.Actions.ListRepositoryWorkflowRuns(context.Background(), repoOwner, repoName, &github.ListWorkflowRunsOptions{
Expand All @@ -230,7 +239,7 @@ func GetWorkflowIdAndUrlFromDiggerJobId(client *github.Client, repoOwner string,
for _, workflowjob := range workflowjobs.Jobs {
for _, step := range workflowjob.Steps {
if strings.Contains(*step.Name, diggerJobID) {
return *workflowRun.ID, fmt.Sprintf("https://github.com/%v/%v/actions/runs/%v", repoOwner, repoName, *workflowRun.ID), nil
return *workflowRun.ID, fmt.Sprintf("https://%v/%v/%v/actions/runs/%v", GetGithubHostname(), repoOwner, repoName, *workflowRun.ID), nil
}
}

Expand Down
Loading