-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathin.go
140 lines (117 loc) · 3.71 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
package resource
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
m "github.com/digitalocean/concourse-resource-library/metadata"
)
// Get (business logic)
func Get(request GetRequest, github Github, git Git, outputDir string) (*GetResponse, error) {
if request.Params.SkipDownload {
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
err = git.Clone(pull.RepositoryURL, pull.BaseRefName, request.Params.GitDepth)
if err != nil {
return nil, fmt.Errorf("failed to clone repository: %s", err)
}
// Fetch the PR and merge the specified commit into the base
if err := git.Fetch(pull.Number, request.Params.GitDepth); err != nil {
return nil, err
}
switch request.Params.IntegrationTool {
case "rebase":
pull.BaseRefOID, err = git.RevParse(pull.BaseRefName)
if err != nil {
return nil, err
}
if err := git.Rebase(pull.BaseRefName, request.Version.Commit); err != nil {
return nil, err
}
case "merge":
pull.BaseRefOID, err = git.RevParse(pull.BaseRefName)
if err != nil {
return nil, err
}
if err := git.Merge(request.Version.Commit); err != nil {
return nil, err
}
case "checkout", "":
if err := git.Checkout(pull.HeadRefName, request.Version.Commit); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("invalid integration tool specified: %s", request.Params.IntegrationTool)
}
if request.Source.GitCryptKey != "" {
if err := git.GitCryptUnlock(request.Source.GitCryptKey); err != nil {
return nil, err
}
}
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)
}
metadata := metadataFactory(pull)
metadata.AddJSON("version", &request.Version)
b, err := metadata.JSON()
if err != nil {
return nil, err
}
err = writeFile("metadata", path, b)
if err != nil {
return nil, err
}
metadata.ToFiles(path)
if request.Params.ListChangedFiles {
cfol, err := github.GetChangedFiles(request.Version.PR)
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+"\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
}
func writeFile(name, path string, b []byte) error {
if err := ioutil.WriteFile(filepath.Join(path, name+".json"), b, 0644); err != nil {
return fmt.Errorf("failed to write %s: %s", name, err)
}
return nil
}
// GetParameters ...
type GetParameters struct {
// SkipDownload will skip downloading the code to the volume, used with `put` steps
SkipDownload bool `json:"skip_download"`
// IntegrationTool defines the method of checking out the code (checkout [default], merge, rebase)
IntegrationTool string `json:"integration_tool"`
// GitDepth sets the number of commits to include in the clone (shallow clone)
GitDepth int `json:"git_depth"`
// ListChangedFiles generates a list of changed files in the `.git` directory
ListChangedFiles bool `json:"list_changed_files"`
}
// 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 m.Metadata `json:"metadata,omitempty"`
}