Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃悰 Duplicate the check run even if we need to re-run the tests #20

Merged
merged 1 commit into from Nov 20, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
51 changes: 25 additions & 26 deletions verify/plugin.go
Expand Up @@ -81,21 +81,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 @@ -168,7 +169,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 @@ -197,7 +198,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 @@ -215,9 +216,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 @@ -275,33 +276,31 @@ func (p PRPlugin) entrypoint(env *ActionsEnv) (err error) {
// onOpen handles "open" actions
func (p PRPlugin) onOpen(env *ActionsEnv) error {
p.Debugf("%q handler", actionOpen)
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 {
p.Debugf("%q handler", actionReopen)
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 @@ -315,37 +314,37 @@ func (p PRPlugin) onReopen(env *ActionsEnv) error {
// onEdit handles "edit" actions
func (p PRPlugin) onEdit(env *ActionsEnv) error {
p.Debugf("%q handler", actionEdit)
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 {
p.Debugf("%q handler", actionSync)
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