mirror of
https://github.com/idanoo/NZCovidBot
synced 2025-07-03 04:02:14 +00:00
- Skip invalid webhooks
- Build cache based on row data instead of entire row - Compare columns instead of entire row - Removed commitHash check and instead just processes on succesfull git pull - No more entire file dumps!
This commit is contained in:
parent
ae11cc53a3
commit
86eef8cc8e
5 changed files with 102 additions and 132 deletions
|
@ -1,22 +1,17 @@
|
|||
package nzcovidbot
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/go-git/go-git/v5"
|
||||
"github.com/go-git/go-git/v5/plumbing"
|
||||
"github.com/waigani/diffparser"
|
||||
)
|
||||
|
||||
// Repo URL
|
||||
var gitRepo *git.Repository
|
||||
|
||||
// Current hash we have parsed
|
||||
var commitHash string
|
||||
|
||||
// Location to store tmp data
|
||||
var tmpDirectory = "./tmp"
|
||||
|
||||
|
@ -33,9 +28,6 @@ func loadRepo(repository string) {
|
|||
|
||||
// Preload cache data of current rows
|
||||
loadRepoIntoCache(tmpDirectory + "/repo")
|
||||
|
||||
commitHash := getCommitHash()
|
||||
log.Printf("Last reported hash: %s", commitHash)
|
||||
gitRepo = r
|
||||
}
|
||||
|
||||
|
@ -57,50 +49,28 @@ func cloneRepo(repository string) *git.Repository {
|
|||
log.Fatal(err)
|
||||
}
|
||||
|
||||
ref, err := r.Head()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
setCommitHash(ref.Hash().String())
|
||||
|
||||
log.Println("Succesfully cloned repo")
|
||||
return r
|
||||
}
|
||||
|
||||
// Set it in memory + write to disk
|
||||
func setCommitHash(hash string) error {
|
||||
log.Printf("Update hash to: %s", hash)
|
||||
commitHash = hash
|
||||
return os.WriteFile(tmpDirectory+"/commithash", []byte(hash), 0644)
|
||||
}
|
||||
|
||||
// Read from memory, or disk
|
||||
func getCommitHash() string {
|
||||
if commitHash != "" {
|
||||
return commitHash
|
||||
}
|
||||
|
||||
hash, err := ioutil.ReadFile(tmpDirectory + "/commithash")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
commitHash = string(hash)
|
||||
return commitHash
|
||||
}
|
||||
|
||||
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.Fatal(err)
|
||||
}
|
||||
currentHash := currentHead.Hash()
|
||||
|
||||
// Pull latest changes if exist
|
||||
worktree, err := gitRepo.Worktree()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = worktree.Pull(&pullOptions)
|
||||
if err != nil {
|
||||
if err == git.NoErrAlreadyUpToDate {
|
||||
|
@ -111,7 +81,7 @@ func checkForUpdates() {
|
|||
}
|
||||
}
|
||||
|
||||
// Get current commit hash
|
||||
// Get current commit hash POST PULL
|
||||
head, err := gitRepo.Head()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
|
@ -124,7 +94,7 @@ func checkForUpdates() {
|
|||
}
|
||||
|
||||
// Get current commit
|
||||
currentCommit, err := gitRepo.CommitObject(plumbing.NewHash(getCommitHash()))
|
||||
currentCommit, err := gitRepo.CommitObject(currentHash)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
@ -153,27 +123,15 @@ func checkForUpdates() {
|
|||
}
|
||||
|
||||
if line.Mode == diffparser.ADDED {
|
||||
parseCsvRow("ADDED", line.Content)
|
||||
parseCsvRow(line.Content)
|
||||
}
|
||||
// switch changeType := line.Mode; changeType {
|
||||
// case diffparser.UNCHANGED:
|
||||
// continue
|
||||
// case diffparser.ADDED:
|
||||
// parseCsvRow("ADDED", line.Content)
|
||||
// case diffparser.REMOVED:
|
||||
// continue
|
||||
// // To re-add in future?
|
||||
// // parseCsvRow("REMOVED", line.Content)
|
||||
// }
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Store current hash for future comparisons
|
||||
setCommitHash(head.Hash().String())
|
||||
|
||||
// SEND IT o/---->
|
||||
postTheUpdates()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue