-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (84 loc) · 2.68 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"context"
"fmt"
"os"
"time"
"github.com/bufbuild/connect-go"
"github.com/gitpod-io/gitpod/components/public-api/go/client"
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
"github.com/google/go-github/v55/github"
"golang.org/x/oauth2"
)
func ListAllOrgs() {
token := os.Getenv("GITPOD_PAT_TOKEN")
gitpod, err := client.New(client.WithCredentials(token))
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to construct gitpod client %v", err)
return
}
response, err := gitpod.Teams.ListTeams(context.Background(), connect.NewRequest(&v1.ListTeamsRequest{}))
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to list teams %v", err)
return
}
fmt.Fprintf(os.Stdout, "Retrieved teams %v", response.Msg.GetTeams())
}
func CreateProjectFromGitHubRepo(name, projectSlug, cloneUrl string) {
token := os.Getenv("GITPOD_PAT_TOKEN")
gitpod, err := client.New(client.WithCredentials(token))
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to construct gitpod client %v", err)
return
}
response, err := gitpod.Projects.CreateProject(context.Background(), connect.NewRequest(&v1.CreateProjectRequest{
Project: &v1.Project{
TeamId: os.Getenv("GITPOD_ORG_ID"),
Name: name,
Slug: projectSlug,
CloneUrl: cloneUrl,
},
}))
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create project %v", err)
return
}
fmt.Fprintf(os.Stdout, "Project has been successfully created\n %v\n", response.Msg)
}
func main() {
/* If you don't have your OrgId, Get your teamId by uncomment the following */
// ListAllOrgs()
// return;
/* Fetch All GitHub Repositories owned by a particular Org/User */
startTime := time.Now()
accessToken := os.Getenv("GITHUB_PAT_TOKEN")
githubOwner := os.Getenv("GITHUB_USERNAME") // e.g., "Siddhant-K-code"
ctx := context.Background()
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: accessToken},
)
tc := oauth2.NewClient(ctx, ts)
client := github.NewClient(tc)
opts := &github.RepositoryListOptions{
ListOptions: github.ListOptions{PerPage: 10},
}
var allRepos []*github.Repository
for {
repos, resp, err := client.Repositories.List(ctx, githubOwner, opts)
if err != nil {
fmt.Println("Error fetching repositories:", err)
return
}
allRepos = append(allRepos, repos...)
if resp.NextPage == 0 {
break
}
opts.Page = resp.NextPage
}
for _, repo := range allRepos {
// fmt.Printf("Name: %v; Repo URL: %v\n", *repo.Name, *repo.CloneURL)
CreateProjectFromGitHubRepo(*repo.Name, *repo.Name, *repo.CloneURL)
}
endTime := time.Now()
fmt.Printf("\n\nTime taken to Fetch %v GitHub repos & add it to Gitpod projects: %v\n", len(allRepos), endTime.Sub(startTime))
}