Skip to content

Commit 784518d

Browse files
authored
Fix detection of github organization for README templates (#374)
1 parent 56cac58 commit 784518d

File tree

8 files changed

+38
-36
lines changed

8 files changed

+38
-36
lines changed

.github/workflows/auto-readme.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
# However, using a personal access token will cause events to be triggered.
1717
# We need that to ensure a status gets posted after the auto-format commit.
1818
# We also want to trigger tests if the auto-format made no changes.
19-
- uses: actions/checkout@v2
19+
- uses: actions/checkout@v4
2020
if: github.event.pull_request.state == 'open'
2121
name: Privileged Checkout
2222
with:

.github/workflows/chatops.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ jobs:
77
default:
88
runs-on: ubuntu-latest
99
steps:
10-
- uses: actions/checkout@v2
10+
- uses: actions/checkout@v4
1111
- name: "Handle common commands"
12-
uses: cloudposse/actions/github/slash-command-dispatch@0.30.0
12+
uses: cloudposse/actions/github/slash-command-dispatch@0.33.0
1313
with:
1414
token: ${{ secrets.REPO_ACCESS_TOKEN }}
1515
reaction-token: ${{ secrets.GITHUB_TOKEN }}
@@ -22,9 +22,9 @@ jobs:
2222
runs-on: ubuntu-latest
2323
steps:
2424
- name: "Checkout commit"
25-
uses: actions/checkout@v2
25+
uses: actions/checkout@v4
2626
- name: "Run tests"
27-
uses: cloudposse/actions/github/slash-command-dispatch@0.30.0
27+
uses: cloudposse/actions/github/slash-command-dispatch@0.33.0
2828
with:
2929
token: ${{ secrets.REPO_ACCESS_TOKEN }}
3030
reaction-token: ${{ secrets.GITHUB_TOKEN }}

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
steps:
1818
- uses: actions/checkout@v4
1919
- shell: bash
20-
run: /usr/bin/make BUILD_HARNESS_PATH=/build-harness PACKAGES_PREFER_HOST=true readme/lint
20+
run: /usr/bin/make BUILD_HARNESS_PATH=/build-harness PACKAGES_PREFER_HOST=true git-safe-directory readme/lint
2121

2222
super-linter:
2323
name: superlinter

.github/workflows/validate-codeowners.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ jobs:
99
runs-on: ubuntu-latest
1010
steps:
1111
- name: "Checkout source code at current commit"
12-
uses: actions/checkout@v2
12+
uses: actions/checkout@v4
13+
# Waiting to update codeowners-validator until https://github.com/mszostok/codeowners-validator/issues/173 is resolved
1314
- uses: mszostok/codeowners-validator@v0.7.1
1415
if: github.event.pull_request.head.repo.full_name == github.repository
1516
name: "Full check of CODEOWNERS"

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
<!-- markdownlint-disable -->
33
[![Project Banner](.github/banner.png?raw=true)](https://cpco.io/homepage)
4-
[![Build Status](https://github.com/cloudposse/build-harness/workflows/docker/badge.svg?branch=master)](https://github.com/cloudposse/build-harness/actions?query=workflow%3Adocker) [![Latest Release](https://img.shields.io/github/release/cloudposse/build-harness.svg)](https://github.com/cloudposse/build-harness/releases/latest) [![Slack Community](https://slack.cloudposse.com/badge.svg)](https://slack.cloudposse.com)
4+
[![Build Status](https://img.shields.io/github/actions/workflow/status/cloudposse/build-harness/docker.yml?style=for-the-badge)](https://github.com/cloudposse/build-harness/actions/workflows/docker.yml) [![Latest Release](https://img.shields.io/github/release/cloudposse/build-harness.svg?style=for-the-badge)](https://github.com/cloudposse/build-harness/releases/latest) [![Last Updated](https://img.shields.io/github/last-commit/cloudposse/build-harness/master?style=for-the-badge)](https://github.com/cloudposse/build-harness/commits/master/) [![Slack Community](https://slack.cloudposse.com/for-the-badge.svg)](https://slack.cloudposse.com)
55
<!-- markdownlint-restore -->
66

77

@@ -26,7 +26,7 @@
2626
2727
-->
2828

29-
This `build-harness` is a collection of Makefiles to facilitate building Golang projects, Dockerfiles, Helm charts, and more.
29+
This `build-harness` is a collection of Makefiles to facilitate building READMEs, Golang projects, Dockerfiles, Helm charts, and more.
3030
It's designed to work with CI/CD systems such as GitHub Actions.
3131

3232
## Screenshots
@@ -387,7 +387,7 @@ We deliver 10x the value for a fraction of the cost of a full-time engineer. Our
387387
[![README Commercial Support][readme_commercial_support_img]][readme_commercial_support_link]
388388
## License
389389

390-
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
390+
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg?style=for-the-badge)](https://opensource.org/licenses/Apache-2.0)
391391

392392
See [LICENSE](LICENSE) for full details.
393393

README.yaml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ github_repo: cloudposse/build-harness
2424
# Badges to display
2525
badges:
2626
- name: "Build Status"
27-
image: "https://github.com/cloudposse/build-harness/workflows/docker/badge.svg?branch=master"
28-
url: "https://github.com/cloudposse/build-harness/actions?query=workflow%3Adocker"
27+
image: "https://img.shields.io/github/actions/workflow/status/cloudposse/build-harness/docker.yml?style=for-the-badge"
28+
url: "https://github.com/cloudposse/build-harness/actions/workflows/docker.yml"
2929
- name: "Latest Release"
30-
image: "https://img.shields.io/github/release/cloudposse/build-harness.svg"
30+
image: "https://img.shields.io/github/release/cloudposse/build-harness.svg?style=for-the-badge"
3131
url: "https://github.com/cloudposse/build-harness/releases/latest"
32+
- name: "Last Updated"
33+
image: https://img.shields.io/github/last-commit/cloudposse/build-harness/master?style=for-the-badge
34+
url: https://github.com/cloudposse/build-harness/commits/master/
3235
- name: "Slack Community"
33-
image: "https://slack.cloudposse.com/badge.svg"
36+
image: "https://slack.cloudposse.com/for-the-badge.svg"
3437
url: "https://slack.cloudposse.com"
3538

3639
related:
@@ -55,7 +58,7 @@ screenshots:
5558

5659
# Short description of this project
5760
description: |-
58-
This `build-harness` is a collection of Makefiles to facilitate building Golang projects, Dockerfiles, Helm charts, and more.
61+
This `build-harness` is a collection of Makefiles to facilitate building READMEs, Golang projects, Dockerfiles, Helm charts, and more.
5962
It's designed to work with CI/CD systems such as GitHub Actions.
6063
6164
# Introduction to the project

modules/readme/Makefile

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,11 @@ export README_LINT ?= $(TMP)/README.md
22
export README_FILE ?= README.md
33
export README_YAML ?= README.yaml
44

5-
export README_TEMPLATE_REPO_REMOTE_NAME ?= origin
6-
export README_TEMPLATE_REPO_REMOTE ?= $(shell [ -d .git ] && git remote get-url $(README_TEMPLATE_REPO_REMOTE_NAME))
7-
8-
# Parse https://github.com/...
9-
ifneq (,$(findstring https://github.com/,$(README_TEMPLATE_REPO_REMOTE)))
10-
URL_NO_PROTOCOL := $(subst https://github.com/,,$(README_TEMPLATE_REPO_REMOTE))
11-
export README_TEMPLATE_REPO_ORG ?= $(firstword $(subst /, ,$(URL_NO_PROTOCOL)))
12-
endif
13-
14-
# Parse git@github.com:...
15-
ifneq (,$(findstring git@github.com:,$(README_TEMPLATE_REPO_REMOTE)))
16-
URL_NO_GIT := $(subst git@github.com:,,$(README_TEMPLATE_REPO_REMOTE))
17-
export README_TEMPLATE_REPO_ORG ?= $(firstword $(subst /, ,$(URL_NO_GIT)))
18-
endif
19-
5+
export README_TEMPLATE_REPO_ORG ?= $(shell [ -f "$(README_YAML)" ] && dirname $$(grep '^github_repo: *' "$(README_YAML)" | cut -d: -f2))
206
export README_TEMPLATE_REPO ?= .github
217
export README_TEMPLATE_REPO_REF ?= main
228
export README_TEMPLATE_REPO_PATH ?= README.md.gotmpl
23-
export README_TEMPLATE_REPO_URL := https://raw.githubusercontent.com/$${README_GITHUB_ORG}/$(README_TEMPLATE_REPO)/$(README_TEMPLATE_REPO_REF)/$(README_TEMPLATE_REPO_PATH)
9+
export README_TEMPLATE_REPO_URL := https://raw.githubusercontent.com/$(README_TEMPLATE_REPO_ORG)/$(README_TEMPLATE_REPO)/$(README_TEMPLATE_REPO_REF)/$(README_TEMPLATE_REPO_PATH)
2410
export README_TEMPLATE_FILE ?= $(BUILD_HARNESS_PATH)/templates/README.md.gotmpl
2511
export README_TEMPLATE_YAML := $(BUILD_HARNESS_PATH)/templates/$(README_YAML)
2612

@@ -43,6 +29,7 @@ export README_ALLOWLIST_ORGS := \
4329
$(README_TEMPLATE_FILE):
4430
@for README_GITHUB_ORG in $(README_ALLOWLIST_ORGS); do \
4531
if [ "$${README_GITHUB_ORG}" == "$${README_TEMPLATE_REPO_ORG}" ]; then \
32+
echo "Fetching README template from $${README_TEMPLATE_REPO_ORG}"; \
4633
if curl -o $@ -fsSL "$(README_TEMPLATE_REPO_URL)"; then \
4734
exit 0; \
4835
else \
@@ -51,7 +38,7 @@ $(README_TEMPLATE_FILE):
5138
fi; \
5239
fi; \
5340
done; \
54-
printf "Detected GitHub Org '%s' is not in the list of organizations allowed to provide README templates.\n" "$(README_TEMPLATE_REPO_ORG)" >&2; \
41+
printf "Detected GitHub Org '%s' is not in the list of organizations allowed to provide README templates.\n" "$${README_TEMPLATE_REPO_ORG}" >&2; \
5542
exit 1
5643

5744
## Alias for readme/build

templates/Makefile.build-harness

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,22 @@ clean::
8383
fi; \
8484
fi
8585

86-
.PHONY: safe-directory
86+
.PHONY: git-safe-directory
8787

8888
# Workaround for https://github.com/actions/checkout/issues/766
89-
safe-directory:
90-
[[ -n "$$GITHUB_WORKSPACE" ]] && git config --global --add safe.directory "$$GITHUB_WORKSPACE" || git config --global --add safe.directory '*'
89+
# Note that if we always add a safe directory, we are recreating the security problem git is trying to solve.
90+
# So we only add the safe directory if we are running in a GitHub Actions environment.
91+
git-safe-directory:
92+
@if remove_protection_cmd=$$(git log -1 2>&1 | grep -F 'git config --global --add safe.directory'); then \
93+
if [[ -n "$$GITHUB_WORKSPACE" ]]; then \
94+
printf "Marking directory %s as safe for git to trust\n" "$$GITHUB_WORKSPACE" >&2; \
95+
git config --global --add safe.directory "$$GITHUB_WORKSPACE"; \
96+
else \
97+
printf "\nGit refused to trust a directory, presumably due to dubious ownership.\n" >&2; \
98+
printf "GitHub Actions environment not detected, so script is not automatically trusting suspicious directory.\n\n" >&2 ;\
99+
printf "To trust the directory git is concerned about, run:\n\n %s\n\n" "$$remove_protection_cmd" >&2; \
100+
fi \
101+
fi
91102

92103
.PHONY: build-harness/shell builder build-harness/shell/pull builder/pull builder/build builder-slim/build
93104

@@ -149,7 +160,7 @@ precommit/terraform pr/auto-format precommit/terraform/host pr/auto-format/host:
149160
pr/readme pr/readme/host: ARGS := readme/deps readme
150161
pr/github-update pr/github-update/host: ARGS := github/update
151162
precommit/terraform pr/auto-format pr/readme pr/github-update: build-harness/runner
152-
precommit/terraform/host pr/auto-format/host pr/readme/host pr/github-update/host: safe-directory
163+
precommit/terraform/host pr/auto-format/host pr/readme/host pr/github-update/host: git-safe-directgory
153164
$(MAKE) $(ARGS)
154165

155166
pr/pre-commit: ARGS := pre-commit/run

0 commit comments

Comments
 (0)