|
| 1 | +/* |
| 2 | +Copyright 2023 cuisongliu@qq.com. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package action |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "github.com/cuisongliu/logger" |
| 23 | + "github.com/google/go-github/v39/github" |
| 24 | + github_go "github.com/labring/gh-rebot/pkg/github-go" |
| 25 | + "github.com/labring/gh-rebot/pkg/types" |
| 26 | + "github.com/labring/gh-rebot/pkg/utils" |
| 27 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 28 | + "os" |
| 29 | + "strings" |
| 30 | +) |
| 31 | + |
| 32 | +func CommentReply() error { |
| 33 | + issueNumber := int(types.ActionConfigJSON.IssueOrPRNumber) |
| 34 | + fileName, _ := GetEnvFromAction("filename") |
| 35 | + comment, _ := GetEnvFromAction("comment") |
| 36 | + isReply, _ := GetEnvFromAction("isReply") |
| 37 | + if fileName == "" && comment == "" { |
| 38 | + return fmt.Errorf("filename or comment is empty") |
| 39 | + } |
| 40 | + |
| 41 | + if fileName != "" { |
| 42 | + fileContent, err := os.ReadFile(fileName) |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + comment = string(fileContent) |
| 47 | + } |
| 48 | + |
| 49 | + if isReply == "true" { |
| 50 | + replyBody, _, _ := unstructured.NestedString(types.ActionConfigJSON.Data, "comment", "body") |
| 51 | + replyBody = utils.QuoteReply(replyBody) |
| 52 | + comment = strings.Join([]string{replyBody, comment}, "\r\n\r\n") |
| 53 | + } |
| 54 | + |
| 55 | + owner, repo, err := getRepo() |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + logger.Info("repo:%s, issueNumber: %d", repo, issueNumber) |
| 60 | + logger.Debug("comment: %s", comment) |
| 61 | + ctx := context.Background() |
| 62 | + client := github_go.GithubClient(ctx) |
| 63 | + githubComment := &github.IssueComment{Body: github.String(comment)} |
| 64 | + _, _, err = client.Issues.CreateComment(ctx, owner, repo, issueNumber, githubComment) |
| 65 | + return err |
| 66 | +} |
0 commit comments