private GithubCommit FetchCommit(string _ref) { if (_ref == null) { Log("Unable to fetch commit: Head - Reference was null"); Console.WriteLine("Unable to fetch commit: Head-Reference was null"); return(null); } string destination = "https://api.github.com/repos/" + repoOwner + "/" + repository + "/commits/" + _ref; HttpClient cli = new HttpClient(); cli.DefaultRequestHeaders.UserAgent.TryParseAdd("c#app"); cli.DefaultRequestHeaders.Add("Authorization", "Bearer " + githubOAuthToken); HttpResponseMessage response = cli.GetAsync(destination).Result; if (response.IsSuccessStatusCode) { string body = response.Content.ReadAsStringAsync().Result; return(GithubCommit.FromJson(body)); } Log("Unable to parse github push event: Commit could not be fetched!"); //Console.WriteLine("Unable to parse github push event: Commit could not be fetched!"); return(null); }
public async Task <string> SendToGithub(Meetup[] meetups) { var meetupsstring = JsonSerializer.Serialize(meetups); string base64Encoded = Convert.ToBase64String(Encoding.UTF8.GetBytes(meetupsstring)); httpClient.DefaultRequestHeaders.Add("User-Agent", "insertyourusernamehere"); httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", "Token insertyourtokenhere"); var returnGithub = await httpClient.GetAsync("https://api.github.com/repos/DevConGalaxy/DevConGalaxy.github.io/contents/src/assets/meetupsdata.json"); GithubCommit githubCommit; if (returnGithub.StatusCode == System.Net.HttpStatusCode.NotFound) { githubCommit = new GithubCommit("insertyourusernamehere", "insertyouremailhere", "Adding meetupsData.json", base64Encoded); } else { var githubResultSearchString = await returnGithub.Content.ReadAsStringAsync(); var githubResultSearch = JsonSerializer.Deserialize <GithubResultSearch>(githubResultSearchString); githubCommit = new GithubCommit("insertyourusernamehere", "insertyouremailhere", "Updating meetupsData.json", base64Encoded, sha: githubResultSearch.sha); } var stringCommit = JsonSerializer.Serialize(githubCommit); var stringContent = new StringContent(stringCommit); var resultAddFile = await httpClient.PutAsync("https://api.github.com/repos/DevConGalaxy/DevConGalaxy.github.io/contents/src/assets/meetupsdata.json", stringContent); return(base64Encoded); }
private void ProcessEvent(WebhookPushEvent _event) { Pusher pusher = _event.Pusher; string name = pusher.Name; Log("Webhook received: New Push from " + name); string commitSha = _event.HeadCommit; GithubCommit commit = FetchCommit(commitSha); if (commit == null) { return; } GithubWebhookService.Models.File[] changedFiles = commit.Files; foreach (GithubWebhookService.Models.File changedFile in changedFiles) { string filename = changedFile.Filename; if (filename.Contains("surveyQuestionService.json")) { Log("Changes to Consul-Configuration have been detected!"); Console.WriteLine("Changes to Consul-Configuration have been detected!"); } } }
private void StartBuild(GithubCommit commit) { var project = PhotonServer.Instance.Projects.FirstOrDefault(x => string.Equals((x.Source as ProjectGithubSource)?.CloneUrl, commit.RepositoryUrl, StringComparison.OrdinalIgnoreCase)); if (project == null) { throw new ApplicationException($"No project found matching git url '{commit.RepositoryUrl}'!"); } var source = (ProjectGithubSource)project.Source; var projectData = PhotonServer.Instance.ProjectData.GetOrCreate(project.Id); var buildNumber = projectData.StartNewBuild(); var session = new ServerBuildSession { Project = project, AssemblyFilename = project.AssemblyFile, PreBuild = project.PreBuild, TaskName = source.HookTaskName, GitRefspec = commit.Refspec, BuildNumber = buildNumber, Roles = source.HookTaskRoles, Commit = commit, }; if (source.NotifyOrigin == NotifyOrigin.Server && !string.IsNullOrEmpty(commit.Sha)) { ApplyGithubNotification(session, source, commit); } PhotonServer.Instance.Sessions.BeginSession(session); PhotonServer.Instance.Queue.Add(session); }
public static Commit ToCommitEntity(this GithubCommit githubCommit, string username, string repository) { if (githubCommit == null) { throw new ArgumentNullException(nameof(githubCommit)); } return(new Commit { UserName = username, Repository = repository, Sha = githubCommit.Sha, Message = githubCommit.Commit?.Message, Committer = githubCommit.Commit?.Committer.ToCommitAuthor() }); }
private async Task StartBuild(GithubCommit commit) { var project = PhotonServer.Instance.Projects.All.FirstOrDefault(x => string.Equals((x.Description.Source as ProjectGithubSource)?.CloneUrl, commit.RepositoryUrl, StringComparison.OrdinalIgnoreCase)); if (project == null) { throw new ApplicationException($"No project found matching git url '{commit.RepositoryUrl}'!"); } var source = (ProjectGithubSource)project.Description.Source; var build = await project.StartNewBuild(); build.TaskName = source.HookTaskName; build.TaskRoles = source.HookTaskRoles; build.PreBuildCommand = project.Description.PreBuild; build.AssemblyFilename = project.Description.AssemblyFile; build.GitRefspec = commit.Refspec; build.Commit = commit; var session = new ServerBuildSession { Project = project.Description, AssemblyFilename = project.Description.AssemblyFile, PreBuild = project.Description.PreBuild, TaskName = source.HookTaskName, GitRefspec = commit.Refspec, Build = build, Roles = source.HookTaskRoles, Commit = commit, }; build.ServerSessionId = session.SessionId; if (source.NotifyOrigin == NotifyOrigin.Server && !string.IsNullOrEmpty(commit.Sha)) { ApplyGithubNotification(session, source, commit); } PhotonServer.Instance.Sessions.BeginSession(session); PhotonServer.Instance.Queue.Add(session); }
private void ApplyGithubNotification(ServerBuildSession session, ProjectGithubSource source, GithubCommit commit) { var su = new CommitStatusUpdater { Username = source.Username, Password = source.Password, StatusUrl = commit.StatusesUrl, Sha = commit.Sha, }; session.PreBuildEvent += (o, e) => { var status = new CommitStatus { State = CommitStates.Pending, Context = "Photon", Description = "Build in progress..." }; su.Post(status).GetAwaiter().GetResult(); }; session.PostBuildEvent += (o, e) => { var status = new CommitStatus { Context = "Photon", }; if (session.Result.Cancelled) { status.State = CommitStates.Error; status.Description = "The build was cancelled."; } else if (session.Result.Successful) { status.State = CommitStates.Success; status.Description = $"Build Successful. {session.Result.Message}"; } else { status.State = CommitStates.Failure; status.Description = $"Build Failed! {session.Result.Message}"; } su.Post(status).GetAwaiter().GetResult(); }; }