Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
35 changes: 35 additions & 0 deletions build-testcase-workflow-merge-queue-demo
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Create a repository and setup a simple workflow
# Prior to running this, make any needed modifications to test-data/workflow-simple.yml_
# that are needed for the testcase.
# The "as delivered" version of workflow-simple.yml_ will trigger the workflow
# with dispatch event
#

normal=$(tput sgr0)
highlight=$(tput setaf 2)
yellow=$(tput setaf 3)

printf "$highlight"

cat << EOF

________ ____
/_ __/ /_ ___ / __ \____ _ _____ _____
/ / / __ \/ _ \ / /_/ / __ \ | /| / / _ \/ ___/
/ / / / / / __/ / ____/ /_/ / |/ |/ / __/ /
/_/ /_/ /_/\___/ /_/ \____/|__/|__/\___/_/

==================================================
$0
==================================================

EOF

printf "${normal}"

printf "${highlight} - Creating repo: ${normal}"
./create-repo.sh | jq -r '.name'
./create-a-repository-ruleset-branch-pattern-merge-queue.sh | jq -r '.name'
printf "${highlight} - Committing workflow file: ${normal}"
./create-commit-workflow-merge-queue-demo.sh | jq -r '.content.path'

93 changes: 93 additions & 0 deletions create-a-repository-ruleset-branch-pattern-merge-queue.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
. ./.gh-api-examples.conf

# https://docs.github.com/en/enterprise-cloud@latest/rest/repos/rules?apiVersion=2022-11-28#create-a-repository-ruleset
# POST /repos/{owner}/{repo}/rulesets


# If the script is passed an argument $1 use that as the name
if [ -z "$1" ]
then
repo=$repo
else
repo=$1
fi

team_id=$(curl ${curl_custom_flags} --silent -H "Authorization: Bearer ${GITHUB_TOKEN}" ${GITHUB_API_BASE_URL}/orgs/${org}/teams/$team_slug | jq '.id')

default_app_id=${app_id}

json_file=tmp/create-a-repository-ruleset.json

jq -n \
--arg name "${ruleset_name}" \
--arg target "${target}" \
--arg team_id ${team_id} \
--arg default_app_id ${default_app_id} \
--arg commit_message_pattern $commit_message_pattern \
--arg operator $operator \
--arg bypass_mode "${bypass_mode}" \
--arg enforcement "${enforcement}" \
--argjson check_response_timeout_minutes ${check_response_timeout_minutes:-360} \
--arg grouping_strategy "${grouping_strategy:-HEADGREEN}" \
--argjson max_entries_to_build ${max_entries_to_build:-5} \
--argjson max_entries_to_merge ${max_entries_to_merge:-5} \
--arg merge_method "${merge_method:-SQUASH}" \
--argjson min_entries_to_merge ${min_entries_to_merge:-0} \
--argjson min_entries_to_merge_wait_minutes ${min_entries_to_merge_wait_minutes:-0} \
'{
name : $name,
target : $target,
enforcement: $enforcement,
"bypass_actors": [
{
"actor_id": $team_id | tonumber,
"actor_type": "Team",
"bypass_mode": $bypass_mode
},
{
"actor_id": $default_app_id | tonumber,
"actor_type": "Integration",
"bypass_mode": $bypass_mode
}
],
"conditions": {
"ref_name": {
"include": [
"refs/heads/main",
"refs/heads/master"
],
"exclude": [
"refs/heads/dev*"
]
}
},
"rules": [
{
"type": "commit_message_pattern",
"parameters": {
"negate": false,
"pattern": $commit_message_pattern,
"operator": $operator
}
},
{
"type": "merge_queue",
"parameters": {
"check_response_timeout_minutes": $check_response_timeout_minutes,
"grouping_strategy": $grouping_strategy,
"max_entries_to_build": $max_entries_to_build,
"max_entries_to_merge": $max_entries_to_merge,
"merge_method": ($merge_method | ascii_upcase),
"min_entries_to_merge": $min_entries_to_merge,
"min_entries_to_merge_wait_minutes": $min_entries_to_merge_wait_minutes
}
}
]
}' > ${json_file}

curl ${curl_custom_flags} \
-H "X-GitHub-Api-Version: ${github_api_version}" \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
"${GITHUB_API_BASE_URL}/repos/${owner}/${repo}/rulesets" --data @${json_file}

2 changes: 2 additions & 0 deletions create-a-repository-ruleset.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ fi

team_id=$(curl ${curl_custom_flags} --silent -H "Authorization: Bearer ${GITHUB_TOKEN}" ${GITHUB_API_BASE_URL}/orgs/${org}/teams/$team_slug | jq '.id')

default_app_id=${app_id}

json_file=tmp/create-a-repository-ruleset.json

jq -n \
Expand Down
49 changes: 49 additions & 0 deletions create-commit-workflow-merge-queue-demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env python3
"""
Module Docstring
"""

__author__ = "David Morris ([email protected])"
__version__ = "0.1.0"
__license__ = "MIT"

import os
import json
import string
import base64
import argparse
import logging
import thepower
from pathlib import Path
from datetime import datetime


def main(args):

power_config = thepower.read_dotcom_config(args.power_config)
args.extension = power_config.get('dummy_section','file_extension').strip('"')
args.default_committer = power_config.get('dummy_section','default_committer',).strip('"')

p = Path('test-data/workflow-merge-queue-demo.yml_')
json_file = f"""tmp/workflow-merge-queue-demo.json"""
filename_in_repo = f"""workflow-merge-queue-demo.yml"""
with open(p, 'rb') as ct:
t = {}
chapter_content = ct.read()
chapter_base64 = base64.encodebytes(chapter_content)
t["message"] = f"""A workflow to demonstrate merge queue."""
t["committer"] = {}
t["committer"]["name"] = args.default_committer
t["committer"]["email"] = f"noreply+{args.default_committer}@example.com"
t["content"] = chapter_base64.decode('UTF-8')
with open(json_file, 'w') as out_file:
out_file.write(json.dumps(t))

if __name__ == "__main__":
""" This is executed when run from the command line """
parser = argparse.ArgumentParser()
parser.add_argument("-c", "--power-config", action="store", dest="power_config", default=".gh-api-examples.conf", help="This is the config file to use to access variables for the power.")
parser.add_argument("-e", "--extension", action="store", dest="extension", default="c")
args = parser.parse_args()

main(args)
24 changes: 24 additions & 0 deletions create-commit-workflow-merge-queue-demo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
. ./.gh-api-examples.conf

set -x
# https://docs.github.com/en/rest/reference/repos#create-or-update-file-contents
# PUT /repos/:owner/:repo/contents/:path

# If the script is passed an argument $1 use that as the name
if [ -z "$1" ]
then
repo=$repo
else
repo=$1
fi

python3 create-commit-workflow-merge-queue-demo.py

curl ${curl_custom_flags} \
-X PUT \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer ${GITHUB_TOKEN}" \
${GITHUB_API_BASE_URL}/repos/${org}/${repo}/contents/.github/workflows/workflow-merge-queue-demo.yml --data @tmp/workflow-merge-queue-demo.json



14 changes: 14 additions & 0 deletions test-data/workflow-merge-queue-demo.yml_
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

name: the-power-workflow-simple
on:
pull_request:
merge_group:
jobs:
pwr-merge-queue-demo:
runs-on: ubuntu-latest
steps:

- name: check-curl-version
run: |
curl --version