Skip to content

Commit

Permalink
Duplicate the check run even if we need to re-run the tests
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Orive <adrian.orive.oneca@gmail.com>
  • Loading branch information
Adirio committed Nov 20, 2020
1 parent 8685728 commit 6fbaf21
Showing 1 changed file with 25 additions and 26 deletions.
51 changes: 25 additions & 26 deletions verify/plugin.go
Expand Up @@ -72,21 +72,22 @@ func (p PRPlugin) processPR(pr *github.PullRequest) (conclusion, summary, text s
}

// processAndSubmit performs the checks and submits the result
func (p PRPlugin) processAndSubmit(env *ActionsEnv, checkRun *github.CheckRun) error {
func (p PRPlugin) processAndSubmit(env *ActionsEnv, checkRun *github.CheckRun) (*github.CheckRun, error) {
// Process the PR
conclusion, summary, text, procErr := p.processPR(env.Event.PullRequest)

// Update the check run
if err := p.finishCheckRun(env.Client, env.Owner, env.Repo, checkRun.GetID(), conclusion, summary, text); err != nil {
return err
checkRun, err := p.finishCheckRun(env.Client, env.Owner, env.Repo, checkRun.GetID(), conclusion, summary, text)
if err != nil {
return checkRun, err
}

// Return failure here too so that the whole suite fails (since the actions
// suite seems to ignore failing check runs when calculating general failure)
if procErr != nil {
return fmt.Errorf("failed: %v", procErr)
return checkRun, fmt.Errorf("failed: %v", procErr)
}
return nil
return checkRun, nil
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -159,7 +160,7 @@ func (p PRPlugin) getCheckRun(client *github.Client, owner, repo, headSHA string
// or if it exists but couldn't be updated.
func (p PRPlugin) resetCheckRun(client *github.Client, owner, repo string, headSHA string) (*github.CheckRun, error) {
checkRun, err := p.getCheckRun(client, owner, repo, headSHA)
// If it was created we don't need to update it, check its status
// If it errored, or it was created but not finished, we don't need to update it
if err != nil || Started.Equal(checkRun.GetStatus()) {
return checkRun, err
}
Expand Down Expand Up @@ -188,7 +189,7 @@ func (p PRPlugin) resetCheckRun(client *github.Client, owner, repo string, headS

// finishCheckRun updates the Check-Run with id checkRunID setting its output.
// It returns an error in case it couldn't be updated.
func (p PRPlugin) finishCheckRun(client *github.Client, owner, repo string, checkRunID int64, conclusion, summary, text string) error {
func (p PRPlugin) finishCheckRun(client *github.Client, owner, repo string, checkRunID int64, conclusion, summary, text string) (*github.CheckRun, error) {
p.Debugf("adding results to check run %q on %s/%s...", p.Name, owner, repo)

checkRun, updateResp, err := client.Checks.UpdateCheckRun(context.TODO(), owner, repo, checkRunID, github.UpdateCheckRunOptions{
Expand All @@ -206,9 +207,9 @@ func (p PRPlugin) finishCheckRun(client *github.Client, owner, repo string, chec
p.Debugf("updated run: %+v", checkRun)

if err != nil {
return fmt.Errorf("unable to update check run with results: %w", err)
return checkRun, fmt.Errorf("unable to update check run with results: %w", err)
}
return nil
return checkRun, nil
}

// duplicateCheckRun creates a new Check-Run with the same info as the provided one but for a new headSHA
Expand Down Expand Up @@ -265,32 +266,30 @@ func (p PRPlugin) entrypoint(env *ActionsEnv) (err error) {

// onOpen handles "open" actions
func (p PRPlugin) onOpen(env *ActionsEnv) error {
headSHA := env.Event.GetPullRequest().GetHead().GetSHA()

// Create the check run
checkRun, err := p.createCheckRun(env.Client, env.Owner, env.Repo, headSHA)
checkRun, err := p.createCheckRun(env.Client, env.Owner, env.Repo, env.Event.GetPullRequest().GetHead().GetSHA())
if err != nil {
return err
}

// Process the PR and submit the results
return p.processAndSubmit(env, checkRun)
_, err = p.processAndSubmit(env, checkRun)
return err
}

// onReopen handles "reopen" actions
func (p PRPlugin) onReopen(env *ActionsEnv) error {
headSHA := env.Event.GetPullRequest().GetHead().GetSHA()

// Get the check run
checkRun, err := p.getCheckRun(env.Client, env.Owner, env.Repo, headSHA)
checkRun, err := p.getCheckRun(env.Client, env.Owner, env.Repo, env.Event.GetPullRequest().GetHead().GetSHA())
if err != nil {
return err
}

// Rerun the tests if they weren't finished
if !Finished.Equal(checkRun.GetStatus()) {
// Process the PR and submit the results
return p.processAndSubmit(env, checkRun)
_, err = p.processAndSubmit(env, checkRun)
return err
}

// Return failure here too so that the whole suite fails (since the actions
Expand All @@ -303,36 +302,36 @@ func (p PRPlugin) onReopen(env *ActionsEnv) error {

// onEdit handles "edit" actions
func (p PRPlugin) onEdit(env *ActionsEnv) error {
headSHA := env.Event.GetPullRequest().GetHead().GetSHA()

// Reset the check run
checkRun, err := p.resetCheckRun(env.Client, env.Owner, env.Repo, headSHA)
checkRun, err := p.resetCheckRun(env.Client, env.Owner, env.Repo, env.Event.GetPullRequest().GetHead().GetSHA())
if err != nil {
return err
}

// Process the PR and submit the results
return p.processAndSubmit(env, checkRun)
_, err = p.processAndSubmit(env, checkRun)
return err
}

// onSync handles "synchronize" actions
func (p PRPlugin) onSync(env *ActionsEnv) error {
before, after := env.Event.GetBefore(), env.Event.GetAfter()

// Get the check run
checkRun, err := p.getCheckRun(env.Client, env.Owner, env.Repo, before)
checkRun, err := p.getCheckRun(env.Client, env.Owner, env.Repo, env.Event.GetBefore())
if err != nil {
return err
}

// Rerun the tests if they weren't finished
if !Finished.Equal(checkRun.GetStatus()) {
// Process the PR and submit the results
return p.processAndSubmit(env, checkRun)
checkRun, err = p.processAndSubmit(env, checkRun)
if err != nil {
return err
}
}

// Create a duplicate for the new commit
checkRun, err = p.duplicateCheckRun(env.Client, env.Owner, env.Repo, after, checkRun)
checkRun, err = p.duplicateCheckRun(env.Client, env.Owner, env.Repo, env.Event.GetAfter(), checkRun)
if err != nil {
return err
}
Expand Down

0 comments on commit 6fbaf21

Please sign in to comment.