-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreusable-create-shared-issue.yml
More file actions
103 lines (88 loc) · 3.74 KB
/
reusable-create-shared-issue.yml
File metadata and controls
103 lines (88 loc) · 3.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
name: Reusable - Create Shared Issue
on:
workflow_call:
inputs:
issue-title:
required: true
type: string
issue-body:
required: true
type: string
target-repos:
required: true
type: string # Space-separated list, e.g. "org/repo1 org/repo2"
project-org:
required: true
type: string
secrets:
gh_token:
required: true
jobs:
create-shared-issue:
runs-on: ubuntu-latest
steps:
- name: Authenticate gh CLI
run: gh auth status
env:
GH_TOKEN: ${{ secrets.gh_token }}
- name: Install gh CLI extension for Projects
run: |
gh extension install github/gh-projects
env:
GH_TOKEN: ${{ secrets.gh_token }}
- name: Loop over target repos and create issues
env:
GH_TOKEN: ${{ secrets.gh_token }}
ISSUE_TITLE: ${{ inputs.issue-title }}
ISSUE_BODY: ${{ inputs.issue-body }}
REPOS: ${{ inputs.target-repos }}
PROJECT_ORG: ${{ inputs.project-org }}
run: |
for repo in $REPOS; do
echo "Checking if issue exists in $repo..."
existing_title=$(gh issue list -R "$repo" --search "$ISSUE_TITLE" --json title -q '.[].title' | grep -F "$ISSUE_TITLE" || true)
if [[ -z "$existing_title" ]]; then
echo "Creating new issue in $repo"
issue_url=$(gh issue create -R "$repo" --title "$ISSUE_TITLE" --body "$ISSUE_BODY" | head -n 1)
echo "Created: $issue_url"
project_name="$(date +'%B %Y') Sprint"
echo "Looking for project: $project_name in $PROJECT_ORG"
gh projects list --org "$PROJECT_ORG" --format json
project_json=$(gh projects list --org "$PROJECT_ORG" --format json 2>/dev/null)
if [[ -z "$project_json" || "$project_json" == "[]" ]]; then
echo "No projects returned by gh projects list"
echo "Are you sure there's a Project (v2) named '$project_name' in org '$PROJECT_ORG'?"
gh projects list --org "$PROJECT_ORG" --format json
exit 1
fi
project_id=$(echo "$project_json" | jq -r --arg name "$project_name" '.projects[] | select(.title == $name) | .id')
if [[ -z "$project_id" ]]; then
echo "Project '$project_name' not found in $PROJECT_ORG"
else
echo "Found project ID: $project_id"
echo "Adding issue to project"
# Extract values
owner=$(echo "$issue_url" | cut -d/ -f4)
repo=$(echo "$issue_url" | cut -d/ -f5)
issue_number=$(echo "$issue_url" | cut -d/ -f7)
# Get the issue node ID
issue_node_id=$(gh api "repos/$owner/$repo/issues/$issue_number" --jq .node_id)
# GraphQL mutation to add issue to project
mutation='
mutation AddIssueToProject($projectId:ID!, $contentId:ID!) {
addProjectV2ItemById(input: {projectId: $projectId, contentId: $contentId}) {
item {
id
}
}
}'
# Add the issue to the project
gh api graphql \
-f query="$mutation" \
-f projectId="$project_id" \
-f contentId="$issue_node_id"
fi
else
echo "Issue already exists in $repo"
fi
done