Skip to content

Commit

Permalink
fetch and clone now use git config url to be able to be cloned/fetched
Browse files Browse the repository at this point in the history
by GitHub app installation tokens.
  • Loading branch information
xvandish committed Jul 1, 2024
1 parent b6730f7 commit facf11a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
17 changes: 13 additions & 4 deletions cmd/zoekt-indexserver/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,20 @@ func gitFetchNeededRepos(repoDir, indexDir string, opts *Options, pendingRepos c
log.Fatal(err)
}

log.Printf("set GITHUB_TOKEN with length=%d\n", len(ghToken))
os.Setenv("GITHUB_TOKEN", ghToken)
log.Printf("setting global git config token url replacement len=%d\n", len(ghToken))
gitUrlReplacement := fmt.Sprintf("url.https://x-access-token:%[email protected]/.insteadof", ghToken)
_, err = exec.Command("git", "config", "--global", gitUrlReplacement, "https://github.com/").Output()
if err != nil {
log.Fatal(err)
}

// remove the token once we're done
defer os.Setenv("GITHUB_TOKEN", "")
defer func() {
log.Printf("unsetting global git config token url replacement\n")
_, err := exec.Command("git", "config", "--global", "--unset", gitUrlReplacement, "https://github.com/").Output()
if err != nil {
log.Fatal(err)
}
}()
}

g, _ := errgroup.WithContext(context.Background())
Expand Down
20 changes: 17 additions & 3 deletions cmd/zoekt-mirror-github/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"net/http"
"net/url"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
Expand Down Expand Up @@ -161,9 +162,22 @@ func main() {
log.Fatal(err)
}

log.Printf("set GITHUB_TOKEN with length=%d\n", len(appInstallToken))
os.Setenv("GITHUB_TOKEN", appInstallToken)
defer os.Setenv("GITHUB_TOKEN", "")
// we set this globally rather than just munging the cloneUrl later so that
// the clone URL (that now includes the token) is not logged anywhere
log.Printf("setting global git config token url replacement len=%d\n", len(appInstallToken))
gitUrlReplacement := fmt.Sprintf("url.https://x-access-token:%[email protected]/.insteadof", appInstallToken)
_, err = exec.Command("git", "config", "--global", gitUrlReplacement, "https://github.com/").Output()
if err != nil {
log.Fatal(err)
}

defer func() {
log.Printf("unsetting global git config token url replacement\n")
_, err := exec.Command("git", "config", "--global", "--unset", gitUrlReplacement, "https://github.com/").Output()
if err != nil {
log.Fatal(err)
}
}()
}

reposFilters := reposFilters{
Expand Down

0 comments on commit facf11a

Please sign in to comment.