-
Notifications
You must be signed in to change notification settings - Fork 3
/
comment.go
74 lines (59 loc) · 1.61 KB
/
comment.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"context"
"errors"
"fmt"
"strings"
"github.com/jenkins-x/go-scm/scm"
"github.com/jenkins-x/go-scm/scm/factory"
)
const commentKey = "<!--woodpeckerci-plugin-surge-preview-->"
type comment struct {
client *scm.Client
}
func (c *comment) Load(driver, serverUrl, oauth string) error {
client, err := factory.NewClient(driver, serverUrl, oauth)
if err != nil {
return err
}
c.client = client
return nil
}
func (c *comment) Find(ctx context.Context, repo string, prID int) (*scm.Comment, error) {
listOptions := scm.ListOptions{
Page: 1,
Size: 1,
}
for {
comments, resp, err := c.client.PullRequests.ListComments(ctx, repo, prID, &listOptions)
if err != nil {
return nil, err
}
for _, comment := range comments {
if strings.Contains(comment.Body, commentKey) {
return comment, nil
}
}
// Exit the loop when we've seen all pages
if resp.Page.Next == 0 {
break
}
if resp.Rate.Remaining == 0 {
return nil, errors.New("Rate limit exceeded")
}
// Update the page number to get the next page
listOptions.Page = resp.Page.Next
}
return nil, errors.New("Comment not found")
}
func (c *comment) UpdateOrCreateComment(ctx context.Context, repo string, prID int, comment *scm.Comment, body string) (*scm.Comment, error) {
commentInput := &scm.CommentInput{
Body: fmt.Sprintf("%s\n%s", commentKey, body),
}
if comment == nil {
comment, _, err := c.client.PullRequests.CreateComment(ctx, repo, prID, commentInput)
return comment, err
}
comment, _, err := c.client.PullRequests.EditComment(ctx, repo, prID, comment.ID, commentInput)
return comment, err
}