-
Notifications
You must be signed in to change notification settings - Fork 167
Add PR Review SLA Tracking #983
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b202148
Add PR review SLA for todo, sidebar, and optional channel alerts
jgheithcock cb418bb
Change review start to use webhook, not PR opening and consolidate ch…
jgheithcock af5e7e6
Address PR review feedback: scheduler lifecycle, theme colors, KV cle…
jgheithcock 6a3528e
Fix same-day digest catch-up and paginate search results
jgheithcock 5d806f3
Use markdown instead of embedded HTML/style
jgheithcock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,136 @@ | ||
| // Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved. | ||
| // See LICENSE.txt for license information. | ||
|
|
||
| package plugin | ||
|
|
||
| import ( | ||
| "crypto/sha256" | ||
| "encoding/hex" | ||
| "strconv" | ||
| "strings" | ||
| "time" | ||
|
|
||
| "github.com/google/go-github/v54/github" | ||
|
|
||
| "github.com/mattermost/mattermost-plugin-github/server/plugin/graphql" | ||
| ) | ||
|
|
||
| const slaReviewReqKeyPrefix = "slarr_v1_" | ||
|
|
||
| // reviewSLAStartKey returns a stable KV key for (repo, PR, requested reviewer login). | ||
| func reviewSLAStartKey(owner, repo string, prNumber int, githubLogin string) string { | ||
| normalized := strings.ToLower(strings.TrimSpace(owner)) + "/" + strings.ToLower(strings.TrimSpace(repo)) + | ||
| "#" + strconv.Itoa(prNumber) + "@" + strings.ToLower(strings.TrimSpace(githubLogin)) | ||
| sum := sha256.Sum256([]byte(normalized)) | ||
| return slaReviewReqKeyPrefix + hex.EncodeToString(sum[:16]) | ||
| } | ||
|
|
||
| // recordReviewRequestSLAStart stores when a reviewer was requested (from pull_request / review_requested webhook). | ||
| // Each new request overwrites the previous time for that (repo, PR, reviewer) pair so the SLA clock restarts on re-request. | ||
| func (p *Plugin) recordReviewRequestSLAStart(event *github.PullRequestEvent, requestedGitHubLogin string) { | ||
| if event.GetRepo() == nil || event.GetPullRequest() == nil { | ||
| return | ||
| } | ||
| owner := event.GetRepo().GetOwner().GetLogin() | ||
| repo := event.GetRepo().GetName() | ||
| num := event.GetPullRequest().GetNumber() | ||
| if owner == "" || repo == "" || num == 0 || requestedGitHubLogin == "" { | ||
| return | ||
| } | ||
| key := reviewSLAStartKey(owner, repo, num, requestedGitHubLogin) | ||
| at := time.Now().UTC() | ||
| val := []byte(at.Format(time.RFC3339Nano)) | ||
| if _, err := p.store.Set(key, val); err != nil { | ||
| p.client.Log.Warn("Failed to store review SLA start time", "key", key, "error", err.Error()) | ||
| } | ||
jgheithcock marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // cleanupReviewSLAKeys deletes all stored SLA start-time keys for a closed/merged PR. | ||
| func (p *Plugin) cleanupReviewSLAKeys(event *github.PullRequestEvent) { | ||
| if event.GetRepo() == nil || event.GetPullRequest() == nil { | ||
| return | ||
| } | ||
| owner := event.GetRepo().GetOwner().GetLogin() | ||
| repo := event.GetRepo().GetName() | ||
| num := event.GetPullRequest().GetNumber() | ||
| if owner == "" || repo == "" || num == 0 { | ||
| return | ||
| } | ||
| for _, reviewer := range event.GetPullRequest().RequestedReviewers { | ||
| login := reviewer.GetLogin() | ||
| if login == "" { | ||
| continue | ||
| } | ||
| key := reviewSLAStartKey(owner, repo, num, login) | ||
| if err := p.store.Delete(key); err != nil { | ||
| p.client.Log.Debug("Failed to delete SLA key on PR close", "key", key, "error", err.Error()) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (p *Plugin) getReviewSLAStartTime(owner, repo string, prNumber int, githubLogin string) time.Time { | ||
| key := reviewSLAStartKey(owner, repo, prNumber, githubLogin) | ||
| var raw []byte | ||
| if err := p.store.Get(key, &raw); err != nil { | ||
| return time.Time{} | ||
| } | ||
| if len(raw) == 0 { | ||
| return time.Time{} | ||
| } | ||
| t, err := time.Parse(time.RFC3339Nano, string(raw)) | ||
| if err != nil { | ||
| t, err = time.Parse(time.RFC3339, string(raw)) | ||
| } | ||
| if err != nil { | ||
| return time.Time{} | ||
| } | ||
| return t.UTC() | ||
| } | ||
|
|
||
| // issueOwnerRepo resolves owner/name for a search result issue (prefers API fields, else HTML URL). | ||
| func issueOwnerRepo(pr *github.Issue, baseURL string) (owner, repo string) { | ||
| if pr.Repository != nil { | ||
| if o := pr.GetRepository().GetOwner(); o != nil { | ||
| owner = o.GetLogin() | ||
| } | ||
| repo = pr.GetRepository().GetName() | ||
| } | ||
| if owner != "" && repo != "" { | ||
| return owner, repo | ||
| } | ||
| return parseOwnerAndRepo(pr.GetHTMLURL(), baseURL) | ||
| } | ||
|
|
||
| // effectiveReviewSLAStart returns the timestamp used for SLA: when we recorded a review_request webhook | ||
| // for this reviewer on this PR, else the PR created time. | ||
| func (p *Plugin) effectiveReviewSLAStart(pr *github.Issue, baseURL, reviewerGitHubLogin string) github.Timestamp { | ||
| owner, repo := issueOwnerRepo(pr, baseURL) | ||
| num := pr.GetNumber() | ||
| if owner == "" || repo == "" || num == 0 { | ||
| return pr.GetCreatedAt() | ||
| } | ||
| if t := p.getReviewSLAStartTime(owner, repo, num, reviewerGitHubLogin); !t.IsZero() { | ||
| return github.Timestamp{Time: t} | ||
| } | ||
| return pr.GetCreatedAt() | ||
| } | ||
|
|
||
| // enrichReviewsWithSLAStart sets review_sla_start on LHS review items so the webapp can match server SLA logic. | ||
| func (p *Plugin) enrichReviewsWithSLAStart(reviews []*graphql.GithubPRDetails, reviewerLogin string) { | ||
| cfg := p.getConfiguration() | ||
| if cfg.ReviewTargetDays <= 0 { | ||
| return | ||
| } | ||
| baseURL := cfg.getBaseURL() | ||
| for _, d := range reviews { | ||
| if d == nil || d.Issue == nil { | ||
| continue | ||
| } | ||
| eff := p.effectiveReviewSLAStart(d.Issue, baseURL, reviewerLogin) | ||
| if eff.IsZero() { | ||
| continue | ||
| } | ||
| s := eff.Time.UTC().Format(time.RFC3339) | ||
| d.ReviewSLAStartAt = &s | ||
| } | ||
| } | ||
This file contains hidden or 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,19 @@ | ||
| // Copyright (c) 2018-present Mattermost, Inc. All Rights Reserved. | ||
| // See LICENSE.txt for license information. | ||
|
|
||
| package plugin | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestReviewSLAStartKeyStable(t *testing.T) { | ||
| k1 := reviewSLAStartKey("Mattermost", "mattermost", 12345, "octocat") | ||
| k2 := reviewSLAStartKey("mattermost", "mattermost", 12345, "OctoCat") | ||
| assert.Equal(t, k1, k2, "key should be case-insensitive") | ||
|
|
||
| k3 := reviewSLAStartKey("Mattermost", "mattermost", 99999, "octocat") | ||
| assert.NotEqual(t, k1, k3) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is probably fine for a first iteration of this feature, but I'm wondering if we'd want to introduce a slash command to make this a bit more configurable.
I feel like in real use-cases, organizations might want to be able to configure these digests into each channel with a list of repositories that might be most relevant for the members of that channel