github.com/diggerhq/digger/libs@v0.0.0-20240604170430-9d61cdf01cc5/comment_utils/summary/updater.go (about)

     1  package comment_updater
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/diggerhq/digger/libs/orchestrator"
     6  	"github.com/diggerhq/digger/libs/orchestrator/scheduler"
     7  	"golang.org/x/text/cases"
     8  	"golang.org/x/text/language"
     9  	"log"
    10  )
    11  
    12  type CommentUpdater interface {
    13  	UpdateComment(jobs []scheduler.SerializedJob, prNumber int, prService orchestrator.PullRequestService, prCommentId int64) error
    14  }
    15  
    16  type BasicCommentUpdater struct {
    17  }
    18  
    19  func (b BasicCommentUpdater) UpdateComment(jobs []scheduler.SerializedJob, prNumber int, prService orchestrator.PullRequestService, prCommentId int64) error {
    20  	jobSpecs, err := scheduler.GetJobSpecs(jobs)
    21  	if err != nil {
    22  		log.Printf("could not get jobspecs: %v", err)
    23  		return err
    24  	}
    25  	firstJobSpec := jobSpecs[0]
    26  	jobType := firstJobSpec.JobType
    27  	isPlan := jobType == string(orchestrator.DiggerCommandPlan)
    28  	jobTypeTitle := cases.Title(language.AmericanEnglish).String(string(jobType))
    29  	message := ""
    30  	if isPlan {
    31  		message = message + fmt.Sprintf("| Project | Status | %v | + | ~ | - |\n", jobTypeTitle)
    32  		message = message + fmt.Sprintf("|---------|--------|------|---|---|---|\n")
    33  	} else {
    34  		message = message + fmt.Sprintf("| Project | Status | %v |\n", jobTypeTitle)
    35  		message = message + fmt.Sprintf("|---------|--------|-------|\n")
    36  	}
    37  	for i, job := range jobs {
    38  		jobSpec := jobSpecs[i]
    39  		prCommentUrl := job.PRCommentUrl
    40  		if isPlan {
    41  			message = message + fmt.Sprintf("|%v **%v** |<a href='%v'>%v</a> | <a href='%v'>%v</a> | %v | %v | %v|\n", job.Status.ToEmoji(), jobSpec.ProjectName, *job.WorkflowRunUrl, job.Status.ToString(), prCommentUrl, jobTypeTitle, job.ResourcesCreated, job.ResourcesUpdated, job.ResourcesDeleted)
    42  		} else {
    43  			message = message + fmt.Sprintf("|%v **%v** |<a href='%v'>%v</a> | <a href='%v'>%v</a> |\n", job.Status.ToEmoji(), jobSpec.ProjectName, *job.WorkflowRunUrl, job.Status.ToString(), prCommentUrl, jobTypeTitle)
    44  		}
    45  	}
    46  
    47  	prService.EditComment(prNumber, prCommentId, message)
    48  	return nil
    49  }
    50  
    51  type NoopCommentUpdater struct {
    52  }
    53  
    54  func (b NoopCommentUpdater) UpdateComment(jobs []scheduler.SerializedJob, prNumber int, prService orchestrator.PullRequestService, prCommentId int64) error {
    55  	return nil
    56  }