This repository was archived by the owner on Feb 19, 2021. It is now read-only.
forked from concourse/github-release-resource
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathin_command.go
More file actions
149 lines (122 loc) · 3.12 KB
/
in_command.go
File metadata and controls
149 lines (122 loc) · 3.12 KB
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
package resource
import (
"errors"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/xanzy/go-gitlab"
)
type InCommand struct {
gitlab GitLab
writer io.Writer
}
type attachment struct {
Name string
URL string
}
func NewInCommand(gitlab GitLab, writer io.Writer) *InCommand {
return &InCommand{
gitlab: gitlab,
writer: writer,
}
}
func (c *InCommand) Run(destDir string, request InRequest) (InResponse, error) {
err := os.MkdirAll(destDir, 0755)
if err != nil {
return InResponse{}, err
}
var foundTag *gitlab.Tag
foundTag, err = c.gitlab.GetTag(request.Version.Tag)
if err != nil {
return InResponse{}, err
}
if foundTag == nil {
return InResponse{}, errors.New("could not find tag")
}
tagPath := filepath.Join(destDir, "tag")
err = ioutil.WriteFile(tagPath, []byte(foundTag.Name), 0644)
if err != nil {
return InResponse{}, err
}
versionParser, err := newVersionParser(request.Source.TagFilter)
if err != nil {
return InResponse{}, err
}
version := versionParser.parse(foundTag.Name)
versionPath := filepath.Join(destDir, "version")
err = ioutil.WriteFile(versionPath, []byte(version), 0644)
if err != nil {
return InResponse{}, err
}
commitPath := filepath.Join(destDir, "commit_sha")
err = ioutil.WriteFile(commitPath, []byte(foundTag.Commit.ID), 0644)
if err != nil {
return InResponse{}, err
}
if foundTag.Release != nil && foundTag.Release.Description != "" {
body := foundTag.Release.Description
bodyPath := filepath.Join(destDir, "body")
err = ioutil.WriteFile(bodyPath, []byte(body), 0644)
if err != nil {
return InResponse{}, err
}
} else {
return InResponse{}, errors.New("release notes for the tag was empty")
}
attachments, err := c.getAttachments(foundTag.Release.Description)
if err != nil {
return InResponse{}, err
}
for _, attachment := range attachments {
path := filepath.Join(destDir, attachment.Name)
var matchFound bool
if len(request.Params.Globs) == 0 {
matchFound = true
} else {
for _, glob := range request.Params.Globs {
matches, err := filepath.Match(glob, attachment.Name)
if err != nil {
return InResponse{}, err
}
if matches {
matchFound = true
break
}
}
}
if !matchFound {
continue
}
err := c.gitlab.DownloadProjectFile(attachment.URL, path)
if err != nil {
return InResponse{}, err
}
}
return InResponse{
Version: versionFromTag(foundTag),
Metadata: metadataFromTag(foundTag),
}, nil
}
// This resource stores the attachments as line-separated markdown links.
func (c *InCommand) getAttachments(releaseBody string) ([]attachment, error) {
var attachments []attachment
lines := strings.Split(releaseBody, "\n")
for _, line := range lines {
nameStart := strings.Index(line, "[")
nameEnd := strings.Index(line, "]")
urlStart := strings.Index(line, "(")
urlEnd := strings.Index(line, ")")
if nameStart == -1 || nameEnd == -1 || urlStart == -1 || urlEnd == -1 {
continue
}
nameStart++
urlStart++
attachments = append(attachments, attachment{
Name: line[nameStart:nameEnd],
URL: line[urlStart:urlEnd],
})
}
return attachments, nil
}