Skip to content

Commit a67f985

Browse files
committed
BUILD/MINOR: ci: do not ask for backport if bot created merge request
1 parent 2183dd8 commit a67f985

File tree

1 file changed

+47
-14
lines changed

1 file changed

+47
-14
lines changed

cmd/gitlab-mr-checker/main.go

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,17 @@ type Thread struct {
6969
IID int `json:"iid"`
7070
}
7171

72-
// GitlabLabel defines the structure for a GitLab label.
73-
// It includes common fields; you are primarily using Name.
7472
type GitlabLabel struct {
7573
ID int `json:"id"`
7674
Name string `json:"name"`
7775
Color string `json:"color"`
7876
Description string `json:"description,omitempty"` // omitempty handles cases where description might be null or absent
7977
}
8078

79+
type MergeRequest struct {
80+
Description string `json:"description"`
81+
}
82+
8183
var baseURL string
8284

8385
const LABEL_COLOR = "#8fbc8f" //nolint:stylecheck
@@ -148,6 +150,17 @@ func main() {
148150
question += "\n" + "| EE | ∞ | " + "backport-ee |"
149151
question += "\n\n" + "please add labels for backporting."
150152

153+
mr, err := getMergeRequest(baseURL, token, CI_PROJECT_ID, CI_MERGE_REQUEST_IID)
154+
if err != nil {
155+
slog.Error(err.Error())
156+
os.Exit(1)
157+
}
158+
159+
if strings.Contains(mr.Description, "<!-- BOT DEPENDABOT -->") {
160+
slog.Info("Dependabot MR detected, skipping backport check.")
161+
os.Exit(0)
162+
}
163+
151164
notes, err := getMergeRequestComments(baseURL, token, CI_PROJECT_ID, CI_MERGE_REQUEST_IID)
152165
if err != nil {
153166
slog.Error(err.Error())
@@ -194,21 +207,41 @@ func startThreadOnMergeRequest(baseURL, token, projectID string, mergeRequestIID
194207
os.Exit(1)
195208
}
196209
defer resp.Body.Close()
210+
}
211+
212+
func getMergeRequest(baseURL, token, projectID string, mergeRequestIID int) (*MergeRequest, error) {
213+
client := &http.Client{}
214+
215+
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet,
216+
fmt.Sprintf("%s/projects/%s/merge_requests/%d", baseURL, url.PathEscape(projectID), mergeRequestIID), nil)
217+
if err != nil {
218+
return nil, err
219+
}
220+
req.Header.Add("PRIVATE-TOKEN", token) //nolint:canonicalheader
221+
222+
resp, err := client.Do(req)
223+
if err != nil {
224+
return nil, err
225+
}
226+
defer resp.Body.Close()
197227

198-
// body, err := io.ReadAll(resp.Body)
199-
// if err != nil {
200-
// slog.Error(err.Error())
201-
// os.Exit(1)
202-
// }
228+
if resp.StatusCode != http.StatusOK {
229+
body, _ := io.ReadAll(resp.Body)
230+
return nil, fmt.Errorf("failed to get merge request: status %s, body: %s", resp.Status, string(body))
231+
}
203232

204-
// var thread Thread
205-
// err = json.Unmarshal(body, &thread)
206-
// if err != nil {
207-
// slog.Error(err.Error())
208-
// os.Exit(1)
209-
// }
233+
body, err := io.ReadAll(resp.Body)
234+
if err != nil {
235+
return nil, err
236+
}
237+
238+
var mr MergeRequest
239+
err = json.Unmarshal(body, &mr)
240+
if err != nil {
241+
return nil, err
242+
}
210243

211-
// slog.Info("Thread started with ID " + strconv.Itoa(thread.ID))
244+
return &mr, nil
212245
}
213246

214247
func getMergeRequestComments(baseURL, token, projectID string, mergeRequestIID int) ([]Note, error) {

0 commit comments

Comments
 (0)