-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add --github-integration on init (#203)
- Loading branch information
Showing
5 changed files
with
75 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -144,6 +144,7 @@ spec: | |
visibility: public | ||
repository: | ||
url: "[email protected]:/semaphoreci/cli.git" | ||
integration_type: github_token | ||
` | ||
|
||
yaml_file_path := "/tmp/project.yaml" | ||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ spec: | |
visibility: public | ||
repository: | ||
url: "[email protected]:/semaphoreci/cli.git" | ||
integration_type: github_token | ||
` | ||
|
||
yaml_file_path := "/tmp/project.yaml" | ||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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{ | ||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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) | ||
|
||
|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
@@ -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) | ||
|
@@ -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) | ||
|