Skip to content

Commit

Permalink
feature(main): add pr comment (#42)
Browse files Browse the repository at this point in the history
Signed-off-by: cuisongliu <[email protected]>
  • Loading branch information
cuisongliu authored Jun 27, 2023
1 parent c199ebe commit fd17418
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
33 changes: 33 additions & 0 deletions .github/workflows/test-pr-comment.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: 'Add cherry-pick comment when PR is merged'
on:
pull_request:
types:
- closed

jobs:
comment:
runs-on: ubuntu-latest
steps:
- name: Comment cherry-pick command
uses: actions/github-script@v5
with:
script: |
const pr = context.payload.pull_request;
if (!pr.merged) {
console.log("PR is not merged. Skipping...");
return;
}
if (!pr.milestone || !pr.milestone.title) {
console.log("Milestone is not set. Skipping...");
return;
}
const milestone = pr.milestone.title;
const cherryPickCmd = `/cherry-pick ${milestone}-release`;
console.log(`Adding comment: ${cherryPickCmd}`);
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
body: cherryPickCmd
});
github-token: ${{ secrets.GH_PAT }}
28 changes: 15 additions & 13 deletions pkg/action/action_pr_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func PRComment() error {
if err != nil {
return err
}
logger.Info("repo:%s, filename: %s, replaceTag: %s, prNumber: %d", repo, fileName, replaceTag, prNumber)
logger.Debug("repo:%s, filename: %s, replaceTag: %s, prNumber: %d", repo, fileName, replaceTag, prNumber)
ctx := context.Background()
client := github_go.GithubClient(ctx)
comments, _, err := client.Issues.ListComments(ctx, owner, repo, prNumber, nil)
Expand All @@ -60,26 +60,28 @@ func PRComment() error {
}
hiddenReplace := fmt.Sprintf("<!-- %s -->", replaceTag)
content := string(fileContent) + "\n" + hiddenReplace
createComment := func() {
comment := &github.IssueComment{Body: github.String(content)}
client.Issues.CreateComment(ctx, owner, repo, prNumber, comment)
}
if hiddenReplace == "" {
//add
createComment()
return nil
}

// Checks existing comments, edits if match found
for _, comment := range comments {
if comment.Body != nil && comment.ID != nil {
if *comment.Body == content {
logger.Info("The comment %d has been already added to the pull request. Skipping...", *comment.ID)
logger.Debug("The comment %d has been already added to the pull request. Skipping...", *comment.ID)
return nil
} else if hiddenReplace != "" && strings.LastIndex(*comment.Body, hiddenReplace) != -1 {
client.Issues.EditComment(ctx, owner, repo, *comment.ID, &github.IssueComment{Body: github.String(content)})
_, _, err = client.Issues.EditComment(ctx, owner, repo, *comment.ID, &github.IssueComment{Body: github.String(content)})
if err != nil {
return fmt.Errorf("Issues.EditComment returned error: %v", err)
}
return nil
}
}
}
createComment()

// Creates new comment
_, _, err = client.Issues.CreateComment(ctx, owner, repo, prNumber, &github.IssueComment{Body: github.String(content)})
if err != nil {
return fmt.Errorf("Issues.CreateComment returned error: %v", err)
}

return nil
}

0 comments on commit fd17418

Please sign in to comment.