New: save wordcounts in PR Comment
This commit is contained in:
@@ -13,7 +13,14 @@ jobs:
|
|||||||
- name: Checkout code
|
- name: Checkout code
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
|
|
||||||
- name: Count words in Markdown files
|
- name: Run Gemini Review
|
||||||
|
env:
|
||||||
|
GITEA_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||||
|
GITEA_URL: ${{ github.server_url }}
|
||||||
|
GITHUB_REPOSITORY: ${{ github.repository }}
|
||||||
|
PR_NUMBER: ${{ github.event.pull_request.number }}
|
||||||
run: |
|
run: |
|
||||||
echo "Counting words in all .md files..."
|
cd scripts/wordcount
|
||||||
find . -name "*.md" -not -path "*/.*" -exec wc -w {} + | sort -rn
|
go build -o wordcount_bin main.go
|
||||||
|
cd ../..
|
||||||
|
./scripts/wordcount/wordcount_bin
|
||||||
|
|||||||
117
scripts/wordcount/main.go
Normal file
117
scripts/wordcount/main.go
Normal file
@@ -0,0 +1,117 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"code.gitea.io/sdk/gitea"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
token := os.Getenv("GITEA_TOKEN")
|
||||||
|
baseURL := os.Getenv("GITEA_URL")
|
||||||
|
repoFullName := os.Getenv("GITHUB_REPOSITORY")
|
||||||
|
prNumberStr := os.Getenv("PR_NUMBER")
|
||||||
|
|
||||||
|
if token == "" || prNumberStr == "" {
|
||||||
|
log.Fatal("Missing required environment variables: GEMINI_API_KEY, GITEA_TOKEN, GITHUB_REPOSITORY, PR_NUMBER")
|
||||||
|
}
|
||||||
|
|
||||||
|
if baseURL == "" {
|
||||||
|
baseURL = "https://gitea.com"
|
||||||
|
}
|
||||||
|
|
||||||
|
prNumber, err := strconv.ParseInt(prNumberStr, 10, 64)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Invalid PR_NUMBER: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
repoParts := strings.Split(repoFullName, "/")
|
||||||
|
if len(repoParts) != 2 {
|
||||||
|
log.Fatalf("Invalid GITHUB_REPOSITORY format: %s", repoFullName)
|
||||||
|
}
|
||||||
|
owner, repo := repoParts[0], repoParts[1]
|
||||||
|
|
||||||
|
client, err := gitea.NewClient(baseURL, gitea.SetToken(token))
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to create Gitea client: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get PR files
|
||||||
|
files, _, err := client.ListPullRequestFiles(owner, repo, prNumber, gitea.ListPullRequestFilesOptions{})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to get PR files: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var counts []string
|
||||||
|
for _, file := range files {
|
||||||
|
if !strings.HasSuffix(file.Filename, ".md") {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("Reviewing file: %s\n", file.Filename)
|
||||||
|
content, err := readFile(file.Filename)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error reading file %s: %v\n", file.Filename, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
count, err := countWordsInFile(content)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("Error getting review for %s: %v\n", file.Filename, err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
counts = append(counts, fmt.Sprintf("#### Review for `%s`\n\n%s", file.Filename, count))
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(counts) > 0 {
|
||||||
|
commentBody := "### 🤖 Gemini Writing Review\n\n" + strings.Join(counts, "\n\n---\n\n")
|
||||||
|
_, _, err = client.CreateIssueComment(owner, repo, prNumber, gitea.CreateIssueCommentOption{
|
||||||
|
Body: commentBody,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("Failed to post PR comment: %v", err)
|
||||||
|
}
|
||||||
|
fmt.Println("Successfully posted review comments.")
|
||||||
|
} else {
|
||||||
|
fmt.Println("No Markdown files to review or no suggestions found.")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func readFile(path string) (string, error) {
|
||||||
|
f, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
|
||||||
|
content, err := io.ReadAll(f)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(content), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func countWordsInFile(path string) (int, error) {
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
content, err := io.ReadAll(file)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return countWords(string(content)), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func countWords(text string) int {
|
||||||
|
return len(strings.Fields(text))
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user