~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to misc/dashboard/codereview/dashboard/mail.go

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-08-20 14:06:23 UTC
  • mfrom: (14.1.23 saucy-proposed)
  • Revision ID: package-import@ubuntu.com-20130820140623-b414jfxi3m0qkmrq
Tags: 2:1.1.2-2ubuntu1
* Merge from Debian unstable (LP: #1211749, #1202027). Remaining changes:
  - 016-armhf-elf-header.patch: Use correct ELF header for armhf binaries.
  - d/control,control.cross: Update Breaks/Replaces for Ubuntu
    versions to ensure smooth upgrades, regenerate control file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2012 The Go Authors. All rights reserved.
 
2
// Use of this source code is governed by a BSD-style
 
3
// license that can be found in the LICENSE file.
 
4
 
 
5
package dashboard
 
6
 
 
7
// This file handles receiving mail.
 
8
 
 
9
import (
 
10
        "net/http"
 
11
        "net/mail"
 
12
        "regexp"
 
13
        "time"
 
14
 
 
15
        "appengine"
 
16
        "appengine/datastore"
 
17
)
 
18
 
 
19
func init() {
 
20
        http.HandleFunc("/_ah/mail/", handleMail)
 
21
}
 
22
 
 
23
var subjectRegexp = regexp.MustCompile(`.*code review (\d+):.*`)
 
24
 
 
25
func handleMail(w http.ResponseWriter, r *http.Request) {
 
26
        c := appengine.NewContext(r)
 
27
 
 
28
        defer r.Body.Close()
 
29
        msg, err := mail.ReadMessage(r.Body)
 
30
        if err != nil {
 
31
                c.Errorf("mail.ReadMessage: %v", err)
 
32
                return
 
33
        }
 
34
 
 
35
        subj := msg.Header.Get("Subject")
 
36
        m := subjectRegexp.FindStringSubmatch(subj)
 
37
        if len(m) != 2 {
 
38
                c.Debugf("Subject %q did not match /%v/", subj, subjectRegexp)
 
39
                return
 
40
        }
 
41
 
 
42
        c.Infof("Found issue %q", m[1])
 
43
 
 
44
        // Track the MessageID.
 
45
        key := datastore.NewKey(c, "CL", m[1], 0, nil)
 
46
        err = datastore.RunInTransaction(c, func(c appengine.Context) error {
 
47
                cl := new(CL)
 
48
                err := datastore.Get(c, key, cl)
 
49
                if err != nil && err != datastore.ErrNoSuchEntity {
 
50
                        return err
 
51
                }
 
52
                if err == datastore.ErrNoSuchEntity {
 
53
                        // Must set sentinel values for time.Time fields
 
54
                        // if this is a new entity.
 
55
                        cl.Created = time.Unix(0, 0)
 
56
                        cl.Modified = time.Unix(0, 0)
 
57
                }
 
58
                cl.LastMessageID = msg.Header.Get("Message-ID")
 
59
                _, err = datastore.Put(c, key, cl)
 
60
                return err
 
61
        }, nil)
 
62
        if err != nil {
 
63
                c.Errorf("datastore transaction failed: %v", err)
 
64
        }
 
65
 
 
66
        // Update the CL after a delay to give Rietveld a chance to catch up.
 
67
        UpdateCLLater(c, m[1], 10*time.Second)
 
68
}