• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

sue445 / gitpanda / 14056688419

25 Mar 2025 09:56AM UTC coverage: 70.98%. Remained the same
14056688419

push

github

web-flow
Merge pull request #1721 from sue445/dependabot/github_actions/golangci/golangci-lint-action-7

0 of 3 new or added lines in 3 files covered. (0.0%)

905 of 1275 relevant lines covered (70.98%)

5.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/main_lambda_linux_amd64.go
1
package main
2

3
import (
4
        "context"
5
        "fmt"
6
        "github.com/aws/aws-lambda-go/events"
7
        "github.com/aws/aws-lambda-go/lambda"
8
        "github.com/aws/aws-sdk-go-v2/aws"
9
        "github.com/aws/aws-sdk-go-v2/config"
10
        "github.com/aws/aws-sdk-go-v2/service/ssm"
11
        "github.com/cockroachdb/errors"
12
        "github.com/sue445/gitpanda/gitlab"
13
        "github.com/sue445/gitpanda/webhook"
14
        "net/http"
15
        "os"
16
        "strings"
17
)
18

19
func startLambda() {
×
20
        lambda.Start(lambdaHandler)
×
21
}
×
22

23
func lambdaHandler(_ context.Context, request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
×
24
        body := strings.TrimSpace(request.Body)
×
25

×
26
        if isDebugLogging {
×
27
                fmt.Printf("[DEBUG] lambdaHandler: body=%s\n", body)
×
28
        }
×
29

30
        sentryClient, releaseSentry, err := NewSentryClient(sentryDsn, isDebugLogging)
×
31
        if err != nil {
×
32
                fmt.Printf("Sentry initialization failed: %v\n", err)
×
33
        }
×
34
        defer releaseSentry()
×
35

×
36
        response, err := lambdaMain(body)
×
37

×
38
        if err != nil {
×
39
                sentryClient.CaptureException(err)
×
40

×
41
                fmt.Printf("[ERROR] %s, error=%v\n", response, err)
×
42
                return events.APIGatewayProxyResponse{
×
43
                        StatusCode: http.StatusInternalServerError,
×
44
                        Body:       response,
×
45
                }, errors.WithStack(err)
×
46
        }
×
47

48
        return events.APIGatewayProxyResponse{
×
49
                StatusCode: http.StatusOK,
×
50
                Body:       response,
×
51
        }, nil
×
52
}
53

54
func lambdaMain(body string) (string, error) {
×
55
        slackOAuthAccessToken, err := GetParameterStoreOrEnv("SLACK_OAUTH_ACCESS_TOKEN", os.Getenv("SLACK_OAUTH_ACCESS_TOKEN_KEY"), true)
×
56
        if err != nil {
×
57
                return "Failed: slackOAuthAccessToken", errors.WithStack(err)
×
58
        }
×
59

60
        slackVerificationToken, err := GetParameterStoreOrEnv("SLACK_VERIFICATION_TOKEN", os.Getenv("SLACK_VERIFICATION_TOKEN_KEY"), false)
×
61
        if err != nil {
×
62
                return "Failed: slackVerificationToken", errors.WithStack(err)
×
63
        }
×
64

65
        gitlabAPIEndpoint, err := GetParameterStoreOrEnv("GITLAB_API_ENDPOINT", os.Getenv("GITLAB_API_ENDPOINT_KEY"), true)
×
66
        if err != nil {
×
67
                return "Failed: gitlabAPIEndpoint", errors.WithStack(err)
×
68
        }
×
69

70
        gitlabBaseURL, err := GetParameterStoreOrEnv("GITLAB_BASE_URL", os.Getenv("GITLAB_BASE_URL_KEY"), true)
×
71
        if err != nil {
×
72
                return "Failed: gitlabBaseURL", errors.WithStack(err)
×
73
        }
×
74

75
        gitlabPrivateToken, err := GetParameterStoreOrEnv("GITLAB_PRIVATE_TOKEN", os.Getenv("GITLAB_PRIVATE_TOKEN_KEY"), true)
×
76
        if err != nil {
×
77
                return "Failed: gitlabPrivateToken", errors.WithStack(err)
×
78
        }
×
79

80
        s := webhook.NewSlackWebhook(
×
81
                slackOAuthAccessToken,
×
82
                slackVerificationToken,
×
83
                &gitlab.URLParserParams{
×
84
                        APIEndpoint:     gitlabAPIEndpoint,
×
85
                        BaseURL:         gitlabBaseURL,
×
86
                        PrivateToken:    gitlabPrivateToken,
×
87
                        GitPandaVersion: Version,
×
88
                        IsDebugLogging:  isDebugLogging,
×
89
                },
×
90
        )
×
91
        response, err := s.Request(body, truncateLines)
×
92

×
93
        if err != nil {
×
94
                return response, errors.WithStack(err)
×
95
        }
×
96

97
        return response, nil
×
98
}
99

100
// GetParameterStoreOrEnv returns Environment variable or Parameter Store variable
101
func GetParameterStoreOrEnv(envKey string, parameterStoreKey string, required bool) (string, error) {
×
102
        if parameterStoreKey != "" {
×
103
                // Get from Parameter Store
×
104
                d, err := NewSsmDecrypter()
×
105

×
106
                if err != nil {
×
107
                        return "", errors.WithStack(err)
×
108
                }
×
109

110
                decryptedValue, err := d.Decrypt(parameterStoreKey)
×
111
                if err != nil {
×
112
                        return fmt.Sprintf("Failed: Decrypt %s", parameterStoreKey), errors.WithStack(err)
×
113
                }
×
114

115
                return decryptedValue, nil
×
116
        }
117

118
        // Get from env
119
        if os.Getenv(envKey) != "" {
×
120
                return os.Getenv(envKey), nil
×
121
        }
×
122

123
        if required {
×
NEW
124
                return "", fmt.Errorf("either %s or %s is required", envKey, parameterStoreKey)
×
125
        }
×
126

127
        return "", nil
×
128
}
129

130
// SsmDecrypter stores the AWS Session used for SSM decrypter.
131
type SsmDecrypter struct {
132
        client *ssm.Client
133
}
134

135
// NewSsmDecrypter returns a new SsmDecrypter.
136
func NewSsmDecrypter() (*SsmDecrypter, error) {
×
137
        cfg, err := config.LoadDefaultConfig(context.TODO())
×
138
        if err != nil {
×
139
                return nil, errors.WithStack(err)
×
140
        }
×
141

142
        client := ssm.NewFromConfig(cfg)
×
143
        return &SsmDecrypter{client}, nil
×
144
}
145

146
// Decrypt decrypts string.
147
func (d *SsmDecrypter) Decrypt(encrypted string) (string, error) {
×
148
        params := &ssm.GetParameterInput{
×
149
                Name:           aws.String(encrypted),
×
150
                WithDecryption: aws.Bool(true),
×
151
        }
×
152
        resp, err := d.client.GetParameter(context.TODO(), params)
×
153
        if err != nil {
×
154
                return "", errors.WithStack(err)
×
155
        }
×
156
        return aws.ToString(resp.Parameter.Value), nil
×
157
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc