Skip to content
Merged
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
2 changes: 1 addition & 1 deletion opal.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mistwarp-api",
"version": "1.4.1",
"version": "1.5.0",
"main": "main.osl",
"scripts": {
"dev": ["osl", "run", "main.osl"],
Expand Down
139 changes: 139 additions & 0 deletions src/community/comment-issues.osl
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
export {
applyCommentIssue,
commentIssueCounts,
commentIssueState,
commentMatchesIssueFilter,
nextCommentIssueNumber,
validIssueFilter,
validIssueState
}

def commentIssueState(object comment) string (
if !comment.contains("issue") or comment.issue == null (
return ""
)
object issue = comment.issue.assert(object)
if issue.contains("state") and issue.state != null (
string state = issue.state.toStr()
if state == "open" or state == "done" (
return state
)
)
return ""
)

def commentIssueNumber(object comment) number (
if commentIssueState(comment) == "" (
return 0
)
object issue = comment.issue.assert(object)
if issue.contains("number") and issue.number != null (
return issue.number.toNum()
)
return 0
)

def validIssueState(string state) boolean (
return state == "open" or state == "done" or state == "none"
)

def validIssueFilter(string filter) boolean (
return filter == "open" or filter == "done" or filter == "any"
)

def nextCommentIssueNumber(array comments) number (
number highest = 0
for i comments.len (
object existing = comments[i].assert(object)
if existing.contains("issue") and existing.issue != null (
object issue = existing.issue.assert(object)
if issue.contains("number") and issue.number != null and issue.number.toNum() > highest (
highest = issue.number.toNum()
)
)
)
return highest + 1
)

def applyCommentIssue(object comment, array comments, string state, string requester, boolean canTriage, number now) object (
if !validIssueState(state) (
return {ok: false, error: "unknown issue state", status: 400}
)
if comment.contains("parent") and comment.parent != null and comment.parent.toStr() != "" (
return {ok: false, error: "only top-level comments can be issues", status: 400}
)
boolean isAuthor = normalizeUsername(comment.author.toStr()) == normalizeUsername(requester)
if !isAuthor and !canTriage (
return {ok: false, error: "comment not found", status: 404}
)
string current = commentIssueState(comment)
if current == "" and state == "none" (
return {ok: true, comment: comment, changed: false}
)
if current == state (
return {ok: true, comment: comment, changed: false}
)
number reserved = 0
if comment.contains("issue") and comment.issue != null (
object stored = comment.issue.assert(object)
if stored.contains("number") and stored.number != null (
reserved = stored.number.toNum()
)
)
object issue = {}
if current != "" (
issue = comment.issue.assert(object)
) else if reserved > 0 (
issue = {number: reserved, openedBy: requester, openedAt: now}
) else (
issue = {number: nextCommentIssueNumber(comments), openedBy: requester, openedAt: now}
)
issue.state = state
if state == "done" (
issue.closedBy = requester
issue.closedAt = now
) else if state == "none" (
issue.removedBy = requester
issue.removedAt = now
if issue.contains("closedBy") (
issue.delete("closedBy")
)
if issue.contains("closedAt") (
issue.delete("closedAt")
)
) else (
if issue.contains("closedBy") (
issue.delete("closedBy")
)
if issue.contains("closedAt") (
issue.delete("closedAt")
)
)
comment.issue = issue
return {ok: true, comment: comment, changed: true}
)

def commentMatchesIssueFilter(object comment, string filter, number wantedNumber) boolean (
string state = commentIssueState(comment)
if state == "" (
return false
)
if wantedNumber > 0 and commentIssueNumber(comment) != wantedNumber (
return false
)
return filter == "any" or filter == state
)

def commentIssueCounts(array roots) object (
number open = 0
number done = 0
for i roots.len (
string state = commentIssueState(roots[i].assert(object))
if state == "open" (
open = open + 1
) else if state == "done" (
done = done + 1
)
)
return {open: open, done: done}
)
68 changes: 68 additions & 0 deletions src/community/comment-issues.test.osl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import "std:testing"
import "std:crypto"
import "std:regex"
import "std:fs"
import "std:json"
import "go:mistwarp.local/storagefs"
import "../core/utils.osl"
import "./comment-issues.osl"

array comments = [
{id: "c1", author: "Sam", content: "The jump is too high", parent: ""},
{id: "c2", author: "mist", content: "Fixed in the next save", parent: "c1"},
{id: "c3", author: "Ana", content: "Love it", parent: ""}
]

object denied = applyCommentIssue(comments[3].assert(object), comments, "open", "Sam", false, 10)
testing.assert(denied.ok == false and denied.status == 404, "people cannot turn someone else's comment into an issue, and cannot tell it exists")

object reply = applyCommentIssue(comments[2].assert(object), comments, "open", "mist", true, 10)
testing.assert(reply.ok == false and reply.status == 400, "replies cannot become issues")

object badState = applyCommentIssue(comments[1].assert(object), comments, "closed", "Sam", false, 10)
testing.assert(badState.ok == false and badState.status == 400, "unknown states are refused")

object opened = applyCommentIssue(comments[1].assert(object), comments, "open", "sam", false, 10)
testing.assert(opened.ok == true and opened.changed == true, "authors can turn their own comment into an issue, ignoring case")
object first = opened.comment.assert(object)
testing.assert(first.issue.number == 1 and first.issue.state == "open" and first.issue.openedBy == "sam" and first.issue.openedAt == 10, "the first issue is number 1")
comments[1] = first

object again = applyCommentIssue(first, comments, "open", "Sam", false, 11)
testing.assert(again.ok == true and again.changed == false, "opening an open issue changes nothing")

object triaged = applyCommentIssue(comments[3].assert(object), comments, "done", "mist", true, 12)
testing.assert(triaged.ok == true and triaged.comment.issue.number == 2 and triaged.comment.issue.state == "done", "people with write access can mark any comment as done")
testing.assert(triaged.comment.issue.closedBy == "mist" and triaged.comment.issue.closedAt == 12, "closing records who closed it and when")
comments[3] = triaged.comment

object closed = applyCommentIssue(first, comments, "done", "Sam", false, 13)
testing.assert(closed.ok == true and closed.comment.issue.number == 1 and closed.comment.issue.state == "done", "authors can close their own issue and it keeps its number")
object reopened = applyCommentIssue(closed.comment.assert(object), comments, "open", "Sam", false, 14)
object reopenedIssue = reopened.comment.assert(object).issue.assert(object)
testing.assert(reopened.ok == true and reopenedIssue.state == "open" and !reopenedIssue.contains("closedBy") and !reopenedIssue.contains("closedAt"), "reopening clears the closing details")
comments[1] = reopened.comment

testing.assert(commentIssueState(comments[1].assert(object)) == "open", "the state reads back as open")
testing.assert(commentIssueState({id: "x", issue: {state: "weird"}}) == "", "unknown stored states are not issues")
testing.assert(commentMatchesIssueFilter(comments[1].assert(object), "open", 0), "an open issue matches the open filter")
testing.assert(!commentMatchesIssueFilter(comments[1].assert(object), "done", 0), "an open issue does not match the done filter")
testing.assert(commentMatchesIssueFilter(comments[3].assert(object), "any", 2), "the number filter finds issue 2")
testing.assert(!commentMatchesIssueFilter(comments[3].assert(object), "any", 1), "the number filter skips other issues")
testing.assert(!commentMatchesIssueFilter(comments[2].assert(object), "any", 0), "plain comments are not issues")

object counts = commentIssueCounts([comments[1], comments[3], {id: "c4", author: "Lee", parent: ""}])
testing.assert(counts.open == 1 and counts.done == 1, "counts split open and done issues")

object removed = applyCommentIssue(comments[3].assert(object), comments, "none", "mist", true, 15)
testing.assert(removed.ok == true and removed.changed == true and commentIssueState(removed.comment.assert(object)) == "", "an issue can go back to a plain comment")
comments[3] = removed.comment
testing.assert(nextCommentIssueNumber(comments) == 3, "a removed issue keeps its number reserved")
testing.assert(!commentMatchesIssueFilter(comments[3].assert(object), "any", 0), "removed issues are not listed")
object counted = commentIssueCounts([comments[1], comments[3]])
testing.assert(counted.open == 1 and counted.done == 0, "removed issues are not counted")
object retracked = applyCommentIssue(comments[3].assert(object), comments, "open", "Ana", false, 16)
testing.assert(retracked.ok == true and retracked.comment.issue.number == 2 and retracked.comment.issue.state == "open", "tracking a removed issue again brings back its number")
object plainNone = applyCommentIssue({id: "c9", author: "Lee", content: "hi", parent: ""}, comments, "none", "Lee", false, 17)
testing.assert(plainNone.ok == true and plainNone.changed == false and !plainNone.comment.assert(object).contains("issue"), "removing an issue from a plain comment changes nothing")
testing.assert(validIssueFilter("any") and !validIssueFilter("none"), "none is a state, not a filter")
Loading
Loading