|
| 1 | +package cmd |
| 2 | + |
| 3 | +/* |
| 4 | + what does this command do? |
| 5 | +
|
| 6 | + if release type is "minor" then : |
| 7 | + creates a new release branch in upstream eks-a repo based off "main" & build tooling repo |
| 8 | +
|
| 9 | + creates a new release branch in forked repo based off newly created release branch in upstream repo |
| 10 | +
|
| 11 | + else : |
| 12 | + creates a new patch branch in users forked repo based off latest release branch upstream |
| 13 | +
|
| 14 | +*/ |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "fmt" |
| 19 | + "os" |
| 20 | + |
| 21 | + "github.com/google/go-github/v62/github" |
| 22 | + "github.com/spf13/cobra" |
| 23 | +) |
| 24 | + |
| 25 | +var ( |
| 26 | + buildToolingRepoName = "eks-anywhere-build-tooling" |
| 27 | + upStreamRepoOwner = "testerIbix" // will eventually be replaced by actual upstream owner, aws |
| 28 | +) |
| 29 | + |
| 30 | +// createBranchCmd represents the createBranch command |
| 31 | +var createBranchCmd = &cobra.Command{ |
| 32 | + Use: "create-branch", |
| 33 | + Short: "Creates new release branch from updated trigger file", |
| 34 | + Long: `A longer description that spans multiple lines and likely contains examples |
| 35 | +and usage of using your command.`, |
| 36 | + |
| 37 | + Run: func(cmd *cobra.Command, args []string) { |
| 38 | + |
| 39 | + err := releaseDecision() |
| 40 | + if err != nil { |
| 41 | + fmt.Printf("error creating branch %s", err) |
| 42 | + } |
| 43 | + }, |
| 44 | +} |
| 45 | + |
| 46 | +func releaseDecision() error { |
| 47 | + RELEASE_TYPE := os.Getenv("RELEASE_TYPE") |
| 48 | + |
| 49 | + if RELEASE_TYPE == "minor" { |
| 50 | + err := createMinorBranches() |
| 51 | + if err != nil { |
| 52 | + fmt.Printf("error calling createMinorBranches %s", err) |
| 53 | + } |
| 54 | + return nil |
| 55 | + } |
| 56 | + // else |
| 57 | + err := createPatchBranch() |
| 58 | + if err != nil { |
| 59 | + fmt.Printf("error calling createPatchBranch %s", err) |
| 60 | + } |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +func createMinorBranches() error { |
| 65 | + |
| 66 | + latestRelease := os.Getenv("LATEST_RELEASE") |
| 67 | + |
| 68 | + //create client |
| 69 | + accessToken := os.Getenv("SECRET_PAT") |
| 70 | + ctx := context.Background() |
| 71 | + client := github.NewClient(nil).WithAuthToken(accessToken) |
| 72 | + |
| 73 | + // create branch in upstream repo based off main branch |
| 74 | + ref := "refs/heads/" + latestRelease |
| 75 | + baseRef := "main" |
| 76 | + |
| 77 | + // Get the reference for the base branch |
| 78 | + baseRefObj, _, err := client.Git.GetRef(ctx, upStreamRepoOwner, EKSAnyrepoName, "heads/"+baseRef) |
| 79 | + if err != nil { |
| 80 | + return fmt.Errorf("error getting base branch reference one: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + // Create a new branch |
| 84 | + newBranchRef, _, err := client.Git.CreateRef(ctx, upStreamRepoOwner, EKSAnyrepoName, &github.Reference{ |
| 85 | + Ref: &ref, |
| 86 | + Object: &github.GitObject{ |
| 87 | + SHA: baseRefObj.Object.SHA, |
| 88 | + }, |
| 89 | + }) |
| 90 | + if err != nil { |
| 91 | + return fmt.Errorf("error creating branch one: %v", err) |
| 92 | + } |
| 93 | + |
| 94 | + // branch created upstream |
| 95 | + fmt.Printf("New release branch '%s' created upstream successfully\n", *newBranchRef.Ref) |
| 96 | + |
| 97 | + // create branch in forked repo based off upstream |
| 98 | + ref = "refs/heads/" + latestRelease |
| 99 | + baseRef = latestRelease |
| 100 | + |
| 101 | + // Get the reference for the base branch from the upstream repository |
| 102 | + baseRefObj, _, err = client.Git.GetRef(ctx, upStreamRepoOwner, EKSAnyrepoName, "heads/"+baseRef) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("error getting base branch reference two: %v", err) |
| 105 | + } |
| 106 | + |
| 107 | + // Create a new branch |
| 108 | + newBranchRef, _, err = client.Git.CreateRef(ctx, usersForkedRepoAccount, EKSAnyrepoName, &github.Reference{ |
| 109 | + Ref: &ref, |
| 110 | + Object: &github.GitObject{ |
| 111 | + SHA: baseRefObj.Object.SHA, |
| 112 | + }, |
| 113 | + }) |
| 114 | + if err != nil { |
| 115 | + return fmt.Errorf("error creating branch two: %v", err) |
| 116 | + } |
| 117 | + |
| 118 | + // branch created upstream |
| 119 | + fmt.Printf("New user fork branch '%s' created successfully\n", *newBranchRef.Ref) |
| 120 | + |
| 121 | + // create branch in upstream build tooling repo based off main branch |
| 122 | + ref = "refs/heads/" + latestRelease |
| 123 | + baseRef = "main" |
| 124 | + |
| 125 | + // Get the reference for the base branch |
| 126 | + baseRefObj, _, err = client.Git.GetRef(ctx, upStreamRepoOwner, buildToolingRepoName, "heads/"+baseRef) |
| 127 | + if err != nil { |
| 128 | + return fmt.Errorf("error getting base branch reference three: %v", err) |
| 129 | + } |
| 130 | + |
| 131 | + // Create a new branch |
| 132 | + newBranchRef, _, err = client.Git.CreateRef(ctx, upStreamRepoOwner, buildToolingRepoName, &github.Reference{ |
| 133 | + Ref: &ref, |
| 134 | + Object: &github.GitObject{ |
| 135 | + SHA: baseRefObj.Object.SHA, |
| 136 | + }, |
| 137 | + }) |
| 138 | + if err != nil { |
| 139 | + return fmt.Errorf("error creating branch three: %v", err) |
| 140 | + } |
| 141 | + |
| 142 | + // branch created upstream |
| 143 | + fmt.Printf("New build tooling branch '%s' created successfully\n", *newBranchRef.Ref) |
| 144 | + |
| 145 | + return nil |
| 146 | +} |
| 147 | + |
| 148 | +func createPatchBranch() error { |
| 149 | + |
| 150 | + latestRelease := os.Getenv("LATEST_RELEASE") |
| 151 | + |
| 152 | + //create client |
| 153 | + accessToken := os.Getenv("SECRET_PAT") |
| 154 | + ctx := context.Background() |
| 155 | + client := github.NewClient(nil).WithAuthToken(accessToken) |
| 156 | + |
| 157 | + // create branch in forked repo based off upstream |
| 158 | + ref := "refs/heads/" + latestRelease + "-releaser-patch" |
| 159 | + baseRef := latestRelease |
| 160 | + |
| 161 | + // Get the reference for the base branch from upstream |
| 162 | + baseRefObj, _, err := client.Git.GetRef(ctx, upStreamRepoOwner, EKSAnyrepoName, "heads/"+baseRef) |
| 163 | + if err != nil { |
| 164 | + return fmt.Errorf("error getting base branch reference: %v", err) |
| 165 | + } |
| 166 | + |
| 167 | + // Create a new branch in fork |
| 168 | + newBranchRef, _, err := client.Git.CreateRef(ctx, usersForkedRepoAccount, EKSAnyrepoName, &github.Reference{ |
| 169 | + Ref: &ref, |
| 170 | + Object: &github.GitObject{ |
| 171 | + SHA: baseRefObj.Object.SHA, |
| 172 | + }, |
| 173 | + }) |
| 174 | + if err != nil { |
| 175 | + return fmt.Errorf("error creating branch: %v", err) |
| 176 | + } |
| 177 | + |
| 178 | + // branch created upstream |
| 179 | + fmt.Printf("New branch '%s' created successfully\n", *newBranchRef.Ref) |
| 180 | + |
| 181 | + return nil |
| 182 | +} |
0 commit comments