Skip to content

Commit cc60adb

Browse files
aviaustapelberg
authored andcommitted
add create-salsa-project command (#83)
1 parent 5a5180d commit cc60adb

File tree

2 files changed

+136
-0
lines changed

2 files changed

+136
-0
lines changed

create_salsa_project.go

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"flag"
7+
"fmt"
8+
"io/ioutil"
9+
"log"
10+
"net/http"
11+
"net/url"
12+
"os"
13+
"strconv"
14+
)
15+
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+
27+
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+
}
47+
48+
func postFormToSalsaApi(path string, data url.Values, token string) (*http.Response, error) {
49+
postUrl := salsaApiUrl + path
50+
data.Add("private_token", token)
51+
return http.PostForm(postUrl, data)
52+
}
53+
54+
func createSalsaWebhook(projectId int, webhookUrl, token string) error {
55+
postPath := "/projects/" + strconv.Itoa(projectId) + "/hooks"
56+
57+
response, err := postFormToSalsaApi(postPath,
58+
url.Values{
59+
"url": {webhookUrl},
60+
"push_events": {"true"},
61+
},
62+
token)
63+
if err != nil {
64+
return err
65+
}
66+
67+
if response.StatusCode != http.StatusCreated {
68+
responseData, _ := ioutil.ReadAll(response.Body)
69+
70+
return fmt.Errorf("http status %d: %s",
71+
response.StatusCode,
72+
responseData)
73+
}
74+
75+
return nil
76+
}
77+
78+
func mustGetProjectName(args []string) string {
79+
fs := flag.NewFlagSet("search", flag.ExitOnError)
80+
81+
if err := fs.Parse(args); err != nil {
82+
log.Fatal(err)
83+
}
84+
85+
if fs.NArg() != 1 {
86+
log.Printf("Usage: %s create-salsa-project <project-name>\n", os.Args[0])
87+
log.Fatalf("Example: %s create-salsa-project golang-github-mattn-go-sqlite3\n", os.Args[0])
88+
}
89+
90+
projectName := fs.Arg(0)
91+
92+
return projectName
93+
}
94+
95+
func mustGetSalsaToken() string {
96+
token := os.Getenv("SALSA_TOKEN")
97+
if token == "" {
98+
log.Printf("Please set the SALSA_TOKEN environment variable.\n")
99+
log.Fatalf("Obtain it from the following page: https://salsa.debian.org/profile/personal_access_tokens\n")
100+
}
101+
return token
102+
}
103+
104+
func createSalsaProject(projectName, token string) (*gitlabProject, error) {
105+
response, err := postFormToSalsaApi("/projects",
106+
url.Values{
107+
"private_token": {token},
108+
"path": {projectName},
109+
"namespace_id": {strconv.Itoa(salsaGroupID)},
110+
"description": {fmt.Sprintf("Debian packaging for %s", projectName)},
111+
"visibility": {"public"},
112+
},
113+
token)
114+
if err != nil {
115+
return nil, err
116+
}
117+
118+
if response.StatusCode != http.StatusCreated {
119+
responseData, err := ioutil.ReadAll(response.Body)
120+
if err != nil {
121+
return nil, err
122+
}
123+
124+
return nil, errors.New(fmt.Sprintf("http status %d: %s", response.StatusCode, responseData))
125+
}
126+
127+
var project gitlabProject
128+
129+
if err := json.NewDecoder(response.Body).Decode(&project); err != nil {
130+
return nil, err
131+
}
132+
133+
return &project, err
134+
}

main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ func main() {
1717
switch cmd {
1818
case "search":
1919
execSearch(args[1:])
20+
case "create-salsa-project":
21+
execCreateSalsaProject(args[1:])
2022
default:
2123
execMake(args)
2224
}

0 commit comments

Comments
 (0)