Skip to content

Commit 53a4d98

Browse files
authored
setup repo action on template creation
1 parent 655a7bc commit 53a4d98

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
Mainly used to setup repository default pacakge.json contents.
3+
You may delete this file after repo creation if not already deleted by the template setup scripts.
4+
*/
5+
6+
const fs = require('fs');
7+
let github, context, org, repoName;
8+
// Include fs module
9+
10+
function _isValidRepoInitEvent(){
11+
// create event is triggered whenever a new branch is created in the repo. so we do a hack that if the master branch
12+
// of the repo is created, we consider it as repo creation as master branch is created only on repo create. This
13+
// should work all the time on create from template and most of the time on external creation.
14+
if(context.eventName !== 'create'){
15+
return false;
16+
}
17+
if(!context.ref.endsWith(`/${context.payload.master_branch}`)){
18+
return false;
19+
}
20+
return true;
21+
}
22+
23+
function setupPackageJSON() {
24+
let data = JSON.parse(fs.readFileSync('./package.json', {encoding:'utf8', flag:'r'}));
25+
data.name = `gh-${org}-${repoName}`;
26+
data.description = context.payload.repository.description || "A simple phcode.dev extension/theme.";
27+
data.title = repoName;
28+
data.license = context.payload.repository.license && context.payload.repository.license.name || "unknown";
29+
data.author = `${context.payload.repository.owner.login}`;
30+
data.homepage = context.payload.repository.html_url;
31+
data.version = "0.0.1";
32+
33+
fs.writeFileSync('./package.json', JSON.stringify(data, null, 4));
34+
console.log("package.json file written successfully\n", data);
35+
}
36+
37+
async function verifyRepoIsPublic() {
38+
if(!context.payload.repository.private){
39+
console.log("Repository is public, all good.");
40+
return;
41+
}
42+
console.log("Repository must be public to publish",
43+
`Repository must be public to publish the extension to the extension store.\n
44+
If you want to create a private source extension, create another public repo just to create releases an publich the extension without making the source code public.`
45+
);
46+
}
47+
48+
async function initRepo(details){
49+
github = details.github;
50+
context = details.context;
51+
org = details.org;
52+
repoName = details.repoName;
53+
console.log("github: ", JSON.stringify(github, null, 4));
54+
console.log("context: ", JSON.stringify(context, null, 4));
55+
if(!_isValidRepoInitEvent()){
56+
console.log("Not a valid repo creation event. This task is only meant to be executed at repo creation. Exiting!");
57+
return;
58+
}
59+
setupPackageJSON();
60+
verifyRepoIsPublic();
61+
62+
// cleanup
63+
fs.unlinkSync('./.github/workflows/setupRepository.cjs');
64+
fs.unlinkSync('./.github/workflows/setupRepository.yml');
65+
}
66+
67+
module.exports.initRepo = initRepo;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Setup and Init repository after create from template.
2+
# only to be run once. You may delete this file after the repo is created from template
3+
4+
on:
5+
create:
6+
7+
jobs:
8+
setup_repository:
9+
runs-on: ubuntu-latest
10+
permissions:
11+
# Give the default GITHUB_TOKEN write permission to commit and push the
12+
# added or changed files to the repository.
13+
contents: write
14+
steps:
15+
- uses: actions/checkout@v3
16+
- uses: actions/github-script@v6
17+
with:
18+
script: |
19+
console.log("Setting up the repository", context);
20+
const org = context.payload.repository.owner.login;
21+
const repoName = context.payload.repository.name;
22+
const setupScript = require('.github/workflows/setupRepository.cjs');
23+
setupScript.initRepo({github, context, org, repoName});
24+
- uses: stefanzweifel/git-auto-commit-action@v4

0 commit comments

Comments
 (0)