|
1 | 1 | package acigithub |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "awesome-ci/src/tools" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "strings" |
| 8 | + |
4 | 9 | "github.com/google/go-github/v39/github" |
5 | 10 | ) |
6 | 11 |
|
| 12 | +var ( |
| 13 | + direction, sort = "asc", "created" |
| 14 | +) |
| 15 | + |
7 | 16 | func GetIssueComments(issueNumber int, owner string, repo string) (issueComments []*github.IssueComment, err error) { |
8 | | - // opts := github.IssueListCommentsOptions{} |
9 | | - issueComments, _, err = GithubClient.Issues.ListComments(ctx, owner, repo, issueNumber, nil) |
| 17 | + var commentOpts = &github.IssueListCommentsOptions{ |
| 18 | + Direction: &direction, |
| 19 | + // Sort: &sort, |
| 20 | + ListOptions: github.ListOptions{ |
| 21 | + PerPage: 100, |
| 22 | + Page: 1, |
| 23 | + }, |
| 24 | + } |
| 25 | + issueComments, _, err = GithubClient.Issues.ListComments(ctx, owner, repo, issueNumber, commentOpts) |
| 26 | + return |
| 27 | +} |
| 28 | + |
| 29 | +func CommentHelpToPullRequest(number int) (err error) { |
| 30 | + if !isgithubRepository { |
| 31 | + log.Fatalln("make shure the GITHUB_REPOSITORY is available!") |
| 32 | + } |
| 33 | + owner, repo := tools.DevideOwnerAndRepo(githubRepository) |
| 34 | + |
| 35 | + var commentOpts = &github.IssueListCommentsOptions{ |
| 36 | + Direction: &direction, |
| 37 | + Sort: &sort, |
| 38 | + ListOptions: github.ListOptions{ |
| 39 | + PerPage: 30, |
| 40 | + Page: 1, |
| 41 | + }, |
| 42 | + } |
| 43 | + comments, _, err := GithubClient.Issues.ListComments(ctx, owner, repo, number, commentOpts) |
| 44 | + if err != nil { |
| 45 | + return fmt.Errorf("unable to list comments: %x", err) |
| 46 | + } |
| 47 | + |
| 48 | + body := `<details><summary>Possible awesome-ci commands for this Pull Request</summary>` + |
| 49 | + `</br>aci_patch_level: major</br>aci_version_override: 2.1.0` + |
| 50 | + `</br></br>Need more help?</br>Have a look at <a href="https://github.com/fullstack-devops/awesome-ci" target="_blank">my repo</a>` + |
| 51 | + `</br>This message was created by awesome-ci and can be disabled by the env variable <code>ACI_SILENT=true</code></details>` |
| 52 | + |
| 53 | + var prComment *github.IssueComment |
| 54 | + for _, prc := range comments { |
| 55 | + if strings.HasPrefix(*prc.Body, `<details><summary>Possible awesome-ci commands for this Pull Request</summary>`) { |
| 56 | + prComment = prc |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if prComment == nil { |
| 61 | + var prComment = &github.IssueComment{ |
| 62 | + Body: &body, |
| 63 | + } |
| 64 | + |
| 65 | + err = CommentPullRequest(number, prComment) |
| 66 | + } else { |
| 67 | + // edit command if newer version deployed and commands changed |
| 68 | + if *prComment.Body != body { |
| 69 | + editedComment := &github.IssueComment{ |
| 70 | + Body: &body, |
| 71 | + } |
| 72 | + _, _, err = GithubClient.Issues.EditComment(ctx, owner, repo, *prComment.ID, editedComment) |
| 73 | + } |
| 74 | + } |
| 75 | + return |
| 76 | +} |
| 77 | + |
| 78 | +func CommentPullRequest(number int, comment *github.IssueComment) (err error) { |
| 79 | + if !isgithubRepository { |
| 80 | + log.Fatalln("make shure the GITHUB_REPOSITORY is available!") |
| 81 | + } |
| 82 | + owner, repo := tools.DevideOwnerAndRepo(githubRepository) |
| 83 | + |
| 84 | + _, _, err = GithubClient.Issues.CreateComment(ctx, owner, repo, number, comment) |
10 | 85 | return |
11 | 86 | } |
0 commit comments