Skip to content

Commit b57c318

Browse files
committed
create salsa projects via a request to pgt-api-server
The source code of the corresponding server can be found at: https://github.com/Debian/pkg-go-tools/tree/master/cmd/pgt-api-server The repository configuration can be found at: https://salsa.debian.org/go-team/ci/blob/master/config/config.go
1 parent 6700b01 commit b57c318

File tree

1 file changed

+11
-133
lines changed

1 file changed

+11
-133
lines changed

create_salsa_project.go

Lines changed: 11 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,15 @@
11
package main
22

33
import (
4-
"encoding/json"
5-
"errors"
64
"flag"
7-
"fmt"
85
"io/ioutil"
96
"log"
107
"net/http"
118
"net/url"
129
"os"
13-
"strconv"
1410
)
1511

16-
const (
17-
salsaGroupID = 2638
18-
salsaApiUrl = "https://salsa.debian.org/api/v4"
19-
)
20-
21-
type gitlabProject struct {
22-
ID int `json:"id"`
23-
Name string `json:"name"`
24-
HttpUrlToRepo string `json:"http_url_to_repo"`
25-
}
26-
2712
func execCreateSalsaProject(args []string) {
28-
projectName := mustGetProjectName(args)
29-
token := mustGetSalsaToken()
30-
31-
gitlabProject, err := createSalsaProject(projectName, token)
32-
if err != nil {
33-
log.Fatalf("Could not create %s on salsa: %s\n", projectName, err)
34-
}
35-
36-
fmt.Printf("Project %s was created on salsa.debian.org: %s\n",
37-
gitlabProject.Name,
38-
gitlabProject.HttpUrlToRepo)
39-
40-
if err := createSalsaWebhook(gitlabProject.ID,
41-
"https://webhook.salsa.debian.org/tagpending/"+gitlabProject.Name,
42-
token); err != nil {
43-
log.Fatalf("Could not create webhook on salsa project: %s\n", err)
44-
}
45-
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-
52-
}
53-
54-
func postFormToSalsaApi(path string, data url.Values, token string) (*http.Response, error) {
55-
postUrl := salsaApiUrl + path
56-
data.Add("private_token", token)
57-
return http.PostForm(postUrl, data)
58-
}
59-
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-
83-
func createSalsaWebhook(projectId int, webhookUrl, token string) error {
84-
postPath := "/projects/" + strconv.Itoa(projectId) + "/hooks"
85-
86-
response, err := postFormToSalsaApi(postPath,
87-
url.Values{
88-
"url": {webhookUrl},
89-
"push_events": {"true"},
90-
},
91-
token)
92-
if err != nil {
93-
return err
94-
}
95-
96-
if response.StatusCode != http.StatusCreated {
97-
responseData, _ := ioutil.ReadAll(response.Body)
98-
99-
return fmt.Errorf("http status %d: %s",
100-
response.StatusCode,
101-
responseData)
102-
}
103-
104-
return nil
105-
}
106-
107-
func mustGetProjectName(args []string) string {
10813
fs := flag.NewFlagSet("search", flag.ExitOnError)
10914

11015
if err := fs.Parse(args); err != nil {
@@ -118,46 +23,19 @@ func mustGetProjectName(args []string) string {
11823

11924
projectName := fs.Arg(0)
12025

121-
return projectName
122-
}
123-
124-
func mustGetSalsaToken() string {
125-
token := os.Getenv("SALSA_TOKEN")
126-
if token == "" {
127-
log.Printf("Please set the SALSA_TOKEN environment variable.\n")
128-
log.Fatalf("Obtain it from the following page: https://salsa.debian.org/profile/personal_access_tokens\n")
129-
}
130-
return token
131-
}
26+
// The source code of the corresponding server can be found at:
27+
// https://github.com/Debian/pkg-go-tools/tree/master/cmd/pgt-api-server
28+
u, _ := url.Parse("https://pgt-api-server.debian.net/v1/createrepo")
29+
q := u.Query()
30+
q.Set("repo", projectName)
31+
u.RawQuery = q.Encode()
13232

133-
func createSalsaProject(projectName, token string) (*gitlabProject, error) {
134-
response, err := postFormToSalsaApi("/projects",
135-
url.Values{
136-
"private_token": {token},
137-
"path": {projectName},
138-
"namespace_id": {strconv.Itoa(salsaGroupID)},
139-
"description": {fmt.Sprintf("Debian packaging for %s", projectName)},
140-
"visibility": {"public"},
141-
},
142-
token)
33+
resp, err := http.Post(u.String(), "", nil)
14334
if err != nil {
144-
return nil, err
145-
}
146-
147-
if response.StatusCode != http.StatusCreated {
148-
responseData, err := ioutil.ReadAll(response.Body)
149-
if err != nil {
150-
return nil, err
151-
}
152-
153-
return nil, errors.New(fmt.Sprintf("http status %d: %s", response.StatusCode, responseData))
35+
log.Fatal(err)
15436
}
155-
156-
var project gitlabProject
157-
158-
if err := json.NewDecoder(response.Body).Decode(&project); err != nil {
159-
return nil, err
37+
if got, want := resp.StatusCode, http.StatusOK; got != want {
38+
b, _ := ioutil.ReadAll(resp.Body)
39+
log.Fatalf("unexpected HTTP status code: got %d, want %d (response: %s)", got, want, string(b))
16040
}
161-
162-
return &project, err
16341
}

0 commit comments

Comments
 (0)