This repository has been archived by the owner on Jul 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.ts
67 lines (58 loc) · 2.19 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as core from '@actions/core';
import * as github from '@actions/github';
import type { GitUpdateRefResponseData, OctokitResponse } from '@octokit/types';
async function run(): Promise<void> {
try {
const githubToken = core.getInput('github-token');
const sourceBranchName = core.getInput('source');
const destBranchName = core.getInput('dest');
const octokit = github.getOctokit(githubToken);
const sourceBranchTip = await octokit.git.getRef({
...github.context.repo,
ref: `heads/${sourceBranchName}`,
});
if (sourceBranchTip.data.object.type !== "commit") {
throw new Error(`Expected branch ${sourceBranchName} to resolve to a commit. Got a ${sourceBranchTip.data.object.type}.`);
}
const sourceBranchSha = sourceBranchTip.data.object.sha;
console.log(`Pushing ${sourceBranchName} (${sourceBranchSha}) to ${destBranchName}.`);
let result: OctokitResponse<GitUpdateRefResponseData>;
try {
result = await octokit.git.updateRef({
...github.context.repo,
ref: `heads/${destBranchName}`,
sha: sourceBranchSha,
force: true,
});
} catch (error: any) {
if (error.message !== 'Reference does not exist') {
throw error;
}
console.log(`${destBranchName} does not exist. Creating it.`);
result = await octokit.git.createRef({
...github.context.repo,
ref: `refs/heads/${destBranchName}`,
sha: sourceBranchSha,
});
}
console.log(`Set ${result.data.ref} to ${result.data.object.sha}.`);
} catch (error: any) {
core.setFailed(error.message);
}
}
run();