Skip to content

Commit 93730a2

Browse files
Merge pull request #1 from VerticeOne/feature/CE-480_template_repo
Include repo object when templating
2 parents 3023dac + 8c82d01 commit 93730a2

File tree

6 files changed

+52
-16
lines changed

6 files changed

+52
-16
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,37 @@ Result:
234234
Created by Maxi (@BetaHuhn)
235235
```
236236

237+
By default, every Nunjucks template context will be pre-populated with the `repo` object containing the following values:
238+
239+
- `repo.url`
240+
- `repo.fullName`
241+
- `repo.uniqueName`
242+
- `repo.host`
243+
- `repo.user`
244+
- `repo.name`
245+
- `repo.branch`
246+
247+
This can be useful for certain bulk templating use cases:
248+
249+
```yml
250+
# sync.yml
251+
252+
group:
253+
- repos: |
254+
user/repo1
255+
user/repo2
256+
...
257+
files:
258+
- source: src/README.md
259+
template: true
260+
```
261+
262+
```yml
263+
# README.md
264+
265+
Hello from {{ repo.name }} by {{ repo.user }}!
266+
```
267+
237268
You can also use `extends` with a relative path to inherit other templates. Take a look at Nunjucks [template syntax](https://mozilla.github.io/nunjucks/templating.html) for more info.
238269

239270
```yml

dist/index.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ const parseRepoName = (fullRepo) => {
161161
const branch = fullRepo.split('@')[1] || 'default'
162162

163163
return {
164+
url: `https://${ host }/${ user }/${ name }`,
164165
fullName: `${ host }/${ user }/${ name }`,
165166
uniqueName: `${ host }/${ user }/${ name }@${ branch }`,
166167
host,
@@ -248,4 +249,4 @@ export async function parseConfig() {
248249
return Object.values(result)
249250
}
250251

251-
export default context
252+
export default context

src/git.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ export default class Git {
123123

124124
async getBaseBranch() {
125125
this.baseBranch = await execCmd(
126-
`git rev-parse --abbrev-ref HEAD`,
126+
`git branch --show-current`,
127127
this.workingDir
128128
)
129129
}
@@ -209,7 +209,7 @@ export default class Git {
209209

210210
async getLastCommitSha() {
211211
this.lastCommitSha = await execCmd(
212-
`git rev-parse HEAD`,
212+
`git log -1 --format='%H'`,
213213
this.workingDir
214214
)
215215
}
@@ -527,4 +527,4 @@ export default class Git {
527527
})
528528
this.lastCommitSha = request.data.sha
529529
}
530-
}
530+
}

src/helpers.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,19 @@ export async function pathIsDirectory(path) {
6969
return stat.isDirectory()
7070
}
7171

72-
export async function write(src, dest, context) {
72+
export async function write(src, dest, context, item) {
7373
if (typeof context !== 'object') {
7474
context = {}
7575
}
76+
77+
// include current repo constants (e.g., host, user, name, branch, etc.)
78+
context.repo = item.repo
79+
7680
const content = nunjucks.render(src, context)
7781
await fs.outputFile(dest, content)
7882
}
7983

80-
export async function copy(src, dest, isDirectory, file) {
84+
export async function copy(src, dest, isDirectory, file, item) {
8185
const deleteOrphaned = isDirectory && file.deleteOrphaned
8286
const exclude = file.exclude
8387

@@ -121,12 +125,12 @@ export async function copy(src, dest, isDirectory, file) {
121125

122126
const srcPath = path.join(src, srcFile)
123127
const destPath = path.join(dest, srcFile)
124-
await write(srcPath, destPath, file.template)
128+
await write(srcPath, destPath, file.template, item)
125129
}
126130
} else {
127131
core.debug(`Render file ${ src } to ${ dest }`)
128132

129-
await write(src, dest, file.template)
133+
await write(src, dest, file.template, item)
130134
}
131135
} else {
132136
core.debug(`Copy ${ src } to ${ dest }`)
@@ -166,4 +170,4 @@ export async function remove(src) {
166170

167171
export function arrayEquals(array1, array2) {
168172
return Array.isArray(array1) && Array.isArray(array2) && array1.length === array2.length && array1.every((value, i) => value === array2[i])
169-
}
173+
}

src/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ async function run() {
3333

3434
await forEach(repos, async (item) => {
3535
core.info(`Repository Info`)
36-
core.info(`Slug : ${ item.repo.name }`)
37-
core.info(`Owner : ${ item.repo.user }`)
38-
core.info(`Https Url : https://${ item.repo.fullName }`)
39-
core.info(`Branch : ${ item.repo.branch }`)
36+
core.info(`Slug : ${ item.repo.name }`)
37+
core.info(`Owner : ${ item.repo.user }`)
38+
core.info(`Url : ${ item.repo.url }`)
39+
core.info(`Branch : ${ item.repo.branch }`)
4040
core.info(' ')
4141
try {
4242

@@ -74,7 +74,7 @@ async function run() {
7474

7575
if (isDirectory) core.info(`Source is directory`)
7676

77-
await copy(source, dest, isDirectory, file)
77+
await copy(source, dest, isDirectory, file, item)
7878

7979
await git.add(file.dest)
8080

@@ -220,4 +220,4 @@ run()
220220
.catch((err) => {
221221
core.setFailed(err.message)
222222
core.debug(err)
223-
})
223+
})

0 commit comments

Comments
 (0)