Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Brański committed Aug 27, 2020
1 parent 6f95278 commit 89f4196
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 0 deletions.
28 changes: 28 additions & 0 deletions modules/github-automated-workflow/Makefile
Original file line number Diff line number Diff line change
@@ -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
40 changes: 40 additions & 0 deletions templates/github-automated-workflow/Makefile.gotmpl
Original file line number Diff line number Diff line change
@@ -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 -}}
6 changes: 6 additions & 0 deletions templates/github-automated-workflow/migration.sh.gotmpl
Original file line number Diff line number Diff line change
@@ -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 }}
10 changes: 10 additions & 0 deletions templates/github-automated-workflow/pr-body.md.gotmpl
Original file line number Diff line number Diff line change
@@ -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 }}
30 changes: 30 additions & 0 deletions templates/github-automated-workflow/workflow.yaml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 89f4196

Please sign in to comment.