package providers import ( "context" "fmt" "html" "log" "net/http" "strings" "git.front.kjuulh.io/kjuulh/contractor/internal/models" "github.com/bradleyfalzon/ghinstallation/v2" "github.com/google/go-github/v53/github" ) type GitHubClient struct { appID *int64 installationID *int64 privateKeyPath *string client *github.Client } func NewGitHubClient(appID, installationID *int64, privateKeyPath *string) *GitHubClient { return &GitHubClient{ appID: appID, installationID: installationID, privateKeyPath: privateKeyPath, } } func (gc *GitHubClient) makeSureClientExist() { if gc.client != nil { return } tr := http.DefaultTransport itr, err := ghinstallation.NewKeyFromFile(tr, *gc.appID, *gc.installationID, *gc.privateKeyPath) if err != nil { log.Fatal(err) } client := github.NewClient(&http.Client{Transport: itr}) gc.client = client } func (gc *GitHubClient) EditComment( ctx context.Context, doneRequest *models.RefreshGitHubRepositoryDoneRequest, ) error { gc.makeSureClientExist() commentBody := html.UnescapeString(doneRequest.CommentBody) startCmnt := "" startIdx := strings.Index(commentBody, startCmnt) endIdx := strings.Index(commentBody, "") if startIdx >= 0 && endIdx >= 0 { log.Println("found comment to replace") var content string if doneRequest.Error != "" { content = fmt.Sprintf("
ERROR: %s
%s
", content, doneRequest.Status) } doneRequest.CommentBody = fmt.Sprintf( "%s