Skip to content

Commit

Permalink
feat: add --github-integration on init (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaspin authored Oct 19, 2022
1 parent e5ecf39 commit 2833b82
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 11 deletions.
13 changes: 7 additions & 6 deletions api/models/project_v1_alpha.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ type Status struct {
}

type PipelineFile struct {
Path string `json:"path"`
Path string `json:"path"`
Level string `json:"level"`
}

type Whitelist struct {
Branches []string `json:"branches,omitempty"`
Tags []string `json:"tags,omitempty"`
Tags []string `json:"tags,omitempty"`
}

type ProjectV1Alpha struct {
Expand All @@ -51,11 +51,12 @@ type ProjectV1Alpha struct {
PipelineFile string `json:"pipeline_file" yaml:"pipeline_file"`
Status *Status `json:"status,omitempty" yaml:"status"`
Whitelist Whitelist `json:"whitelist" yaml:"whitelist"`
IntegrationType string `json:"integration_type" yaml:"integration_type"`
} `json:"repository,omitempty"`
Schedulers []Scheduler `json:"schedulers,omitempty" yaml:"schedulers,omitempty"`
CustomPermissions *bool `json:"custom_permissions,omitempty" yaml:"custom_permissions,omitempty"`
DebugPermissions []string `json:"debug_permissions,omitempty" yaml:"debug_permissions,omitempty"`
AttachPermissions []string `json:"attach_permissions,omitempty" yaml:"attach_permissions,omitempty"`
Schedulers []Scheduler `json:"schedulers,omitempty" yaml:"schedulers,omitempty"`
CustomPermissions *bool `json:"custom_permissions,omitempty" yaml:"custom_permissions,omitempty"`
DebugPermissions []string `json:"debug_permissions,omitempty" yaml:"debug_permissions,omitempty"`
AttachPermissions []string `json:"attach_permissions,omitempty" yaml:"attach_permissions,omitempty"`
} `json:"spec,omitempty"`
}

Expand Down
3 changes: 2 additions & 1 deletion cmd/apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ spec:
visibility: public
repository:
url: "[email protected]:/semaphoreci/cli.git"
integration_type: github_token
`

yaml_file_path := "/tmp/project.yaml"
Expand All @@ -164,7 +165,7 @@ spec:
RootCmd.SetArgs([]string{"apply", "-f", yaml_file_path})
RootCmd.Execute()

expected := `{"apiVersion":"v1alpha","kind":"Project","metadata":{"name":"Test","id":"a13949b7-b2f6-4286-8f26-3962d7e97828"},"spec":{"visibility":"public","repository":{"url":"[email protected]:/semaphoreci/cli.git","forked_pull_requests":{},"pipeline_file":"","whitelist":{}}}}`
expected := `{"apiVersion":"v1alpha","kind":"Project","metadata":{"name":"Test","id":"a13949b7-b2f6-4286-8f26-3962d7e97828"},"spec":{"visibility":"public","repository":{"url":"[email protected]:/semaphoreci/cli.git","forked_pull_requests":{},"pipeline_file":"","whitelist":{},"integration_type":"github_token"}}}`

if received != expected {
t.Errorf("Expected the API to receive PATCH project with: %s, got: %s", expected, received)
Expand Down
3 changes: 2 additions & 1 deletion cmd/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ spec:
visibility: public
repository:
url: "[email protected]:/semaphoreci/cli.git"
integration_type: github_token
`

yaml_file_path := "/tmp/project.yaml"
Expand All @@ -45,7 +46,7 @@ spec:
RootCmd.SetArgs([]string{"create", "-f", yaml_file_path})
RootCmd.Execute()

expected := `{"apiVersion":"v1alpha","kind":"Project","metadata":{"name":"Test"},"spec":{"visibility":"public","repository":{"url":"[email protected]:/semaphoreci/cli.git","forked_pull_requests":{},"pipeline_file":"","whitelist":{}}}}`
expected := `{"apiVersion":"v1alpha","kind":"Project","metadata":{"name":"Test"},"spec":{"visibility":"public","repository":{"url":"[email protected]:/semaphoreci/cli.git","forked_pull_requests":{},"pipeline_file":"","whitelist":{},"integration_type":"github_token"}}}`

if received != expected {
t.Errorf("Expected the API to receive POST projects with: %s, got: %s", expected, received)
Expand Down
26 changes: 26 additions & 0 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ import (
gitconfig "github.com/tcnksm/go-gitconfig"
)

const (
GithubIntegrationOAuthToken = "github_token"
GithubIntegrationApp = "github_app"
)

var flagProjectName string
var flagRepoUrl string
var flagGithubIntegration string

func InitCmd() cobra.Command {
cmd := cobra.Command{
Expand All @@ -34,6 +40,13 @@ func InitCmd() cobra.Command {
cmd.Flags().StringVar(&flagRepoUrl, "repo-url", "", "explicitly set the repository url, if not set it is extracted from local git repository")
cmd.Flags().StringVar(&flagProjectName, "project-name", "", "explicitly set the project name, if not set it is extracted from the repo-url")

cmd.Flags().StringVar(
&flagGithubIntegration,
"github-integration",
GithubIntegrationOAuthToken,
fmt.Sprintf("github integration for the project. Possible values are: %s", validIntegrationTypes()),
)

return cmd
}

Expand Down Expand Up @@ -64,10 +77,19 @@ func RunInit(cmd *cobra.Command, args []string) {
utils.Check(err)
}

if flagGithubIntegration != GithubIntegrationOAuthToken && flagGithubIntegration != GithubIntegrationApp {
utils.Fail(fmt.Sprintf(
"Invalid GitHub integration '%s' for project. Possible values are %s",
flagGithubIntegration,
validIntegrationTypes(),
))
}

c := client.NewProjectV1AlphaApi()
projectModel := models.NewProjectV1Alpha(name)
projectModel.Spec.Repository.Url = repoUrl
projectModel.Spec.Repository.RunOn = []string{"branches", "tags"}
projectModel.Spec.Repository.IntegrationType = flagGithubIntegration

project, err := c.CreateProject(&projectModel)

Expand Down Expand Up @@ -127,3 +149,7 @@ func getGitOriginUrl() (string, error) {
return "[email protected]:/renderedtext/something.git", nil
}
}

func validIntegrationTypes() string {
return fmt.Sprintf("\"%s\" (OAuth token), \"%s\"", GithubIntegrationOAuthToken, GithubIntegrationApp)
}
41 changes: 38 additions & 3 deletions cmd/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestRunInit__NoParams(t *testing.T) {
cmd := InitCmd()
cmd.Execute()

expected := `{"apiVersion":"v1alpha","kind":"Project","metadata":{"name":"something"},"spec":{"repository":{"url":"[email protected]:/renderedtext/something.git","run_on":["branches","tags"],"forked_pull_requests":{},"pipeline_file":"","whitelist":{}}}}`
expected := `{"apiVersion":"v1alpha","kind":"Project","metadata":{"name":"something"},"spec":{"repository":{"url":"[email protected]:/renderedtext/something.git","run_on":["branches","tags"],"forked_pull_requests":{},"pipeline_file":"","whitelist":{},"integration_type":"github_token"}}}`

if received != expected {
t.Errorf("Expected the API to receive project create req with '%s' instead '%s'.", expected, received)
Expand Down Expand Up @@ -59,7 +59,42 @@ func TestRunInit__NameParamPassed(t *testing.T) {

cmd.Execute()

expected := `{"apiVersion":"v1alpha","kind":"Project","metadata":{"name":"another-name"},"spec":{"repository":{"url":"[email protected]:/renderedtext/something.git","run_on":["branches","tags"],"forked_pull_requests":{},"pipeline_file":"","whitelist":{}}}}`
expected := `{"apiVersion":"v1alpha","kind":"Project","metadata":{"name":"another-name"},"spec":{"repository":{"url":"[email protected]:/renderedtext/something.git","run_on":["branches","tags"],"forked_pull_requests":{},"pipeline_file":"","whitelist":{},"integration_type":"github_token"}}}`

fmt.Print(expected)
fmt.Print(received)

if received != expected {
t.Errorf("Expected the API to receive project create req, want: %s got: %s.", expected, received)
}
}

func TestRunInit__IntegrationTypeParamPassed(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()

received := ""

httpmock.RegisterResponder("POST", "https://org.semaphoretext.xyz/api/v1alpha/projects",
func(req *http.Request) (*http.Response, error) {
body, _ := ioutil.ReadAll(req.Body)

received = string(body)

return httpmock.NewStringResponse(200, string(received)), nil
},
)

cmd := InitCmd()

cmd.SetArgs([]string{
`--project-name=another-name`,
`--github-integration=github_app`,
})

cmd.Execute()

expected := `{"apiVersion":"v1alpha","kind":"Project","metadata":{"name":"another-name"},"spec":{"repository":{"url":"[email protected]:/renderedtext/something.git","run_on":["branches","tags"],"forked_pull_requests":{},"pipeline_file":"","whitelist":{},"integration_type":"github_app"}}}`

fmt.Print(expected)
fmt.Print(received)
Expand Down Expand Up @@ -93,7 +128,7 @@ func TestRunInit__RepoUrlParamPassed(t *testing.T) {

cmd.Execute()

expected := `{"apiVersion":"v1alpha","kind":"Project","metadata":{"name":"a"},"spec":{"repository":{"url":"[email protected]:/renderedtext/a.git","run_on":["branches","tags"],"forked_pull_requests":{},"pipeline_file":"","whitelist":{}}}}`
expected := `{"apiVersion":"v1alpha","kind":"Project","metadata":{"name":"a"},"spec":{"repository":{"url":"[email protected]:/renderedtext/a.git","run_on":["branches","tags"],"forked_pull_requests":{},"pipeline_file":"","whitelist":{},"integration_type":"github_token"}}}`

fmt.Print(expected)
fmt.Print(received)
Expand Down

0 comments on commit 2833b82

Please sign in to comment.