Skip to content

Commit 37f630a

Browse files
aviaustapelberg
authored andcommitted
protect salsa project branches (#84)
1 parent cc60adb commit 37f630a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

create_salsa_project.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ func execCreateSalsaProject(args []string) {
4343
log.Fatalf("Could not create webhook on salsa project: %s\n", err)
4444
}
4545

46+
for _, branch := range []string{"master", "debian/*", "upstream", "upstream/*", "pristine-tar"} {
47+
if err := protectSalsaProjectBranch(gitlabProject.ID, branch, token); err != nil {
48+
log.Printf("Could not protect branch %s: %s\n", branch, err)
49+
}
50+
}
51+
4652
}
4753

4854
func postFormToSalsaApi(path string, data url.Values, token string) (*http.Response, error) {
@@ -51,6 +57,29 @@ func postFormToSalsaApi(path string, data url.Values, token string) (*http.Respo
5157
return http.PostForm(postUrl, data)
5258
}
5359

60+
func protectSalsaProjectBranch(projectId int, branch, token string) error {
61+
postPath := "/projects/" + strconv.Itoa(projectId) + "/protected_branches"
62+
63+
response, err := postFormToSalsaApi(postPath,
64+
url.Values{
65+
"name": {branch},
66+
},
67+
token)
68+
if err != nil {
69+
return err
70+
}
71+
72+
if response.StatusCode != http.StatusCreated {
73+
responseData, _ := ioutil.ReadAll(response.Body)
74+
75+
return fmt.Errorf("http status %d: %s",
76+
response.StatusCode,
77+
responseData)
78+
}
79+
80+
return nil
81+
}
82+
5483
func createSalsaWebhook(projectId int, webhookUrl, token string) error {
5584
postPath := "/projects/" + strconv.Itoa(projectId) + "/hooks"
5685

0 commit comments

Comments
 (0)