-
Notifications
You must be signed in to change notification settings - Fork 15
/
update_go.go
117 lines (104 loc) · 3.44 KB
/
update_go.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
/*
* Copyright 2018-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package octo
import (
"fmt"
"os"
"path/filepath"
"github.com/paketo-buildpacks/pipeline-builder/octo/actions"
"github.com/paketo-buildpacks/pipeline-builder/octo/actions/event"
"github.com/paketo-buildpacks/pipeline-builder/octo/jitter"
)
func ContributeUpdateGo(descriptor Descriptor) (*Contribution, error) {
entries, err := os.ReadDir(descriptor.Path)
if err != nil {
return nil, fmt.Errorf("unable to Find go.mod or go.sum files in %s\n%w", descriptor.Path, err)
}
goModFiles := []string{}
for _, entry := range entries {
if entry.Name() == "go.mod" || entry.Name() == "go.sum" {
goModFiles = append(goModFiles, entry.Name())
}
}
if len(goModFiles) != 2 {
return nil, nil
}
repoName := os.Getenv("GITHUB_REPOSITORY")
if len(repoName) == 0 {
cwd, err := os.Getwd()
if err != nil {
return nil, fmt.Errorf("unable to fetch current working directory\n%w", err)
}
// get the repo name, which should like `paketo-buildpacks/foo`
repoName = fmt.Sprintf("%s/%s",
filepath.Base(filepath.Dir(filepath.Dir(cwd))),
filepath.Base(filepath.Dir(cwd)))
}
seed := fmt.Sprintf("Update Go %s", repoName)
cron := jitter.
New(seed).
Jitter(event.Cron{Hour: "2", DayOfWeek: "1", Month: "*", DayOfMonth: "*"})
w := actions.Workflow{
Name: "Update Go",
On: map[event.Type]event.Event{
event.ScheduleType: event.Schedule{cron},
event.WorkflowDispatchType: event.WorkflowDispatch{},
},
Jobs: map[string]actions.Job{
"update": {
Name: "Update Go",
RunsOn: []actions.VirtualEnvironment{actions.UbuntuLatest},
Steps: []actions.Step{
{
Uses: "actions/setup-go@v5",
With: map[string]interface{}{"go-version": GoVersion},
},
{
Uses: "actions/checkout@v4",
},
{
Id: "update-go",
Name: "Update Go Version & Modules",
Run: StatikString("/update-go.sh"),
Env: map[string]string{
"GO_VERSION": GoVersion,
},
},
{
Uses: "peter-evans/create-pull-request@v5",
With: map[string]interface{}{
"token": descriptor.GitHub.Token,
"author": fmt.Sprintf("%[1]s <%[1]s@users.noreply.github.com>", descriptor.GitHub.Username),
"commit-message": `${{ steps.update-go.outputs.commit-title }}
${{ steps.update-go.outputs.commit-body }}`,
"signoff": true,
"branch": "update/go",
"delete-branch": true,
"title": "${{ steps.update-go.outputs.commit-title }}",
"body": "${{ steps.update-go.outputs.commit-body }}\n\n<details>\n<summary>Release Notes</summary>\n${{ steps.pipeline.outputs.release-notes }}\n</details>",
"labels": "${{ steps.update-go.outputs.commit-semver }}, type:task",
},
},
},
},
},
}
c, err := NewActionContributionWithNamespace(Namespace, w)
if err != nil {
return nil, err
}
return &c, nil
}