From 97f24641e0e58b849bb69b265ea1c711d0540033 Mon Sep 17 00:00:00 2001 From: alan Date: Sat, 13 Jan 2024 02:12:20 -0500 Subject: [PATCH] Properly copies issues over now (minus due date, lol) --- src/index.ts | 10 ++++------ src/issue.ts | 16 ++++++++++------ src/requests.ts | 11 ++++++----- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/src/index.ts b/src/index.ts index 9dd2049..f0fe65c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,12 +29,8 @@ fetch(url).then((res) => { _.forEach(data, (ticket) => { const issue = Issue.fromGitea(ticket, CONFIG['repository-default']); database.push(issue); - - console.log(issue); - - console.log(issue.toJira(uGiteaToJira, SECRETS['jira-project'])); // Now, search Jira issues to make sure this isn't already duplicated - const jiraIssueSearch = `project = "AAA" and summary ~ "${issue.title_jira}"\nORDER BY created DESC` + const jiraIssueSearch = `project = "${SECRETS['jira-project']}" and summary ~ "${issue.title_jira}"\nORDER BY created DESC` jiraFetch(`issue/picker?currentJQL=${jiraIssueSearch}`, 'GET').then((jiraSearch) => { jiraSearch.json().then((searchJSON) => { @@ -55,9 +51,11 @@ fetch(url).then((res) => { jiraFetch('issue', 'POST', issue.toJira(uGiteaToJira, SECRETS['jira-project'])).then((res) => { res.json().then((createdIssue) => { console.log(createdIssue); - issue.id_jira = createdIssue['id']; + issue.id_jira = createdIssue['key']; }) }) + } else { + console.log(`Issue "${issue.title_jira}" already existed, skipping`); } }) }) diff --git a/src/issue.ts b/src/issue.ts index 980f68b..9c4efaf 100644 --- a/src/issue.ts +++ b/src/issue.ts @@ -26,7 +26,7 @@ class Issue { const labels: string[] = []; _.forEach(obj['labels'], (label: any) => { - labels.push(label['id']) + labels.push(label['name']) }); // Fetch assignees @@ -60,7 +60,7 @@ class Issue { * @summary Converts this to a Jira JSoN object * https://developer.atlassian.com/server/jira/platform/jira-rest-api-examples/#creating-an-issue-examples */ - public toJira(remap: any, parent: string): any { + public toJira(remap: any, projectKey: string): any { const obj: any = { "labels": this.labels, "summary": this.title_jira, @@ -82,19 +82,23 @@ class Issue { "reporter": { "id": remap[this.reporter] }, - "parent": { - "key": parent + "project": { + "key": projectKey }, + "issuetype": { + // If there is a bug inside the labels, count the issue as a bug, else it's a task + "name": this.labels.includes('Bug') ? 'Bug' : 'Task' + } } if (this.assignees.length > 0) { obj['assignee'] = remap[this.assignees[0]]; } if (this.dueDate !== '') { - obj['duedate'] = this.dueDate.slice(0, this.dueDate.indexOf('T')); + // obj['duedate'] = this.dueDate.slice(0, this.dueDate.indexOf('T')); } - return {"fields": obj, "update": {}}; + return {"fields": obj}; } public get title_jira(): string { diff --git a/src/requests.ts b/src/requests.ts index 9433b49..45b86aa 100644 --- a/src/requests.ts +++ b/src/requests.ts @@ -6,19 +6,20 @@ const OAUTH_JIRA = `${SECRETS['jira-email']}:${SECRETS['jira-token']}`; const OAUTH_GITEA = `token=${SECRETS['gitea-token']}`; function jiraFetch(protocol: string, method: string = 'POST', body: any = null) { - const options: any = { + const options: RequestInit = { method: method, headers: { 'Authorization': `Basic ${Buffer.from(OAUTH_JIRA).toString('base64')}`, - 'Accept': 'application/json' - } + 'Accept': 'application/json', + 'Content-Type': 'application/json', + }, }; if (body !== null) { - options['body'] = JSON.stringify(body); + options.body = JSON.stringify(body); } - return fetch(`https://${SECRETS['jira-site']}.atlassian.net/rest/api/3/${protocol}`, options); + return fetch(`https://${SECRETS['jira-site']}.atlassian.net/rest/api/3/${protocol}`, options); } export { jiraFetch };