-
Notifications
You must be signed in to change notification settings - Fork 378
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ai-function): create testset if not set testSetID
- Loading branch information
1 parent
8cad1c9
commit 2eb1daa
Showing
5 changed files
with
297 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright (c) 2021 Terminus, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package bundle | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/pkg/errors" | ||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/erda-project/erda/apistructs" | ||
"github.com/erda-project/erda/bundle/apierrors" | ||
"github.com/erda-project/erda/pkg/http/httpserver" | ||
"github.com/erda-project/erda/pkg/http/httputil" | ||
) | ||
|
||
func (b *Bundle) CreateTestSet(req apistructs.TestSetCreateRequest) (*apistructs.TestSet, error) { | ||
host, err := b.urls.ErdaServer() | ||
if err != nil { | ||
return &apistructs.TestSet{}, err | ||
} | ||
var resp httpserver.Resp | ||
r, err := b.hc.Post(host).Path("/api/testsets"). | ||
Header(httputil.InternalHeader, "AI"). | ||
Header(httputil.UserHeader, req.IdentityInfo.UserID). | ||
JSONBody(&req).Do().JSON(&resp) | ||
|
||
if err != nil { | ||
log.Errorf("CreateTestSet err: %v", err) | ||
return &apistructs.TestSet{}, apierrors.ErrInvoke.InternalError(err) | ||
} | ||
if !r.IsOK() || !resp.Success { | ||
log.Errorf("CreateTestSet response is not ok with StatusCode %d", r.StatusCode()) | ||
return &apistructs.TestSet{}, apierrors.ErrInvoke.InternalError(errors.Errorf("CreateTestSet response is not ok with StatusCode %d", r.StatusCode())) | ||
} | ||
|
||
testSetJsonBytes, err := json.Marshal(resp.Data) | ||
if err != nil { | ||
return &apistructs.TestSet{}, errors.Errorf("json.Marshal(resp.Data) failed: %v", err) | ||
} | ||
|
||
var testSet apistructs.TestSet | ||
err = json.Unmarshal(testSetJsonBytes, &testSet) | ||
if err != nil { | ||
return &apistructs.TestSet{}, errors.Errorf("json.Unmarshal(testSetJsonBytes, &testSet) to apistructs.TestSet failed: %v", err) | ||
} | ||
|
||
return &testSet, nil | ||
} | ||
|
||
func (b *Bundle) GetTestSets(req apistructs.TestSetListRequest) ([]apistructs.TestSet, error) { | ||
host, err := b.urls.ErdaServer() | ||
if err != nil { | ||
return nil, err | ||
} | ||
pathVars := fmt.Sprintf("?recycled=%v&parentID=%d&projectID=%d", req.Recycled, *req.ParentID, *req.ProjectID) | ||
var resp httpserver.Resp | ||
r, err := b.hc.Get(host).Path("/api/testsets"+pathVars). | ||
Header(httputil.InternalHeader, "AI"). | ||
JSONBody(&req).Do().JSON(&resp) | ||
|
||
if err != nil { | ||
log.Errorf("CreateTestSet err: %v", err) | ||
return nil, apierrors.ErrInvoke.InternalError(err) | ||
} | ||
if !r.IsOK() || !resp.Success { | ||
return nil, apierrors.ErrInvoke.InternalError(errors.Errorf("CreateTestSet response is not ok with StatusCode %d", r.StatusCode())) | ||
} | ||
|
||
testSetJsonBytes, err := json.Marshal(resp.Data) | ||
if err != nil { | ||
return nil, errors.Errorf("json.Marshal(resp.Data) failed: %v", err) | ||
} | ||
|
||
var testSets []apistructs.TestSet | ||
err = json.Unmarshal(testSetJsonBytes, &testSets) | ||
if err != nil { | ||
return nil, errors.Errorf("json.Unmarshal(testSetJsonBytes, &testSet) to apistructs.TestSet failed: %v", err) | ||
} | ||
|
||
return testSets, nil | ||
} |
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
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
Oops, something went wrong.