Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix detection of github organization for README templates #374

Merged
merged 9 commits into from
Feb 1, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- shell: bash
run: /usr/bin/make BUILD_HARNESS_PATH=/build-harness PACKAGES_PREFER_HOST=true readme/lint
run: /usr/bin/make BUILD_HARNESS_PATH=/build-harness PACKAGES_PREFER_HOST=true git-safe-directory readme/lint

super-linter:
name: superlinter
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

<!-- markdownlint-disable -->
[![Project Banner](.github/banner.png?raw=true)](https://cpco.io/homepage)
[![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)
[![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)
<!-- markdownlint-restore -->


Expand All @@ -26,7 +26,7 @@

-->

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

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

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

See [LICENSE](LICENSE) for full details.

Expand Down
13 changes: 8 additions & 5 deletions README.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,16 @@ github_repo: cloudposse/build-harness
# Badges to display
badges:
- name: "Build Status"
image: "https://github.com/cloudposse/build-harness/workflows/docker/badge.svg?branch=master"
url: "https://github.com/cloudposse/build-harness/actions?query=workflow%3Adocker"
image: "https://img.shields.io/github/actions/workflow/status/cloudposse/build-harness/docker.yml?style=for-the-badge"
url: "https://github.com/cloudposse/build-harness/actions/workflows/docker.yml"
- name: "Latest Release"
image: "https://img.shields.io/github/release/cloudposse/build-harness.svg"
image: "https://img.shields.io/github/release/cloudposse/build-harness.svg?style=for-the-badge"
url: "https://github.com/cloudposse/build-harness/releases/latest"
- name: "Last Updated"
image: https://img.shields.io/github/last-commit/cloudposse/build-harness/master?style=for-the-badge
url: https://github.com/cloudposse/build-harness/commits/master/
- name: "Slack Community"
image: "https://slack.cloudposse.com/badge.svg"
image: "https://slack.cloudposse.com/for-the-badge.svg"
url: "https://slack.cloudposse.com"

related:
Expand All @@ -55,7 +58,7 @@ screenshots:

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

# Introduction to the project
Expand Down
21 changes: 4 additions & 17 deletions modules/readme/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,11 @@ export README_LINT ?= $(TMP)/README.md
export README_FILE ?= README.md
export README_YAML ?= README.yaml

export README_TEMPLATE_REPO_REMOTE_NAME ?= origin
export README_TEMPLATE_REPO_REMOTE ?= $(shell [ -d .git ] && git remote get-url $(README_TEMPLATE_REPO_REMOTE_NAME))

# Parse https://github.com/...
ifneq (,$(findstring https://github.com/,$(README_TEMPLATE_REPO_REMOTE)))
URL_NO_PROTOCOL := $(subst https://github.com/,,$(README_TEMPLATE_REPO_REMOTE))
export README_TEMPLATE_REPO_ORG ?= $(firstword $(subst /, ,$(URL_NO_PROTOCOL)))
endif

# Parse [email protected]:...
ifneq (,$(findstring [email protected]:,$(README_TEMPLATE_REPO_REMOTE)))
URL_NO_GIT := $(subst [email protected]:,,$(README_TEMPLATE_REPO_REMOTE))
export README_TEMPLATE_REPO_ORG ?= $(firstword $(subst /, ,$(URL_NO_GIT)))
endif

export README_TEMPLATE_REPO_ORG ?= $(shell [ -f "$(README_YAML)" ] && dirname $$(grep '^github_repo: *' "$(README_YAML)" | cut -d: -f2))
export README_TEMPLATE_REPO ?= .github
export README_TEMPLATE_REPO_REF ?= main
export README_TEMPLATE_REPO_PATH ?= README.md.gotmpl
export README_TEMPLATE_REPO_URL := https://raw.githubusercontent.com/$${README_GITHUB_ORG}/$(README_TEMPLATE_REPO)/$(README_TEMPLATE_REPO_REF)/$(README_TEMPLATE_REPO_PATH)
export README_TEMPLATE_REPO_URL := https://raw.githubusercontent.com/$(README_TEMPLATE_REPO_ORG)/$(README_TEMPLATE_REPO)/$(README_TEMPLATE_REPO_REF)/$(README_TEMPLATE_REPO_PATH)
export README_TEMPLATE_FILE ?= $(BUILD_HARNESS_PATH)/templates/README.md.gotmpl
export README_TEMPLATE_YAML := $(BUILD_HARNESS_PATH)/templates/$(README_YAML)

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

## Alias for readme/build
Expand Down
19 changes: 15 additions & 4 deletions templates/Makefile.build-harness
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,22 @@ clean::
fi; \
fi

.PHONY: safe-directory
.PHONY: git-safe-directory

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

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

Expand Down Expand Up @@ -149,7 +160,7 @@ precommit/terraform pr/auto-format precommit/terraform/host pr/auto-format/host:
pr/readme pr/readme/host: ARGS := readme/deps readme
pr/github-update pr/github-update/host: ARGS := github/update
precommit/terraform pr/auto-format pr/readme pr/github-update: build-harness/runner
precommit/terraform/host pr/auto-format/host pr/readme/host pr/github-update/host: safe-directory
precommit/terraform/host pr/auto-format/host pr/readme/host pr/github-update/host: git-safe-directgory
$(MAKE) $(ARGS)

pr/pre-commit: ARGS := pre-commit/run
Expand Down
Loading