Skip to content

Commit

Permalink
feat: get change-log api (#234)
Browse files Browse the repository at this point in the history
* feat: get changelog API

* fix: lint

* fix: undo migration change

* fix: refactor to create a new changelog service

* fix: update proton
  • Loading branch information
Mryashbhardwaj authored Jun 10, 2024
1 parent d8d4a2d commit d6ef67c
Show file tree
Hide file tree
Showing 16 changed files with 1,537 additions and 721 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ NAME = "github.com/goto/optimus"
LAST_COMMIT := $(shell git rev-parse --short HEAD)
LAST_TAG := "$(shell git rev-list --tags --max-count=1)"
OPMS_VERSION := "$(shell git describe --tags ${LAST_TAG})-next"
PROTON_COMMIT := "f9c7065042b7465da89125d5a16f0ed930bc9588"
PROTON_COMMIT := "1090b06ae7c9a95bb3086b55d068b949e50b2aa0"


.PHONY: build test test-ci generate-proto unit-test-ci integration-test vet coverage clean install lint
Expand Down
46 changes: 40 additions & 6 deletions core/job/handler/v1beta1/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ const (
)

type JobHandler struct {
l log.Logger
jobService JobService
l log.Logger
jobService JobService
changeLogService ChangeLogService

pb.UnimplementedJobSpecificationServiceServer
}

func NewJobHandler(jobService JobService, logger log.Logger) *JobHandler {
func NewJobHandler(jobService JobService, changeLogService ChangeLogService, logger log.Logger) *JobHandler {
return &JobHandler{
jobService: jobService,
l: logger,
jobService: jobService,
changeLogService: changeLogService,
l: logger,
}
}

Expand All @@ -60,6 +62,10 @@ type JobService interface {
GetDownstream(ctx context.Context, job *job.Job, localJob bool) ([]*job.Downstream, error)
}

type ChangeLogService interface {
GetChangelog(ctx context.Context, projectName tenant.ProjectName, jobName job.Name) ([]*job.ChangeLog, error)
}

func (jh *JobHandler) AddJobSpecifications(ctx context.Context, jobSpecRequest *pb.AddJobSpecificationsRequest) (*pb.AddJobSpecificationsResponse, error) {
jobTenant, err := tenant.NewTenant(jobSpecRequest.ProjectName, jobSpecRequest.NamespaceName)
if err != nil {
Expand Down Expand Up @@ -218,7 +224,7 @@ func (jh *JobHandler) GetJobSpecification(ctx context.Context, req *pb.GetJobSpe
}
jobName, err := job.NameFrom(req.GetJobName())
if err != nil {
jh.l.Error("error adapating job name [%s]: %s", req.GetJobName(), err)
jh.l.Error("error adapting job name [%s]: %s", req.GetJobName(), err)
return nil, err
}

Expand All @@ -235,6 +241,34 @@ func (jh *JobHandler) GetJobSpecification(ctx context.Context, req *pb.GetJobSpe
}, nil
}

func (jh *JobHandler) GetJobChangelog(ctx context.Context, req *pb.GetJobChangelogRequest) (*pb.GetJobChangelogResponse, error) {
projectName, err := tenant.ProjectNameFrom(req.GetProjectName())
if err != nil {
jh.l.Error("invalid project information request project [%s]: %s", req.GetProjectName(), err)
return nil, err
}
jobName, err := job.NameFrom(req.GetJobName())
if err != nil {
jh.l.Error("error adapting job name [%s]: %s", req.GetJobName(), err)
return nil, err
}

changeLog, err := jh.changeLogService.GetChangelog(ctx, projectName, jobName)
if err != nil && !errors.IsErrorType(err, errors.ErrNotFound) {
errorMsg := "failed to get job specification"
jh.l.Error(fmt.Sprintf("%s: %s", err.Error(), errorMsg))
return nil, errors.GRPCErr(err, errorMsg)
}
history := make([]*pb.JobChangelog, len(changeLog))
for i, clog := range changeLog {
history[i] = toJobChangeLogProto(clog)
}

return &pb.GetJobChangelogResponse{
History: history,
}, nil
}

func (jh *JobHandler) GetJobSpecifications(ctx context.Context, req *pb.GetJobSpecificationsRequest) (*pb.GetJobSpecificationsResponse, error) {
jobSpecs, merr := jh.jobService.GetByFilter(ctx,
filter.WithString(filter.ResourceDestination, req.GetResourceDestination()),
Expand Down
15 changes: 15 additions & 0 deletions core/job/handler/v1beta1/job_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,21 @@ func toUpstreamProtos(upstreams []*job.Upstream, upstreamSpec *job.UpstreamSpec,
}
}

func toJobChangeLogProto(changeLog *job.ChangeLog) *pb.JobChangelog {
pbChange := &pb.JobChangelog{
EventType: changeLog.Type,
Timestamp: changeLog.Time.String(),
}
pbChange.Change = make([]*pb.JobChange, len(changeLog.Change))
for i, change := range changeLog.Change {
pbChange.Change[i] = &pb.JobChange{
AttributeName: change.Property,
Diff: change.Diff,
}
}
return pbChange
}

func toHTTPUpstreamProtos(httpUpstreamSpecs []*job.SpecHTTPUpstream) []*pb.HttpDependency {
var httpUpstreamProtos []*pb.HttpDependency
for _, httpUpstream := range httpUpstreamSpecs {
Expand Down
Loading

0 comments on commit d6ef67c

Please sign in to comment.