Skip to content

Commit

Permalink
added case for rerun job for expried failed workflow
Browse files Browse the repository at this point in the history
Co-authored-by: Madhav Jivrajani <[email protected]>
  • Loading branch information
AvineshTripathi and MadhavJivrajani committed Mar 5, 2024
1 parent e035ba9 commit b62deb5
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
2 changes: 2 additions & 0 deletions prow/github/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,8 @@ func (c *client) requestRetryWithContext(ctx context.Context, method, path, acce
} else if errors.Is(err, &appsAuthError{}) {
c.logger.WithError(err).Error("Stopping retry due to appsAuthError")
return resp, err
} else if strings.Contains(err.Error(), "Unable to retry this workflow run because it was created over a month ago") {
return resp, err
} else if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
return resp, err
} else {
Expand Down
5 changes: 4 additions & 1 deletion prow/github/fakegithub/fakegithub.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ type FakeClient struct {
// ListIssueCommentsWithContextError will be returned if set when ListIssueCommentsWithContext is called
ListIssueCommentsWithContextError error

// TriggerFailedGitHubWorkflowError will be returned if set when TriggerFailedGitHubWorkflow is called
TriggerFailedGitHubWorkflowError error

// WasLabelAddedByHumanVal determines the return of the method with the same name
WasLabelAddedByHumanVal bool

Expand Down Expand Up @@ -1194,7 +1197,7 @@ func (f *FakeClient) TriggerGitHubWorkflow(org, repo string, id int) error {
}

func (f *FakeClient) TriggerFailedGitHubWorkflow(org, repo string, id int) error {
return nil
return f.TriggerFailedGitHubWorkflowError
}

func (f *FakeClient) RequestReview(org, repo string, number int, logins []string) error {
Expand Down
31 changes: 27 additions & 4 deletions prow/plugins/trigger/generic-comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package trigger

import (
"fmt"
"strings"
"sync"

"github.com/sirupsen/logrus"
"k8s.io/test-infra/prow/kube"
Expand Down Expand Up @@ -148,21 +150,42 @@ func handleGenericComment(c Client, trigger plugins.Trigger, gc github.GenericCo
if err != nil {
c.Logger.Errorf("%v: unable to get failed github action runs for branch %v", err, pr.Head.Ref)
} else {
var expiredWorkflows []string
var wg sync.WaitGroup
var mu sync.Mutex

for _, run := range failedRuns {
log := c.Logger.WithFields(logrus.Fields{
"runID": run.ID,
"runName": run.Name,
"org": org,
"repo": repo,
})
runID := run.ID
go func() {
wg.Add(1)
go func(runName string, runID int, log *logrus.Entry) {
defer wg.Done()

if err := c.GitHubClient.TriggerFailedGitHubWorkflow(org, repo, runID); err != nil {
log.Errorf("attempt to trigger github run failed: %v", err)
log.Errorf("attempt to trigger github run failed for %v: %v", runName, err)
if strings.Contains(err.Error(), "Unable to retry this workflow run because it was created over a month ago") {
mu.Lock()
expiredWorkflows = append(expiredWorkflows, runName)
mu.Unlock()
}
} else {
log.Infof("successfully triggered action run")
}
}()
}(run.Name, run.ID, log)
}

wg.Wait()

if len(expiredWorkflows) > 0 {
comment := fmt.Sprintf(`The following workflows could not be re-triggered because they are now expired (created over a month ago):
- %s
`, strings.Join(expiredWorkflows, "\n- "))
c.Logger.Infof("Commenting \"%s\".", comment)
c.GitHubClient.CreateComment(org, repo, number, plugins.FormatResponseRaw(gc.Body, gc.HTMLURL, gc.User.Login, comment))
}
}
}
Expand Down
36 changes: 36 additions & 0 deletions prow/plugins/trigger/generic-comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package trigger

import (
"errors"
"fmt"
"log"
"reflect"
Expand Down Expand Up @@ -1481,3 +1482,38 @@ func TestRetestFilter(t *testing.T) {
})
}
}

func TestFailingWorkflow(t *testing.T) {
var testCases = []struct {
name string
triggerFailedGitHubWorkflowError error
expectedMessage string
}{
{
name: "riggered workflow failed",
triggerFailedGitHubWorkflowError: nil,
expectedMessage: "",
},
{
name: "Triggered workflow failed due to expired workflow",
triggerFailedGitHubWorkflowError: errors.New(`{"message": "Unable to retry this workflow run because it was created over a month ago","documentation_url": "https://docs.github.com/rest/actions/workflow-runs#re-run-failed-jobs-from-a-workflow-run"}`),
expectedMessage: `{"message": "Unable to retry this workflow run because it was created over a month ago","documentation_url": "https://docs.github.com/rest/actions/workflow-runs#re-run-failed-jobs-from-a-workflow-run"}`,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
c := fakegithub.NewFakeClient()
c.TriggerFailedGitHubWorkflowError = tc.triggerFailedGitHubWorkflowError
err := c.TriggerFailedGitHubWorkflow("org", "repo", 1)
if c.TriggerFailedGitHubWorkflowError == nil && err != nil {
t.Errorf("Expected error message:\n%s\nGot:\n%s", tc.expectedMessage, err.Error())
}
if c.TriggerFailedGitHubWorkflowError != nil && err == nil{
t.Errorf("Expected error message:\n%s\nGot:\n<nil>", tc.expectedMessage)
}
if c.TriggerFailedGitHubWorkflowError != nil && err.Error() != tc.expectedMessage {
t.Errorf("Expected error message:\n%s\nGot:\n%s", tc.expectedMessage, err.Error())
}
})
}
}

0 comments on commit b62deb5

Please sign in to comment.