Skip to content

Commit

Permalink
Merge pull request #73 from whywaita/refactor/export-struct
Browse files Browse the repository at this point in the history
export struct for POST /target
  • Loading branch information
whywaita authored Jul 9, 2021
2 parents 995257c + 1804dea commit 4e869fe
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 8 deletions.
2 changes: 1 addition & 1 deletion internal/testutils/testutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func IntegrationTestRunner(m *testing.M) int {
log.Fatalf("failed to create datastore instance: %s", err)
}

testDB, err = sqlx.Open("mysql", fmt.Sprintf("root:%s@(localhost:%s)/mysql?parseTime=true&loc=Local", mysqlRootPassword, resource.GetPort("3306/tcp")))
testDB, err = sqlx.Open("mysql", fmt.Sprintf("root:%s@(localhost:%s)/mysql?parseTime=true&loc=UTC", mysqlRootPassword, resource.GetPort("3306/tcp")))
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/datastore/mysql/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var testScopeRepo = "octocat/hello-world"
var testGitHubToken = "this-code-is-github-token"
var testRunnerVersion = "v999.99.9"
var testProviderURL = "/shoes-mock"
var testTime = time.Date(2037, 9, 3, 0, 0, 0, 0, time.Local)
var testTime = time.Date(2037, 9, 3, 0, 0, 0, 0, time.UTC)

func TestMySQL_CreateTarget(t *testing.T) {
testDatastore, teardown := testutils.GetTestDatastore()
Expand Down
8 changes: 5 additions & 3 deletions pkg/web/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import (
"goji.io/pat"
)

type targetCreateParam struct {
// TargetCreateParam is parameter for POST /target
type TargetCreateParam struct {
datastore.Target

RunnerUser string `json:"runner_user"`
Expand Down Expand Up @@ -79,7 +80,7 @@ func toNullString(input string) sql.NullString {
}
}

func isValidTargetCreateParam(input targetCreateParam) (bool, error) {
func isValidTargetCreateParam(input TargetCreateParam) (bool, error) {
if input.Scope == "" || input.ResourceType == datastore.ResourceTypeUnknown {
return false, fmt.Errorf("scope, resource_type must be set")
}
Expand All @@ -105,7 +106,8 @@ func isValidTargetCreateParam(input targetCreateParam) (bool, error) {
return true, nil
}

func (t *targetCreateParam) toDS(appToken string, tokenExpired time.Time) datastore.Target {
// ToDS convert to datastore.Target
func (t *TargetCreateParam) ToDS(appToken string, tokenExpired time.Time) datastore.Target {
gheDomain := toNullString(t.GHEDomain)
runnerUser := toNullString(t.RunnerUser)
runnerVersion := toNullString(t.RunnerVersion)
Expand Down
4 changes: 2 additions & 2 deletions pkg/web/target_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
func handleTargetCreate(w http.ResponseWriter, r *http.Request, ds datastore.Datastore) {
// input values: scope, gpt, ghe_domain, resource_type
ctx := r.Context()
inputTarget := targetCreateParam{}
inputTarget := TargetCreateParam{}
if err := json.NewDecoder(r.Body).Decode(&inputTarget); err != nil {
logger.Logf(false, "failed to decode request body: %+v", err)
outputErrorMsg(w, http.StatusBadRequest, "json decode error")
Expand All @@ -40,7 +40,7 @@ func handleTargetCreate(w http.ResponseWriter, r *http.Request, ds datastore.Dat
logger.Logf(false, "failed to generate")
}

t := inputTarget.toDS(token, *expiredAt)
t := inputTarget.ToDS(token, *expiredAt)
if err := isValidScopeAndToken(ctx, t.Scope, t.GHEDomain.String, token); err != nil {
outputErrorMsg(w, http.StatusBadRequest, err.Error())
return
Expand Down
2 changes: 1 addition & 1 deletion pkg/web/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (

var testInstallationID = int64(100000000)
var testGitHubAppToken = "secret-app-token"
var testTime = time.Date(2037, 9, 3, 0, 0, 0, 0, time.Local)
var testTime = time.Date(2037, 9, 3, 0, 0, 0, 0, time.UTC)

func parseResponse(resp *http.Response) ([]byte, int) {
defer resp.Body.Close()
Expand Down

0 comments on commit 4e869fe

Please sign in to comment.