Skip to content

Commit

Permalink
[ACC-1177] Stop using GITHUB_TOKEN (#297)
Browse files Browse the repository at this point in the history
* Get head SHA using git ls-remote
* Remove method that uses GITHUB_TOKEN. Remove GITHUB_TOKEN env check
  • Loading branch information
Errol Cheong authored Jun 3, 2021
1 parent 8c69371 commit a783632
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 91 deletions.
2 changes: 0 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,6 @@ update:
return errors.Wrap(err, "failed to create lazydocker config file")
}

git.CheckGithubAPIToken()

// THE REGISTRY ZONE

if len(tbrc.Registries) == 0 {
Expand Down
19 changes: 19 additions & 0 deletions git/git.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package git

import (
"bytes"
"fmt"
"path/filepath"

Expand All @@ -27,6 +28,24 @@ func Pull(repo, repoDir string) error {
return nil
}

func GetBranchHeadSha(repo, branch string) (string, error) {
repoUrl := "git@github.com:" + repo + ".git"
w := log.WithField("id", "git-ls-remote").WriterLevel(log.DebugLevel)
defer w.Close()
stdout := new(bytes.Buffer)
cmd := command.New(command.WithStdout(stdout), command.WithStderr(w))
err := cmd.Exec("git", "ls-remote", repoUrl, branch)
if err != nil {
return "", errors.Wrapf(err, "failed to get %s head sha of %s", branch, repo)
}

result := stdout.String()
if len(result) < 40 {
return "", errors.Errorf("ls-remote sha too short from %s of %s", branch, repo)
}
return result[0:40], nil
}

func execGit(id string, args ...string) error {
w := log.WithField("id", id).WriterLevel(log.DebugLevel)
defer w.Close()
Expand Down
89 changes: 0 additions & 89 deletions git/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,103 +5,14 @@ import (
"fmt"
"io/ioutil"
"net/http"
"os"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

const (
apiURL = "https://api.github.com"
githubTokenVarName = "GITHUB_TOKEN"
)

// CheckGithubAPIToken will check if the GITHUB_TOKEN env var is set.
// It also supports HOMEBREW_GITHUB_API_TOKEN but this is deprecated.
func CheckGithubAPIToken() {
log.Debugf("Checking if %s is set", githubTokenVarName)
token := os.Getenv(githubTokenVarName)
if token != "" {
log.Debugf("%s is set", githubTokenVarName)
return
}

// HOMEBREW_GITHUB_API_TOKEN is supported for backwards compatibility
// but it's deprecated
const varName = "HOMEBREW_GITHUB_API_TOKEN"
token = os.Getenv(varName)
if token == "" {
return
}

log.Warnf("Using %s is deprecated. Please use %s instead.", varName, githubTokenVarName)
os.Setenv(githubTokenVarName, token)
}

type getBranchResponse struct {
Commit struct {
Sha string `json:"sha"`
} `json:"commit"`
}

func GetBranchHeadSha(repo, branch string) (string, error) {
url := fmt.Sprintf("%s/repos/%s/branches/%s", apiURL, repo, branch)
client := &http.Client{}

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return "", errors.Wrap(err, "Failed to create GET request to GitHub API")
}

// Add auth token if it's set
hasAuth := false
if tokenVal := os.Getenv(githubTokenVarName); tokenVal != "" {
token := fmt.Sprintf("token %s", tokenVal)
req.Header.Add("Authorization", token)
hasAuth = true
}

// Use v3 API
req.Header.Add("Accept", "application/vnd.github.v3+json")

res, err := client.Do(req)
if err != nil {
return "", errors.Wrapf(err, "Unable to complete GET request %s", url)
}

defer res.Body.Close()

body, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", errors.Wrap(err, "Unable to read response body")
}

if res.StatusCode != http.StatusOK {
// GitHub responds with 404 instead of 401 if you don't have access to protect private repos
if res.StatusCode == http.StatusNotFound {
msg := fmt.Sprintf("GitHub repo: %s, branch: %s not found", repo, branch)

// Try to help people out
if !hasAuth {
msg += fmt.Sprintf("\nIf it's a private repo you need to set %s to access it", githubTokenVarName)
}

return "", errors.New(msg)
}

return "", errors.Errorf("Got %d response from GitHub API:\n%s", res.StatusCode, string(body))
}

getBranchResp := getBranchResponse{}
err = json.Unmarshal(body, &getBranchResp)
if err != nil {
return "", errors.Wrap(err, "Failed to parse JSON from reponse body")
}

sha := getBranchResp.Commit.Sha
return sha, nil
}

func GetLatestRelease() (string, error) {
url := fmt.Sprintf("%s/repos/TouchBistro/tb/releases/latest", apiURL)
client := &http.Client{}
Expand Down

0 comments on commit a783632

Please sign in to comment.