handle updates by updating the branch ref
This commit is contained in:
parent
35dbaca7d5
commit
714e9be377
2 changed files with 61 additions and 2 deletions
28
dist/index.js
vendored
28
dist/index.js
vendored
|
@ -1287,8 +1287,9 @@ class GitHubHelper {
|
||||||
branchName: branch
|
branchName: branch
|
||||||
});
|
});
|
||||||
core.debug(`Fetched information for branch '${branch}' - '${JSON.stringify(branchRef)}'`);
|
core.debug(`Fetched information for branch '${branch}' - '${JSON.stringify(branchRef)}'`);
|
||||||
|
const branchExists = branchRef.repository.ref != null;
|
||||||
// if the branch does not exist, then first we need to create the branch from base
|
// if the branch does not exist, then first we need to create the branch from base
|
||||||
if (branchRef.repository.ref == null) {
|
if (!branchExists) {
|
||||||
core.debug(`Branch does not exist - '${branch}'`);
|
core.debug(`Branch does not exist - '${branch}'`);
|
||||||
branchRef = yield this.octokit.graphql(refQuery, {
|
branchRef = yield this.octokit.graphql(refQuery, {
|
||||||
repoOwner: repoOwner,
|
repoOwner: repoOwner,
|
||||||
|
@ -1376,6 +1377,31 @@ class GitHubHelper {
|
||||||
const commit = yield this.octokit.graphql(pushCommitMutation, pushCommitVars);
|
const commit = yield this.octokit.graphql(pushCommitMutation, pushCommitVars);
|
||||||
core.debug(`Pushed commit - '${JSON.stringify(commit)}'`);
|
core.debug(`Pushed commit - '${JSON.stringify(commit)}'`);
|
||||||
core.info(`Pushed commit with hash - '${commit.createCommitOnBranch.commit.oid}' on branch - '${commit.createCommitOnBranch.ref.name}'`);
|
core.info(`Pushed commit with hash - '${commit.createCommitOnBranch.commit.oid}' on branch - '${commit.createCommitOnBranch.ref.name}'`);
|
||||||
|
if (branchExists) {
|
||||||
|
// The branch existed so update the branch ref to point to the new commit
|
||||||
|
// This is the same behavior as force pushing the branch
|
||||||
|
core.info(`Updating branch '${branch}' to commit '${commit.createCommitOnBranch.commit.oid}'`);
|
||||||
|
const updateBranchMutation = `
|
||||||
|
mutation UpdateBranch($branchId: ID!, $commitOid: GitObjectID!) {
|
||||||
|
updateRef(input: {
|
||||||
|
refId: $branchId,
|
||||||
|
oid: $commitOid,
|
||||||
|
force: true
|
||||||
|
}) {
|
||||||
|
ref {
|
||||||
|
id
|
||||||
|
name
|
||||||
|
prefix
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
const updatedBranch = yield this.octokit.graphql(updateBranchMutation, {
|
||||||
|
branchId: branchRef.repository.ref.id,
|
||||||
|
commitOid: commit.createCommitOnBranch.commit.oid
|
||||||
|
});
|
||||||
|
core.debug(`Updated branch - '${JSON.stringify(updatedBranch)}'`);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -235,8 +235,10 @@ export class GitHubHelper {
|
||||||
`Fetched information for branch '${branch}' - '${JSON.stringify(branchRef)}'`
|
`Fetched information for branch '${branch}' - '${JSON.stringify(branchRef)}'`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const branchExists = branchRef.repository.ref != null
|
||||||
|
|
||||||
// if the branch does not exist, then first we need to create the branch from base
|
// if the branch does not exist, then first we need to create the branch from base
|
||||||
if (branchRef.repository.ref == null) {
|
if (!branchExists) {
|
||||||
core.debug(`Branch does not exist - '${branch}'`)
|
core.debug(`Branch does not exist - '${branch}'`)
|
||||||
branchRef = await this.octokit.graphql<{repository: TempRepository}>(
|
branchRef = await this.octokit.graphql<{repository: TempRepository}>(
|
||||||
refQuery,
|
refQuery,
|
||||||
|
@ -357,5 +359,36 @@ export class GitHubHelper {
|
||||||
core.info(
|
core.info(
|
||||||
`Pushed commit with hash - '${commit.createCommitOnBranch.commit.oid}' on branch - '${commit.createCommitOnBranch.ref.name}'`
|
`Pushed commit with hash - '${commit.createCommitOnBranch.commit.oid}' on branch - '${commit.createCommitOnBranch.ref.name}'`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if (branchExists) {
|
||||||
|
// The branch existed so update the branch ref to point to the new commit
|
||||||
|
// This is the same behavior as force pushing the branch
|
||||||
|
core.info(
|
||||||
|
`Updating branch '${branch}' to commit '${commit.createCommitOnBranch.commit.oid}'`
|
||||||
|
)
|
||||||
|
const updateBranchMutation = `
|
||||||
|
mutation UpdateBranch($branchId: ID!, $commitOid: GitObjectID!) {
|
||||||
|
updateRef(input: {
|
||||||
|
refId: $branchId,
|
||||||
|
oid: $commitOid,
|
||||||
|
force: true
|
||||||
|
}) {
|
||||||
|
ref {
|
||||||
|
id
|
||||||
|
name
|
||||||
|
prefix
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
const updatedBranch = await this.octokit.graphql<{updateRef: {ref: Ref}}>(
|
||||||
|
updateBranchMutation,
|
||||||
|
{
|
||||||
|
branchId: branchRef.repository.ref!.id,
|
||||||
|
commitOid: commit.createCommitOnBranch.commit.oid
|
||||||
|
}
|
||||||
|
)
|
||||||
|
core.debug(`Updated branch - '${JSON.stringify(updatedBranch)}'`)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue