Skip to content

Commit

Permalink
Merge pull request #6 from xbmlz/dev
Browse files Browse the repository at this point in the history
feat: 增加代理支持
  • Loading branch information
xbmlz authored Jun 22, 2022
2 parents badb50b + da94bfa commit 62b9567
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ docker run \
--restart=always \
-p 9010:9010 \
xbmlz/sonar-dingtalk-plugin

# 使用代理
docker run \
-d \
--name=sonar-dingtalk-plugin \
--restart=always \
-p 9010:9010 \
-e HTTPS_PROXY=http://username:password@ip:port \
xbmlz/sonar-dingtalk-plugin
```

#### 二进制安装
Expand Down
22 changes: 12 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,32 @@ import (
"net/http"
)

var httpClient = &http.Client{}

// dingtalkHandler
func dingtalkHandler(w http.ResponseWriter, r *http.Request) {
r.ParseForm()
sonarRsp := make(map[string]interface{})
accessToken := r.Form.Get("access_token")
sonarToken := r.Form.Get("sonar_token")
if accessToken == "" {
fmt.Fprintf(w, "access_token不能为空")
return
}
sonarToken := r.Form.Get("sonar_token")
if err := json.NewDecoder(r.Body).Decode(&sonarRsp); err != nil {
r.Body.Close()
log.Fatal(err)
fmt.Fprintf(w, "解析Sonar参数错误")
fmt.Fprintf(w, "解析Sonar参数错误:"+err.Error())
return
}

serverUrl := sonarRsp["serverUrl"]
projectName := sonarRsp["project"].(map[string]interface{})["name"]
projectKey := sonarRsp["project"].(map[string]interface{})["key"]
branch := sonarRsp["branch"].(map[string]interface{})["name"]

// create http client
httpClient := &http.Client{
Transport: &http.Transport{
// 设置代理 HTTPS_PROXY
Proxy: http.ProxyFromEnvironment,
},
}
// get measures info
url := fmt.Sprintf("%s/api/measures/search?projectKeys=%s&metricKeys=alert_status,bugs,reliability_rating,vulnerabilities,security_rating,code_smells,sqale_rating,duplicated_lines_density,coverage,ncloc,ncloc_language_distribution",
serverUrl, projectKey)
Expand All @@ -41,14 +43,14 @@ func dingtalkHandler(w http.ResponseWriter, r *http.Request) {
}
measuresRsp, err := httpClient.Do(req)
if err != nil {
fmt.Fprintf(w, "获取measures失败")
fmt.Fprintf(w, "获取measures失败: "+err.Error())
return
}
measuresObj := make(map[string]interface{})
if err := json.NewDecoder(measuresRsp.Body).Decode(&measuresObj); err != nil {
measuresRsp.Body.Close()
log.Fatal(err)
fmt.Fprintf(w, "解析Measures失败")
fmt.Fprintf(w, "解析Measures失败: "+err.Error())
return
}

measures := measuresObj["measures"].([]interface{})
Expand Down

0 comments on commit 62b9567

Please sign in to comment.