/ contracts / src / GitHubRepositoryReg.sol
GitHubRepositoryReg.sol
  1  /** 
  2   * GitHubRepositoryReg.sol
  3   * Registers the master branch of a Repository for GitHubOracle tracking.
  4   * Ricardo Guilherme Schmidt <3esmit@gmail.com>
  5   */
  6  
  7  import "./GitHubAPIReg.sol";
  8  import "./NameRegistry.sol";
  9  import "./GitRepository.sol";
 10  import "strings.sol";
 11  
 12  pragma solidity ^0.4.11;
 13  
 14  contract GitHubRepositoryReg is NameRegistry, GitHubAPIReg {
 15      using strings for string;
 16      using strings for strings.slice;
 17      
 18      mapping (uint256 => Repository) public repositories; 
 19  
 20      event NewRepository(address addr, uint256 projectId, string full_name, string default_branch);
 21      
 22      struct Repository {
 23          address addr; 
 24          string name;
 25          string branch; 
 26      }
 27  
 28      function register(string _repository, string _cred) payable {
 29          if(bytes(_cred).length == 0) _cred = cred; 
 30          uint gas = getAddr(_repository) == 0x00? 4000000 : 1000000;
 31          oraclize_query("URL", _query_script(_repository,_cred), gas);
 32      }
 33  
 34      function getAddr(uint256 _id) public constant returns(address addr) {
 35          return repositories[_id].addr;
 36      }
 37  
 38       function getName(address _addr) public constant returns(string name){
 39          return repositories[indexes[sha3(_addr)]].name;
 40      } 
 41  
 42      function getAddr(string _name) public constant returns(address addr) {
 43          return repositories[indexes[sha3(_name)]].addr;
 44      }
 45      
 46      function getBranch(uint256 _id) public constant returns(string branch) {
 47          return repositories[_id].branch;
 48      }
 49  
 50      //oraclize response callback
 51      function __callback(bytes32 myid, string result, bytes proof) {
 52          OracleEvent(myid,result,proof);
 53          if (msg.sender != oraclize.cbAddress()){
 54            throw;  
 55          }else {
 56              _setRepository(result);
 57          }
 58      }
 59  
 60      function _setRepository(string result) internal //[85743750, "ethereans/TheEtherian", "master"]
 61      {
 62          bytes memory v = bytes(result);
 63          uint8 pos = 0;
 64          uint256 projectId; 
 65          (projectId,pos) = getNextUInt(v,pos);
 66          string memory full_name;
 67          (full_name,pos) = getNextString(v,pos);
 68          string memory default_branch;
 69          (default_branch,pos) = getNextString(v,pos);
 70          address repoAddr = repositories[projectId].addr;
 71          if(repoAddr == 0x0){   
 72              GitRepositoryI repo = GitFactory.newGitRepository(projectId, full_name);
 73              repo.setOwner(owner);
 74              repoAddr = address(repo);
 75              indexes[sha3(repoAddr)] = projectId; 
 76              indexes[sha3(full_name)] = projectId;
 77              NewRepository(repoAddr, projectId, full_name, default_branch);
 78              repositories[projectId] = Repository({addr: repoAddr, name: full_name, branch: default_branch});
 79          }else{
 80              bytes32 _new = sha3(full_name);
 81              bytes32 _old = sha3(repositories[projectId].name);
 82              if(_new != _old){
 83                  _updateIndex(_old, _new);
 84              }
 85          }
 86      }
 87      //internal helper functions
 88      function _query_script(string _repository, string _cred) internal returns (string)  {
 89         strings.slice [] memory cm  = new strings.slice[](5);
 90         cm[0] = strings.toSlice("json(https://api.github.com/repos/");
 91         cm[1] = _repository.toSlice();
 92         cm[2] = _cred.toSlice();
 93         cm[4] = strings.toSlice(").$.id,full_name,default_branch");
 94         return strings.toSlice("").join(cm);        
 95      }
 96  }
 97  library GHRepoReg {
 98  
 99      function create() returns (GitHubRepositoryReg){
100          return new GitHubRepositoryReg();
101      }
102  
103  }