This repository has been archived by the owner on Jun 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
in.go
113 lines (98 loc) · 2.93 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
package resource
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
)
// 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
if err := git.Init(pull.BaseRefName); err != nil {
return nil, err
}
if err := git.Pull(pull.Repository.URL, pull.BaseRefName); 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); err != nil {
return nil, err
}
switch request.Source.IntegrationTool {
case "rebase":
if err := git.Rebase(pull.BaseRefName, pull.Tip.OID); err != nil {
return nil, err
}
default:
if err := git.Merge(pull.Tip.OID); err != nil {
return nil, err
}
}
if request.Source.GitCryptKey != "" {
if err := git.GitCryptUnlock(request.Source.GitCryptKey); err != nil {
return nil, err
}
}
// Create the metadata
var metadata Metadata
metadata.Add("pr", strconv.Itoa(pull.Number))
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)
// 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)
}
return &GetResponse{
Version: request.Version,
Metadata: metadata,
}, nil
}
// GetParameters ...
type GetParameters struct {
SkipDownload bool `json:"skip_download"`
}
// 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"`
}