-
Notifications
You must be signed in to change notification settings - Fork 0
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
fix: github token getter #205
Conversation
load GitHub token from env var.
Based on the pull request title and diff, it seems that the key change in this pull request is to fix the GitHub token getter in the One potential problem with this change is that it assumes that the To address this issue, you could consider adding some additional error handling to the Overall, this seems like a reasonable change to the code and should improve its reliability and security. |
Based on the pull request's title, it seems that the change only involves fixing the GitHub token getter. However, the PR description is empty, so it's hard to tell what prompted this change. The diff shows that the previous hard-coded GitHub token was removed, and a new function However, there are some potential problems with this pull request:
To fix these problems, the code should be updated as follows: diff --git a/change-insight/lib/github/common.go b/change-insight/lib/github/common.go
index 4b60eb8..e4d31e9 100644
--- a/change-insight/lib/github/common.go
+++ b/change-insight/lib/github/common.go
@@ -3,12 +3,14 @@ package github
import (
"log"
"net/http"
+ "os"
+ "fmt"
)
type Repo struct {
Org string // eg: pingcap
Repo string // eg: tidb
}
-func getGitHubToken() string
+func getGitHubToken() string {
return os.Getenv("GITHUB_TOKEN")
}
@@ -25,17 +27,32 @@ type labelInfo struct {
Description string `json:"description"`
}
-var token string = "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"
var apiURL string = "https://api.github.com/repos/"
func apiCall(apiUrl string) (*http.Response, error) {
req, err := http.NewRequest("GET", apiUrl, nil)
if err != nil {
- log.Printf("request init error: %s\n", err.Error())
+ log.Printf("request init error: %s\n", err)
return nil, err
}
req.Header.Set("Accept", "application/vnd.github+json")
- req.Header.Set("Authorization", "token "+token)
+ token := getGitHubToken()
+ if token == "" {
+ return nil, fmt.Errorf("GITHUB_TOKEN environment variable not set")
+ }
+ req.Header.Set("Authorization", "token " + token)
+
+ resp, err := http.DefaultClient.Do(req)
+ if err != nil {
+ log.Printf("request error: %s\n", err)
+ return nil, err
+ }
+
+ if resp.StatusCode != http.StatusOK {
+ log.Printf("unexpected status code: %d\n", resp.StatusCode)
+ return resp, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
+ }
+
+ return resp, nil
} In summary, the pull request is a good change that removes a hard-coded secret from the code. However, it needs some fixes to compile and handle errors correctly. |
Based on the pull request title and diff, it seems that the change is related to the GitHub API token. The As for potential problems, there are a couple of things to note:
To fix these issues, I would suggest modifying the Here is an example of how the func getGitHubToken() (string, error) {
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
return "", fmt.Errorf("GITHUB_TOKEN environment variable is not set")
}
return token, nil
} And here is an example of how the func apiCall(apiUrl string) (*http.Response, error) {
token, err := getGitHubToken()
if err != nil {
log.Printf("error getting GitHub token: %s\n", err.Error())
return nil, err
}
req, err := http.NewRequest("GET", apiUrl, nil)
if err != nil {
log.Printf("request init error: %s\n", err.Error())
return nil, err
}
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("Authorization", "token "+token)
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Printf("request error: %s\n", err.Error())
return nil, err
}
if resp.StatusCode != http.StatusOK {
log.Printf("unexpected status code: %d\n", resp.StatusCode)
return resp, fmt.Errorf("unexpected status code: %d", resp.StatusCode)
}
return resp, nil
} |
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: wuhuizuo The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
This pull request includes several changes to the
change-insight/lib/github/common.go
file to enhance security and improve error handling. The most important changes include the addition of environment variable support for GitHub tokens and improved error handling for API calls.Enhancements to security:
change-insight/lib/github/common.go
: Replaced the hardcoded GitHub token with a function that retrieves the token from the environment variableGITHUB_TOKEN
.Improvements to error handling:
change-insight/lib/github/common.go
: Added error handling for unexpected status codes in theapiCall
function, logging the status code and returning an error if it is nothttp.StatusOK
.Currently, the sub app that changed in this PR is planed to be deprecated.