mirror of
https://github.com/idanoo/NZCovidBot
synced 2025-07-01 11:12:15 +00:00
144 lines
2.9 KiB
Go
144 lines
2.9 KiB
Go
package nzcovidbot
|
|
|
|
import (
|
|
"log"
|
|
"os"
|
|
"strings"
|
|
|
|
"github.com/go-git/go-git/v5"
|
|
"github.com/waigani/diffparser"
|
|
)
|
|
|
|
// Repo URL
|
|
var gitRepo *git.Repository
|
|
|
|
// Location to store tmp data
|
|
var tmpDirectory = "./tmp"
|
|
|
|
// loadRepo Load repo into gitRepo var
|
|
func loadRepo(repository string) {
|
|
r, err := git.PlainOpen(tmpDirectory + "/repo")
|
|
if err != nil {
|
|
if err.Error() == "repository does not exist" {
|
|
r = cloneRepo(repository)
|
|
} else {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
// Preload cache data of current rows
|
|
loadRepoIntoCache(tmpDirectory + "/repo")
|
|
gitRepo = r
|
|
}
|
|
|
|
// cloneRepo Clone git repo
|
|
func cloneRepo(repository string) *git.Repository {
|
|
if _, err := os.Stat(tmpDirectory); os.IsNotExist(err) {
|
|
err = os.Mkdir(tmpDirectory, 0755)
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
}
|
|
|
|
r, err := git.PlainClone(tmpDirectory+"/repo", false, &git.CloneOptions{
|
|
URL: repository,
|
|
Progress: os.Stdout,
|
|
})
|
|
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
|
|
log.Println("Succesfully cloned repo")
|
|
return r
|
|
}
|
|
|
|
func checkForUpdates() {
|
|
// Fetch updates from remote
|
|
pullOptions := git.PullOptions{
|
|
RemoteName: "origin",
|
|
}
|
|
|
|
// Get current commit hash PRE PULL
|
|
currentHead, err := gitRepo.Head()
|
|
if err != nil {
|
|
log.Printf("Err getting head: %s", err)
|
|
return
|
|
}
|
|
currentHash := currentHead.Hash()
|
|
|
|
// Pull latest changes if exist
|
|
worktree, err := gitRepo.Worktree()
|
|
if err != nil {
|
|
log.Printf("Err getting worktree: %s", err)
|
|
}
|
|
err = worktree.Pull(&pullOptions)
|
|
if err != nil {
|
|
if err == git.NoErrAlreadyUpToDate {
|
|
log.Println("No updates")
|
|
return
|
|
} else {
|
|
log.Printf("Err pulling: %s", err)
|
|
return
|
|
}
|
|
}
|
|
|
|
// Get current commit hash POST PULL
|
|
head, err := gitRepo.Head()
|
|
if err != nil {
|
|
log.Printf("Err getting new head: %s", err)
|
|
return
|
|
}
|
|
|
|
// Get latest commit
|
|
latestCommit, err := gitRepo.CommitObject(head.Hash())
|
|
if err != nil {
|
|
log.Printf("Err getting latest commit: %s", err)
|
|
return
|
|
}
|
|
|
|
// Get current commit
|
|
currentCommit, err := gitRepo.CommitObject(currentHash)
|
|
if err != nil {
|
|
log.Printf("Err getting new commit: %s", err)
|
|
return
|
|
}
|
|
|
|
// Get patch of changes
|
|
patch, err := currentCommit.Patch(latestCommit)
|
|
if err != nil {
|
|
log.Printf("Err getting change patch: %s", err)
|
|
return
|
|
}
|
|
|
|
// Parse git diff
|
|
diff, err := diffparser.Parse(patch.String())
|
|
if err != nil {
|
|
log.Printf("Err parsing diff: %s", err)
|
|
return
|
|
}
|
|
|
|
// Loop through file changes
|
|
for _, file := range diff.Files {
|
|
if strings.HasSuffix(file.NewName, ".csv") {
|
|
if strings.HasPrefix(file.NewName, "locations-of-interest/") {
|
|
for _, hunk := range file.Hunks {
|
|
newRange := hunk.WholeRange
|
|
for _, line := range newRange.Lines {
|
|
if strings.Contains(line.Content, "Start,End,Advice") {
|
|
continue
|
|
}
|
|
|
|
if line.Mode == diffparser.ADDED {
|
|
parseCsvRow(line.Content)
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// SEND IT o/---->
|
|
postTheUpdates()
|
|
}
|