Question

I am trying to create GitHub Repository from PHP code, as well as i wanted to upload the files into the created GitHub Repository.

I been trying to use https://github.com/ornicar/php-github-api this PHP API, But i couldn't find anywhere that how can i upload Files into the created GitHub Repository.

So Please Help me to solve this issue, Thanks in Advance.

Ex:

  1. I want to create a Respo Named myProject1

  2. Then i wanted to add three files(papi.php, index.php, jquery.min.js).

So please help me.

Was it helpful?

Solution

As mentioned in "GitHub API - write to repo", you would need to create a blob.

You can see an example in KnpLabs/php-github-api/test/Github/Tests/Api/GitData/BlobsTest.php#L53-L68

(I am using KnpLabs/php-github-api, since ornicar/php-github-api is deprecated and refers to it)

/**
 * @test
 */
public function shouldCreateBlob()
{
    $expectedValue = array('blob' => 'some data');
    $data = array('content' => 'some cotent', 'encoding' => 'utf8');

    $api = $this->getApiMock();
    $api->expects($this->once())
        ->method('post')
        ->with('repos/l3l0/l3l0repo/git/blobs', $data)
        ->will($this->returnValue($expectedValue));

    $this->assertEquals($expectedValue, $api->create('l3l0', 'l3l0repo', $data));
}

There is no filename yet: you upload content;

To create a file though, as shown in this example, you would still need to:

  • get the SHA the current master branch points to
  • fetch the tree this SHA belongs to
  • create a new tree object with the new blob, based on the old tree
  • create a new commit object using the new tree and point its parent to the current master
  • finally update the heads/master reference to point to the new commit
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top