Skip to content
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 - Support Token File for Git Commands #5067

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 21 additions & 2 deletions server/events/event_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"encoding/json"
"fmt"
"net/url"
"os"
"path"
"strings"

Expand Down Expand Up @@ -357,6 +358,7 @@ type EventParsing interface {
type EventParser struct {
GithubUser string
GithubToken string
GithubTokenFile string
GitlabUser string
GitlabToken string
GiteaUser string
Expand All @@ -372,7 +374,15 @@ type EventParser struct {
func (e *EventParser) ParseAPIPlanRequest(vcsHostType models.VCSHostType, repoFullName string, cloneURL string) (models.Repo, error) {
switch vcsHostType {
case models.Github:
return models.NewRepo(vcsHostType, repoFullName, cloneURL, e.GithubUser, e.GithubToken)
token := e.GithubToken
if e.GithubTokenFile != "" {
content, err := os.ReadFile(e.GithubTokenFile)
if err != nil {
return models.Repo{}, fmt.Errorf("failed reading github token file: %w", err)
}
token = string(content)
}
return models.NewRepo(vcsHostType, repoFullName, cloneURL, e.GithubUser, token)
case models.Gitea:
return models.NewRepo(vcsHostType, repoFullName, cloneURL, e.GiteaUser, e.GiteaToken)
case models.Gitlab:
Expand Down Expand Up @@ -626,7 +636,16 @@ func (e *EventParser) ParseGithubPull(logger logging.SimpleLogging, pull *github
// returns a repo into the Atlantis model.
// See EventParsing for return value docs.
func (e *EventParser) ParseGithubRepo(ghRepo *github.Repository) (models.Repo, error) {
return models.NewRepo(models.Github, ghRepo.GetFullName(), ghRepo.GetCloneURL(), e.GithubUser, e.GithubToken)
token := e.GithubToken
if e.GithubTokenFile != "" {
content, err := os.ReadFile(e.GithubTokenFile)
if err != nil {
return models.Repo{}, fmt.Errorf("failed reading github token file: %w", err)
}
token = string(content)
}

return models.NewRepo(models.Github, ghRepo.GetFullName(), ghRepo.GetCloneURL(), e.GithubUser, token)
}

// ParseGiteaRepo parses the response from the Gitea API endpoint that
Expand Down
1 change: 1 addition & 0 deletions server/events/event_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
var parser = events.EventParser{
GithubUser: "github-user",
GithubToken: "github-token",
GithubTokenFile: "",
GitlabUser: "gitlab-user",
GitlabToken: "gitlab-token",
AllowDraftPRs: false,
Expand Down
1 change: 1 addition & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ func NewServer(userConfig UserConfig, config Config) (*Server, error) {
eventParser := &events.EventParser{
GithubUser: userConfig.GithubUser,
GithubToken: userConfig.GithubToken,
GithubTokenFile: userConfig.GithubTokenFile,
GitlabUser: userConfig.GitlabUser,
GitlabToken: userConfig.GitlabToken,
GiteaUser: userConfig.GiteaUser,
Expand Down
Loading