-
-
Notifications
You must be signed in to change notification settings - Fork 84
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
Changes from all commits
4bc84f8
5bc8ccf
3cc4f8b
acdcd8b
1f23736
77c71b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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') | ||
} | ||
|
||
async getLastCommitSha() { | ||
|
@@ -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 }`, | ||
|
@@ -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), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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( | ||
|
@@ -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) | ||
} | ||
|
There was a problem hiding this comment.
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 theexec
command buffer size, and also the encoding that this command output produces.