Example #1
0
  def AsDict(self):
    d = {
        'server': self.server,
        'change': self.change,
        'revision': self.revision,
    }

    try:
      d.update(commit_cache.Get(self.id_string))
      d['created'] = d['created'].isoformat()
    except KeyError:
      patch_info = gerrit_service.GetChange(
          self.server, self.change,
          fields=('ALL_REVISIONS', 'DETAILED_ACCOUNTS', 'COMMIT_FOOTERS'))
      revision_info = patch_info['revisions'][self.revision]
      url = '%s/c/%s/+/%d/%d' % (
          self.server, patch_info['project'],
          patch_info['_number'], revision_info['_number'])
      author = revision_info['uploader']['email']
      created = datetime.datetime.strptime(
          revision_info['created'], '%Y-%m-%d %H:%M:%S.%f000')
      subject = patch_info['subject']
      current_revision = patch_info['current_revision']
      message = patch_info['revisions'][current_revision]['commit_with_footers']

      d.update({
          'url': url,
          'author': author,
          'created': created.isoformat(),
          'subject': subject,
          'message': message,
      })
      commit_cache.Put(self.id_string, url, author, created, subject, message)

    return d
Example #2
0
 def GetOrCacheCommitInfo(self):
     try:
         return commit_cache.Get(self.id_string)
     except KeyError:
         commit_info = gitiles_service.CommitInfo(self.repository_url,
                                                  self.git_hash)
         return self.CacheCommitInfo(commit_info)
Example #3
0
    def AsDict(self):
        d = {
            'repository': self.repository,
            'git_hash': self.git_hash,
        }

        try:
            d.update(commit_cache.Get(self.id_string))
            d['created'] = d['created'].isoformat()
        except KeyError:
            commit_info = gitiles_service.CommitInfo(self.repository_url,
                                                     self.git_hash)
            url = self.repository_url + '/+/' + commit_info['commit']
            author = commit_info['author']['email']

            created = ParseDateWithUTCOffset(commit_info['committer']['time'])

            subject = commit_info['message'].split('\n', 1)[0]
            message = commit_info['message']

            d.update({
                'url': url,
                'author': author,
                'created': created.isoformat(),
                'subject': subject,
                'message': message,
            })
            commit_cache.Put(self.id_string, url, author, created, subject,
                             message)

        commit_position = _ParseCommitPosition(d['message'])
        if commit_position:
            d['commit_position'] = commit_position

        return d
Example #4
0
    def testPutAndGet(self):
        created = datetime.datetime.now()
        commit_cache.Put('id string', 'https://example.url',
                         '*****@*****.**', created, 'Subject.',
                         'Subject.\n\nMessage.')

        expected = {
            'url': 'https://example.url',
            'author': '*****@*****.**',
            'created': created,
            'subject': 'Subject.',
            'message': 'Subject.\n\nMessage.',
        }
        self.assertEqual(commit_cache.Get('id string'), expected)
Example #5
0
  def FromDict(cls, data):
    """Create a Commit from a dict.

    If the repository is a repository URL, it will be translated to its short
    form name.

    Raises:
      KeyError: The repository name is not in the local datastore,
                or the git hash is not valid.
    """
    repository = data['repository']
    git_hash = data['git_hash']

    # Translate repository if it's a URL.
    if repository.startswith('https://'):
      repository = repository_module.RepositoryName(repository)

    try:
      # If they send in something like HEAD, resolve to a hash.
      repository_url = repository_module.RepositoryUrl(repository)

      try:
        # If it's already in the hash, then we've resolved this recently, and we
        # don't go resolving the data from the gitiles service.
        result = commit_cache.Get(git_hash)
      except KeyError:
        result = gitiles_service.CommitInfo(repository_url, git_hash)
        git_hash = result['commit']
    except gitiles_service.NotFoundError as e:
      raise KeyError(str(e))

    commit = cls(repository, git_hash)
    commit._repository_url = repository_url

    # IF this is something like HEAD, cache this for a short time so that we
    # avoid hammering gitiles.
    if not gitiles_service.IsHash(data['git_hash']):
      commit.CacheCommitInfo(result, memcache_timeout=30*60)

    return commit
Example #6
0
  def CacheCommitInfo(self):
    try:
      return commit_cache.Get(self.id_string)
    except KeyError:
      commit_info = gitiles_service.CommitInfo(
          self.repository_url, self.git_hash)
      url = self.repository_url + '/+/' + commit_info['commit']
      author = commit_info['author']['email']

      created = ParseDateWithUTCOffset(commit_info['committer']['time'])

      subject = commit_info['message'].split('\n', 1)[0]
      message = commit_info['message']

      commit_cache.Put(self.id_string, url, author, created, subject, message)

      return {
          'url': url,
          'author': author,
          'created': created,
          'subject': subject,
          'message': message,
      }
Example #7
0
 def testCommitNotFound(self):
     with self.assertRaises(KeyError):
         commit_cache.Get('id string')