-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
150 lines (122 loc) · 3.88 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
package main
import (
"flag"
"fmt"
lorem "github.com/drhodes/golorem"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/lmittmann/tint"
"golang.org/x/exp/slog"
"log"
"os"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing/object"
)
type Options struct {
Repo string
UserName string
AccessToken string
EmailAddress string
CommitsPerDay int
WorkDaysOnly bool
StartDate string
EndDate string
}
var (
logger = slog.New(tint.NewHandler(os.Stderr, nil))
)
func main() {
// set global logger with custom options
slog.SetDefault(slog.New(
tint.NewHandler(os.Stderr, &tint.Options{
Level: slog.LevelError,
TimeFormat: time.Kitchen,
}),
))
opts := new(Options)
flag.IntVar(&opts.CommitsPerDay, "commitsPerDay", 10, "number of commits per day")
flag.BoolVar(&opts.WorkDaysOnly, "workdaysOnly", false, "only workdays")
flag.StringVar(&opts.StartDate, "startDate", "", "start date")
flag.StringVar(&opts.EndDate, "endDate", "", "end date")
flag.StringVar(&opts.Repo, "repo", "", "your private repo url (e.g. https://github.com/navicstein/fake-contributions-repo.git")
flag.StringVar(&opts.UserName, "username", "navicstein", "your github username")
flag.StringVar(&opts.AccessToken, "accessToken", "", "your github access token used for pushing & cloning private repos")
flag.StringVar(&opts.EmailAddress, "emailAddress", "navicsteinrotciv@gmail.com", "your commit email address")
flag.Parse()
if err := runFakeContributions(opts); err != nil {
panic(err)
}
logger.Info("If you're using this tool, please consider giving me a star on GitHub, I'll appreciate it :)")
}
func runFakeContributions(opts *Options) (err error) {
var (
tmpFolder = "tmp-contributions-repo"
commitsPerDay = opts.CommitsPerDay
workdaysOnly = opts.WorkDaysOnly
startDateStr = opts.StartDate
endDateStr = opts.EndDate
)
commitsPerDayIntervals, err := parseCommitsPerDay(commitsPerDay)
if err != nil {
log.Panicf("Error parsing commitsPerDay: %s", err)
return
}
startDate := parseDateOrDefault(startDateStr, time.Now().AddDate(-1, 0, 0))
endDate := parseDateOrDefault(endDateStr, time.Now())
commitDateList := createCommitDateList(commitsPerDayIntervals, workdaysOnly, startDate, endDate)
// Remove git history folder in case if it already exists.
if _, err := os.Stat(tmpFolder); !os.IsNotExist(err) {
if err := os.RemoveAll(tmpFolder); err != nil {
return err
}
}
// Create git history folder.
_ = os.Mkdir(tmpFolder, os.ModePerm)
// Clone the repository
logger.Info("Cloning repository into", "path", tmpFolder, "cloneUrl", opts.Repo)
repo, err := git.PlainClone(tmpFolder, false, &git.CloneOptions{
Auth: &http.BasicAuth{
Username: opts.UserName,
Password: opts.AccessToken,
},
Progress: os.Stdout,
URL: opts.Repo,
})
if err != nil {
return
}
// Create commits.
for _, date := range commitDateList {
dateFormatted := date.Format("January 2, 2006")
msg := lorem.Sentence(4, 8)
w, err := repo.Worktree()
if err != nil {
return fmt.Errorf("unable to create worktree: %+v", err)
}
_, err = w.Commit(msg, &git.CommitOptions{
Author: &object.Signature{
Name: opts.UserName,
Email: opts.EmailAddress,
When: date,
},
})
if err != nil {
return fmt.Errorf("unable to commit to the repository: %+v", err)
}
logger.Debug("Generating your GitHub activity", "date", dateFormatted, "message", msg)
}
logger.Info("Total commits have been created:", "length", len(commitDateList))
// Push commits to the repository.
logger.Info("Pushing commits to the repository: ", "repo", opts.Repo)
pushOpts := git.PushOptions{
Auth: &http.BasicAuth{
Username: opts.UserName,
Password: opts.AccessToken,
},
Progress: os.Stdout,
}
if err := repo.Push(&pushOpts); err != nil {
return fmt.Errorf("unable to push to the repository: %+v", err)
}
return nil
}