Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix verified commits #163

Merged
merged 6 commits into from
Feb 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 43 additions & 12 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17417,6 +17417,7 @@ const github = __nccwpck_require__(5438)
const { GitHub, getOctokitOptions } = __nccwpck_require__(3030)
const { throttling } = __nccwpck_require__(9968)
const path = __nccwpck_require__(1017)
const fs = __nccwpck_require__(7147)

const {
GITHUB_TOKEN,
Expand Down Expand Up @@ -17602,12 +17603,11 @@ class Git {
}
}

async getBlobContent(objectSha) {
return await execCmd(
`git show ${ objectSha }`,
this.workingDir,
false // Do not trim the result
)
async getBlobBase64Content(file) {
const fileRelativePath = path.join(this.workingDir, file)
const fileContent = await fs.promises.readFile(fileRelativePath)

return fileContent.toString('base64')
}

async getLastCommitSha() {
Expand Down Expand Up @@ -17645,7 +17645,7 @@ class Git {
)
}

// Creates a tree object with all the blobs of each commit
// Returns a git tree parsed for the specified commit sha
async getTree(commitSha) {
const output = await execCmd(
`git ls-tree -r --full-tree ${ commitSha }`,
Expand All @@ -17655,19 +17655,49 @@ class Git {
const tree = []
for (const treeObject of output.split('\n')) {
const [ mode, type, sha ] = treeObject.split(/\s/)
const treeObjectPath = treeObject.split('\t')[1]
const file = treeObject.split('\t')[1]

const treeEntry = {
mode,
type,
content: await this.getBlobContent(sha),
path: treeObjectPath
sha,
path: file
}

tree.push(treeEntry)
}

return tree
}

// Creates the blob objects in GitHub for the files that are not in the previous commit only
async createGithubBlobs(commitSha) {
core.debug('Creating missing blobs on GitHub')
const [ previousTree, tree ] = await Promise.all([ this.getTree(`${ commitSha }~1`), this.getTree(commitSha) ])
const promisesGithubCreateBlobs = []

for (const treeEntry of tree) {
// If the current treeEntry are in the previous tree, that means that the blob is uploaded and it doesn't need to be uploaded to GitHub again.
if (previousTree.findIndex((entry) => entry.sha === treeEntry.sha) !== -1) {
continue
}

const base64Content = await this.getBlobBase64Content(treeEntry.path)

// Creates the blob. We don't need to store the response because the local sha is the same and we can use it to reference the blob
const githubCreateBlobRequest = this.github.git.createBlob({
owner: this.repo.user,
repo: this.repo.name,
content: base64Content,
encoding: 'base64'
})
promisesGithubCreateBlobs.push(githubCreateBlobRequest)
}

// Wait for all the file uploads to be completed
await Promise.all(promisesGithubCreateBlobs)
}

// Gets the commit list in chronological order
async getCommitsToPush() {
const output = await execCmd(
Expand All @@ -17692,9 +17722,10 @@ class Git {

const commitsData = []
for (const commitSha of commitsToPush) {
const [ commitMessage, tree ] = await Promise.all([ this.getCommitMessage(commitSha), this.getTree(commitSha), this.createGithubBlobs(commitSha) ])
const commitData = {
commitMessage: await this.getCommitMessage(commitSha),
tree: await this.getTree(commitSha)
commitMessage,
tree
}
commitsData.push(commitData)
}
Expand Down
55 changes: 43 additions & 12 deletions src/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const github = require('@actions/github')
const { GitHub, getOctokitOptions } = require('@actions/github/lib/utils')
const { throttling } = require('@octokit/plugin-throttling')
const path = require('path')
const fs = require('fs')

const {
GITHUB_TOKEN,
Expand Down Expand Up @@ -189,12 +190,11 @@ class Git {
}
}

async getBlobContent(objectSha) {
return await execCmd(
`git show ${ objectSha }`,
this.workingDir,
false // Do not trim the result
)
async getBlobBase64Content(file) {
const fileRelativePath = path.join(this.workingDir, file)
const fileContent = await fs.promises.readFile(fileRelativePath)

return fileContent.toString('base64')
Comment on lines +193 to +197
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of pushing the content of the file, this solution converts the file to base64.

Also, I have replaced the way of getting the content of the file. Previously I used the git show command, but I found that this could be problematic because of the exec command buffer size, and also the encoding that this command output produces.

}

async getLastCommitSha() {
Expand Down Expand Up @@ -232,7 +232,7 @@ class Git {
)
}

// Creates a tree object with all the blobs of each commit
// Returns a git tree parsed for the specified commit sha
async getTree(commitSha) {
const output = await execCmd(
`git ls-tree -r --full-tree ${ commitSha }`,
Expand All @@ -242,19 +242,49 @@ class Git {
const tree = []
for (const treeObject of output.split('\n')) {
const [ mode, type, sha ] = treeObject.split(/\s/)
const treeObjectPath = treeObject.split('\t')[1]
const file = treeObject.split('\t')[1]

const treeEntry = {
mode,
type,
content: await this.getBlobContent(sha),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't upload the content of the file when creating the tree, the files/blobs are references by its SHA only.

path: treeObjectPath
sha,
path: file
}

tree.push(treeEntry)
}

return tree
}

// Creates the blob objects in GitHub for the files that are not in the previous commit only
async createGithubBlobs(commitSha) {
core.debug('Creating missing blobs on GitHub')
const [ previousTree, tree ] = await Promise.all([ this.getTree(`${ commitSha }~1`), this.getTree(commitSha) ])
const promisesGithubCreateBlobs = []

for (const treeEntry of tree) {
// If the current treeEntry are in the previous tree, that means that the blob is uploaded and it doesn't need to be uploaded to GitHub again.
if (previousTree.findIndex((entry) => entry.sha === treeEntry.sha) !== -1) {
continue
}

const base64Content = await this.getBlobBase64Content(treeEntry.path)

// Creates the blob. We don't need to store the response because the local sha is the same and we can use it to reference the blob
const githubCreateBlobRequest = this.github.git.createBlob({
owner: this.repo.user,
repo: this.repo.name,
content: base64Content,
encoding: 'base64'
})
promisesGithubCreateBlobs.push(githubCreateBlobRequest)
}

// Wait for all the file uploads to be completed
await Promise.all(promisesGithubCreateBlobs)
}
Comment on lines +261 to +286
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function compares the content of the tree to commit with the previous tree.

Iterates over each blob entry for the current commit checking if it was present on the previous tree. If it's not present, it uploads it to GitHub, if not just skip the upload.


// Gets the commit list in chronological order
async getCommitsToPush() {
const output = await execCmd(
Expand All @@ -279,9 +309,10 @@ class Git {

const commitsData = []
for (const commitSha of commitsToPush) {
const [ commitMessage, tree ] = await Promise.all([ this.getCommitMessage(commitSha), this.getTree(commitSha), this.createGithubBlobs(commitSha) ])
const commitData = {
commitMessage: await this.getCommitMessage(commitSha),
tree: await this.getTree(commitSha)
commitMessage,
tree
}
commitsData.push(commitData)
}
Expand Down