diff --git a/Program.cs b/Program.cs index 1a13d95..2cea834 100644 --- a/Program.cs +++ b/Program.cs @@ -6,6 +6,7 @@ new LoadingUnloading().Save(); Environment.Exit(0); // User canceled program }; +Console.WriteLine($"{DateTime.Now} | Program started..."); ConsoleHelp con = new ConsoleHelp(); Github git = new Github(); Balena bel = new Balena(); @@ -18,18 +19,21 @@ while (true) { // Version checking with github logic - may be organized into it's own class down the road + bool updated = false; if (git.NeedsCloned()) { - if (git.NeedsPulled()) - { - git.PullRepo(); // Update - if (BalenaStatus.NeedsAuth) bel.BalenaLogin(); // Prompt for login if initial run - bel.FleetPush(); // Push update to balena for building - if (!String.IsNullOrEmpty(SettingsStatic.Settings.webhook)) Discord.SendWebhook(); - Console.WriteLine($"{DateTime.Now} | Fleet updated!"); - } - Console.WriteLine($"{DateTime.Now} | Update not needed"); + git.CloneRepo(); } + if (git.NeedsPulled()) + { + git.PullRepo(); // Update + if (BalenaStatus.NeedsAuth) bel.BalenaLogin(); // Prompt for login if initial run + bel.FleetPush(); // Push update to balena for building + if (!String.IsNullOrEmpty(SettingsStatic.Settings.webhook)) Discord.SendWebhook(); + Console.WriteLine($"{DateTime.Now} | Fleet updated!"); + updated = true; + } + if (!updated) { Console.WriteLine($"{DateTime.Now} | Update not needed"); } Thread.Sleep(60000); } diff --git a/Tools/Github.cs b/Tools/Github.cs index 7d0c87e..cdc3203 100644 --- a/Tools/Github.cs +++ b/Tools/Github.cs @@ -40,25 +40,34 @@ public bool PullRepo() { } private string CurrentCommitHash() { string current = ""; - using (var repo = new Repository(_gitLoc)) - { - foreach (Branch b in repo.Branches.Where(b => !b.IsRemote)) + try { + using (var repo = new Repository(_gitLoc)) { - if (b.FriendlyName.Contains("master")) { - var logs = b.TrackedBranch.Commits; - foreach (var item in logs) + foreach (Branch b in repo.Branches.Where(b => !b.IsRemote)) + { + if (b.FriendlyName.Contains("master")) { - var tmp = item.Id; - current = tmp.Sha; + var logs = b.TrackedBranch.Commits; + foreach (var item in logs) + { + var tmp = item.Id; + current = tmp.Sha; break; // we just want the newest (first listed) + } } } } + } catch { // repo needs cloned } + return current; } public bool NeedsPulled() { - if (CurrentCommitHash() != SettingsStatic.Settings.CurrentCommit) return true; + var curHash = CurrentCommitHash(); + if (curHash != SettingsStatic.Settings.CurrentCommit) { + SettingsStatic.Settings.CurrentCommit = curHash; + return true; + } return false; } }