Sfoglia il codice sorgente

we do not need channels

Mohammed Nafees 3 anni fa
parent
commit
80cd2bd36a
1 ha cambiato i file con 9 aggiunte e 20 eliminazioni
  1. 9 20
      api/server/handlers/webhook/github_incoming.go

+ 9 - 20
api/server/handlers/webhook/github_incoming.go

@@ -7,7 +7,6 @@ import (
 	"strconv"
 	"strings"
 	"sync"
-	"time"
 
 	"github.com/bradleyfalzon/ghinstallation/v2"
 	"github.com/google/go-github/v41/github"
@@ -190,17 +189,16 @@ func (c *GithubIncomingWebhookHandler) processPullRequestEvent(event *github.Pul
 			// check for already running workflows we should be cancelling
 			var wg sync.WaitGroup
 			statuses := []string{"in_progress", "queued", "requested", "waiting"}
-			errChan := make(chan error, len(statuses))
+			chanErr := fmt.Errorf("")
 
 			wg.Add(len(statuses))
 
 			for _, status := range statuses {
 				go func(status string) {
-					reqCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
-					defer cancel()
+					defer wg.Done()
 
 					runs, _, err := client.Actions.ListWorkflowRunsByFileName(
-						reqCtx, owner, repo, fmt.Sprintf("porter_%s_env.yml", env.Name),
+						context.Background(), owner, repo, fmt.Sprintf("porter_%s_env.yml", env.Name),
 						&github.ListWorkflowRunsOptions{
 							Branch: event.GetPullRequest().GetHead().GetRef(),
 							Status: status,
@@ -209,30 +207,21 @@ func (c *GithubIncomingWebhookHandler) processPullRequestEvent(event *github.Pul
 
 					if err == nil {
 						for _, run := range runs.WorkflowRuns {
-							_, err := client.Actions.CancelWorkflowRunByID(reqCtx, owner, repo, run.GetID())
+							resp, err := client.Actions.CancelWorkflowRunByID(context.Background(), owner, repo, run.GetID())
 
-							if err != nil {
-								errChan <- fmt.Errorf("error cancelling %s: %w", run.GetHTMLURL(), err)
+							if err != nil && resp.StatusCode != http.StatusAccepted {
+								// the go library we are using returns a 202 Accepted status as an error
+								// in this case, we should rule this out as an error
+								chanErr = fmt.Errorf("%s: error cancelling %s: %w", chanErr.Error(), run.GetHTMLURL(), err)
 							}
 						}
 					} else {
-						errChan <- fmt.Errorf("error listing workflows for status %s: %w", status, err)
+						chanErr = fmt.Errorf("%s: error listing workflows for status %s: %w", chanErr.Error(), status, err)
 					}
-
-					wg.Done()
 				}(status)
 			}
 
 			wg.Wait()
-			close(errChan)
-
-			chanErr := fmt.Errorf("")
-
-			for err := range errChan {
-				chanErr = fmt.Errorf("%s: %w", chanErr.Error(), err)
-			}
-
-			fmt.Println("GOING TO DELETE DEPLOYMENT NOW")
 
 			err = c.deleteDeployment(r, depl, env, client)