Example #1
0
    def _getLatestTagInfo(self):
        """ fill the information about the latest tag in the repository"""
        if self.latestTagInfo is not None:
            self.log.debug("Latest Tag Info already filled")
            return self.latestTagInfo  ## already filled
        tags = self.getGithubTags()
        sortedTags = sorted(tags,
                            key=itemgetter("name"),
                            reverse=True,
                            cmp=versionComp)
        if self._lastTag is None:
            di = sortedTags[0]
        else:
            try:
                di = [
                    tagInfo for tagInfo in tags
                    if tagInfo['name'] == self._lastTag
                ][0]
            except IndexError:
                raise RuntimeError(
                    "LastTag given, but not found in tags for this package")
        self.latestTagInfo = di
        self.latestTagInfo['pre'] = True if 'pre' in di['name'] else False
        self.latestTagInfo['sha'] = di['commit']['sha']
        self.log.info("Found latest tag %s", di['name'])

        if not tags:
            self.log.warning("No tags found for %s", self)
            self.latestTagInfo = {}
            self.latestTagInfo['date'] = "1977-01-01T"
            self.latestTagInfo['sha'] = "SHASHASHA"
            self.latestTagInfo['name'] = "00-00"
        else:
            commitInfo = curl2Json(
                ghHeaders(),
                self._github("git/commits/%s" % self.latestTagInfo['sha']))
            self.latestTagInfo['date'] = commitInfo['committer']['date']

        lastCommitInfo = curl2Json(ghHeaders(),
                                   self._github("branches/%s" % self.branch))
        self._lastCommitOnBranch = lastCommitInfo['commit']

        # tags = self.getGithubReleases()
        # for di in sorted(tags, key=itemgetter('created_at') ):
        #   self.latestTagInfo = di
        #   self.latestTagInfo['pre'] = di['prerelease']
        #   self.latestTagInfo['date'] = di['created_at']
        #   self.log.info( "Found latest tag %s", di['name'] )
        #   break

        return self.latestTagInfo
Example #2
0
 def getTreeShaForCommit( self, commit ):
   """ return the sha of the tree for given commit"""
   result = curl2Json(self._github( "git/commits/%s" % commit))
   if 'sha' not in result:
     raise RuntimeError( "Error: Commit not found" )
   treesha = result['tree']['sha']
   return treesha
Example #3
0
    def createGithubPR(self, sourceBranch, targetRepo, targetBranch):
        """ create a PR on Github


    :param str sourceBranch: branch the PR is based on
    :param str targetRepo: target repository for the PR
    :param str targetBranch: target for the PR
    """

        title = "Mirror from gitlab MR: %s" % sourceBranch
        prDict = dict(branch=sourceBranch,
                      base=targetBranch,
                      title=title,
                      sourceRepo=self.repo,
                      targetRepo=targetRepo)
        if not all(
                opt in prDict
                for opt in ('branch', 'base', 'title', 'sourceRepo', "FIXME")):
            self.log.error("Missing some option in the option dict: %s",
                           prDict)

        result = curl2Json(
            ghHeaders(),
            '-d { "title": "%(title)s", "head": "%(branch)s", "base": "%(base)s"}'
            % prDict, self._github("pulls"))

        if 'errors' in result:
            for error in result['errors']:
                self.log.error("ERROR creating PullRequest: %s",
                               error['message'])
            return False
        else:
            self.log.info("Created pullrequest")
            url = result['html_url']
            return url
 def getTreeShaForCommit( self, commit ):
   """ return the sha of the tree for given commit"""
   result = curl2Json(self._github( "git/commits/%s" % commit))
   if 'sha' not in result:
     raise RuntimeError( "Error: Commit not found" )
   treesha = result['tree']['sha']
   return treesha
Example #5
0
  def createGithubCommit( self, filename, fileSha, content, message ):
    """
    create a commit on the repository with `version` and on `branch`, makes tag on last commit of the branch

    :param str treeSha: sha of the tree the commit will go
    :param str filename: full path to the file
    :param str content: base64 encoded content of the file


    PUT /repos/:owner/:repo/contents/:path

    Parameters
    Name  Type  Description
    path  string  Required. The content path.
    message   string  Required. The commit message.
    content   string  Required. The updated file content, Base64 encoded.
    sha   string  Required. The blob SHA of the file being replaced.
    branch  string  The branch name. Default: the repository's default branch (usually master)
    Optional Parameters

    """
    coDict = dict( path=filename, content=content, branch=self.branch, sha=fileSha, message=message, force='true' )
    if self._dryRun:
      coDict.pop('content')
      self.log.info( "DryRun: not actually making commit: %s", coDict )
      return

    result = curl2Json(requestType='PUT', parameterDict=coDict,
                       url=self._github("contents/%s" % filename))

    if 'commit' not in result:
      raise RuntimeError( "Failed to update file: %s" % result['message'] )

    return result
  def createGithubCommit( self, filename, fileSha, content, message ):
    """
    create a commit on the repository with `version` and on `branch`, makes tag on last commit of the branch

    :param str treeSha: sha of the tree the commit will go
    :param str filename: full path to the file
    :param str content: base64 encoded content of the file


    PUT /repos/:owner/:repo/contents/:path

    Parameters
    Name  Type  Description
    path  string  Required. The content path.
    message   string  Required. The commit message.
    content   string  Required. The updated file content, Base64 encoded.
    sha   string  Required. The blob SHA of the file being replaced.
    branch  string  The branch name. Default: the repository's default branch (usually master)
    Optional Parameters

    """
    coDict = dict( path=filename, content=content, branch=self.branch, sha=fileSha, message=message, force='true' )
    if self._dryRun:
      coDict.pop('content')
      self.log.info( "DryRun: not actually making commit: %s", coDict )
      return

    result = curl2Json(requestType='PUT', parameterDict=coDict,
                       url=self._github("contents/%s" % filename))

    if 'commit' not in result:
      raise RuntimeError( "Failed to update file: %s" % result['message'] )

    return result
Example #7
0
  def getGithubReleases( self):
    """ get the last few releases from github

    :returns: list of releases
    """
    result = curl2Json(url=self._github("releases"))
    #pprint(result)
    return result
  def getGithubReleases( self):
    """ get the last few releases from github

    :returns: list of releases
    """
    result = curl2Json(url=self._github("releases"))
    #pprint(result)
    return result
Example #9
0
  def getHeadOfBranch( self ):
    """return the commit sha of the head of the branch"""
    result = curl2Json(url=self._github("git/refs/heads/%s" % self.branch))
    if 'message' in result:
      raise RuntimeError( "Error:",result['message'] )

    commitsha = result['object']['sha']

    return commitsha
  def getHeadOfBranch( self ):
    """return the commit sha of the head of the branch"""
    result = curl2Json(url=self._github("git/refs/heads/%s" % self.branch))
    if 'message' in result:
      raise RuntimeError( "Error:",result['message'] )

    commitsha = result['object']['sha']

    return commitsha
Example #11
0
 def getFileFromBranch( self, filename ):
   """return the content of the file in the given branch, filename needs to be the full path"""
   result = curl2Json(url=self._github( "contents/%s?ref=%s" %(filename, self.branch)))
   encoded = result.get( 'content', None )
   if encoded is None:
     self.log.error( "File %s not found for %s", filename, self )
     raise RuntimeError( "File not found" )
   content = encoded.decode("base64")
   sha = result['sha']
   return content, sha, encoded
Example #12
0
  def _getLatestTagInfo( self ):
    """ fill the information about the latest tag in the repository"""
    if self.latestTagInfo is not None:
      self.log.debug( "Latest Tag Info already filled" )
      return self.latestTagInfo ## already filled
    tags = self.getGithubTags()
    sortedTags = sorted(tags, key=itemgetter("name"), reverse=True, cmp=versionComp)
    if self._lastTag is None:
      di = sortedTags[0]
    else:
      try:
        di = [ tagInfo for tagInfo in tags if tagInfo['name'] == self._lastTag][0]
      except IndexError:
        raise RuntimeError( "LastTag given, but not found in tags for this package")
    self.latestTagInfo = di
    self.latestTagInfo['pre'] = True if 'pre' in di['name'] else False
    self.latestTagInfo['sha'] = di['commit']['sha']
    self.log.info( "Found latest tag %s", di['name'] )


    if not tags:
      self.log.warning( "No tags found for %s", self )
      self.latestTagInfo={}
      self.latestTagInfo['date'] = "1977-01-01T"
      self.latestTagInfo['sha'] = "SHASHASHA"
      self.latestTagInfo['name'] = "00-00"
    else:
      commitInfo = curl2Json(url=self._github("git/commits/%s" % self.latestTagInfo['sha']))
      self.latestTagInfo['date'] = commitInfo['committer']['date']

    lastCommitInfo = curl2Json(url=self._github("branches/%s" % self.branch))
    self._lastCommitOnBranch =  lastCommitInfo['commit']

    # tags = self.getGithubReleases()
    # for di in sorted(tags, key=itemgetter('created_at') ):
    #   self.latestTagInfo = di
    #   self.latestTagInfo['pre'] = di['prerelease']
    #   self.latestTagInfo['date'] = di['created_at']
    #   self.log.info( "Found latest tag %s", di['name'] )
    #   break

    return self.latestTagInfo
Example #13
0
  def getFileFromBranch( self, filename ):
    """return the content of the file in the given branch, filename needs to be the full path"""

    result = curl2Json(url=self._github( "contents/%s?ref=%s" %(filename,self.branch)))
    encoded = result.get( 'content', None )
    if encoded is None:
      self.log.error( "File %s not found for %s", filename, self )
      raise RuntimeError( "File not found" )
    content = encoded.decode("base64")
    sha = result['sha']
    return content, sha, encoded
Example #14
0
 def formatSkeletonFiles( self , description ):
   """ format all template files of this repository. 
   Only work if it has been forked from the Skeleton repository """
   files = ["Readme.md", "CMakeLists.txt", "source/CMakeLists.txt"]
   fmtDict = dict(repository=self.repo, description=description)
   
   for f in files:
     self.formatFileAndCommit(fmtDict, f)
     
   # Set the repository description
   editDict = dict( name=self.repo, description=description )
   result = curl2Json(requestType="PATCH", parameterDict=editDict, url=self._githubAPI("repos/{0}/{1}".format(self.owner, self.repo)))
  def getCommentsForPR( self, prID ):
    """get the comments for a PR

    :param int prID: pull request ID to get the comments from
    :returns: list of comments
    """
    self.log.debug( "Getting comments for PR %s", prID )
    comments = curl2Json(url=self._github("issues/%s/comments" % prID))
    commentTexts = []
    for comment in comments:
      commentTexts.append( comment['body'] )

    return commentTexts
Example #16
0
  def getCommentsForPR( self, prID ):
    """get the comments for a PR

    :param int prID: pull request ID to get the comments from
    :returns: list of comments
    """
    self.log.debug( "Getting comments for PR %s", prID )
    comments = curl2Json(url=self._github("issues/%s/comments" % prID))
    commentTexts = []
    for comment in comments:
      commentTexts.append( comment['body'] )

    return commentTexts
Example #17
0
  def getGithubTags( self):
    """ get all tags from github

    u'commit': {u'sha': u'49680c32f9c0734dcbf0efe2f01e2363dab3c64e',
                u'url': u'https://api.github.com/repos/andresailer/Marlin/commits/49680c32f9c0734dcbf0efe2f01e2363dab3c64e'},
    u'name': u'v01-02-01',
    u'tarball_url': u'https://api.github.com/repos/andresailer/Marlin/tarball/v01-02-01',
    u'zipball_url': u'https://api.github.com/repos/andresailer/Marlin/zipball/v01-02-01'},

    :returns: list of tags
    """
    result = curl2Json(url=self._github("tags"))
    if isinstance( result, dict ) and 'Not Found' in result.get('message'):
      raise RuntimeError( "Package not found: %s" % str(self) )
    return result
  def getGithubTags( self):
    """ get all tags from github

    u'commit': {u'sha': u'49680c32f9c0734dcbf0efe2f01e2363dab3c64e',
                u'url': u'https://api.github.com/repos/andresailer/Marlin/commits/49680c32f9c0734dcbf0efe2f01e2363dab3c64e'},
    u'name': u'v01-02-01',
    u'tarball_url': u'https://api.github.com/repos/andresailer/Marlin/tarball/v01-02-01',
    u'zipball_url': u'https://api.github.com/repos/andresailer/Marlin/zipball/v01-02-01'},

    :returns: list of tags
    """
    result = curl2Json(url=self._github("tags"))
    if isinstance( result, dict ) and 'Not Found' in result.get('message'):
      raise RuntimeError( "Package not found: %s" % str(self) )
    return result
Example #19
0
    def isPRMerged(self, prID):
        """ returns True/False wether the PR has been merged or not

    :param int prID: ID of the pull request
    :returns: True/False
    """
        result = curl2Json(ghHeaders(),
                           self._github("pulls/%d/merge" % prID),
                           checkStatusOnly=True)
        for line in result.splitlines():
            if "Status" in line:
                if "204" in line:
                    self.log.debug("PR %d is merged", prID)
                    return True
                else:
                    self.log.debug("PR %d is NOT merged", prID)
                    return False
        return False
  def createGithubRelease( self ):
    """ make a release on github """
    if self.isUpToDate():
      self.log.warning( "Package %s is up to date, will not make new release", self )
      return


    releaseDict = dict( tag_name=self.newTag,
                        target_commitish=self.branch,
                        name=self.newTag,
                        body=self.formatReleaseNotes(),
                        prerelease=self.isPreRelease,
                      )
    if self._dryRun:
      self.log.info( "DryRun: not actually making Release: %s", releaseDict )
      return {}
    result = curl2Json(url=self._github( "releases" ), parameterDict=releaseDict, requestType='POST')
    #pprint(result)
    return result
Example #21
0
  def createGithubRelease( self ):
    """ make a release on github """
    if self.isUpToDate():
      self.log.warning( "Package %s is up to date, will not make new release", self )
      return


    releaseDict = dict( tag_name=self.newTag,
                        target_commitish=self.branch,
                        name=self.newTag,
                        body=self.formatReleaseNotes(),
                        prerelease=self.isPreRelease,
                      )
    if self._dryRun:
      self.log.info( "DryRun: not actually making Release: %s", releaseDict )
      return {}
    result = curl2Json(url=self._github( "releases" ), parameterDict=releaseDict, requestType='POST')
    #pprint(result)
    return result
Example #22
0
  def getGithubPRs( self, state="open", mergedOnly=False, perPage=100):
    """ get all PullRequests from github

    :param str state: state of the PRs, open/closed/all, default open
    :param bool merged: if PR has to be merged, only sensible for state=closed
    :returns: list of githubPRs
    """
    url = self._github( "pulls?state=%s&per_page=%s" % (state, perPage) )
    prs = curl2Json(url=url)
    #pprint(prs)
    if not mergedOnly:
      return prs

    ## only merged PRs
    prsToReturn = []
    for pr in prs:
      if pr.get( 'merged_at', None ) is not None:
        prsToReturn.append(pr)

    return prsToReturn
  def getGithubPRs( self, state="open", mergedOnly=False, perPage=100):
    """ get all PullRequests from github

    :param str state: state of the PRs, open/closed/all, default open
    :param bool merged: if PR has to be merged, only sensible for state=closed
    :returns: list of githubPRs
    """
    url = self._github( "pulls?state=%s&per_page=%s" % (state, perPage) )
    prs = curl2Json(url=url)
    #pprint(prs)
    if not mergedOnly:
      return prs

    ## only merged PRs
    prsToReturn = []
    for pr in prs:
      if pr.get( 'merged_at', None ) is not None:
        prsToReturn.append(pr)

    return prsToReturn
Example #24
0
 def addCollaborator( self, user ):
   """ add a collaborator to this repository """
   addDict = dict(permission="push")
   result = curl2Json(requestType="PUT", parameterDict=addDict, url=self._githubAPI("repos/{0}/{1}/collaborators/{2}".format(self.owner, self.repo, user)))
   
   print result