Skip to content

Commit

Permalink
Use DIGGER_GITHUB_HOSTNAME env var for job links (#1771)
Browse files Browse the repository at this point in the history
* Use GITHUB_BASE_URL env var for job links
---------

Co-authored-by: motatoes <[email protected]>
  • Loading branch information
ZIJ and motatoes authored Oct 24, 2024
1 parent 9cd46a2 commit 0b17bfe
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Dockerfile_backend
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ RUN go build -ldflags="-X 'main.Version=${COMMIT_SHA}'" -o backend_exe ./backend

# Multi-stage build will just copy the binary to an alpine image.
FROM ubuntu:24.04 as runner
ENV ATLAS_VERSION v0.16.0
ENV ATLAS_VERSION v0.28.0
ARG COMMIT_SHA
WORKDIR /app

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile_backend_ee
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ RUN go build -ldflags="-X 'main.Version=${COMMIT_SHA}'" -o backend_exe ./ee/back

# Multi-stage build will just copy the binary to an alpine image.
FROM ubuntu:24.04 as runner
ENV ATLAS_VERSION v0.16.0
ENV ATLAS_VERSION v0.28.0
ARG COMMIT_SHA
WORKDIR /app

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile_drift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ RUN go build -ldflags="-X 'main.Version=${COMMIT_SHA}'" -o drift_exe ./ee/drift/

# Multi-stage build will just copy the binary to an alpine image.
FROM ubuntu:24.04 as runner
ENV ATLAS_VERSION v0.16.0
ENV ATLAS_VERSION v0.28.0
ARG COMMIT_SHA
WORKDIR /app

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile_next
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ RUN go build -ldflags="-X 'main.Version=${COMMIT_SHA}'" -o next_exe ./next/

# Multi-stage build will just copy the binary to an alpine image.
FROM ubuntu:24.04 as runner
ENV ATLAS_VERSION v0.16.0
ENV ATLAS_VERSION v0.28.0
ARG COMMIT_SHA
WORKDIR /app

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile_tasks
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ RUN go build -ldflags="-X 'main.Version=${COMMIT_SHA}'" -o tasks_exe ./backend/t

# Multi-stage build will just copy the binary to an alpine image.
FROM ubuntu:24.04 as runner
ENV ATLAS_VERSION v0.16.0
ENV ATLAS_VERSION v0.28.0
ARG COMMIT_SHA
WORKDIR /app

Expand Down
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

0 comments on commit 0b17bfe

Please sign in to comment.