-
Notifications
You must be signed in to change notification settings - Fork 61
feat: support write the report to a HTTP server #367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support write the report to a HTTP server #367
Conversation
我看了SonarCloud Code Analysis check失败的原因,一个是测试代码pwd疑似硬编码,一个是测试代码中代码重复率较高,不知道这两点需要改吗 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pkg/runner/writer_http.go
Outdated
if w.templateFile == "" { | ||
// 使用默认模板将数据序列化为 JSON 格式 | ||
payload, err := json.Marshal(result) | ||
if err != nil { | ||
log.Fatal("Error marshaling JSON:", err) | ||
return err | ||
} | ||
req, err = http.NewRequest(w.requestMethod, url, bytes.NewBuffer(payload)) | ||
if err != nil { | ||
log.Println("error when NewRequest", err) | ||
return err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这段逻辑基本是对着的,但是下面的部分有点问题。模板文件应该是特定格式的文件,例如:JSON、YAML 等,这也是常见的 Payload 格式;另外,还需要添加响应的 Content-type
Header 描述 Payload 的格式。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我理解是,要提供一下JSON、XML、YAML等常见payload格式供调用者选择,并正确设置Content-type Header ?而不是像我这里这样直接就使用json了
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不需要提供那么多的选择,你搜索下 Go Template,只需要提供一个设置模板的参数即可。也就是你去调用 Go Template 去把模板渲染下作为 HTTP 的请求体(也就是 Payload )。至于格式的话,感觉可以写死 JSON(毕竟大部分 API 都才用这种格式),后续有其他诉求了再增加不同格式的支持也可以。
pkg/runner/writer_http.go
Outdated
func (w *httpResultWriter) getTemplateFileRequest(url string) (req *http.Request, err error) { | ||
|
||
// 打开要发送的文件 | ||
file, err := os.Open(w.templateFile) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
对于默认的模板,你可以采用 Golang Embed 的方式来获取模板文件内容,不需要再读取文件了。当然,如果用户传递的是一个本地文件,可以采用这个方式。
如果你想做的更加方便一点,甚至可以从 URL 里读取一个模板文件。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我本来理解是调用者传递一个本地文件给remote server
是需要再加上识别该文件如果是go模板文件则使用Golang Embed 的方式来获取模板文件内容来进行优化吗?如果是其他格式的文件则维持原逻辑?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Golang Embed 是为了读取内置的模板。统一用 Go Template 来替代 json.Marshal(result)
,这样可扩展性就很好。
当业务逻辑代码 review 通过后,我们可以综合考虑来看是否要忽略 Sonar 给出的建议。你可以参考 Sonar 给出的建议来试着完善、优化代码。通过 ChatGPT 或类似产品,也可以帮忙优化代码。一般来说,如果单元测试难度很大可以降低覆盖率。 |
a175594
to
bf2e023
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pkg/runner/writer_http.go
Outdated
|
||
func (w *httpResultWriter) getTemplateFileRequest(url string, result []ReportResult) (req *http.Request, err error) { | ||
|
||
tmplData, err := content.ReadFile("writer_templates/" + w.templateFile.filename) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
用户传过来的模板文件就没有办法通过 Embed 的方式来读取了,Embed 会把相应的文件在构建的时候一并放在二进制文件中。
pkg/runner/writer_http.go
Outdated
payload, err := json.Marshal(result) | ||
if err != nil { | ||
log.Fatal("Error marshaling JSON:", err) | ||
return err | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
按照我的理解,你可以在这里读取 Embed 的 JSON 格式的模板文件,并渲染。因此,判断的逻辑可以是如果用户给了模板文件,就从本地文件中读取,如果没有,就从 Embed 中读取。然后,所有的情况都通过 Go Template 来渲染。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果用户没有给模板文件,就从embed里面读取我自己事先编好的模板文件吗?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是的,这样流程、逻辑比较简单和统一
url以及请求类型是post、get还是别的类型以及请求参数是不是也需要用户指定呀 |
cmd/run.go
Outdated
@@ -153,6 +153,9 @@ func (o *runOption) preRunE(cmd *cobra.Command, args []string) (err error) { | |||
case "github": | |||
o.githubReportOption.ReportFile = o.reportFile | |||
o.reportWriter, err = runner.NewGithubPRCommentWriter(o.githubReportOption) | |||
case "http": | |||
templateOption := runner.NewTemplateOption("./pkg/runner/writer_templates/example.tpl", "json") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NewTemplateOption
的第一个参数应该是用户给的,你可以添加一个如下参数:
--report-template
而且,默认值是空的。
cmd/run.go
Outdated
@@ -153,6 +153,9 @@ func (o *runOption) preRunE(cmd *cobra.Command, args []string) (err error) { | |||
case "github": | |||
o.githubReportOption.ReportFile = o.reportFile | |||
o.reportWriter, err = runner.NewGithubPRCommentWriter(o.githubReportOption) | |||
case "http": | |||
templateOption := runner.NewTemplateOption("./pkg/runner/writer_templates/example.tpl", "json") | |||
o.reportWriter = runner.NewHTTPResultWriter("POST", "http://localhost:8080", nil, templateOption) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
你可以使用这个常量:http.MethodPost
替换 POST
http://localhost:8080
不能写死,需要用户传递,参数可以是 --report-dest
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
嗯,是的
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
其实不用生成一个文件,把用户传递的模板文件在内存中渲染了,直接作为 HTTP Payload 发出去就可以了,并不需要持久化的。另外,JSON 是非常流行的、主流的格式,个人感觉目前可以只支持 JSON 格式。
pkg/runner/writer_http.go
Outdated
url = fmt.Sprintf("%s&%s=%s", url, key, value) | ||
} | ||
} | ||
log.Println(url) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could provide more context. For example: log.Println("will send report to:" + url)
pkg/runner/writer_http.go
Outdated
var tmpl *template.Template | ||
if w.templateFile == nil { | ||
// use the default template file to serialize the data to JSON format | ||
tmpl, err = template.New("example template").Parse(exampleTemplate) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please give it a meaning full name. For example: template.New("HTTP report template")
pkg/runner/writer_http.go
Outdated
} else { | ||
content, err := os.ReadFile(w.templateFile.filename) | ||
if err != nil { | ||
fmt.Println("Error reading file:", err) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use log
instead of fmt.Println
pkg/runner/writer_http.go
Outdated
return err | ||
} | ||
|
||
tmpl, err = template.New("mytemplate").Parse(string(content)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use a meaning full name.
pkg/runner/writer_http.go
Outdated
} | ||
|
||
//go:embed writer_templates/example.tpl | ||
var exampleTemplate string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about change it to defaultTemplate
.
按照comment进行了相应的修改,后续如果没有更改我再合并一下commit |
This is required as well. |
dabbf93
to
afc65d4
Compare
afc65d4
to
6dc057a
Compare
6dc057a
to
f2a0dab
Compare
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job. Thank you for your patience and understanding.
So happly to have as the 16th contributor. |
* chore(deps): update jenkins/jenkins docker tag to v2.420 * Update app version --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: github-action update-app-version <githubaction@githubaction.com>
No description provided.