drone/app/api/controller/aiagent/analyse_execution.go
Yogesh Chauhan 0af264d09f feat: [ML-338]: APIs for Harness Intelligence (#2667)
* Return empty summary/branch for now
* Fix Lint Issues
* feat: [ML-288]: API skeletons for Harness Intelligence
* fix ai pipelines (#2659)

* fix ai pipelines
* Update pipeline name (#2658)

* Update pipeline name
* fix: [ML-288]: update remediation prompt and suggestions (#2652)

* fix: [ML-288]: update remediation prompt and suggestions
* fix: [ML-288]: update url for artifact registry (#2651)

* fix: [ML-288]: update url for artifact registry
* Add snippet to mark code as written by Harness Intelligence (#2628)

* Add snippet to mark code as written by Harness Intelligence
* Add snippet to mark code as written by Harness Intelligence (#2626)

* Add snippet to mark code as written by Harness Intelligence
* feat: [ML-288]: link to harness artifact registry (#2619)

* some linting fixes
* feat: [ML-288]: link to harness artifact registry
* feat: [ML-286]: updating pipeline suggestions (#2620)

* updating suggestions
* feat: [ML-286]: updating pipeline suggestions
* feat: [ML-288]: adding slackbot (#2603)

* updating slack responses
* prevent responding to any bot messages
* feat: [ML-288]: adding slackbot
* throw error for commit
* commit stubbed
* feat: [ML-286]: moving stubbed response earlier in execution (#2602)

* feat: [ML-286]: moving stubbed response earlier in execution
* feat: [ML-286]: adding stubbed response for analyze execution (#2600)

* updating branch name
* adding updated-main for analyse-execution endpoint
* feat: [ML-286]: adding stubbed response for analyze execution
* Update AnalyseExecutionOutput to contain multi file diff (#2592)

* Create branch and commit changes on analyse
* Simplify AnalyseExecutionOutput to contain only high level summary and branch
* Update AnalyseExecutionOutput to contain multi file diff
* updating endpoints for unscripted demo (#2594)

* cleaning up code, adding suggested pipeline response
* updating suggestions api
* updating endpoints for unscripted demo
* Stub out generate pipeline yaml
2024-09-09 17:49:44 +00:00

139 lines
3.9 KiB
Go

// Copyright 2023 Harness, 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 aiagent
import (
"context"
"fmt"
"time"
apiauth "github.com/harness/gitness/app/api/auth"
"github.com/harness/gitness/app/api/controller"
"github.com/harness/gitness/app/api/usererror"
"github.com/harness/gitness/app/auth"
"github.com/harness/gitness/app/bootstrap"
"github.com/harness/gitness/git"
"github.com/harness/gitness/git/sha"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"
)
func (c *Controller) GetAnalysis(
ctx context.Context,
session *auth.Session,
repoRef string,
pipelineIdentifier string,
executionNum int64,
) (*types.AnalyseExecutionOutput, error) {
repo, err := c.repoStore.FindByRef(ctx, repoRef)
if err != nil {
return nil, usererror.BadRequestf("failed to find repo %s", repoRef)
}
err = apiauth.CheckPipeline(ctx, c.authorizer, session, repo.Path, pipelineIdentifier, enum.PermissionPipelineView)
if err != nil {
return nil, usererror.Forbidden(fmt.Sprintf("not allowed to view pipeline %s", pipelineIdentifier))
}
pipeline, err := c.pipelineStore.FindByIdentifier(ctx, repo.ID, pipelineIdentifier)
if err != nil {
return nil, usererror.BadRequestf("failed to find pipeline: %s", pipelineIdentifier)
}
execution, err := c.executionStore.FindByNumber(ctx, pipeline.ID, executionNum)
if err != nil {
return nil, usererror.BadRequestf("failed to find execution %d", executionNum)
}
if execution.Status == enum.CIStatusSuccess {
return nil, usererror.BadRequestf("execution %d is not a failed execution", executionNum)
}
// ToDo: put actual values
payload := &CommitPayload{}
branch := ""
_, err = c.commit(ctx, session, repo, payload)
if err != nil {
return &types.AnalyseExecutionOutput{}, err
}
return &types.AnalyseExecutionOutput{Branch: branch, Summary: ""}, nil
}
type CommitPayload struct {
Title string
Message string
Branch string
NewBranch string
Files []*Files
}
type Files struct {
action git.FileAction
path string
content string
SHA sha.SHA
}
func (c *Controller) commit(ctx context.Context,
session *auth.Session,
repo *types.Repository,
payload *CommitPayload) (types.CommitFilesResponse, error) {
files := payload.Files
actions := make([]git.CommitFileAction, len(files))
for i, file := range files {
rawPayload := []byte(file.content)
actions[i] = git.CommitFileAction{
Action: file.action,
Path: file.path,
Payload: rawPayload,
SHA: file.SHA,
}
}
writeParams, err := controller.CreateRPCInternalWriteParams(ctx, c.urlProvider, session, repo)
if err != nil {
return types.CommitFilesResponse{}, fmt.Errorf("failed to create RPC write params: %w", err)
}
now := time.Now()
commit, err := c.git.CommitFiles(ctx, &git.CommitFilesParams{
WriteParams: writeParams,
Title: payload.Title,
Message: payload.Message,
Branch: payload.Branch,
NewBranch: payload.NewBranch,
Actions: actions,
Committer: identityFromPrincipal(bootstrap.NewSystemServiceSession().Principal),
CommitterDate: &now,
Author: identityFromPrincipal(session.Principal),
AuthorDate: &now,
})
if err != nil {
return types.CommitFilesResponse{}, err
}
return types.CommitFilesResponse{
CommitID: commit.CommitID.String(),
}, nil
}
func identityFromPrincipal(p types.Principal) *git.Identity {
return &git.Identity{
Name: p.DisplayName,
Email: p.Email,
}
}