Skip to content

Commit

Permalink
Create new commit for changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjrw committed Jul 27, 2020
1 parent 02940fb commit f5640f4
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 17 deletions.
64 changes: 64 additions & 0 deletions src/commit.ts
@@ -0,0 +1,64 @@
import { Octokit } from "@octokit/rest/index"
import { Context } from "@actions/github/lib/context"

import { Change } from '@/play'

export async function makeCommit (
message: string,
changes: Change[],
octokit: Octokit,
context: Context,
): Promise<void> {
/**
* From the given list of changes, makes a single commit to implement them.
*
* @param changes: The list of changes to make.
*/

// Grab the SHA of the latest commit
const remoteCommits = await octokit.repos.listCommits({
owner: context.repo.owner,
repo: context.repo.repo,
sha: "play",
per_page: 1,
})
let latestCommitSha = remoteCommits.data[0].sha
const treeSha = remoteCommits.data[0].commit.tree.sha

// Make a new tree for these changes
const newTree = await octokit.git.createTree({
owner: context.repo.owner,
repo: context.repo.repo,
base_tree: treeSha,
tree: changes.map(change => {
const subTree: Octokit.GitCreateTreeParamsTree = {
path: change.path,
mode: '100644',
}
if (change.content) {
subTree.content = change.content
} else {
subTree.sha = "null"
}
return subTree
})
})
const newTreeSha = newTree.data.sha

const newCommit = await octokit.git.createCommit({
owner: context.repo.owner,
repo: context.repo.repo,
message,
tree: newTreeSha,
parents: [latestCommitSha]
})
latestCommitSha = newCommit.data.sha

// Set HEAD of play branch to the new commit
await octokit.git.updateRef({
owner: context.repo.owner,
repo: context.repo.repo,
sha: latestCommitSha,
ref: "heads/play",
})
}
34 changes: 17 additions & 17 deletions src/play.ts
Expand Up @@ -6,6 +6,8 @@ import { addReaction } from '@/issues'
import { handleError } from '@/error'
import { resetGame } from '@/new'
import { makeMove } from '@/move'
import { makeCommit } from '@/commit'
import { getPlayerTeam } from '@/player'

export interface Change {
path: string
Expand Down Expand Up @@ -39,32 +41,30 @@ export default async function play (

const gamePath = "games/current"

// Prepare a list of trees, which will be made into a single commit to the
// Prepare a list of changes, which will be made into a single commit to the
// play branch
const trees: Octokit.GitCreateTreeParamsTree[] = []
let changes: Change[] = []

try {
addReaction("eyes", octokit, context)
const [command, move] = parseIssueTitle(title)
if (command === "new") {
trees.push(...await resetGame(gamePath, octokit, context))
changes = changes.concat(
await resetGame(gamePath, octokit, context)
)
} else if (command === "move") {
trees.push(...await makeMove(move, gamePath, octokit, context))
changes = changes.concat(
await makeMove(move, gamePath, octokit, context)
)
}

// All the blobs have been collected, so commit them

// Get the SHA of the play branch

// Make the commit
await octokit.git.createCommit({
owner: context.repo.owner,
repo: context.repo.repo,
parents: ["TODO"],
message: `@${context.actor} ${command} (#${context.issue.number})`,
tree: trees,

})
// All the changes have been collected - commit them
await makeCommit(
`@${context.actor} ${command === "new" ? "Start a new game" : `Move ${getPlayerTeam(context.actor) === "b" ? "black" : "white"} ${move} (#${context.issue.number})`}`,
changes,
octokit,
context,
)
} catch (error) {
// If there was an error, forward it to the user, then stop
handleError(error, octokit, context, core)
Expand Down

0 comments on commit f5640f4

Please sign in to comment.