-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
147 lines (125 loc) · 3.83 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"os"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
log "github.com/sirupsen/logrus"
)
// Repository: struct for repository information
type Repository struct {
URL string `json:"git_http_url"`
Name string `json:"name"`
}
// Commit: struct for commit information
type Commit struct {
ID string `json:"id"`
Message string `json:"message"`
}
// WebHookRequest: Location of the repository and other relevant information
type WebHookRequest struct {
Repository Repository `json:"repository"`
Commits []Commit `json:"commits"`
}
// WebHookResponse: Location of the zip file in S3
type WebHookResponse struct {
Bucket string
Key string
}
var (
API_KEY = os.Getenv("API_KEY")
USERNAME = os.Getenv("USERNAME")
PASSWORD = os.Getenv("PASSWORD")
S3_BUCKET = os.Getenv("S3_BUCKET")
S3_REGION = os.Getenv("S3_REGION")
)
func init() {
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.JSONFormatter{})
// Output to stdout for CloudWatch
log.SetOutput(os.Stdout)
// Only log the info severity or above.
log.SetLevel(log.InfoLevel)
}
// CheckIfError: check if an error occurred and log if necessary
func CheckIfError(err error) bool {
if err != nil {
log.WithField("Error", err).Errorf("An error occurred")
return true
}
return false
}
func handler(request events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) {
webHookRequest := &WebHookRequest{}
// unmarshal body to struct object
err := json.Unmarshal([]byte(request.Body), webHookRequest)
if CheckIfError(err) {
return events.APIGatewayProxyResponse{
Body: err.Error(),
StatusCode: 501,
}, err
}
log.WithFields(log.Fields{
"URL": *webHookRequest,
"Body": request.Body,
}).Info("Got following Parameter:")
// clone repository and put into zip file
file, err := GitCloneAndZip(&GitCloneAndZipInput{apiKey: API_KEY, username: USERNAME, password: PASSWORD, repositoryUrl: webHookRequest.Repository.URL})
if CheckIfError(err) {
return events.APIGatewayProxyResponse{
Body: err.Error(),
StatusCode: 502,
}, err
}
// generate filename from repository name and id of last commit
fileName := fmt.Sprintf("%s-%s.zip", webHookRequest.Repository.Name, webHookRequest.Commits[0].ID)
// upload the zip file to S3
err = addFileToS3(file.File, fileName, file.Size)
if CheckIfError(err) {
return events.APIGatewayProxyResponse{
Body: err.Error(),
StatusCode: 503,
}, err
}
// marshal response to json
response, err := json.Marshal(WebHookResponse{Bucket: S3_BUCKET, Key: fileName})
if CheckIfError(err) {
return events.APIGatewayProxyResponse{
Body: err.Error(),
StatusCode: 503,
}, err
}
// Return the location data to API Gateway
return events.APIGatewayProxyResponse{
Body: string(response),
StatusCode: 200,
}, err
}
// addFileToS3: puts the given file into the defined bucket to the given path
// if an error occurs it is returned for further handling
func addFileToS3(file []byte, path string, size int64) error {
log.WithField("Path", path).Info("Got Parameters for S3 Put")
s, err := session.NewSession(&aws.Config{Region: aws.String(S3_REGION)})
// create buffer
buffer := make([]byte, size)
// upload object to s3
_, err = s3.New(s).PutObject(&s3.PutObjectInput{
Bucket: aws.String(S3_BUCKET),
Key: aws.String(path),
ACL: aws.String("private"),
Body: bytes.NewReader(file),
ContentLength: aws.Int64(size),
ContentType: aws.String(http.DetectContentType(buffer)),
ContentDisposition: aws.String("attachment"),
})
return err
}
func main() {
lambda.Start(handler)
}