| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- 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
- }
- _, err = commitGithubFile(
- 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 = createPorterPreviewBranch(opts, defaultBranch)
- if err != nil {
- return fmt.Errorf("write-protected branch %s. Error creating porter-preview branch: %w", defaultBranch, err)
- }
- _, err = commitGithubFile(
- 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 = commitGithubFile(
- 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 = commitGithubFile(
- 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
- }
- }
- 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.GitRepoName,
- "v0.1.0",
- ),
- }
- actionYAML := GithubActionYAML{
- On: []string{"pull_request"},
- 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.GitInstallationID,
- opts.GitRepoName,
- "v0.1.0",
- ),
- }
- actionYAML := GithubActionYAML{
- On: map[string]interface{}{
- "pull_request": map[string]interface{}{
- "types": []string{"closed"},
- },
- },
- Name: "Porter Preview Environment",
- Jobs: map[string]GithubActionYAMLJob{
- "porter-delete-preview": {
- RunsOn: "ubuntu-latest",
- Steps: gaSteps,
- },
- },
- }
- return yaml.Marshal(actionYAML)
- }
- func createPorterPreviewBranch(opts *EnvOpts, defaultBranch string) error {
- _, resp, err := opts.Client.Repositories.GetBranch(
- context.Background(), opts.GitRepoOwner, opts.GitRepoName, "porter-preview", false,
- )
- if resp.StatusCode == http.StatusNotFound {
- branch, _, err := opts.Client.Repositories.GetBranch(
- context.Background(), opts.GitRepoOwner, opts.GitRepoName, defaultBranch, false,
- )
- if err != nil {
- return err
- }
- _, _, err = opts.Client.Git.CreateRef(
- context.Background(), opts.GitRepoOwner, opts.GitRepoName, &github.Reference{
- Ref: github.String("refs/heads/porter-preview"),
- Object: &github.GitObject{
- SHA: branch.Commit.SHA,
- },
- },
- )
- if err != nil {
- return err
- }
- return nil
- }
- if err != nil {
- return err
- }
- return nil
- }
|