-
-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: execute actions once all pipelines have been executed (#2040)
* fix: execute actions once all pipelines have been executed * execute actions once all pipelines have been executed * split long function into multiple small ones Signed-off-by: Olblak <me@olblak.com> * chore: change github pullrequest log level message Signed-off-by: Olblak <me@olblak.com> * fix: Don't run similar action multiple times Signed-off-by: Olblak <me@olblak.com> --------- Signed-off-by: Olblak <me@olblak.com>
- Loading branch information
Showing
7 changed files
with
182 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package engine | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/updatecli/updatecli/pkg/core/pipeline/action" | ||
"github.com/updatecli/updatecli/pkg/core/result" | ||
) | ||
|
||
// RunActions runs all actions defined in the configuration. | ||
// To avoid the situation where a pipeline close a pullrequest even thought the next pipeline need | ||
// to update the same pullrequest, we need to run pipeline actions once all pipelines' targets have been executed. | ||
// The goal is to respect an order where we first handle pipelines in ATTENTION state, then FAILURE, then SUCCESS and finally SKIPPED. | ||
// cfr https://github.com/updatecli/updatecli/issues/2039 | ||
|
||
// 1. ATTENTION: to update existing pull request | ||
// 2. FAILURE: which may clean up existing pull request | ||
// 3. SUCCESS: which may clean up existing pull request | ||
// 4. SKIPPED: which may clean up existing pull request | ||
// | ||
// It worth reminding that a pipeline can contain multiple actions. | ||
// If at least one action is in an attention state, | ||
// then the pipeline is considered in attention state as well. | ||
// So the same logic must be applied differently based on the different actions state. | ||
func (e *Engine) runActions() error { | ||
|
||
errs := []string{} | ||
|
||
logrus.Infof("\n\n%s\n", strings.ToTitle("Actions")) | ||
logrus.Infof("%s\n\n", strings.Repeat("=", len("Actions")+1)) | ||
|
||
// actionsHashTable is used to avoid running the same action multiple times. | ||
actionsHashTable := make(map[uint64]*action.Action) | ||
|
||
for _, pipelineState := range []string{result.ATTENTION, result.FAILURE, result.SUCCESS, result.SKIPPED} { | ||
for id := range e.Pipelines { | ||
pipeline := e.Pipelines[id] | ||
if len(pipeline.Actions) > 0 { | ||
if err := pipeline.RunActions(pipelineState, actionsHashTable); err != nil { | ||
errs = append(errs, err.Error()) | ||
pipeline.Report.Result = result.FAILURE | ||
logrus.Errorf("action stage:\t%q", err.Error()) | ||
continue | ||
} | ||
} | ||
} | ||
} | ||
if len(errs) > 0 { | ||
return fmt.Errorf( | ||
"errors occurred while running actions:\n\t* %s", | ||
strings.Join(errs, "\n\t* ")) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package engine | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// showReports display the reports | ||
// and return an error if at least one pipeline failed | ||
func (e *Engine) showReports() error { | ||
e.Reports.Sort() | ||
|
||
err := e.Reports.Show() | ||
if err != nil { | ||
return err | ||
} | ||
totalSuccessPipeline, totalChangedAppliedPipeline, totalFailedPipeline, totalSkippedPipeline := e.Reports.Summary() | ||
|
||
totalPipeline := totalSuccessPipeline + totalChangedAppliedPipeline + totalFailedPipeline + totalSkippedPipeline | ||
|
||
logrus.Infof("Run Summary") | ||
logrus.Infof("===========\n") | ||
logrus.Infof("Pipeline(s) run:") | ||
logrus.Infof(" * Changed:\t%d", totalChangedAppliedPipeline) | ||
logrus.Infof(" * Failed:\t%d", totalFailedPipeline) | ||
logrus.Infof(" * Skipped:\t%d", totalSkippedPipeline) | ||
logrus.Infof(" * Succeeded:\t%d", totalSuccessPipeline) | ||
logrus.Infof(" * Total:\t%d", totalPipeline) | ||
|
||
// Exit on error if at least one pipeline failed | ||
if totalFailedPipeline > 0 { | ||
return fmt.Errorf("%d over %d pipeline failed", totalFailedPipeline, totalPipeline) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package engine | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/updatecli/updatecli/pkg/core/cmdoptions" | ||
"github.com/updatecli/updatecli/pkg/core/udash" | ||
) | ||
|
||
// publishToUdash publish pipeline reports to the Udash service. | ||
// This service is still experimental and should be used with caution. | ||
// More information on https://github.com/updatecli/udash | ||
func (e *Engine) publishToUdash() error { | ||
|
||
errs := []string{} | ||
|
||
if !cmdoptions.Experimental { | ||
return nil | ||
} | ||
|
||
for id := range e.Pipelines { | ||
pipeline := e.Pipelines[id] | ||
if err := udash.Publish(&pipeline.Report); err != nil && | ||
!errors.Is(err, udash.ErrNoUdashBearerToken) && | ||
!errors.Is(err, udash.ErrNoUdashAPIURL) { | ||
errs = append(errs, pipeline.Name+err.Error()) | ||
} | ||
e.Pipelines[id] = pipeline | ||
} | ||
|
||
if len(errs) > 0 { | ||
return fmt.Errorf( | ||
"errors occurred while publishing to Udash:\n\t* %s", | ||
strings.Join(errs, "\n\t* "), | ||
) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters