feat: Major rework
- Refactor bits of code - Remove unused functions `PrintComments` - Introduce configuration file - Implement support for multiple languages Signed-off-by: Matej Focko <mfocko@redhat.com>
This commit is contained in:
parent
49a0221611
commit
36efeb2aab
3 changed files with 98 additions and 46 deletions
9
go.mod
Normal file
9
go.mod
Normal file
|
@ -0,0 +1,9 @@
|
|||
module git.mfocko.xyz/mfocko/frag-review
|
||||
|
||||
go 1.16
|
||||
|
||||
require (
|
||||
code.gitea.io/sdk/gitea v0.14.0 // indirect
|
||||
github.com/bbrks/wrap/v2 v2.5.0 // indirect
|
||||
gopkg.in/yaml.v2 v2.4.0
|
||||
)
|
14
go.sum
Normal file
14
go.sum
Normal file
|
@ -0,0 +1,14 @@
|
|||
code.gitea.io/sdk/gitea v0.14.0 h1:m4J352I3p9+bmJUfS+g0odeQzBY/5OXP91Gv6D4fnJ0=
|
||||
code.gitea.io/sdk/gitea v0.14.0/go.mod h1:89WiyOX1KEcvjP66sRHdu0RafojGo60bT9UqW17VbWs=
|
||||
github.com/bbrks/wrap/v2 v2.5.0 h1:2gn3SiiwgttdyW9CFJz1M/WbDKPsN857x7Era5/oAPI=
|
||||
github.com/bbrks/wrap/v2 v2.5.0/go.mod h1:FdEamYFrsjX8zlv3UXgnT3JxirrDv67jCDYaE0Q/qww=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI=
|
||||
github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
121
main.go
121
main.go
|
@ -8,14 +8,28 @@ import (
|
|||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode"
|
||||
|
||||
"code.gitea.io/sdk/gitea"
|
||||
"github.com/bbrks/wrap"
|
||||
"github.com/bbrks/wrap/v2"
|
||||
"gopkg.in/yaml.v2"
|
||||
)
|
||||
|
||||
const INSTANCE_URL string = "https://git.mfocko.xyz"
|
||||
const REPOSITORY_OWNER string = "mfocko"
|
||||
const REPOSITORY_NAME string = "2021_pb161_reviews"
|
||||
type Config struct {
|
||||
Gitea struct {
|
||||
InstanceURL string `yaml:"instance_url"`
|
||||
Owner string `yaml:"owner"`
|
||||
Repository string `yaml:"repository"`
|
||||
} `yaml:"gitea"`
|
||||
Language struct {
|
||||
OpeningOnSeparateLine bool `yaml:"separate_opening"`
|
||||
Opening string `yaml:"opening"`
|
||||
Continuation string `yaml:"continuation"`
|
||||
Closing string `yaml:"closing"`
|
||||
} `yaml:"language"`
|
||||
}
|
||||
|
||||
var config Config = Config{}
|
||||
|
||||
type ByLineNum []*gitea.PullReviewComment
|
||||
|
||||
|
@ -31,13 +45,17 @@ func ExitOnError(err error) {
|
|||
}
|
||||
|
||||
func RetrieveComments(client *gitea.Client, prID, reviewID int64) []*gitea.PullReviewComment {
|
||||
comments, _, err := client.ListPullReviewComments(REPOSITORY_OWNER, REPOSITORY_NAME, prID, reviewID)
|
||||
comments, _, err := client.ListPullReviewComments(
|
||||
config.Gitea.Owner, config.Gitea.Repository, prID, reviewID,
|
||||
)
|
||||
ExitOnError(err)
|
||||
|
||||
return comments
|
||||
}
|
||||
|
||||
func SplitByFile(splitComments map[string]([]*gitea.PullReviewComment), comments []*gitea.PullReviewComment) map[string]([]*gitea.PullReviewComment) {
|
||||
func SplitByFile(
|
||||
splitComments map[string]([]*gitea.PullReviewComment),
|
||||
comments []*gitea.PullReviewComment,
|
||||
) map[string]([]*gitea.PullReviewComment) {
|
||||
for _, comment := range comments {
|
||||
arr, found := splitComments[comment.Path]
|
||||
|
||||
|
@ -51,24 +69,6 @@ func SplitByFile(splitComments map[string]([]*gitea.PullReviewComment), comments
|
|||
return splitComments
|
||||
}
|
||||
|
||||
func PrintComments(wrapper *wrap.Wrapper, commentsByFile map[string]([]*gitea.PullReviewComment)) {
|
||||
for filepath, comments := range commentsByFile {
|
||||
fmt.Printf("FILE: %s\n", filepath)
|
||||
|
||||
for _, comment := range comments {
|
||||
body := comment.Body
|
||||
|
||||
if len(body) >= 80 {
|
||||
body = "\n" + wrapper.Wrap(body, 80)
|
||||
} else {
|
||||
body = " " + body
|
||||
}
|
||||
|
||||
fmt.Printf("L%04d:\n/**%s **/\n\n", comment.LineNum, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BackUpSource(filepath string) {
|
||||
inputFile, err := os.Open(filepath)
|
||||
ExitOnError(err)
|
||||
|
@ -82,6 +82,23 @@ func BackUpSource(filepath string) {
|
|||
ExitOnError(err)
|
||||
}
|
||||
|
||||
func GetSeparatorForMultiline() string {
|
||||
if config.Language.OpeningOnSeparateLine {
|
||||
return "\n"
|
||||
}
|
||||
return " "
|
||||
}
|
||||
|
||||
func FormatComment(body string, beenWrapped bool) string {
|
||||
if beenWrapped && !config.Language.OpeningOnSeparateLine {
|
||||
body = " " + strings.TrimPrefix(body, " "+config.Language.Continuation)
|
||||
}
|
||||
if !beenWrapped && !config.Language.OpeningOnSeparateLine && !strings.HasSuffix(body, "\n") {
|
||||
body = body + "\n"
|
||||
}
|
||||
return fmt.Sprintf("%s%s%s", config.Language.Opening, body, config.Language.Closing)
|
||||
}
|
||||
|
||||
func PatchFiles(wrapper *wrap.Wrapper, commentsByFile map[string]([]*gitea.PullReviewComment)) {
|
||||
for filepath, comments := range commentsByFile {
|
||||
fmt.Printf("FILE: %s\n", filepath)
|
||||
|
@ -104,39 +121,37 @@ func PatchFiles(wrapper *wrap.Wrapper, commentsByFile map[string]([]*gitea.PullR
|
|||
for (nextComment < commentsLength) && comments[nextComment].LineNum == i {
|
||||
body := comments[nextComment].Body
|
||||
|
||||
if len(body) >= 80 {
|
||||
body = "\n" + wrapper.Wrap(body, 80)
|
||||
beenWrapped := len(body)+len(config.Language.Opening)+len(config.Language.Closing) > 100
|
||||
if beenWrapped {
|
||||
body = GetSeparatorForMultiline() + wrapper.Wrap(body, 100)
|
||||
} else {
|
||||
body = " " + body
|
||||
}
|
||||
|
||||
body = strings.Replace(body, "\r", "", -1)
|
||||
body = strings.Replace(body, " ** \n", " **\n", -1)
|
||||
body = strings.Replace(
|
||||
body,
|
||||
config.Language.Continuation+"\n",
|
||||
strings.TrimRightFunc(config.Language.Continuation, unicode.IsSpace)+"\n",
|
||||
-1,
|
||||
)
|
||||
comment := FormatComment(body, beenWrapped)
|
||||
|
||||
fmt.Fprintf(outputFile, "/**%s **/\n", body)
|
||||
fmt.Fprint(outputFile, comment)
|
||||
fmt.Printf("L%04d:\n%s\n", comments[nextComment].LineNum, comment)
|
||||
nextComment++
|
||||
}
|
||||
|
||||
fmt.Fprintln(outputFile, scanner.Text())
|
||||
i++
|
||||
}
|
||||
|
||||
for _, comment := range comments {
|
||||
body := comment.Body
|
||||
|
||||
if len(body) >= 80 {
|
||||
body = "\n" + wrapper.Wrap(body, 80)
|
||||
} else {
|
||||
body = " " + body
|
||||
}
|
||||
|
||||
fmt.Printf("L%04d:\n/**%s **/\n\n", comment.LineNum, body)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func GetReviewsPerPR(client *gitea.Client, prID int64) []int64 {
|
||||
reviews, _, err := client.ListPullReviews(REPOSITORY_OWNER, REPOSITORY_NAME, prID, gitea.ListPullReviewsOptions{})
|
||||
reviews, _, err := client.ListPullReviews(
|
||||
config.Gitea.Owner, config.Gitea.Repository, prID, gitea.ListPullReviewsOptions{},
|
||||
)
|
||||
ExitOnError(err)
|
||||
|
||||
result := make([]int64, 0)
|
||||
|
@ -147,14 +162,29 @@ func GetReviewsPerPR(client *gitea.Client, prID int64) []int64 {
|
|||
return result
|
||||
}
|
||||
|
||||
func LoadConfig(filename string) {
|
||||
if filename == "" {
|
||||
filename = ".frag_review.yml"
|
||||
}
|
||||
|
||||
configFile, err := os.Open(filename)
|
||||
ExitOnError(err)
|
||||
defer configFile.Close()
|
||||
|
||||
decoder := yaml.NewDecoder(configFile)
|
||||
err = decoder.Decode(&config)
|
||||
ExitOnError(err)
|
||||
}
|
||||
|
||||
func main() {
|
||||
token := os.Getenv("GITEA_TOKEN")
|
||||
LoadConfig(os.Getenv("FRAG_REVIEW_CONFIG"))
|
||||
|
||||
client, err := gitea.NewClient(INSTANCE_URL, gitea.SetToken(token))
|
||||
client, err := gitea.NewClient(config.Gitea.InstanceURL, gitea.SetToken(token))
|
||||
ExitOnError(err)
|
||||
|
||||
if len(os.Args) < 2 {
|
||||
fmt.Fprintf(os.Stderr, "Usage: %s pr-id", os.Args[0])
|
||||
fmt.Fprintf(os.Stderr, "Usage: %s pr-id\n", os.Args[0])
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
|
@ -171,8 +201,7 @@ func main() {
|
|||
}
|
||||
|
||||
wrapper := wrap.NewWrapper()
|
||||
wrapper.OutputLinePrefix = " ** "
|
||||
wrapper.OutputLinePrefix = config.Language.Continuation
|
||||
|
||||
// PrintComments(&wrapper, splitComments)
|
||||
PatchFiles(&wrapper, splitComments)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue