-
Notifications
You must be signed in to change notification settings - Fork 3
/
in.go
166 lines (146 loc) · 5.01 KB
/
in.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package resource
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strconv"
)
// Get (business logic)
func Get(request GetRequest, github Github, git Git, outputDir string) (*GetResponse, error) {
if request.Params.SkipDownload {
log.Print("`skip_download` has been deprecated, use `no_get` instead to prevent this implicit get step from running.")
return &GetResponse{Version: request.Version}, nil
}
pull, err := github.GetPullRequest(request.Version.PR, request.Version.Commit)
if err != nil {
return nil, fmt.Errorf("failed to retrieve pull request: %s", err)
}
// Initialize and pull the base for the PR
if err := git.Init(pull.BaseRefName); err != nil {
return nil, err
}
if err := git.Pull(pull.Repository.URL, pull.BaseRefName, request.Params.GitDepth, request.Params.Submodules, request.Params.FetchTags); err != nil {
return nil, err
}
// Get the last commit SHA in base for the metadata
baseSHA, err := git.RevParse(pull.BaseRefName)
if err != nil {
return nil, err
}
// Fetch the PR and merge the specified commit into the base
if err := git.Fetch(pull.Repository.URL, pull.Number, request.Params.GitDepth, request.Params.Submodules); err != nil {
return nil, err
}
// Create the metadata
var metadata Metadata
metadata.Add("pr", strconv.Itoa(pull.Number))
metadata.Add("title", pull.Title)
metadata.Add("body", pull.Body)
metadata.Add("url", pull.URL)
metadata.Add("head_name", pull.HeadRefName)
metadata.Add("head_sha", pull.Tip.OID)
metadata.Add("base_name", pull.BaseRefName)
metadata.Add("base_sha", baseSHA)
metadata.Add("message", pull.Tip.Message)
metadata.Add("author", pull.Tip.Author.User.Login)
metadata.Add("author_email", pull.Tip.Author.Email)
metadata.Add("state", string(pull.State))
// Write version and metadata for reuse in PUT
path := filepath.Join(outputDir, ".git", "resource")
if err := os.MkdirAll(path, os.ModePerm); err != nil {
return nil, fmt.Errorf("failed to create output directory: %s", err)
}
b, err := json.Marshal(request.Version)
if err != nil {
return nil, fmt.Errorf("failed to marshal version: %s", err)
}
if err := ioutil.WriteFile(filepath.Join(path, "version.json"), b, 0644); err != nil {
return nil, fmt.Errorf("failed to write version: %s", err)
}
b, err = json.Marshal(metadata)
if err != nil {
return nil, fmt.Errorf("failed to marshal metadata: %s", err)
}
if err := ioutil.WriteFile(filepath.Join(path, "metadata.json"), b, 0644); err != nil {
return nil, fmt.Errorf("failed to write metadata: %s", err)
}
for _, d := range metadata {
filename := d.Name
content := []byte(d.Value)
if err := ioutil.WriteFile(filepath.Join(path, filename), content, 0644); err != nil {
return nil, fmt.Errorf("failed to write metadata file %s: %s", filename, err)
}
}
metadataMap := make(map[string]string)
for _, d := range metadata {
metadataMap[d.Name] = d.Value
}
if b, err = json.Marshal(metadataMap); err != nil {
return nil, fmt.Errorf("failed to marshal map of metadata: %s", err)
}
if err := ioutil.WriteFile(filepath.Join(path, "metadata-map.json"), b, 0644); err != nil {
return nil, fmt.Errorf("failed to write metadata map file: %s", err)
}
switch tool := request.Params.IntegrationTool; tool {
case "rebase":
if err := git.Rebase(pull.BaseRefName, pull.Tip.OID, request.Params.Submodules); err != nil {
return nil, err
}
case "merge", "":
if err := git.Merge(pull.Tip.OID, request.Params.Submodules); err != nil {
return nil, err
}
case "checkout":
if err := git.Checkout(pull.HeadRefName, pull.Tip.OID, request.Params.Submodules); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("invalid integration tool specified: %s", tool)
}
if request.Source.GitCryptKey != "" {
if err := git.GitCryptUnlock(request.Source.GitCryptKey); err != nil {
return nil, err
}
}
if request.Params.ListChangedFiles {
cfol, err := github.GetChangedFiles(request.Version.PR, request.Version.Commit)
if err != nil {
return nil, fmt.Errorf("failed to fetch list of changed files: %s", err)
}
var fl []byte
for _, v := range cfol {
fl = append(fl, []byte(v.Path+"\n")...)
}
// Create List with changed files
if err := ioutil.WriteFile(filepath.Join(path, "changed_files"), fl, 0644); err != nil {
return nil, fmt.Errorf("failed to write file list: %s", err)
}
}
return &GetResponse{
Version: request.Version,
Metadata: metadata,
}, nil
}
// GetParameters ...
type GetParameters struct {
SkipDownload bool `json:"skip_download"`
IntegrationTool string `json:"integration_tool"`
GitDepth int `json:"git_depth"`
Submodules bool `json:"submodules"`
ListChangedFiles bool `json:"list_changed_files"`
FetchTags bool `json:"fetch_tags"`
}
// GetRequest ...
type GetRequest struct {
Source Source `json:"source"`
Version Version `json:"version"`
Params GetParameters `json:"params"`
}
// GetResponse ...
type GetResponse struct {
Version Version `json:"version"`
Metadata Metadata `json:"metadata,omitempty"`
}