Skip to content
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
24 changes: 12 additions & 12 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

- name: Setup Go
uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5 # v6.2.0
uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
with:
go-version: '1.25'
go-version: '1.27'

- name: Run go vet
run: go vet ./...
Expand All @@ -39,12 +39,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

- name: Setup Go
uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5 # v6.2.0
uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
with:
go-version: '1.25'
go-version: '1.27'

- name: Run unit tests
run: make test-unit
Expand All @@ -53,12 +53,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

- name: Setup Go
uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5 # v6.2.0
uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
with:
go-version: '1.25'
go-version: '1.27'

- name: Run integration tests
run: make test-integration
Expand All @@ -67,12 +67,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

- name: Setup Go
uses: actions/setup-go@7a3fe6cf4cb3a834922a1244abfce67bcef6a0c5 # v6.2.0
uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0
with:
go-version: '1.25'
go-version: '1.27'

- name: Build
run: make build
11 changes: 8 additions & 3 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@ on:
permissions:
contents: write

concurrency:
group: release-${{ github.ref }}
cancel-in-progress: false

jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Bump Version
id: tag_version
uses: mathieudutour/github-tag-action@a22cf08638b34d5badda920f9daf6e72c477b07b # v6.2.0
uses: mathieudutour/github-tag-action@af99e60ce8132224b8e6ebab5023449fe256ed46 # v7.0.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
default_bump: minor
custom_release_rules: bug:patch:Fixes,chore:patch:Chores,docs:patch:Documentation,feat:minor:Features,refactor:minor:Refactors,test:patch:Tests,ci:patch:Development,dev:patch:Development
- name: Create Release
uses: ncipollo/release-action@b7eabc95ff50cbeeedec83973935c8f306dfcd0b # v1.20.0
if: steps.tag_version.outputs.new_tag != ''
uses: ncipollo/release-action@339a81892b84b4eeb0f6e744e4574d79d0d9b8dd # v1.21.0
with:
tag: ${{ steps.tag_version.outputs.new_tag }}
name: ${{ steps.tag_version.outputs.new_tag }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/semantic-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- uses: amannn/action-semantic-pull-request@48f256284bd46cdaab1048c3721360e808335d50 # v6.1.1
name: Check PR for Semantic Commit Message
env:
Expand Down
43 changes: 27 additions & 16 deletions configstore/aws_ssm_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,36 +119,47 @@ func (s *AWSSSMStore) Save(ctx context.Context, creds *AppCredentials) error {
}

// putParameter creates or updates a single SSM parameter.
//
// SSM rejects PutParameter requests that set both Tags and Overwrite, so when
// tags are configured the parameter is first created with tags (no overwrite)
// and, if it already exists, updated in place without tags.
func (s *AWSSSMStore) putParameter(ctx context.Context, name, value string) error {
input := &ssm.PutParameterInput{
Name: aws.String(s.ParameterPrefix + name),
Value: aws.String(value),
Type: types.ParameterTypeSecureString,
Overwrite: aws.Bool(true),
DataType: aws.String("text"),
Name: aws.String(s.ParameterPrefix + name),
Value: aws.String(value),
Type: types.ParameterTypeSecureString,
DataType: aws.String("text"),
}

if s.KMSKeyID != "" {
input.KeyId = aws.String(s.KMSKeyID)
}

if len(s.Tags) > 0 {
var tags []types.Tag
for key, value := range s.Tags {
tags = append(tags, types.Tag{
Key: aws.String(key),
Value: aws.String(value),
})
}
input.Tags = tags
if len(s.Tags) == 0 {
input.Overwrite = aws.Bool(true)
_, err := s.ssmClient.PutParameter(ctx, input)
return err
}

tags := make([]types.Tag, 0, len(s.Tags))
for key, value := range s.Tags {
tags = append(tags, types.Tag{
Key: aws.String(key),
Value: aws.String(value),
})
}
input.Tags = tags

_, err := s.ssmClient.PutParameter(ctx, input)
if err != nil {
var alreadyExists *types.ParameterAlreadyExists
if !errors.As(err, &alreadyExists) {
return err
}

return nil
input.Tags = nil
input.Overwrite = aws.Bool(true)
_, err = s.ssmClient.PutParameter(ctx, input)
return err
}

// Status returns the current registration state by checking required SSM parameters.
Expand Down
25 changes: 24 additions & 1 deletion configstore/aws_ssm_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ func (m *mockSSMClient) PutParameter(ctx context.Context, params *ssm.PutParamet
if m.putErr != nil {
return nil, m.putErr
}
// Mirror SSM validation: tags and overwrite are mutually exclusive, and
// existing parameters are only replaced when overwrite is set.
overwrite := aws.ToBool(params.Overwrite)
if overwrite && len(params.Tags) > 0 {
return nil, fmt.Errorf("ValidationException: tags and overwrite can't be used together")
}
if _, exists := m.parameters[*params.Name]; exists && !overwrite {
return nil, &types.ParameterAlreadyExists{}
}
m.parameters[*params.Name] = *params.Value
return &ssm.PutParameterOutput{}, nil
}
Expand Down Expand Up @@ -221,12 +230,26 @@ func TestAWSSSMStore_Save_WithTags(t *testing.T) {
t.Fatalf("Save() error = %v", err)
}

// Verify tags were applied to all parameters
// Verify tags were applied to all newly created parameters
for _, call := range mock.putCalls {
if len(call.Tags) != 2 {
t.Errorf("Parameter %q has %d tags, want 2", *call.Name, len(call.Tags))
}
}

// Saving again must update existing parameters even though tags are set.
creds.ClientSecret = "rotated"
mock.putCalls = nil
if err := store.Save(context.Background(), creds); err != nil {
t.Fatalf("second Save() error = %v", err)
}
if got := mock.parameters["/prefix/"+EnvGitHubClientSecret]; got != "rotated" {
t.Errorf("client secret = %q, want %q", got, "rotated")
}

if err := store.DisableInstaller(context.Background()); err != nil {
t.Fatalf("DisableInstaller() error = %v", err)
}
}

func TestAWSSSMStore_Save_OmitsEmptyOptionalFields(t *testing.T) {
Expand Down
49 changes: 23 additions & 26 deletions configstore/local_env_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"bufio"
"context"
"fmt"
"maps"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
)
Expand All @@ -32,35 +34,33 @@ func (s *LocalEnvFileStore) Save(ctx context.Context, creds *AppCredentials) err
return fmt.Errorf("failed to create directory %s: %w", dir, err)
}

existingValues, originalLines, err := parseEnvFile(s.FilePath)
_, originalLines, err := parseEnvFile(s.FilePath)
if err != nil && !os.IsNotExist(err) {
return fmt.Errorf("failed to read existing .env file: %w", err)
}
if existingValues == nil {
existingValues = make(map[string]string)
}

updates := make(map[string]string)
for key, value := range creds.CustomFields {
if value != "" {
existingValues[key] = value
updates[key] = value
}
}

singleLinePEM := strings.ReplaceAll(creds.PrivateKey, "\n", "\\n")

existingValues[EnvGitHubAppID] = fmt.Sprintf("%d", creds.AppID)
existingValues[EnvGitHubWebhookSecret] = creds.WebhookSecret
existingValues[EnvGitHubClientID] = creds.ClientID
existingValues[EnvGitHubClientSecret] = creds.ClientSecret
existingValues[EnvGitHubAppPrivateKey] = singleLinePEM
updates[EnvGitHubAppID] = fmt.Sprintf("%d", creds.AppID)
updates[EnvGitHubWebhookSecret] = creds.WebhookSecret
updates[EnvGitHubClientID] = creds.ClientID
updates[EnvGitHubClientSecret] = creds.ClientSecret
updates[EnvGitHubAppPrivateKey] = singleLinePEM
if creds.AppSlug != "" {
existingValues[EnvGitHubAppSlug] = creds.AppSlug
updates[EnvGitHubAppSlug] = creds.AppSlug
}
if creds.HTMLURL != "" {
existingValues[EnvGitHubAppHTMLURL] = creds.HTMLURL
updates[EnvGitHubAppHTMLURL] = creds.HTMLURL
}

if err := writeEnvFile(s.FilePath, existingValues, originalLines); err != nil {
if err := writeEnvFile(s.FilePath, updates, originalLines); err != nil {
return fmt.Errorf("failed to write .env file: %w", err)
}

Expand Down Expand Up @@ -131,6 +131,9 @@ func parseEnvFile(path string) (map[string]string, []string, error) {
return values, lines, nil
}

// writeEnvFile rewrites only the lines whose keys appear in values and appends
// any keys not already present. All other lines are preserved verbatim so that
// quoting, inline comments, and formatting of unrelated entries are untouched.
func writeEnvFile(path string, values map[string]string, originalLines []string) error {
var outputLines []string
writtenKeys := make(map[string]bool)
Expand Down Expand Up @@ -159,9 +162,9 @@ func writeEnvFile(path string, values map[string]string, originalLines []string)
}
}

for key, value := range values {
for _, key := range slices.Sorted(maps.Keys(values)) {
if !writtenKeys[key] {
outputLines = append(outputLines, formatEnvLine(key, value))
outputLines = append(outputLines, formatEnvLine(key, values[key]))
}
}

Expand Down Expand Up @@ -225,19 +228,13 @@ func (s *LocalEnvFileStore) DisableInstaller(ctx context.Context) error {
return fmt.Errorf("failed to create directory %s: %w", dir, err)
}

values, originalLines, err := parseEnvFile(s.FilePath)
if err != nil {
if !os.IsNotExist(err) {
return err
}
}
if values == nil {
values = make(map[string]string)
_, originalLines, err := parseEnvFile(s.FilePath)
if err != nil && !os.IsNotExist(err) {
return err
}

values[EnvGitHubAppInstallerEnabled] = "false"

if err := writeEnvFile(s.FilePath, values, originalLines); err != nil {
updates := map[string]string{EnvGitHubAppInstallerEnabled: "false"}
if err := writeEnvFile(s.FilePath, updates, originalLines); err != nil {
return fmt.Errorf("failed to persist installer flag: %w", err)
}

Expand Down
46 changes: 46 additions & 0 deletions configstore/local_env_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,49 @@ MIIEowIBAAKCAQEA0Z3VS5JJcds3xfn/ygWyF8PbnGy0AHB7MhgHW1FZ
t.Error("Custom field ANOTHER_FIELD was not saved")
}
}

func TestLocalEnvFileStore_PreservesUnrelatedLines(t *testing.T) {
tmpDir := t.TempDir()
envPath := filepath.Join(tmpDir, ".env")

unrelated := []string{
`GREETING="hello \"world\""`,
`PORT=8080 # http port`,
`HOME_REF='$HOME'`,
}
initial := strings.Join(unrelated, "\n") + "\n"
if err := os.WriteFile(envPath, []byte(initial), 0600); err != nil {
t.Fatalf("failed to write initial .env: %v", err)
}

store := NewLocalEnvFileStore(envPath)
creds := &AppCredentials{
AppID: 1,
ClientID: "client",
ClientSecret: "secret",
WebhookSecret: "webhook",
PrivateKey: "key",
}

// Save repeatedly and disable the installer; unrelated lines must survive
// byte-for-byte rather than being re-quoted on every write.
for i := 0; i < 2; i++ {
if err := store.Save(context.Background(), creds); err != nil {
t.Fatalf("Save() error = %v", err)
}
}
if err := store.DisableInstaller(context.Background()); err != nil {
t.Fatalf("DisableInstaller() error = %v", err)
}

content, err := os.ReadFile(envPath)
if err != nil {
t.Fatalf("failed to read .env: %v", err)
}
lines := strings.Split(string(content), "\n")
for i, want := range unrelated {
if lines[i] != want {
t.Errorf("line %d = %q, want %q", i+1, lines[i], want)
}
}
}
Loading
Loading