Skip to content

Commit fb7ea7e

Browse files
author
Jeroen Baten
committed
Seems to work.
1 parent 5499d28 commit fb7ea7e

File tree

1 file changed

+100
-2
lines changed

1 file changed

+100
-2
lines changed

AddIssuesToProject.py

+100-2
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,54 @@ def get_open_issues_from_github(repo,token,project_id):
9494
result=requests.get(url, headers=headers)
9595
return result
9696

97-
97+
def get_project_cards_from_github(repo,token,column_id):
98+
# GET /projects/columns/:column_id/cards
99+
urlparts=(str(GITHUB_URL),"projects" , "columns", str(column_id),"cards")
100+
url="/".join(urlparts)
101+
102+
pprint(url)
103+
104+
headers={"Authorization": "token "+ GITHUB_TOKEN,
105+
"Accept": "application/vnd.github.inertia-preview+json" }
106+
# "Accept": "application/vnd.github.symmetra-preview+json"}
107+
#result=requests.get(url, headers=headers, data = json.dumps(d))
108+
result=requests.get(url, headers=headers)
109+
return result
110+
111+
def create_project_card(repo,token,my_column_id,my_issue_id):
112+
# Create a project card
113+
# POST /projects/columns/:column_id/cards
114+
# Parameters
115+
# Name Type Description
116+
# note string The card's note content. Only valid for cards
117+
# without another type of content, so this must be omitted
118+
# if content_id and content_type are specified.
119+
# content_id integer The id of the issue to associate with this card.
120+
# content_type string Required if you specify a content_id.
121+
# The type of content to associate with this card.
122+
# Can only be "Issue" at this time.
123+
urlparts=(str(GITHUB_URL),"projects" , "columns", str(my_column_id),"cards")
124+
url="/".join(urlparts)
125+
126+
pprint(url)
127+
128+
headers={"Authorization": "token "+ GITHUB_TOKEN,
129+
"Accept": "application/vnd.github.inertia-preview+json" }
130+
131+
card=dict()
132+
card["content_id"]=my_issue_id
133+
card["content_type"]="Issue"
134+
135+
# print "url: " + url
136+
# print "headers: " + pformat(headers)
137+
# print "data: " + pformat(card)
138+
# print "payload: " + json.dumps(card)
139+
140+
# sys.exit(1)
141+
result=requests.post(url, headers=headers, data = json.dumps(card))
142+
# pprint(result.json())
143+
return result
144+
98145

99146
# Find all defined projects
100147
projects=get_projects_from_github(GITHUB_REPO,GITHUB_TOKEN)
@@ -122,12 +169,63 @@ def get_open_issues_from_github(repo,token,project_id):
122169
print "My column id is " + str(my_column_id)
123170

124171
# Read all issues from GitHub
172+
print "Retrieving all current issues from GitHub"
125173
issues=get_open_issues_from_github(GITHUB_REPO,GITHUB_TOKEN,my_project_id)
126174
if issues.status_code==200:
127-
file = open("testfile.txt","w")
175+
file = open("issues.txt","w")
128176
file.write(pformat(issues.json()))
129177
file.close()
130178

179+
# Read all project cards from GitHub
180+
# print "Retrieving all current cards from GitHub"
181+
# cards=get_project_cards_from_github(GITHUB_REPO,GITHUB_TOKEN,my_column_id)
182+
# if cards.status_code==200:
183+
# file = open("cards.txt","w")
184+
# file.write(pformat(cards.json()))
185+
# file.close()
186+
187+
# We now have everything to find out what needs to be done.
188+
189+
# create temporary index for easy of use
190+
print 80 * "*"
191+
print "Creating temporary index of existing cards"
192+
card_index=set()
193+
for column in columns.json():
194+
cards=get_project_cards_from_github(GITHUB_REPO,GITHUB_TOKEN,column["id"])
195+
for card in cards.json():
196+
#pprint(card)
197+
if card.get("content_url") is not None:
198+
id=card.get("content_url").split("/")[-1]
199+
print "Issue with number " + str(id) + " already in cards"
200+
card_index.add(id)
201+
202+
#pprint(card_index)
203+
204+
print "About to start adding " + str(len(issues.json())) + " issues to the project."
205+
print "Adding cards linked to issues"
206+
issue_counter=0
207+
#file = open("AddIssuesToProject.log","a")
208+
for issue in issues.json():
209+
issue_counter+=1
210+
print "Doing issue: " + str(issue_counter)
211+
# if issue already in cards do nothing
212+
if str(issue["number"]) in card_index:
213+
print "Card for issue " + str(issue["number"]) + " exists"
214+
else:
215+
print "Creating card for issue " + str(issue["number"]) + " (\""+ issue["title"] + "\")"
216+
# else add card in correct column linking to issue
217+
# create card in desired project and desired column
218+
# Note that you have to send the id of the issue, not the number!
219+
result=create_project_card(GITHUB_REPO,GITHUB_TOKEN,my_column_id,issue["id"])
220+
# And try this ONLY ONCE!
221+
#pprint(result)
222+
pprint(result.json())
223+
with open("AddIssuesToProject.log", "a") as logfile:
224+
logfile.write(pformat(result.json()))
225+
#file.write(pformat(result.json()))
226+
#sys.exit(1)
227+
228+
file.close()
131229

132230
# The end of handling all issue
133231

0 commit comments

Comments
 (0)