Skip to content

Commit

Permalink
fix: issue with GH App credential not writing if lines already exist (#…
Browse files Browse the repository at this point in the history
…3679)

* Fix issue with GH App credential not writing if lines already exist

* Fix lint issue of unused variable.

---------

Co-authored-by: PePe Amengual <[email protected]>
Co-authored-by: Dylan Page <[email protected]>
  • Loading branch information
3 people authored Sep 25, 2023
1 parent 078af70 commit 352bbed
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
34 changes: 30 additions & 4 deletions server/events/vcs/git_cred_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,23 @@ func WriteGitCreds(gitUser string, gitToken string, gitHostname string, home str
}

if ghAccessToken {
// Need to replace the line.
if err := fileLineReplace(config, gitUser, gitHostname, credsFile); err != nil {
return errors.Wrap(err, "replacing git credentials line for github app")
hasGHToken, err := fileHasGHToken(gitUser, gitHostname, credsFile)
if err != nil {
return err
}
if hasGHToken {
// Need to replace the line.
if err := fileLineReplace(config, gitUser, gitHostname, credsFile); err != nil {
return errors.Wrap(err, "replacing git credentials line for github app")
}
logger.Info("updated git app credentials in %s", credsFile)
} else {
if err := fileAppend(config, credsFile); err != nil {
return err
}
logger.Info("wrote git credentials to %s", credsFile)
}
logger.Info("updated git app credentials in %s", credsFile)

} else {
// Otherwise we need to append the line.
if err := fileAppend(config, credsFile); err != nil {
Expand Down Expand Up @@ -113,3 +125,17 @@ func fileLineReplace(line, user, host, filename string) error {

return os.WriteFile(filename, []byte(toWrite), 0600)
}

func fileHasGHToken(user, host, filename string) (bool, error) {
currContents, err := os.ReadFile(filename) // nolint: gosec
if err != nil {
return false, err
}
prevLines := strings.Split(string(currContents), "\n")
for _, l := range prevLines {
if strings.HasPrefix(l, "https://"+user) && strings.HasSuffix(l, host) {
return true, nil
}
}
return false, nil
}
19 changes: 19 additions & 0 deletions server/events/vcs/git_cred_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ func TestWriteGitCreds_ReplaceApp(t *testing.T) {
Equals(t, expContets, string(actContents))
}

// Test that the github app credential gets added even if there are other credentials.
func TestWriteGitCreds_AppendAppWhenFileNotEmpty(t *testing.T) {
logger := logging.NewNoopLogger(t)
tmp := t.TempDir()
t.Setenv("HOME", tmp)

credsFile := filepath.Join(tmp, ".git-credentials")
contents := "line1\nhttps://user:[email protected]\nline2"
err := os.WriteFile(credsFile, []byte(contents), 0600)
Ok(t, err)

err = vcs.WriteGitCreds("x-access-token", "token", "github.com", tmp, logger, true)
Ok(t, err)
expContets := "line1\nhttps://user:[email protected]\nline2\nhttps://x-access-token:[email protected]"
actContents, err := os.ReadFile(filepath.Join(tmp, ".git-credentials"))
Ok(t, err)
Equals(t, expContets, string(actContents))
}

// Test that the github app credentials get updated when cred file is empty.
func TestWriteGitCreds_AppendApp(t *testing.T) {
logger := logging.NewNoopLogger(t)
Expand Down

0 comments on commit 352bbed

Please sign in to comment.