@@ -4,15 +4,20 @@ import (
4
4
"context"
5
5
"errors"
6
6
"fmt"
7
+ k "github.com/openshift-pipelines/hack/internal/konflux"
7
8
"io/fs"
8
9
"log"
9
10
"os"
10
11
"path/filepath"
11
12
"strings"
12
13
)
13
14
14
- func cloneAndCheckout (ctx context.Context , repo , branch , dir string ) error {
15
+ const baseBranchPrefix = "actions/update/konflux-configuration-"
16
+
17
+ func cloneAndCheckout (ctx context.Context , repo , branch , dir string , config k.Config ) error {
18
+ branchPrefix := baseBranchPrefix + config .Name
15
19
exists , err := exists (filepath .Join (dir , ".git" ))
20
+
16
21
if err != nil {
17
22
return err
18
23
}
@@ -27,6 +32,7 @@ func cloneAndCheckout(ctx context.Context, repo, branch, dir string) error {
27
32
return fmt .Errorf ("failed to clone repository: %s, %s" , err , out )
28
33
}
29
34
}
35
+
30
36
if out , err := run (ctx , dir , "git" , "reset" , "--hard" , "HEAD" , "--" ); err != nil {
31
37
return fmt .Errorf ("failed to reset %s branch: %s, %s" , branch , err , out )
32
38
}
@@ -47,13 +53,15 @@ func cloneAndCheckout(ctx context.Context, repo, branch, dir string) error {
47
53
if out , err := run (ctx , dir , "git" , "checkout" , "origin/" + branch , "-B" , branch ); err != nil {
48
54
return fmt .Errorf ("failed to checkout %s branch: %s, %s" , branch , err , out )
49
55
}
50
- if out , err := run (ctx , dir , "git" , "checkout" , "-B" , "actions/update/konflux-configuration-" + branch ); err != nil {
56
+ if out , err := run (ctx , dir , "git" , "checkout" , "-B" , branchPrefix + branch ); err != nil {
51
57
return fmt .Errorf ("failed to checkout branch for PR: %s, %s" , err , out )
52
58
}
53
59
return nil
54
60
}
55
61
56
- func commitAndPullRequest (ctx context.Context , dir , branch string ) error {
62
+ func commitAndPullRequest (ctx context.Context , dir , branch string , config k.Config ) error {
63
+ branchPrefix := baseBranchPrefix + config .Name
64
+
57
65
if out , err := run (ctx , dir , "git" , "status" , "--porcelain" ); err != nil {
58
66
return fmt .Errorf ("failed to check git status: %s, %s" , err , out )
59
67
} else if string (out ) == "" {
@@ -69,17 +77,17 @@ func commitAndPullRequest(ctx context.Context, dir, branch string) error {
69
77
if out , err := run (ctx , dir , "git" , "commit" , "-m" , fmt .Sprintf ("[bot:%s] update konflux configuration" , branch )); err != nil {
70
78
return fmt .Errorf ("failed to commit: %s, %s" , err , out )
71
79
}
72
- if out , err := run (ctx , dir , "git" , "push" , "-f" , "origin" , "actions/update/konflux-configuration-" + branch ); err != nil {
80
+ if out , err := run (ctx , dir , "git" , "push" , "-f" , "origin" , branchPrefix + branch ); err != nil {
73
81
return fmt .Errorf ("failed to push: %s, %s" , err , out )
74
82
}
75
- if out , err := run (ctx , dir , "bash" , "-c" , "gh pr list --base " + branch + " --head actions/update/konflux-configuration-" + branch + " --json url --jq 'length'" ); err != nil {
83
+ if out , err := run (ctx , dir , "bash" , "-c" , "gh pr list --base " + branch + " --head " + branchPrefix + branch + " --json url --jq 'length'" ); err != nil {
76
84
return fmt .Errorf ("failed to check if a pr exists: %s, %s" , err , out )
77
85
} else if strings .TrimSpace (string (out )) == "0" {
78
86
if out , err := run (ctx , dir , "gh" , "pr" , "create" ,
79
87
"--base" , branch ,
80
- "--head" , "actions/update/konflux-configuration-" + branch ,
88
+ "--head" , branchPrefix + branch ,
81
89
"--label=hack" , "--label=automated" ,
82
- "--title" , fmt .Sprintf ("[bot:%s] update konflux configuration" , branch ),
90
+ "--title" , fmt .Sprintf ("[bot:%s:%s ] update konflux configuration" , config . Name , branch ),
83
91
"--body" , "This PR was automatically generated by the konflux command from openshift-pipelines/hack repository" ); err != nil {
84
92
return fmt .Errorf ("failed to create the pr: %s, %s" , err , out )
85
93
}
0 commit comments