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

Extract PR number from event instead of using RefName #145

Merged
merged 1 commit into from
Sep 24, 2024
Merged
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
6 changes: 5 additions & 1 deletion delete/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ func main() {
appName := in.appName
if appName == "" {
repoOwner, repo := ghCtx.Repo()
appName = utils.GenerateAppName(repoOwner, repo, ghCtx.RefName)
prRef, err := utils.PRRefFromContext(ghCtx)
if err != nil {
a.Fatalf("failed to get PR number: %v", err)
}
appName = utils.GenerateAppName(repoOwner, repo, prRef)
}

app, err := utils.FindAppByName(ctx, do.Apps, appName)
Expand Down
24 changes: 23 additions & 1 deletion utils/preview.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package utils
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"strings"

Expand All @@ -18,9 +19,13 @@ import (
// - Setting the reference of all relevant components to point to the PRs ref.
func SanitizeSpecForPullRequestPreview(spec *godo.AppSpec, ghCtx *gha.GitHubContext) error {
repoOwner, repo := ghCtx.Repo()
prRef, err := PRRefFromContext(ghCtx)
if err != nil {
return fmt.Errorf("failed to get PR number: %w", err)
}

// Override app name to something that identifies this PR.
spec.Name = GenerateAppName(repoOwner, repo, ghCtx.RefName)
spec.Name = GenerateAppName(repoOwner, repo, prRef)

// Unset any domains as those might collide with production apps.
spec.Domains = nil
Expand Down Expand Up @@ -69,3 +74,20 @@ func GenerateAppName(repoOwner, repo, ref string) string {

return baseName[:limit] + suffix
}

// PRRefFromContext extracts the PR number from the given GitHub context.
// It mimics the RefName attribute that GitHub Actions provides but is also available
// on merge events, which isn't the case for the RefName attribute.
// See: https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows#pull_request.
func PRRefFromContext(ghCtx *gha.GitHubContext) (string, error) {
prFields, ok := ghCtx.Event["pull_request"].(map[string]any)
if !ok {
return "", fmt.Errorf("pull_request field didn't exist on event: %v", ghCtx.Event)
}
// The event is parsed as a JSON object and Golang represents numbers as float64.
prNumber, ok := prFields["number"].(float64)
if !ok {
return "", errors.New("missing pull request number")
}
return fmt.Sprintf("%d/merge", int(prNumber)), nil
}
6 changes: 5 additions & 1 deletion utils/preview_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,12 @@ func TestSanitizeSpecForPullRequestPreview(t *testing.T) {

ghCtx := &gha.GitHubContext{
Repository: "foo/bar",
RefName: "3/merge",
HeadRef: "feature-branch",
Event: map[string]any{
"pull_request": map[string]any{
"number": float64(3),
},
},
}

err := SanitizeSpecForPullRequestPreview(spec, ghCtx)
Expand Down
Loading