James Moger
2014-03-17 261ddf0fcf9a55fbb5b4e7c6c2cdb4c2f8c860fe
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
/*
 * Copyright 2014 gitblit.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.gitblit.transport.ssh.gitblit;
 
import java.util.HashSet;
import java.util.Set;
 
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
 
import com.gitblit.models.TicketModel.Change;
import com.gitblit.models.TicketModel.Patchset;
import com.gitblit.models.TicketModel.Score;
import com.gitblit.models.UserModel;
import com.gitblit.transport.ssh.commands.CommandMetaData;
import com.gitblit.transport.ssh.commands.SshCommand;
import com.gitblit.wicket.GitBlitWebSession;
 
@CommandMetaData(name = "review", description = "Verify, approve and/or submit one or more patch sets", hidden = true)
public class ReviewCommand extends SshCommand {
 
    private final static short REV_ID_LEN = 40;
    private final Set<Patchset> patchSets = new HashSet<Patchset>();
 
    @Argument(index = 0, required = true, multiValued = true, metaVar = "{COMMIT | CHANGE,PATCHSET}", usage = "list of commits or patch sets to review")
    void addPatchSetId(final String token) {
        try {
            patchSets.add(parsePatchSet(token));
        } catch (UnloggedFailure e) {
            throw new IllegalArgumentException(e.getMessage(), e);
        }
    }
 
    @Option(name = "--project", required = true, aliases = "-p", usage = "project containing the specified patch set(s)")
    private String project;
 
    @Option(name = "--message", aliases = "-m", usage = "cover message to publish on change(s)", metaVar = "MESSAGE")
    private String changeComment;
 
    @Option(name = "--vote", aliases = "-v", usage = "vote on this patch set", metaVar = "VOTE")
    private int vote;
 
    @Option(name = "--submit", aliases = "-s", usage = "submit the specified patch set(s)")
    private boolean submitChange;
 
    @Override
    public void run() throws UnloggedFailure {
        UserModel user = GitBlitWebSession.get().getUser();
        // TODO ensure user has permission to score +2/-2
        for (Patchset ps : patchSets) {
            // review
            Change change = new Change(user.username);
            change.review(ps, Score.fromScore(vote), false);
            // TODO(davido): add patchset comment
            if (submitChange) {
                // TODO(davido): merge (when desired and the change is mergeable)
            }
        }
    }
 
    private Patchset parsePatchSet(String ps) throws UnloggedFailure {
        // By commit?
        //
        if (ps.matches("^([0-9a-fA-F]{4," + REV_ID_LEN + "})$")) {
            // TODO; parse
        }
 
        // By older style change,patchset?
        //
        if (ps.matches("^[1-9][0-9]*,[1-9][0-9]*$")) {
            // TODO: parse
        }
 
        throw new UnloggedFailure(1, "fatal: Cannot parse patchset: " + ps);
    }
}