@@ -24,12 +24,13 @@ import (
24
24
)
25
25
26
26
type GitlabClient struct {
27
- client * gitlab.Client
28
- username string
29
- ownerName string
30
- repoName string
31
- token string
32
- projectId int
27
+ client * gitlab.Client
28
+ username string
29
+ ownerName string
30
+ repoName string
31
+ token string
32
+ projectId int
33
+ branchToMR map [string ]int
33
34
}
34
35
35
36
func NewGitlabClient () * GitlabClient {
@@ -61,12 +62,13 @@ func NewGitlabClient() *GitlabClient {
61
62
}
62
63
63
64
return & GitlabClient {
64
- client : gitlabClient ,
65
- username : gitlabUsername ,
66
- ownerName : ownerName ,
67
- repoName : repoName ,
68
- token : gitlabToken ,
69
- projectId : project .ID ,
65
+ client : gitlabClient ,
66
+ username : gitlabUsername ,
67
+ ownerName : ownerName ,
68
+ repoName : repoName ,
69
+ token : gitlabToken ,
70
+ projectId : project .ID ,
71
+ branchToMR : make (map [string ]int ),
70
72
}
71
73
72
74
}
@@ -107,53 +109,44 @@ func (g GitlabClient) DeleteAtlantisHook(ctx context.Context, hookID int64) erro
107
109
return nil
108
110
}
109
111
110
- func (g GitlabClient ) CreatePullRequest (ctx context.Context , title , branchName string ) (string , PullRequest , error ) {
112
+ func (g GitlabClient ) CreatePullRequest (ctx context.Context , title , branchName string ) (string , int , error ) {
113
+ // log.Printf("Sleeping 10 seconds to prevent race conditions creating MR")
114
+ // time.Sleep(time.Second * 10)
111
115
mr , _ , err := g .client .MergeRequests .CreateMergeRequest (g .projectId , & gitlab.CreateMergeRequestOptions {
112
116
Title : gitlab .Ptr (title ),
113
117
SourceBranch : gitlab .Ptr (branchName ),
114
118
TargetBranch : gitlab .Ptr ("main" ),
115
119
})
116
120
if err != nil {
117
- return "" , PullRequest {} , fmt .Errorf ("error while creating new pull request: %v" , err )
121
+ return "" , 0 , fmt .Errorf ("error while creating new pull request: %v" , err )
118
122
}
119
- return mr .WebURL , PullRequest {
120
- id : mr .IID ,
121
- branch : branchName ,
122
- }, nil
123
+ g .branchToMR [branchName ] = mr .IID
124
+ return mr .WebURL , mr .IID , nil
123
125
124
126
}
125
127
126
- func (g GitlabClient ) GetAtlantisStatus (ctx context.Context , pullRequest PullRequest ) (string , error ) {
128
+ func (g GitlabClient ) GetAtlantisStatus (ctx context.Context , branchName string ) (string , error ) {
127
129
128
- fmt .Println (pullRequest )
129
- res , _ , err := g .client .MergeRequests .ListMergeRequestPipelines (g .projectId , pullRequest .id )
130
+ pipelineInfos , _ , err := g .client .MergeRequests .ListMergeRequestPipelines (g .projectId , g .branchToMR [branchName ])
131
+ if err != nil {
132
+ return "" , err
133
+ }
134
+ // Possible todo: determine which status in the pipeline we care about?
135
+ if len (pipelineInfos ) != 1 {
136
+ return "" , fmt .Errorf ("unexpected pipelines: %d" , len (pipelineInfos ))
137
+ }
138
+ pipelineInfo := pipelineInfos [0 ]
139
+ pipeline , _ , err := g .client .Pipelines .GetPipeline (g .projectId , pipelineInfo .ID )
130
140
if err != nil {
131
- fmt .Println ("I have error" , err )
132
141
return "" , err
133
142
}
134
- fmt .Println (res )
135
- return "" , nil
136
- /*
137
-
138
- // check repo status
139
- combinedStatus, _, err := g.client.Repositories.GetCombinedStatus(ctx, g.ownerName, g.repoName, branchName, nil)
140
- if err != nil {
141
- return "", err
142
- }
143
-
144
- for _, status := range combinedStatus.Statuses {
145
- if status.GetContext() == "atlantis/plan" {
146
- return status.GetState(), nil
147
- }
148
- }
149
143
150
- return "", nil
151
- */
144
+ return pipeline .Status , nil
152
145
}
153
146
154
- func (g GitlabClient ) ClosePullRequest (ctx context.Context , pullRequest PullRequest ) error {
147
+ func (g GitlabClient ) ClosePullRequest (ctx context.Context , pullRequestNumber int ) error {
155
148
// clean up
156
- _ , _ , err := g .client .MergeRequests .UpdateMergeRequest (g .projectId , pullRequest . id , & gitlab.UpdateMergeRequestOptions {
149
+ _ , _ , err := g .client .MergeRequests .UpdateMergeRequest (g .projectId , pullRequestNumber , & gitlab.UpdateMergeRequestOptions {
157
150
StateEvent : gitlab .Ptr ("close" ),
158
151
})
159
152
if err != nil {
@@ -163,13 +156,11 @@ func (g GitlabClient) ClosePullRequest(ctx context.Context, pullRequest PullRequ
163
156
164
157
}
165
158
func (g GitlabClient ) DeleteBranch (ctx context.Context , branchName string ) error {
166
- panic ("I'mdeleting branch" )
167
- /*
168
-
169
- _, err := g.client.Git.DeleteRef(ctx, g.ownerName, g.repoName, branchName)
170
- if err != nil {
171
- return fmt.Errorf("error while deleting branch %s: %v", branchName, err)
172
- }
173
- return nil
174
- */
159
+ _ , err := g .client .Branches .DeleteBranch (g .projectId , branchName )
160
+
161
+ if err != nil {
162
+ return fmt .Errorf ("error while deleting branch %s: %v" , branchName , err )
163
+ }
164
+ return nil
165
+
175
166
}
0 commit comments