-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
214 lines (181 loc) · 5.29 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// package main
// import (
// "fmt"
// "log"
// "os"
// "os/exec"
// "time"
// git "gopkg.in/src-d/go-git.v4"
// "gopkg.in/src-d/go-git.v4/plumbing/object"
// )
// func main() {
// // Get the current working directory instead of creating a new one
// workDir, err := os.Getwd()
// if err != nil {
// log.Fatalf("Error getting working directory: %v", err)
// }
// // Open the existing repository instead of cloning
// repo, err := git.PlainOpen(workDir)
// if err != nil {
// log.Fatalf("Error opening repository: %v", err)
// }
// // Make changes to README.md
// filePath := workDir + "/README.md"
// file, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
// if err != nil {
// log.Fatalf("Error opening file: %v", err)
// }
// defer file.Close()
// _, err = file.WriteString(fmt.Sprintf("\nContribution made on %s", time.Now().Format(time.RFC1123)))
// if err != nil {
// log.Fatalf("Error writing to file: %v", err)
// }
// fmt.Println("Changes made successfully!")
// // Commit the changes
// worktree, err := repo.Worktree()
// if err != nil {
// log.Fatalf("Error getting worktree: %v", err)
// }
// _, err = worktree.Add("README.md")
// if err != nil {
// log.Fatalf("Error staging file: %v", err)
// }
// commit, err := worktree.Commit("Updated README.md with a new contribution", &git.CommitOptions{
// Author: &object.Signature{
// Name: "github-actions[bot]",
// Email: "github-actions[bot]@users.noreply.github.com",
// When: time.Now(),
// },
// })
// if err != nil {
// log.Fatalf("Error committing changes: %v", err)
// }
// fmt.Printf("Commit created: %s\n", commit)
// // Push using git command line instead of go-git
// pushCmd := "git push origin HEAD:main"
// output, err := exec.Command("sh", "-c", pushCmd).CombinedOutput()
// if err != nil {
// log.Fatalf("Error pushing changes: %v\nOutput: %s", err, string(output))
// }
// fmt.Println("Changes pushed successfully!")
// }
package main
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"time"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/object"
"gopkg.in/src-d/go-git.v4/plumbing/transport/http"
)
// Repository struct to hold repo information
type Repository struct {
Name string
URL string
}
func main() {
token := os.Getenv("GITHUB_TOKEN")
if token == "" {
log.Fatal("GITHUB_TOKEN environment variable not set")
}
// List of target repositories
repositories := []Repository{
{
Name: "VR-Security-Assignment",
URL: "https://github.com/RickDeb2004/VR-Security-Assignment",
},
{
Name: "VR",
URL: "https://github.com/RickDeb2004/vr",
},
// Add more repositories here as needed
}
baseDir := "./repos"
if err := os.MkdirAll(baseDir, 0755); err != nil {
log.Fatalf("Error creating base directory: %v", err)
}
for _, repo := range repositories {
processRepository(repo, baseDir, token)
}
}
func processRepository(repo Repository, baseDir, token string) {
repoDir := filepath.Join(baseDir, repo.Name)
repoURL := fmt.Sprintf("https://RickDeb2004:%s@%s", token, repo.URL[8:]) // Convert https://github.com to include token
// Clone repository
fmt.Printf("Cloning %s...\n", repo.Name)
r, err := git.PlainClone(repoDir, false, &git.CloneOptions{
URL: repoURL,
Auth: &http.BasicAuth{
Username: "RickDeb2004",
Password: token,
},
})
if err != nil {
log.Printf("Error cloning %s: %v\n", repo.Name, err)
return
}
// Make 20 commits
for i := 1; i <= 20; i++ {
if err := makeCommit(r, repoDir, i, repo.Name); err != nil {
log.Printf("Error making commit %d for %s: %v\n", i, repo.Name, err)
continue
}
// Push after each commit
if err := pushChanges(repoDir, token); err != nil {
log.Printf("Error pushing commit %d for %s: %v\n", i, repo.Name, err)
continue
}
// Add delay between commits
time.Sleep(2 * time.Second)
}
}
func makeCommit(r *git.Repository, repoDir string, commitNum int, repoName string) error {
// Update README.md
readmePath := filepath.Join(repoDir, "README.md")
file, err := os.OpenFile(readmePath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf("error opening README: %v", err)
}
defer file.Close()
commitMsg := fmt.Sprintf("\nCommit #%d made on %s for %s\n",
commitNum,
time.Now().Format(time.RFC1123),
repoName)
if _, err := file.WriteString(commitMsg); err != nil {
return fmt.Errorf("error writing to README: %v", err)
}
// Stage changes
w, err := r.Worktree()
if err != nil {
return fmt.Errorf("error getting worktree: %v", err)
}
_, err = w.Add("README.md")
if err != nil {
return fmt.Errorf("error staging file: %v", err)
}
// Commit changes
_, err = w.Commit(fmt.Sprintf("Update #%d: Daily contribution", commitNum), &git.CommitOptions{
Author: &object.Signature{
Name: "RickDeb2004",
Email: "[email protected]",
When: time.Now(),
},
})
return err
}
func pushChanges(repoDir string, token string) error {
cmd := exec.Command("git", "push", "origin", "main")
cmd.Dir = repoDir
cmd.Env = append(os.Environ(),
fmt.Sprintf("GIT_ASKPASS=echo"),
fmt.Sprintf("GIT_USERNAME=RickDeb2004"),
fmt.Sprintf("GIT_PASSWORD=%s", token))
output, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("push error: %v, output: %s", err, string(output))
}
return nil
}