From 89f4196e7c9136defbcd541cca6dfcf6b0dec079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Bra=C5=84ski?= Date: Thu, 27 Aug 2020 20:59:09 +0200 Subject: [PATCH] WIP --- modules/github-automated-workflow/Makefile | 28 +++++++++++++ .../github-automated-workflow/Makefile.gotmpl | 40 +++++++++++++++++++ .../migration.sh.gotmpl | 6 +++ .../pr-body.md.gotmpl | 10 +++++ .../github-automated-workflow/workflow.yaml | 30 ++++++++++++++ 5 files changed, 114 insertions(+) create mode 100644 modules/github-automated-workflow/Makefile create mode 100644 templates/github-automated-workflow/Makefile.gotmpl create mode 100644 templates/github-automated-workflow/migration.sh.gotmpl create mode 100644 templates/github-automated-workflow/pr-body.md.gotmpl create mode 100644 templates/github-automated-workflow/workflow.yaml diff --git a/modules/github-automated-workflow/Makefile b/modules/github-automated-workflow/Makefile new file mode 100644 index 00000000..1ebf475e --- /dev/null +++ b/modules/github-automated-workflow/Makefile @@ -0,0 +1,28 @@ +WORKFLOW_TEMPLATE_DIR ?= $(BUILD_HARNESS_PATH)/templates/github-automated-workflow + +## Create new workflow +github-automated-workflow/init: + @if [ -z $(name) ]; then echo -e "Required argument "name" not present\n"; exit 1; fi + @$(eval WORKFLOW_NAME := $(shell echo "`date +"%Y-%m-%d"`-$(name)")) + @if [ -d $(WORKFLOW_NAME) ]; then echo "Workflow named $(WORKFLOW_NAME) already exists"; exit 1; fi + @mkdir -p $(WORKFLOW_NAME) + @cp $(WORKFLOW_TEMPLATE_DIR)/workflow.yaml $(WORKFLOW_NAME) + @sed -i '' -e "s/__NAME__/$(name)/" $(WORKFLOW_NAME)/workflow.yaml + @echo "Workflow $(WORKFLOW_NAME) created." + @echo "Update $(WORKFLOW_NAME)/workflow.yaml configuration file and run workflow with 'make github-automated-workflow/configure name=$(WORKFLOW_NAME)'" + +# Create scripts based on yaml configuration +github-automated-workflow/configure: + @if [ -z $(name) ]; then echo -e "Required argument "name" not present\n"; exit 1; fi + @$(eval WORKFLOW_NAME := $(shell echo "$(name)")) + @if [ ! -d $(WORKFLOW_NAME) ]; then echo "Workflow named $(WORKFLOW_NAME) does not exists"; exit 1; fi + @cd $(name) && gomplate --file $(WORKFLOW_TEMPLATE_DIR)/migration.sh.gotmpl --out migration.sh + @cd $(name) && gomplate --file $(WORKFLOW_TEMPLATE_DIR)/Makefile.gotmpl --out Makefile + @cd $(name) && gomplate --file $(WORKFLOW_TEMPLATE_DIR)/pr-body.md.gotmpl --out pr-body.md + @cd $(name) && chmod a+x migration.sh + @mkdir -p $(name)/log + +github-automated-workflow/run: + @if [ -z $(name) ]; then echo -e "Required argument "name" not present\n"; exit 1; fi + @$(eval WORKFLOW_NAME := $(shell echo "$(name)")) + @if [ ! -d $(WORKFLOW_NAME) ]; then echo "Workflow named $(WORKFLOW_NAME) does not exists"; exit 1; fi diff --git a/templates/github-automated-workflow/Makefile.gotmpl b/templates/github-automated-workflow/Makefile.gotmpl new file mode 100644 index 00000000..8d34ad98 --- /dev/null +++ b/templates/github-automated-workflow/Makefile.gotmpl @@ -0,0 +1,40 @@ +{{ defineDatasource "config" "workflow.yaml" | regexp.Replace ".*" "" -}} +WORKFLOW_TEMPLATE_DIR ?= $(BUILD_HARNESS_PATH)/templates/github-automated-workflow/ + +mp-init: + @mp init --repo-search "{{ (ds "config").github.repository_search }}" > log/mp_init.log + @mp clone >> log/mp_clone.log + +mp-plan: + @mp plan -b {{ (ds "config").github.branch }} -m '{{ (ds "config").github.commit_message }}' -- sh -c $$PWD/migration.sh > log/mp_plan.log + +{{ if eq (ds "config").github.pr.enabled true -}} +mp-push: + echo Create PR + @mp push -b pr-body.md -a {{ (ds "config").github.pr.reviewer }} > log/mp_push.log + + {{- if ne (len (ds "config").github.pr.labels) 0 }} + echo Label PR + @echo '{"labels":["{{ join (ds "config").github.pr.labels "\",\"" }}"]}' > labels.json + echo 'LABELS_JSON=`cat labels.json`; cd {}; PR_NUMBER=`gh pr create 2>&1 | tail -n1 | sed "s#.*pull/##"`; echo $$LABELS_JSON | gh api -X PATCH repos/:owner/:repo/issues/$$PR_NUMBER --input -' + @find . -name 'planned' -maxdepth 4 -type d -exec sh -c 'LABELS_JSON=`cat labels.json`; cd {}; PR_NUMBER=`gh pr create 2>&1 | tail -n1 | sed "s#.*pull/##"`; echo $$LABELS_JSON | gh api -X PATCH repos/:owner/:repo/issues/$$PR_NUMBER --input -' \; >> log/mp_push.log + @rm labels.json + {{- end }} + + echo Add PR reviewers TODO + # https://developer.github.com/v3/pulls/review_requests/#request-reviewers-for-a-pull-request + + {{ if eq (ds "config").tests true }} + echo Run tests + @find . -name 'planned' -maxdepth 4 -type d -exec sh -c 'cd {}; gh pr review --comment -b "/test all"' \; + {{ end -}} + +{{ end }} +status: + @mp status + +{{ if eq (ds "config").tests true -}} +# Run tests with `gh` command in planned repositories +tests: + @find . -name 'planned' -maxdepth 4 -type d -exec sh -c 'cd {}; gh pr review --comment -b "/test all"' \; +{{ end -}} diff --git a/templates/github-automated-workflow/migration.sh.gotmpl b/templates/github-automated-workflow/migration.sh.gotmpl new file mode 100644 index 00000000..e5df1683 --- /dev/null +++ b/templates/github-automated-workflow/migration.sh.gotmpl @@ -0,0 +1,6 @@ +#!/bin/bash -e +# This script was automatically generated from workflow.yaml +{{ defineDatasource "config" "workflow.yaml" | regexp.Replace ".*" "" -}} +{{- range $migration := (ds "config").migrations }} +{{ $migration }} >> migration.log +{{- end }} diff --git a/templates/github-automated-workflow/pr-body.md.gotmpl b/templates/github-automated-workflow/pr-body.md.gotmpl new file mode 100644 index 00000000..493171a1 --- /dev/null +++ b/templates/github-automated-workflow/pr-body.md.gotmpl @@ -0,0 +1,10 @@ +{{ defineDatasource "config" "workflow.yaml" | regexp.Replace ".*" "" -}} +## what +{{- range $element := (ds "config").github.pr.body.what }} +- {{ $element }} +{{- end }} + +## why +{{- range $element := (ds "config").github.pr.body.why }} +- {{ $element }} +{{- end }} diff --git a/templates/github-automated-workflow/workflow.yaml b/templates/github-automated-workflow/workflow.yaml new file mode 100644 index 00000000..5148fe43 --- /dev/null +++ b/templates/github-automated-workflow/workflow.yaml @@ -0,0 +1,30 @@ +name: __NAME__ + +github: + # https://docs.github.com/en/github/searching-for-information-on-github/searching-for-repositories + # all modules + # repo_search: "org:cloudposse" + # terraform modules + repository_search: "repo:cloudposse/terraform-aws-sns-topic" + + branch: auto-__NAME__ + commit_message: auto-__NAME__ + pr: + enabled: true + body: + what: + - what + why: + - why + reviewer: 3h4x + # cloudposse/engineering + + labels: + - auto-merge + # - auto-release + +tests: true + +migrations: + - make + - make readme