Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion backend/controllers/github_pull_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
return fmt.Errorf("error getting ghService to post error comment")
}

if !slices.Contains([]string{"closed", "opened", "reopened", "synchronize", "converted_to_draft"}, action) {
if !slices.Contains([]string{"closed", "opened", "reopened", "synchronize", "converted_to_draft", "ready_for_review"}, action) {
slog.Info("Ignoring event with action not requiring processing", "action", action, "prNumber", prNumber)
return nil
}
Expand Down Expand Up @@ -173,6 +173,19 @@ func handlePullRequestEvent(gh utils.GithubClientProvider, payload *github.PullR
"configLength", len(diggerYmlStr),
)

// ConvertGithubPullRequestEventToJobs would build no jobs for this, but
// returning now skips impacted-project detection and the check updates
// that follow an empty job list.
if action == "ready_for_review" && config.AllowDraftPRs {
slog.Info("Ignoring ready_for_review, draft PRs are planned on push",
"prNumber", prNumber,
)
if os.Getenv("DIGGER_REPORT_BEFORE_LOADING_CONFIG") == "1" {
commentReporterManager.UpdateComment(":construction_worker: Ignoring ready for review, as draft PRs were already planned")
}
return nil
}

impactedProjects, impactedProjectsSourceMapping, _, err := github2.ProcessGitHubPullRequestEvent(payload, config, projectsGraph, ghService)
if err != nil {
slog.Error("Error processing GitHub pull request event",
Expand Down
7 changes: 7 additions & 0 deletions docs/ce/howto/draft-prs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ By default, Digger ignores Draft pull requests. You can enable support of draft
allow_draft_prs: true
```

## Planning when a draft is marked ready for review

When a GitHub PR changes status from "draft" to "ready for review":

- if `allow_draft_prs` is `false`, digger will run `on_pull_request_pushed` (`digger plan` by default), as if the PR had just been opened. With PR locks enabled, this takes the locks that converting to draft released.
- if `allow_draft_prs` is `true`, digger will not plan again (because pushes to the draft were already planned)

## Unlocking on changing back to draft

When a PR changes status from "ready to review" back to "draft":
Expand Down
6 changes: 3 additions & 3 deletions docs/ce/reference/digger.yml.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ workflows:
</ParamField>

<ParamField path="allow_draft_prs" type="boolean" default="false">
Allow Digger to run on draft pull requests.
Allow Digger to run on draft pull requests. When `false`, Digger skips draft pull requests and plans them once they're marked ready for review. See [Draft PRs](/ce/howto/draft-prs).
</ParamField>

<ParamField path="apply_after_merge" type="boolean" default="false">
Expand Down Expand Up @@ -595,7 +595,7 @@ Define custom workflows using the `workflows` map. Each workflow can have its ow
Configure how the workflow responds to CI events.

<ParamField path="on_pull_request_pushed" type="array" default='["digger plan"]'>
Commands to run when a pull request is created or updated. Options: `digger plan`, `digger apply`, `digger lock`, `digger unlock`.
Commands to run when a pull request is created or updated, or marked ready for review while `allow_draft_prs` is `false`. Options: `digger plan`, `digger apply`, `digger lock`, `digger unlock`.
</ParamField>

<ParamField path="on_pull_request_closed" type="array" default='["digger unlock"]'>
Expand Down Expand Up @@ -641,7 +641,7 @@ apply:

Workflow configuration describes how to react to CI events. It has 4 sections:

- **on_pull_request_pushed** - describes what to do when a pull request is created or updated
- **on_pull_request_pushed** - describes what to do when a pull request is created or updated, or marked ready for review while `allow_draft_prs` is `false`
- **on_pull_request_closed** - describes what to do when a pull request is closed
- **on_pull_request_to_draft** - describes what to do when a pull request is converted to draft
- **on_commit_to_default** - describes what to do when a pull request is merged into the default branch
Expand Down
4 changes: 3 additions & 1 deletion libs/ci/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,9 @@ func ConvertGithubPullRequestEventToJobs(payload *github.PullRequestEvent, impac
CognitoOidcConfig: project.AwsCognitoOidcConfig,
SkipMergeCheck: skipMerge,
})
} else if action == "opened" || action == "reopened" || action == "synchronize" {
} else if action == "opened" || action == "reopened" || action == "synchronize" ||
// With draft PRs allowed, pushes to the draft were already planned.
(action == "ready_for_review" && !config.AllowDraftPRs) {
slog.Info("processing PR update",
"prNumber", *pullRequestNumber,
"project", project.Name,
Expand Down
44 changes: 44 additions & 0 deletions libs/ci/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/google/go-github/v61/github"
"github.com/migueleliasweb/go-github-mock/src/mock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFindAllProjectsDependantOnImpactedProjects(t *testing.T) {
Expand Down Expand Up @@ -170,3 +171,46 @@ func TestGetApprovalsPaginatesBeyondFirstPage(t *testing.T) {
assert.NoError(t, err)
assert.ElementsMatch(t, []string{"alice", "carol"}, approvals)
}

func pullRequestEvent(action string) *github.PullRequestEvent {
return &github.PullRequestEvent{
Action: github.String(action),
Repo: &github.Repository{
FullName: github.String("org/repo"),
DefaultBranch: github.String("main"),
},
PullRequest: &github.PullRequest{
Number: github.Int(7),
Head: &github.PullRequestBranch{Ref: github.String("feature")},
Base: &github.PullRequestBranch{Ref: github.String("main")},
},
Sender: &github.User{Login: github.String("someone")},
}
}

func TestConvertGithubPullRequestEventToJobsReadyForReview(t *testing.T) {
config := digger_config.DiggerConfig{
Workflows: map[string]digger_config.Workflow{
"default": {
Configuration: &digger_config.WorkflowConfiguration{
OnPullRequestPushed: []string{"digger plan"},
},
},
},
}
projects := []digger_config.Project{{Name: "p", Dir: ".", Workflow: "default"}}

ready, _, err := ConvertGithubPullRequestEventToJobs(pullRequestEvent("ready_for_review"), projects, nil, config, false)
require.NoError(t, err)
pushed, _, err := ConvertGithubPullRequestEventToJobs(pullRequestEvent("synchronize"), projects, nil, config, false)
require.NoError(t, err)

require.Len(t, ready, 1)
assert.Equal(t, []string{"digger plan"}, ready[0].Commands)
assert.Equal(t, pushed, ready)

config.AllowDraftPRs = true
ready, _, err = ConvertGithubPullRequestEventToJobs(pullRequestEvent("ready_for_review"), projects, nil, config, false)
require.NoError(t, err)
assert.Empty(t, ready, "drafts were already planned on push")
}
Loading