Skip to content

Commit 2d8923a

Browse files
authored
Dispatch MM unit test workflows based on modular-magician downstream SHAs (#10671)
1 parent 4c35ce0 commit 2d8923a

File tree

7 files changed

+81
-17
lines changed

7 files changed

+81
-17
lines changed

.ci/magician/cmd/generate_downstream.go

+8
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,14 @@ func createCommit(scratchRepo *source.Repo, commitMessage string, rnr exec.ExecR
335335
commitSha = strings.TrimSpace(commitSha)
336336
fmt.Printf("Commit sha on the branch is: `%s`\n", commitSha)
337337

338+
variablePath := fmt.Sprintf("/workspace/commitSHA_modular-magician_%s.txt", scratchRepo.Name)
339+
fmt.Println("variablePath: ", variablePath)
340+
341+
err = rnr.WriteFile(variablePath, commitSha)
342+
if err != nil {
343+
fmt.Println("Error:", err)
344+
}
345+
338346
return commitSha, err
339347
}
340348

.ci/magician/cmd/test_tgc.go

+32-4
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,39 @@ var testTGCCmd = &cobra.Command{
4747
}
4848

4949
func execTestTGC(commit, pr string, gh ttGithub) error {
50+
contentTPGB, err := os.ReadFile("/workspace/commitSHA_modular-magician_terraform-provider-google-beta.txt")
51+
if err != nil {
52+
fmt.Println("Error:", err)
53+
}
54+
55+
contentTGC, err := os.ReadFile("/workspace/commitSHA_modular-magician_terraform-google-conversion.txt")
56+
if err != nil {
57+
fmt.Println("Error:", err)
58+
}
59+
60+
commitShaOrBranchUpstreamTPGB := string(contentTPGB)
61+
commitShaOrBranchUpstreamTGC := string(contentTGC)
62+
63+
if commitShaOrBranchUpstreamTPGB == "" {
64+
// fall back to branch if commit SHA can't be found
65+
commitShaOrBranchUpstreamTPGB = "auto-pr-" + pr
66+
}
67+
68+
if commitShaOrBranchUpstreamTGC == "" {
69+
// fall back to branch if commit SHA can't be found
70+
commitShaOrBranchUpstreamTGC = "auto-pr-" + pr
71+
}
72+
73+
fmt.Println("commitShaOrBranchUpstreamTPGB: ", commitShaOrBranchUpstreamTPGB)
74+
fmt.Println("commitShaOrBranchUpstreamTGC: ", commitShaOrBranchUpstreamTGC)
75+
5076
if err := gh.CreateWorkflowDispatchEvent("test-tgc.yml", map[string]any{
51-
"owner": "modular-magician",
52-
"repo": "terraform-google-conversion",
53-
"branch": "auto-pr-" + pr,
54-
"sha": commit,
77+
"owner": "modular-magician",
78+
"repo": "terraform-google-conversion",
79+
"tpgb-branch": commitShaOrBranchUpstreamTPGB,
80+
"tgc-branch": commitShaOrBranchUpstreamTGC,
81+
"pr-number": pr,
82+
"sha": commit,
5583
}); err != nil {
5684
return fmt.Errorf("error creating workflow dispatch event: %w", err)
5785
}

.ci/magician/cmd/test_tgc_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestExecTestTGC(t *testing.T) {
2828
execTestTGC("sha1", "pr1", gh)
2929

3030
method := "CreateWorkflowDispatchEvent"
31-
expected := [][]any{{"test-tgc.yml", map[string]any{"branch": "auto-pr-pr1", "owner": "modular-magician", "repo": "terraform-google-conversion", "sha": "sha1"}}}
31+
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"}}}
3232
if calls, ok := gh.calledMethods[method]; !ok {
3333
t.Fatal("Workflow dispatch event not created")
3434
} else if !reflect.DeepEqual(calls, expected) {

.ci/magician/cmd/test_tpg.go

+24-5
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"fmt"
2020
"magician/github"
2121
"os"
22-
2322
"github.com/spf13/cobra"
2423
)
2524

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

5554
func execTestTPG(version, commit, pr string, gh ttGithub) error {
5655
var repo string
56+
var content []byte
57+
var err error
5758
if version == "ga" {
5859
repo = "terraform-provider-google"
60+
content, err = os.ReadFile("/workspace/commitSHA_modular-magician_terraform-provider-google.txt")
61+
if err != nil {
62+
fmt.Println("Error:", err)
63+
}
5964
} else if version == "beta" {
6065
repo = "terraform-provider-google-beta"
66+
content, err = os.ReadFile("/workspace/commitSHA_modular-magician_terraform-provider-google-beta.txt")
67+
if err != nil {
68+
fmt.Println("Error:", err)
69+
}
6170
} else {
6271
return fmt.Errorf("invalid version specified")
6372
}
6473

74+
commitShaOrBranchUpstream := string(content)
75+
76+
if commitShaOrBranchUpstream == ""{
77+
// fall back to branch if commit SHA can't be found
78+
commitShaOrBranchUpstream = "auto-pr-" + pr
79+
}
80+
81+
fmt.Println("commitShaOrBranchUpstream: ", commitShaOrBranchUpstream)
82+
6583
if err := gh.CreateWorkflowDispatchEvent("test-tpg.yml", map[string]any{
66-
"owner": "modular-magician",
67-
"repo": repo,
68-
"branch": "auto-pr-" + pr,
69-
"sha": commit,
84+
"owner": "modular-magician",
85+
"repo": repo,
86+
"branch": commitShaOrBranchUpstream,
87+
"pr-number": pr,
88+
"sha": commit,
7089
}); err != nil {
7190
return fmt.Errorf("error creating workflow dispatch event: %w", err)
7291
}

.ci/magician/cmd/test_tpg_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func TestExecTestTPG(t *testing.T) {
2828
execTestTPG("beta", "sha1", "pr1", gh)
2929

3030
method := "CreateWorkflowDispatchEvent"
31-
expected := [][]any{{"test-tpg.yml", map[string]any{"branch": "auto-pr-pr1", "owner": "modular-magician", "repo": "terraform-provider-google-beta", "sha": "sha1"}}}
31+
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"}}}
3232
if calls, ok := gh.calledMethods[method]; !ok {
3333
t.Fatal("Workflow dispatch event not created")
3434
} else if !reflect.DeepEqual(calls, expected) {
@@ -40,7 +40,7 @@ func TestExecTestTPG(t *testing.T) {
4040
execTestTPG("ga", "sha1", "pr1", gh)
4141

4242
method = "CreateWorkflowDispatchEvent"
43-
expected = [][]any{{"test-tpg.yml", map[string]any{"branch": "auto-pr-pr1", "owner": "modular-magician", "repo": "terraform-provider-google", "sha": "sha1"}}}
43+
expected = [][]any{{"test-tpg.yml", map[string]any{"branch": "auto-pr-pr1", "pr-number": "pr1", "owner": "modular-magician", "repo": "terraform-provider-google", "sha": "sha1"}}}
4444
if calls, ok := gh.calledMethods[method]; !ok {
4545
t.Fatal("Workflow dispatch event not created")
4646
} else if !reflect.DeepEqual(calls, expected) {

.github/workflows/test-tgc.yml

+10-4
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,21 @@ on:
1616
description: 'The Base Repository to pull from'
1717
required: false
1818
default: 'terraform-google-conversion'
19-
branch:
19+
tpgb-branch:
20+
description: 'The branch or sha of the tpgb execute against'
21+
required: true
22+
tgc-branch:
2023
description: 'The branch or sha of the tgc execute against'
2124
required: true
25+
pr-number:
26+
description: 'The pull request number in magic-modules repository'
27+
required: true
2228
sha:
2329
description: "The commit SHA in magic-modules repository where the status result will be posted"
2430
required: true
2531

2632
concurrency:
27-
group: test-tgc-${{ github.event.inputs.owner }}-${{ github.event.inputs.repo }}-${{ github.event.inputs.branch }}
33+
group: test-tgc-${{ github.event.inputs.owner }}-${{ github.event.inputs.repo }}-${{ github.event.inputs.pr-number }}
2834
cancel-in-progress: true
2935

3036
jobs:
@@ -56,7 +62,7 @@ jobs:
5662
uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # v4.1.2
5763
with:
5864
repository: ${{ github.event.inputs.owner }}/${{ github.event.inputs.repo }}
59-
ref: ${{ github.event.inputs.branch }}
65+
ref: ${{ github.event.inputs.tgc-branch }}
6066
fetch-depth: 2
6167
- name: Check for Code Changes
6268
id: pull_request
@@ -75,7 +81,7 @@ jobs:
7581
- name: Build Terraform Google Conversion
7682
if: ${{ !failure() && steps.pull_request.outputs.has_changes == 'true' }}
7783
run: |
78-
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 }}
84+
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 }}
7985
go mod tidy
8086
make build
8187
- name: Run Unit Tests

.github/workflows/test-tpg.yml

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@ on:
1919
branch:
2020
description: 'The branch or sha of the provider execute against'
2121
required: true
22+
pr-number:
23+
description: 'The pull request number in magic-modules repository'
24+
required: true
2225
sha:
2326
description: "The commit SHA in magic-modules repository where the status result will be posted"
2427
required: true
2528

2629
concurrency:
27-
group: test-tpg-${{ github.event.inputs.owner }}-${{ github.event.inputs.repo }}-${{ github.event.inputs.branch }}
30+
group: test-tpg-${{ github.event.inputs.owner }}-${{ github.event.inputs.repo }}-${{ github.event.inputs.pr-number }}
2831
cancel-in-progress: true
2932

3033
jobs:

0 commit comments

Comments
 (0)