-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpulls.osl
More file actions
277 lines (263 loc) · 10.1 KB
/
Copy pathpulls.osl
File metadata and controls
277 lines (263 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
def projectGitHead(object project) string (
if project.contains("gitHead") and project.gitHead != null (
return project.gitHead.toStr()
)
return ""
)
def projectHasWorkspace(object project) boolean (
return projectWorkspaceKeys(project).len > 0 and projectGitHead(project) != ""
)
def pullDocument(string targetId, string index) object (
return pullDocs.findById(targetId ++ ":" ++ index)
)
def pullResponse(object pull) object (
return {
id: pull["_id"],
index: pull.index,
title: pull.title,
body: pull.body,
state: pull.state,
user: pull.author,
sourceOwner: pull.sourceOwner,
sourceProjectId: pull.sourceProjectId,
sourceTitle: pull.sourceTitle,
targetProjectId: pull.targetProjectId,
baseCommit: pull.baseCommit,
headCommit: pull.headCommit,
targetHead: pull.targetHead,
baseBranch: pull.targetBranch,
headBranch: pull.sourceBranch,
created: pull.created,
updated: pull.updated,
merged: pull.state.toStr() == "merged",
mergedCommit: pull.mergedCommit
}
)
def createNativePull(*serve.Context c, object target, object source, string title, string description, string bountyId) object (
if !projectHasWorkspace(target) or !projectHasWorkspace(source) (
return {ok: false, status: 400, error: "both projects need MistWarp history before changes can be sent"}
)
if projectRole(source, c.getString("username")) != "owner" (
return {ok: false, status: 403, error: "use a remix you own"}
)
if source.remixParent.toStr() != target.id.toStr() (
return {ok: false, status: 400, error: "the source project is not a direct remix of this project"}
)
if title == "" or title.len > 200 (
return {ok: false, status: 400, error: "invalid title"}
)
string baseCommit = ""
if source.contains("remixBaseCommit") and source.remixBaseCommit != null (
baseCommit = source.remixBaseCommit.toStr()
)
if baseCommit == "" (
return {ok: false, status: 400, error: "the remix does not record where it started"}
)
number sequence = 0
if target.contains("nativePullSeq") and target.nativePullSeq != null (
sequence = target.nativePullSeq.toNum()
)
sequence = sequence + 1
target.nativePullSeq = sequence
saveProject(target)
string id = target.id.toStr() ++ ":" ++ sequence.toStr()
string sourceBranch = "main"
if source.contains("gitBranch") and source.gitBranch != null (
sourceBranch = source.gitBranch.toStr()
)
string targetBranch = "main"
if target.contains("gitBranch") and target.gitBranch != null (
targetBranch = target.gitBranch.toStr()
)
object pull = {
_id: id,
index: sequence,
title: title,
body: description.trim(1, 5000),
state: "open",
author: c.getString("username"),
authorId: c.getString("userId"),
sourceOwner: source.owner,
sourceOwnerId: source.ownerId,
sourceProjectId: source.id,
sourceTitle: source.title,
sourceBranch: sourceBranch,
targetProjectId: target.id,
targetBranch: targetBranch,
baseCommit: baseCommit,
headCommit: projectGitHead(source),
targetHead: projectGitHead(target),
created: timestamp,
updated: timestamp,
mergedCommit: "",
bountyId: bountyId
}
if !putDocument(pullDocs, id, pull) (
return {ok: false, status: 500, error: "could not store the pull request"}
)
addNotification(target.owner.toStr(), c.getString("username"), "contribution", {projectId: target.id, projectTitle: target.title, pull: sequence})
return {ok: true, pull: pullResponse(pull)}
)
def handleGetCommits(*serve.Context c) (
auto result = loadProject(c.param("id"))
if !result.ok or !canViewProject(result.project.assert(object), c) (
c.notFound("project not found")
return
)
object project = result.project.assert(object)
object history = projectVersionHistory(project)
c.json(200, {
ok: true,
head: projectGitHead(project),
branch: history.branch,
commits: history.commits,
graph: history.graph,
virtual: history.virtual
})
)
def handleListPulls(*serve.Context c) (
auto result = loadProject(c.param("id"))
if !result.ok or !canViewProject(result.project.assert(object), c) (
c.notFound("project not found")
return
)
array stored = pullDocs.where("targetProjectId", db.equal, c.param("id")).sort("created", db.desc).limit(200).all()
array pulls = []
for i stored.len (
pulls.append(pullResponse(stored[i].assert(object)))
)
c.json(200, {ok: true, pulls: pulls})
)
def handleCreatePull(*serve.Context c) (
auto targetResult = loadProject(c.param("id"))
object body = c.bodyJSON()
auto sourceResult = loadProject(body.sourceProjectId.toStr())
if !targetResult.ok or !sourceResult.ok (
c.notFound("project not found")
return
)
string description = ""
if body.contains("body") and body.body != null (
description = body.body.toStr()
)
string bountyId = ""
if body.contains("bountyId") and body.bountyId != null (
bountyId = body.bountyId.toStr().strip()
)
object created = createNativePull(c, targetResult.project.assert(object), sourceResult.project.assert(object), body.title.toStr().strip(), description, bountyId)
if !created.ok (
c.json(created.status.toInt(), {ok: false, error: created.error})
return
)
c.json(201, created)
)
def handleGetPull(*serve.Context c) (
object pull = pullDocument(c.param("id"), c.param("index"))
if pull.isEmpty() == true (
c.notFound("pull request not found")
return
)
c.json(200, {ok: true, pull: pullResponse(pull)})
)
def canAccessPullWorkspace(object project, string pullIndex, *serve.Context c) boolean (
if !c.getBool("authenticated") (
return false
)
array matches = pullDocs.where("index", db.equal, pullIndex.toNum()).limit(20).all()
for i matches.len (
object pull = matches[i].assert(object)
boolean related = pull.sourceProjectId.toStr() == project.id.toStr() or pull.targetProjectId.toStr() == project.id.toStr()
if related (
auto targetResult = loadProject(pull.targetProjectId.toStr())
if targetResult.ok (
object target = targetResult.project.assert(object)
string viewer = normalizeUsername(c.getString("username"))
if canEditProject(target, c) or viewer == normalizeUsername(pull.sourceOwner.toStr()) (
return true
)
)
)
)
return false
)
def handleGetPullDiff(*serve.Context c) (
object pull = pullDocument(c.param("id"), c.param("index"))
if pull.isEmpty() == true (
c.notFound("pull request not found")
return
)
auto targetResult = loadProject(pull.targetProjectId.toStr())
auto sourceResult = loadProject(pull.sourceProjectId.toStr())
if !targetResult.ok or !sourceResult.ok (
c.notFound("project not found")
return
)
object target = targetResult.project.assert(object)
object source = sourceResult.project.assert(object)
if !canAccessPullWorkspace(target, pull.index.toStr(), c) (
c.forbidden("only the fork owner and project maintainers can inspect this pull request")
return
)
string suffix = "?pull=" ++ pull.index.toStr()
c.json(200, {
ok: true,
pull: pullResponse(pull),
targetWorkspaceUrl: appURL ++ "/api/projects/" ++ target.id.toStr() ++ "/workspace.mwp" ++ suffix,
sourceWorkspaceUrl: appURL ++ "/api/projects/" ++ source.id.toStr() ++ "/workspace.mwp" ++ suffix,
expectedHead: projectGitHead(target)
})
)
def handleMergePull(*serve.Context c) (
object pull = pullDocument(c.param("id"), c.param("index"))
if pull.isEmpty() == true or pull.state.toStr() != "open" (
c.notFound("open pull request not found")
return
)
auto result = loadProject(c.param("id"))
if !result.ok or !canEditProject(result.project.assert(object), c) (
c.forbidden("only the project owner can merge")
return
)
handleGetPullDiff(c)
)
def completeNativePull(object project, string pullIndex, string expectedHead, string username) boolean (
object pull = pullDocument(project.id.toStr(), pullIndex)
if pull.isEmpty() == true or pull.state.toStr() != "open" (
return false
)
string role = projectRole(project, username)
if role != "owner" and role != "maintainer" (
return false
)
if expectedHead == "" or pull.targetHead.toStr() != expectedHead (
return false
)
pull.state = "merged"
pull.mergedCommit = projectGitHead(project)
pull.updated = timestamp
putDocument(pullDocs, pull["_id"].toStr(), pull)
number acceptedChanges = 0
if project.contains("acceptedChanges") and project.acceptedChanges != null (
acceptedChanges = project.acceptedChanges.toNum()
)
project.acceptedChanges = acceptedChanges + 1
array mergedContributors = []
if project.contains("mergedContributors") and project.mergedContributors != null (
mergedContributors = project.mergedContributors.assert(array)
)
string contributor = pull.sourceOwner.toStr()
if contributor != "" and !mergedContributors.contains(contributor) (
mergedContributors.append(contributor)
)
project.mergedContributors = mergedContributors
saveProject(project)
upsertIndexEntry(project)
addNotification(pull.sourceOwner.toStr(), project.owner.toStr(), "contribution_merged", {projectId: project.id, projectTitle: project.title, pull: pull.index})
if pull.contains("bountyId") and pull.bountyId != null and pull.bountyId.toStr() != "" (
boolean bountyPaid = awardContributionBounty(pull.bountyId.toStr(), project.id.toStr(), pull.sourceOwner.toStr())
if !bountyPaid (
log "WARNING: could not award contribution bounty " ++ pull.bountyId.toStr()
)
)
return true
)