| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441 |
- package actions
- import (
- "context"
- "fmt"
- "net/http"
- "strings"
- "github.com/google/go-github/v41/github"
- "gopkg.in/yaml.v2"
- )
- type EnvOpts struct {
- Client *github.Client
- ServerURL string
- PorterToken string
- GitRepoOwner, GitRepoName string
- EnvironmentName string
- ProjectID, ClusterID, GitInstallationID uint
- }
- func SetupEnv(opts *EnvOpts) error {
- // make a best-effort to create a Github environment. this is a non-fatal operation,
- // as the environments API is not enabled for private repositories that don't have
- // github enterprise.
- _, resp, err := opts.Client.Repositories.GetEnvironment(
- context.Background(),
- opts.GitRepoOwner,
- opts.GitRepoName,
- opts.EnvironmentName,
- )
- if resp != nil && resp.StatusCode == http.StatusNotFound {
- opts.Client.Repositories.CreateUpdateEnvironment(
- context.Background(),
- opts.GitRepoOwner,
- opts.GitRepoName,
- opts.EnvironmentName,
- nil,
- )
- }
- // create porter token secret
- err = createGithubSecret(
- opts.Client,
- getPorterTokenSecretName(opts.ProjectID),
- opts.PorterToken,
- opts.GitRepoOwner,
- opts.GitRepoName,
- )
- if err != nil {
- return err
- }
- // get the repository to find the default branch
- repo, _, err := opts.Client.Repositories.Get(
- context.TODO(),
- opts.GitRepoOwner,
- opts.GitRepoName,
- )
- if err != nil {
- return err
- }
- defaultBranch := repo.GetDefaultBranch()
- applyWorkflowYAML, err := getPreviewApplyActionYAML(opts)
- if err != nil {
- return err
- }
- deleteWorkflowYAML, err := getPreviewDeleteActionYAML(opts)
- if err != nil {
- return err
- }
- githubBranch, _, err := opts.Client.Repositories.GetBranch(
- context.Background(), opts.GitRepoOwner, opts.GitRepoName, defaultBranch, true,
- )
- if err != nil {
- return err
- }
- if githubBranch.GetProtected() {
- err = createNewBranch(opts.Client, opts.GitRepoOwner, opts.GitRepoName, defaultBranch, "porter-preview")
- if err != nil {
- return fmt.Errorf(
- "Unable to create PR to merge workflow files into protected branch: %s.\n"+
- "To enable Porter Preview Environment deployments, please create Github workflow "+
- "files in this branch with the following contents:\n"+
- "--------\n%s--------\n--------\n%s--------\nERROR: %w",
- defaultBranch, string(applyWorkflowYAML), string(deleteWorkflowYAML), ErrCreatePRForProtectedBranch,
- )
- }
- _, err = commitWorkflowFile(
- opts.Client,
- fmt.Sprintf("porter_%s_env.yml", strings.ToLower(opts.EnvironmentName)),
- applyWorkflowYAML, opts.GitRepoOwner,
- opts.GitRepoName, "porter-preview", false,
- )
- if err != nil {
- return fmt.Errorf(
- "Unable to create PR to merge workflow files into protected branch: %s.\n"+
- "To enable Porter Preview Environment deployments, please create Github workflow "+
- "files in this branch with the following contents:\n"+
- "--------\n%s--------\n--------\n%s--------\nERROR: %w",
- defaultBranch, string(applyWorkflowYAML), string(deleteWorkflowYAML), ErrCreatePRForProtectedBranch,
- )
- }
- _, err = commitWorkflowFile(
- opts.Client,
- fmt.Sprintf("porter_%s_delete_env.yml", strings.ToLower(opts.EnvironmentName)),
- deleteWorkflowYAML, opts.GitRepoOwner,
- opts.GitRepoName, "porter-preview", false,
- )
- if err != nil {
- return fmt.Errorf(
- "Unable to create PR to merge workflow files into protected branch: %s.\n"+
- "To enable Porter Preview Environment deployments, please create a Github workflow "+
- "file in this branch with the following contents:\n"+
- "--------\n%s--------\nERROR: %w",
- defaultBranch, string(deleteWorkflowYAML), ErrCreatePRForProtectedBranch,
- )
- }
- pr, _, err := opts.Client.PullRequests.Create(
- context.Background(), opts.GitRepoOwner, opts.GitRepoName, &github.NewPullRequest{
- Title: github.String("Enable Porter Preview Environment deployments"),
- Base: github.String(defaultBranch),
- Head: github.String("porter-preview"),
- },
- )
- if err != nil {
- return err
- }
- return fmt.Errorf("Please merge %s to enable Porter Preview Environment deployments.\nERROR: %w",
- pr.GetHTMLURL(), ErrProtectedBranch)
- }
- _, err = commitWorkflowFile(
- opts.Client,
- fmt.Sprintf("porter_%s_env.yml", strings.ToLower(opts.EnvironmentName)),
- applyWorkflowYAML,
- opts.GitRepoOwner,
- opts.GitRepoName,
- defaultBranch,
- false,
- )
- if err != nil {
- if strings.Contains(err.Error(), "409 Could not create file") {
- // possibly a write-protected branch
- err = createNewBranch(opts.Client, opts.GitRepoOwner, opts.GitRepoName, defaultBranch, "porter-preview")
- if err != nil {
- return fmt.Errorf("write-protected branch %s. Error creating porter-preview branch: %w", defaultBranch, err)
- }
- _, err = commitWorkflowFile(
- opts.Client,
- fmt.Sprintf("porter_%s_env.yml", strings.ToLower(opts.EnvironmentName)),
- applyWorkflowYAML,
- opts.GitRepoOwner,
- opts.GitRepoName,
- "porter-preview",
- false,
- )
- if err != nil {
- return fmt.Errorf("write-protected branch %s. Error committing to porter-preview branch: %w", defaultBranch, err)
- }
- _, err = commitWorkflowFile(
- opts.Client,
- fmt.Sprintf("porter_%s_delete_env.yml", strings.ToLower(opts.EnvironmentName)),
- deleteWorkflowYAML,
- opts.GitRepoOwner,
- opts.GitRepoName,
- "porter-preview",
- false,
- )
- if err != nil {
- return fmt.Errorf("write-protected branch %s. Error committing to porter-preview branch: %w", defaultBranch, err)
- }
- pr, _, err := opts.Client.PullRequests.Create(
- context.Background(), opts.GitRepoOwner, opts.GitRepoName, &github.NewPullRequest{
- Title: github.String("Merge Porter preview environment Github Actions workflow files"),
- Base: github.String(defaultBranch),
- Head: github.String("porter-preview"),
- },
- )
- if err != nil {
- return err
- }
- return fmt.Errorf("write-protected branch %s. Please merge %s to enable preview environment for your repository", defaultBranch, pr.GetURL())
- }
- return err
- }
- _, err = commitWorkflowFile(
- opts.Client,
- fmt.Sprintf("porter_%s_delete_env.yml", strings.ToLower(opts.EnvironmentName)),
- deleteWorkflowYAML,
- opts.GitRepoOwner,
- opts.GitRepoName,
- defaultBranch,
- false,
- )
- if err != nil {
- return err
- }
- return err
- }
- func DeleteEnv(opts *EnvOpts) error {
- // get the repository to find the default branch
- repo, _, err := opts.Client.Repositories.Get(
- context.TODO(),
- opts.GitRepoOwner,
- opts.GitRepoName,
- )
- if err != nil {
- return err
- }
- defaultBranch := repo.GetDefaultBranch()
- // delete GitHub Environment: check that environment exists before deletion
- _, resp, err := opts.Client.Repositories.GetEnvironment(
- context.Background(),
- opts.GitRepoOwner,
- opts.GitRepoName,
- opts.EnvironmentName,
- )
- if err == nil && resp != nil && resp.StatusCode == http.StatusOK {
- _, err = opts.Client.Repositories.DeleteEnvironment(
- context.Background(),
- opts.GitRepoOwner,
- opts.GitRepoName,
- opts.EnvironmentName,
- )
- if err != nil {
- return err
- }
- }
- githubBranch, _, err := opts.Client.Repositories.GetBranch(
- context.Background(), opts.GitRepoOwner, opts.GitRepoName, defaultBranch, true,
- )
- if err != nil {
- return err
- }
- if githubBranch.GetProtected() {
- return ErrProtectedBranch
- }
- err = deleteGithubFile(
- opts.Client,
- fmt.Sprintf("porter_%s_env.yml", strings.ToLower(opts.EnvironmentName)),
- opts.GitRepoOwner,
- opts.GitRepoName,
- defaultBranch,
- false,
- )
- if err != nil {
- return err
- }
- return deleteGithubFile(
- opts.Client,
- fmt.Sprintf("porter_%s_delete_env.yml", strings.ToLower(opts.EnvironmentName)),
- opts.GitRepoOwner,
- opts.GitRepoName,
- defaultBranch,
- false,
- )
- }
- func getPreviewApplyActionYAML(opts *EnvOpts) ([]byte, error) {
- gaSteps := []GithubActionYAMLStep{
- getCheckoutCodeStep(),
- getCreatePreviewEnvStep(
- opts.ServerURL,
- getPorterTokenSecretName(opts.ProjectID),
- opts.ProjectID,
- opts.ClusterID,
- opts.GitInstallationID,
- opts.GitRepoOwner,
- opts.GitRepoName,
- "v0.2.0",
- ),
- }
- actionYAML := GithubActionYAML{
- On: map[string]interface{}{
- "workflow_dispatch": map[string]interface{}{
- "inputs": map[string]interface{}{
- "pr_number": map[string]interface{}{
- "description": "Pull request number",
- "type": "number",
- "required": true,
- },
- "pr_title": map[string]interface{}{
- "description": "Pull request title",
- "type": "string",
- "required": true,
- },
- "pr_branch_from": map[string]interface{}{
- "description": "Pull request head branch",
- "type": "string",
- "required": true,
- },
- "pr_branch_into": map[string]interface{}{
- "description": "Pull request base branch",
- "type": "string",
- "required": true,
- },
- },
- },
- },
- Name: "Porter Preview Environment",
- Jobs: map[string]GithubActionYAMLJob{
- "porter-preview": {
- RunsOn: "ubuntu-latest",
- Steps: gaSteps,
- },
- },
- }
- return yaml.Marshal(actionYAML)
- }
- func getPreviewDeleteActionYAML(opts *EnvOpts) ([]byte, error) {
- gaSteps := []GithubActionYAMLStep{
- getDeletePreviewEnvStep(
- opts.ServerURL,
- getPorterTokenSecretName(opts.ProjectID),
- opts.ProjectID,
- opts.ClusterID,
- opts.GitRepoName,
- "v0.2.0",
- ),
- }
- actionYAML := GithubActionYAML{
- On: map[string]interface{}{
- "workflow_dispatch": map[string]interface{}{
- "inputs": map[string]interface{}{
- "deployment_id": map[string]interface{}{
- "description": "Deployment ID",
- "type": "number",
- "required": true,
- },
- },
- },
- },
- Name: "Porter Preview Environment",
- Jobs: map[string]GithubActionYAMLJob{
- "porter-delete-preview": {
- RunsOn: "ubuntu-latest",
- Steps: gaSteps,
- },
- },
- }
- return yaml.Marshal(actionYAML)
- }
- func createNewBranch(
- client *github.Client,
- gitRepoOwner, gitRepoName, baseBranch, headBranch string,
- ) error {
- _, resp, err := client.Repositories.GetBranch(
- context.Background(), gitRepoOwner, gitRepoName, headBranch, true,
- )
- headBranchRef := fmt.Sprintf("refs/heads/%s", headBranch)
- if err == nil {
- // delete the stale branch
- _, err := client.Git.DeleteRef(
- context.Background(), gitRepoOwner, gitRepoName, headBranchRef,
- )
- if err != nil {
- return err
- }
- } else if resp.StatusCode != http.StatusNotFound {
- return err
- }
- base, _, err := client.Repositories.GetBranch(
- context.Background(), gitRepoOwner, gitRepoName, baseBranch, true,
- )
- if err != nil {
- return err
- }
- _, _, err = client.Git.CreateRef(
- context.Background(), gitRepoOwner, gitRepoName, &github.Reference{
- Ref: github.String(headBranchRef),
- Object: &github.GitObject{
- SHA: base.Commit.SHA,
- },
- },
- )
- if err != nil {
- return err
- }
- return nil
- }
|