Skip to content

Commit

Permalink
Dispatch MM unit test workflows based on modular-magician downstream …
Browse files Browse the repository at this point in the history
  • Loading branch information
shuyama1 authored Aug 9, 2024
1 parent 4c35ce0 commit 2d8923a
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 17 deletions.
8 changes: 8 additions & 0 deletions .ci/magician/cmd/generate_downstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,14 @@ func createCommit(scratchRepo *source.Repo, commitMessage string, rnr exec.ExecR
commitSha = strings.TrimSpace(commitSha)
fmt.Printf("Commit sha on the branch is: `%s`\n", commitSha)

variablePath := fmt.Sprintf("/workspace/commitSHA_modular-magician_%s.txt", scratchRepo.Name)
fmt.Println("variablePath: ", variablePath)

err = rnr.WriteFile(variablePath, commitSha)
if err != nil {
fmt.Println("Error:", err)
}

return commitSha, err
}

Expand Down
36 changes: 32 additions & 4 deletions .ci/magician/cmd/test_tgc.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,39 @@ var testTGCCmd = &cobra.Command{
}

func execTestTGC(commit, pr string, gh ttGithub) error {
contentTPGB, err := os.ReadFile("/workspace/commitSHA_modular-magician_terraform-provider-google-beta.txt")
if err != nil {
fmt.Println("Error:", err)
}

contentTGC, err := os.ReadFile("/workspace/commitSHA_modular-magician_terraform-google-conversion.txt")
if err != nil {
fmt.Println("Error:", err)
}

commitShaOrBranchUpstreamTPGB := string(contentTPGB)
commitShaOrBranchUpstreamTGC := string(contentTGC)

if commitShaOrBranchUpstreamTPGB == "" {
// fall back to branch if commit SHA can't be found
commitShaOrBranchUpstreamTPGB = "auto-pr-" + pr
}

if commitShaOrBranchUpstreamTGC == "" {
// fall back to branch if commit SHA can't be found
commitShaOrBranchUpstreamTGC = "auto-pr-" + pr
}

fmt.Println("commitShaOrBranchUpstreamTPGB: ", commitShaOrBranchUpstreamTPGB)
fmt.Println("commitShaOrBranchUpstreamTGC: ", commitShaOrBranchUpstreamTGC)

if err := gh.CreateWorkflowDispatchEvent("test-tgc.yml", map[string]any{
"owner": "modular-magician",
"repo": "terraform-google-conversion",
"branch": "auto-pr-" + pr,
"sha": commit,
"owner": "modular-magician",
"repo": "terraform-google-conversion",
"tpgb-branch": commitShaOrBranchUpstreamTPGB,
"tgc-branch": commitShaOrBranchUpstreamTGC,
"pr-number": pr,
"sha": commit,
}); err != nil {
return fmt.Errorf("error creating workflow dispatch event: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion .ci/magician/cmd/test_tgc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestExecTestTGC(t *testing.T) {
execTestTGC("sha1", "pr1", gh)

method := "CreateWorkflowDispatchEvent"
expected := [][]any{{"test-tgc.yml", map[string]any{"branch": "auto-pr-pr1", "owner": "modular-magician", "repo": "terraform-google-conversion", "sha": "sha1"}}}
expected := [][]any{{"test-tgc.yml", map[string]any{"tpgb-branch": "auto-pr-pr1", "tgc-branch": "auto-pr-pr1", "pr-number":"pr1", "owner": "modular-magician", "repo": "terraform-google-conversion", "sha": "sha1"}}}
if calls, ok := gh.calledMethods[method]; !ok {
t.Fatal("Workflow dispatch event not created")
} else if !reflect.DeepEqual(calls, expected) {
Expand Down
29 changes: 24 additions & 5 deletions .ci/magician/cmd/test_tpg.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"fmt"
"magician/github"
"os"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -54,19 +53,39 @@ var testTPGCmd = &cobra.Command{

func execTestTPG(version, commit, pr string, gh ttGithub) error {
var repo string
var content []byte
var err error
if version == "ga" {
repo = "terraform-provider-google"
content, err = os.ReadFile("/workspace/commitSHA_modular-magician_terraform-provider-google.txt")
if err != nil {
fmt.Println("Error:", err)
}
} else if version == "beta" {
repo = "terraform-provider-google-beta"
content, err = os.ReadFile("/workspace/commitSHA_modular-magician_terraform-provider-google-beta.txt")
if err != nil {
fmt.Println("Error:", err)
}
} else {
return fmt.Errorf("invalid version specified")
}

commitShaOrBranchUpstream := string(content)

if commitShaOrBranchUpstream == ""{
// fall back to branch if commit SHA can't be found
commitShaOrBranchUpstream = "auto-pr-" + pr
}

fmt.Println("commitShaOrBranchUpstream: ", commitShaOrBranchUpstream)

if err := gh.CreateWorkflowDispatchEvent("test-tpg.yml", map[string]any{
"owner": "modular-magician",
"repo": repo,
"branch": "auto-pr-" + pr,
"sha": commit,
"owner": "modular-magician",
"repo": repo,
"branch": commitShaOrBranchUpstream,
"pr-number": pr,
"sha": commit,
}); err != nil {
return fmt.Errorf("error creating workflow dispatch event: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions .ci/magician/cmd/test_tpg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestExecTestTPG(t *testing.T) {
execTestTPG("beta", "sha1", "pr1", gh)

method := "CreateWorkflowDispatchEvent"
expected := [][]any{{"test-tpg.yml", map[string]any{"branch": "auto-pr-pr1", "owner": "modular-magician", "repo": "terraform-provider-google-beta", "sha": "sha1"}}}
expected := [][]any{{"test-tpg.yml", map[string]any{"branch": "auto-pr-pr1", "pr-number": "pr1", "owner": "modular-magician", "repo": "terraform-provider-google-beta", "sha": "sha1"}}}
if calls, ok := gh.calledMethods[method]; !ok {
t.Fatal("Workflow dispatch event not created")
} else if !reflect.DeepEqual(calls, expected) {
Expand All @@ -40,7 +40,7 @@ func TestExecTestTPG(t *testing.T) {
execTestTPG("ga", "sha1", "pr1", gh)

method = "CreateWorkflowDispatchEvent"
expected = [][]any{{"test-tpg.yml", map[string]any{"branch": "auto-pr-pr1", "owner": "modular-magician", "repo": "terraform-provider-google", "sha": "sha1"}}}
expected = [][]any{{"test-tpg.yml", map[string]any{"branch": "auto-pr-pr1", "pr-number": "pr1", "owner": "modular-magician", "repo": "terraform-provider-google", "sha": "sha1"}}}
if calls, ok := gh.calledMethods[method]; !ok {
t.Fatal("Workflow dispatch event not created")
} else if !reflect.DeepEqual(calls, expected) {
Expand Down
14 changes: 10 additions & 4 deletions .github/workflows/test-tgc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,21 @@ on:
description: 'The Base Repository to pull from'
required: false
default: 'terraform-google-conversion'
branch:
tpgb-branch:
description: 'The branch or sha of the tpgb execute against'
required: true
tgc-branch:
description: 'The branch or sha of the tgc execute against'
required: true
pr-number:
description: 'The pull request number in magic-modules repository'
required: true
sha:
description: "The commit SHA in magic-modules repository where the status result will be posted"
required: true

concurrency:
group: test-tgc-${{ github.event.inputs.owner }}-${{ github.event.inputs.repo }}-${{ github.event.inputs.branch }}
group: test-tgc-${{ github.event.inputs.owner }}-${{ github.event.inputs.repo }}-${{ github.event.inputs.pr-number }}
cancel-in-progress: true

jobs:
Expand Down Expand Up @@ -56,7 +62,7 @@ jobs:
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.2
with:
repository: ${{ github.event.inputs.owner }}/${{ github.event.inputs.repo }}
ref: ${{ github.event.inputs.branch }}
ref: ${{ github.event.inputs.tgc-branch }}
fetch-depth: 2
- name: Check for Code Changes
id: pull_request
Expand All @@ -75,7 +81,7 @@ jobs:
- name: Build Terraform Google Conversion
if: ${{ !failure() && steps.pull_request.outputs.has_changes == 'true' }}
run: |
go mod edit -replace github.com/hashicorp/terraform-provider-google-beta=github.com/${{ github.event.inputs.owner }}/terraform-provider-google-beta@${{ github.event.inputs.branch }}
go mod edit -replace github.com/hashicorp/terraform-provider-google-beta=github.com/${{ github.event.inputs.owner }}/terraform-provider-google-beta@${{ github.event.inputs.tpgb-branch }}
go mod tidy
make build
- name: Run Unit Tests
Expand Down
5 changes: 4 additions & 1 deletion .github/workflows/test-tpg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ on:
branch:
description: 'The branch or sha of the provider execute against'
required: true
pr-number:
description: 'The pull request number in magic-modules repository'
required: true
sha:
description: "The commit SHA in magic-modules repository where the status result will be posted"
required: true

concurrency:
group: test-tpg-${{ github.event.inputs.owner }}-${{ github.event.inputs.repo }}-${{ github.event.inputs.branch }}
group: test-tpg-${{ github.event.inputs.owner }}-${{ github.event.inputs.repo }}-${{ github.event.inputs.pr-number }}
cancel-in-progress: true

jobs:
Expand Down

0 comments on commit 2d8923a

Please sign in to comment.