Commit a549c0f4 authored by Hemang Kumar's avatar Hemang Kumar
Browse files

Content: Add Content support

parent 66c7d739
Loading
Loading
Loading
Loading
+40 −2
Changes for IGitt/GitHub/GitHubContent.py: 40 added lines, 2 removed lines.
Original line number Diff line number Diff line
"""
This contains the Content implementation for GitHub.
"""
from IGitt.GitHub import GitHubMixin, GitHubToken
from base64 import b64encode

from IGitt.GitHub import GitHubMixin, GitHubToken, get, delete, put
from IGitt.Interfaces.Content import Content


@@ -12,4 +14,40 @@ class GitHubContent(Content, GitHubMixin):
    def __init__(self,  token: GitHubToken, repository: str, path: str):
        self._token = token
        self._repository = repository
        self._url = '/repos' + repository + '/contents/' + path
        self._url = '/repos/' + repository + '/contents/' + path

    def get_content(self, ref='master'):
        data = {
            'path': self._url,
            'ref': ref
        }
        self.data = get(token=self._token, url=self._url, params=data)

    def delete(self, message: str, branch: (str, None)=None):

        if branch is None:
            branch = 'master'

        data = {
            'path': self._url,
            'message' : message,
            'sha': self.data['sha'],
            'branch': branch
        }
        delete(token=self._token, url=self._url, params=data)

    def update(self, message: str, content: str, branch: (str, None)=None):

        content = b64encode(content.encode()).decode('utf-8')

        if branch is None:
            branch = 'master'

        data = {
            'path': self._url,
            'message': message,
            'sha': self.data['sha'],
            'branch': branch,
            'content' : content
        }
        put(token=self._token, url=self._url, data=data)
+33 −1
Changes for IGitt/GitLab/GitLabContent.py: 33 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ This contains the Content implementation for GitLab.
"""
from urllib.parse import quote_plus

from IGitt.GitLab import GitLabMixin, GitLabOAuthToken, GitLabPrivateToken
from IGitt.GitLab import GitLabMixin, GitLabOAuthToken, GitLabPrivateToken, get, delete, put
from IGitt.Interfaces.Content import Content


@@ -16,3 +16,35 @@ class GitLabContent(Content, GitLabMixin):
        self._token = token
        self._repository = repository
        self._url = '/projects/' + quote_plus(repository) + '/repository/files/' + path

    def get_content(self, ref='master'):
        data = {
            'path': self._url,
            'ref': ref
        }
        self.data = get(token=self._token, url=self._url, params=data)

    def delete(self, message: str, branch: (str, None) = None):

        if branch is None:
            branch = 'master'

        data = {
            'file_path': self._url,
            'branch': branch,
            'commit_message': message
        }
        delete(self._token, url=self._url, params=data)

    def update(self, message: str, content: str, branch: (str, None) = None):

        if branch is None:
            branch = 'master'

        data = {
            'file_path': self._url,
            'commit_message': message,
            'branch': branch,
            'content' : content
        }
        put(token=self._token, url=self._url, data=data)
+19
Changes for IGitt/Interfaces/Content.py: 19 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -7,3 +7,22 @@ class Content:
    """
    Represents content on GitHub or GitLab or a bug report on bugzilla or so.
    """

    def get_content(self, ref: (str, None)=None):
        """
        Get content
        """
        raise NotImplementedError

    def delete(self, message: str, branch: (str, None)=None):
        """
        Delete content
        """
        raise NotImplementedError

    def update(self, message: str, content: str,
               branch: (str, None)=None):
        """
        Updates existing file in repository
        """
        raise NotImplementedError
+140
Changes for tests/GitHub/cassettes/github_content_delete.yaml: 140 added lines, 0 removed lines.
Original line number Diff line number Diff line
interactions:
- request:
    body: '{"path": "deleteme", "message": "hello", "branch": "master", "content":
      "aGVsbG8="}'
    headers:
      Accept: ['*/*']
      Accept-Encoding: ['gzip, deflate']
      Connection: [keep-alive]
      Content-Length: ['83']
      Content-Type: [application/json]
      User-Agent: [python-requests/2.13.0]
    method: PUT
    uri: https://api.github.com/repos/gitmate-test-user/test/contents/deleteme?User-Agent=IGitt
  response:
    body: {string: '{"content":{"name":"deleteme","path":"deleteme","sha":"b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0","size":5,"url":"https://api.github.com/repos/gitmate-test-user/test/contents/deleteme?ref=master","html_url":"https://github.com/gitmate-test-user/test/blob/master/deleteme","git_url":"https://api.github.com/repos/gitmate-test-user/test/git/blobs/b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0","download_url":"https://raw.githubusercontent.com/gitmate-test-user/test/master/deleteme","type":"file","_links":{"self":"https://api.github.com/repos/gitmate-test-user/test/contents/deleteme?ref=master","git":"https://api.github.com/repos/gitmate-test-user/test/git/blobs/b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0","html":"https://github.com/gitmate-test-user/test/blob/master/deleteme"}},"commit":{"sha":"40a1c10f1911ccbc00aee00b35b7f398182c59b5","url":"https://api.github.com/repos/gitmate-test-user/test/git/commits/40a1c10f1911ccbc00aee00b35b7f398182c59b5","html_url":"https://github.com/gitmate-test-user/test/commit/40a1c10f1911ccbc00aee00b35b7f398182c59b5","author":{"name":"gitmate-test-user","email":"hostmaster@gitmate.io","date":"2017-07-19T15:04:23Z"},"committer":{"name":"gitmate-test-user","email":"hostmaster@gitmate.io","date":"2017-07-19T15:04:23Z"},"tree":{"sha":"9973ec7c3ed72c8c19c6b1d336b37df4c94355b5","url":"https://api.github.com/repos/gitmate-test-user/test/git/trees/9973ec7c3ed72c8c19c6b1d336b37df4c94355b5"},"message":"hello","parents":[{"sha":"645961c0841a84c1dd2a58535aa70ad45be48c46","url":"https://api.github.com/repos/gitmate-test-user/test/git/commits/645961c0841a84c1dd2a58535aa70ad45be48c46","html_url":"https://github.com/gitmate-test-user/test/commit/645961c0841a84c1dd2a58535aa70ad45be48c46"}]}}'}
    headers:
      Access-Control-Allow-Origin: ['*']
      Access-Control-Expose-Headers: ['ETag, Link, X-GitHub-OTP, X-RateLimit-Limit,
          X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes,
          X-Poll-Interval']
      Cache-Control: ['private, max-age=60, s-maxage=60']
      Content-Length: ['1720']
      Content-Security-Policy: [default-src 'none']
      Content-Type: [application/json; charset=utf-8]
      Date: ['Wed, 19 Jul 2017 15:04:24 GMT']
      ETag: ['"fa5e4c462c69ee435e94ad313a9d0fc4"']
      Server: [GitHub.com]
      Status: [201 Created]
      Strict-Transport-Security: [max-age=31536000; includeSubdomains; preload]
      Vary: ['Accept, Authorization, Cookie, X-GitHub-OTP']
      X-Accepted-OAuth-Scopes: ['']
      X-Content-Type-Options: [nosniff]
      X-Frame-Options: [deny]
      X-GitHub-Media-Type: [github.v3; format=json]
      X-GitHub-Request-Id: ['B712:169B:F43F7:2295A0:596F74F6']
      X-OAuth-Scopes: ['admin:org, admin:org_hook, admin:public_key, admin:repo_hook,
          delete_repo, gist, notifications, repo, user']
      X-RateLimit-Limit: ['5000']
      X-RateLimit-Remaining: ['4999']
      X-RateLimit-Reset: ['1500480264']
      X-Runtime-rack: ['0.664367']
      X-XSS-Protection: [1; mode=block]
    status: {code: 201, message: Created}
- request:
    body: null
    headers:
      Accept: ['*/*']
      Accept-Encoding: ['gzip, deflate']
      Connection: [keep-alive]
      User-Agent: [python-requests/2.13.0]
    method: GET
    uri: https://api.github.com/repos/gitmate-test-user/test/contents/deleteme?User-Agent=IGitt
  response:
    body:
      string: !!binary |
        H4sIAAAAAAAAA7WS3W7CMAyF3yXXQMpPu1EJ7ZI32NUk5KQurUiTqnaFNsS7z9m6CZi4oNLuoqP4
        s32OT8pDgypXBTpklOdEtcDVtUIViGCy0q5stkhM9lSs03KdLiG1cztfLBMAMGlhUpibRBBUfwg0
        nai+c1JZMbeUaw1tPdvXXPVmZkOjO2wDaREaYJwyEk97wk7Hl7bBM3om/TPZS4flpgFi7KRDxY3b
        XdMvyHeYxgWjvxG/WGHJ7xvUI4NKtY5g0g8YVISjdwGKm74dHAeDohGDBV9e3dno7zL83sY8y9rF
        LAeECLB9JbN93rx5kdHbUNR+H1MFwmwl2s7V/kAqPylCV/5ParLFSPAol+ORXPQbeSDn8ycNkGYq
        JgMAAA==
    headers:
      Access-Control-Allow-Origin: ['*']
      Access-Control-Expose-Headers: ['ETag, Link, X-GitHub-OTP, X-RateLimit-Limit,
          X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes,
          X-Poll-Interval']
      Cache-Control: ['private, max-age=60, s-maxage=60']
      Content-Encoding: [gzip]
      Content-Security-Policy: [default-src 'none']
      Content-Type: [application/json; charset=utf-8]
      Date: ['Wed, 19 Jul 2017 15:04:26 GMT']
      ETag: [W/"388151cd4213bb97c916c94c7ad4a443"]
      Last-Modified: ['Wed, 19 Jul 2017 15:04:23 GMT']
      Server: [GitHub.com]
      Status: [200 OK]
      Strict-Transport-Security: [max-age=31536000; includeSubdomains; preload]
      Vary: ['Accept, Authorization, Cookie, X-GitHub-OTP']
      X-Accepted-OAuth-Scopes: ['']
      X-Content-Type-Options: [nosniff]
      X-Frame-Options: [deny]
      X-GitHub-Media-Type: [github.v3; format=json]
      X-GitHub-Request-Id: ['AA17:169F:209D44:44EB79:596F74F9']
      X-OAuth-Scopes: ['admin:org, admin:org_hook, admin:public_key, admin:repo_hook,
          delete_repo, gist, notifications, repo, user']
      X-RateLimit-Limit: ['5000']
      X-RateLimit-Remaining: ['4998']
      X-RateLimit-Reset: ['1500480264']
      X-Runtime-rack: ['0.128149']
      X-XSS-Protection: [1; mode=block]
    status: {code: 200, message: OK}
- request:
    body: '{"path": "/repos/gitmate-test-user/test/contents/deleteme", "sha": "b6fc4c620b67d95f953a5c1c1230aaab5db5a1b0",
      "message": "Delete file", "branch": "master"}'
    headers:
      Accept: ['*/*']
      Accept-Encoding: ['gzip, deflate']
      Connection: [keep-alive]
      Content-Length: ['156']
      Content-Type: [application/json]
      User-Agent: [python-requests/2.13.0]
    method: DELETE
    uri: https://api.github.com/repos/gitmate-test-user/test/contents/deleteme?User-Agent=IGitt
  response:
    body:
      string: !!binary |
        H4sIAAAAAAAAA7WSzU7DMAzHXwXlvC5OmyxJTxx4BE4ghNLUWSv1S016mvruuIxJIIS0aeIW27F/
        /ts+MT8OCYfEymHpuh2Zfd+SdWKxcaxkCgAUSGukll7LQyGt9xU4b6zOUWsjijpHW7EdW+aOEpqU
        plhy7qZ2f2xTs1R7qslnnMbIydG7hFnCmLIl4sy31+bmZ3DkNwCb1HfvP6nfiH+wzpxbMG5JzThv
        IxlcjyTxV2USj71rP+WPkSTGhPPj17d9O1K8JtkUzkHoDHQm7LNQJcgyNy9svYydsv4Xk2akLi67
        xWALUCLUAMGj1wcMtcXgD9IErRSIUAgbtLl7txs28qtxNI8eY3THbWJP2GHCh9B2SH1MbqZjjax8
        vYiQ4IQXEIQVgi7TAzhEgKpQlQ6FNcLkXtlK3S3icqA3AO850Ksx69u6fgAaOH3PxwMAAA==
    headers:
      Access-Control-Allow-Origin: ['*']
      Access-Control-Expose-Headers: ['ETag, Link, X-GitHub-OTP, X-RateLimit-Limit,
          X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes,
          X-Poll-Interval']
      Cache-Control: ['private, max-age=60, s-maxage=60']
      Content-Encoding: [gzip]
      Content-Security-Policy: [default-src 'none']
      Content-Type: [application/json; charset=utf-8]
      Date: ['Wed, 19 Jul 2017 15:04:29 GMT']
      ETag: [W/"b5e5f120151186ba815dab07cf8a7f48"]
      Server: [GitHub.com]
      Status: [200 OK]
      Strict-Transport-Security: [max-age=31536000; includeSubdomains; preload]
      Vary: ['Accept, Authorization, Cookie, X-GitHub-OTP']
      X-Accepted-OAuth-Scopes: ['']
      X-Content-Type-Options: [nosniff]
      X-Frame-Options: [deny]
      X-GitHub-Media-Type: [github.v3; format=json]
      X-GitHub-Request-Id: ['B70C:169F:209E3B:44ED76:596F74FB']
      X-OAuth-Scopes: ['admin:org, admin:org_hook, admin:public_key, admin:repo_hook,
          delete_repo, gist, notifications, repo, user']
      X-RateLimit-Limit: ['5000']
      X-RateLimit-Remaining: ['4997']
      X-RateLimit-Reset: ['1500480264']
      X-Runtime-rack: ['0.574744']
      X-XSS-Protection: [1; mode=block]
    status: {code: 200, message: OK}
version: 1
+49
Changes for tests/GitHub/cassettes/github_content_get.yaml: 49 added lines, 0 removed lines.
Original line number Diff line number Diff line
interactions:
- request:
    body: null
    headers:
      Accept: ['*/*']
      Accept-Encoding: ['gzip, deflate']
      Connection: [keep-alive]
      User-Agent: [python-requests/2.13.0]
    method: GET
    uri: https://api.github.com/repos/gitmate-test-user/test/contents/README.md?User-Agent=IGitt&path=%2Frepos%2Fgitmate-test-user%2Ftest%2Fcontents%2FREADME.md&ref=master
  response:
    body:
      string: !!binary |
        H4sIAAAAAAAAA7WSW08CMRCF/0ufgQJ7CZIQ466IPOgDD4rEhMy23UvsZbMtEiD8d2eVKBJ5YBPf
        mpPON3POzI5oUIIMyWx8c/sw7ihOWqQEl59INgdUQt8bMM/r9a/CXj9IQz/wEhbyHgAE4QBY0mci
        CPwBMmyxRaznt8iqkliaO1faIaVQFp2scPkq6TCjaCVKYykKCpxoO2Fde2VFResXZUY7oZ2l38Nd
        VyIdKbBOVNgjd0ouf+OP0GegiTQJ/UL8cBGG309Yl4yK1bQmW3pBRtystTTAT/pWsD5EVEdxCOEz
        rTOW/nDjNmW91bSQAr0dGChMN1F3MX/sxmqc8cnTlsfRZjGP3uOsDBbPd2Z6PzMv89l2OpHdOBuN
        XjWWC80ML3SG9QlYEfqoLWWh3ywZ7ogVMv2nBaPfhuRG+6jv6ahf01va7z8A9p8NjlYDAAA=
    headers:
      Access-Control-Allow-Origin: ['*']
      Access-Control-Expose-Headers: ['ETag, Link, X-GitHub-OTP, X-RateLimit-Limit,
          X-RateLimit-Remaining, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes,
          X-Poll-Interval']
      Cache-Control: ['private, max-age=60, s-maxage=60']
      Content-Encoding: [gzip]
      Content-Security-Policy: [default-src 'none']
      Content-Type: [application/json; charset=utf-8]
      Date: ['Wed, 19 Jul 2017 15:04:32 GMT']
      ETag: [W/"cd2d7739e03fb40dd7b3da6af44f8ffb"]
      Last-Modified: ['Wed, 19 Jul 2017 15:04:28 GMT']
      Server: [GitHub.com]
      Status: [200 OK]
      Strict-Transport-Security: [max-age=31536000; includeSubdomains; preload]
      Vary: ['Accept, Authorization, Cookie, X-GitHub-OTP']
      X-Accepted-OAuth-Scopes: ['']
      X-Content-Type-Options: [nosniff]
      X-Frame-Options: [deny]
      X-GitHub-Media-Type: [github.v3; format=json]
      X-GitHub-Request-Id: ['981B:169F:20A008:44F11A:596F74FE']
      X-OAuth-Scopes: ['admin:org, admin:org_hook, admin:public_key, admin:repo_hook,
          delete_repo, gist, notifications, repo, user']
      X-RateLimit-Limit: ['5000']
      X-RateLimit-Remaining: ['4996']
      X-RateLimit-Reset: ['1500480264']
      X-Runtime-rack: ['0.068597']
      X-XSS-Protection: [1; mode=block]
    status: {code: 200, message: OK}
version: 1
Loading