Move from JavaScript to TypeScript #2

Merged
alan merged 7 commits from typescript into master 2024-01-13 03:00:34 -05:00
2 changed files with 81 additions and 46 deletions
Showing only changes of commit 81ed441c7a - Show all commits

View File

@ -1,9 +1,6 @@
import fs from 'fs'
import https from 'https'
import bodyParser from 'body-parser'
import _ from 'lodash'
import express from 'express'
import fetch from 'node-fetch'
// import bodyParser from 'body-parser'
// import express from 'express'
import Issue from './issue';
@ -11,56 +8,76 @@ import Issue from './issue';
import CONFIG from '../config/server.json';
import SECRETS from '../config/secrets.json';
import uGiteaToJira from '../config/user_remap.json';
import { jiraFetch } from './requests'
// BUILD OAUTH
const OAUTH_JIRA = `${SECRETS['jira-email']}:${SECRETS['jira-token']}`;
const OAUTH_GITEA = `token=${SECRETS['gitea-token']}`;
const URL_GITEA = `${CONFIG['gitea']}/api/v1`;
const url = `${URL_GITEA}/repos/${CONFIG['repositories']['bot']}/issues?state=all&${OAUTH_GITEA}`
import { fetchGitea, fetchJira } from './requests'
const database: Issue[] = [];
// Copy issues over from Gitea to Jira
fetch(url).then((res) => {
res.json().then((data) => {
_.forEach(data, (ticket) => {
const issue = Issue.fromGitea(ticket, CONFIG['repository-default']);
database.push(issue);
// Now, search Jira issues to make sure this isn't already duplicated
const jiraIssueSearch = `project = "${SECRETS['jira-project']}" and summary ~ "${issue.title_jira}"\nORDER BY created DESC`
// GOAL: Copy issues over from Gitea to Jira
// First, iterate through all relevant Gitea repositories and corresponding keys
_.forEach(CONFIG['repositories'], (repoURL, repoName) => {
// Perform a search for all open issues
fetchGitea(`repos/${repoURL}/issues?state=open`)
.then((giteaSearchData) => {
// Convert search results to JSON for indexing
giteaSearchData.json().then((giteaSearch) => {
console.log(`Indexxing repository "${repoURL}" -----------`);
jiraFetch(`issue/picker?currentJQL=${jiraIssueSearch}`, 'GET').then((jiraSearch) => {
jiraSearch.json().then((searchJSON) => {
let issueFound: any = null;
// For each search result, parse out an issue
_.forEach(giteaSearch, (ticket) => {
// Generate issue from ticket JSON
const issue = Issue.fromGitea(ticket, repoName);
database.push(issue); // Save to our database for posterity
_.forEach(searchJSON['sections'], (searchSection) => {
const issueList: any[] = searchSection['issues']
if (issueList.length > 0) { // If any issues were found
issueFound = issueList[0]; // Select the first one (probably matches best)
}
})
// Now, search Jira issues (using JQL) to make sure this isn't already duplicated
// To do this, we need to search for issues using our given Project key
// ...then search for a matching title
const jiraIssueSearch = `project = "${SECRETS['jira-project']}" and summary ~ "${issue.title_jira}"\nORDER BY created DESC`
// If the issue does not exist...
if (issueFound === null) {
// ...then create one!
console.log(`Issue "${issue.title_jira}" not found on Jira, creating one...`);
fetchJira(`issue/picker?currentJQL=${jiraIssueSearch}`)
.then((jiraSearchData) => {
// Convert search results to JSON for indexing
jiraSearchData.json().then((jiraSearch) => {
let issueFound: any = null;
jiraFetch('issue', 'POST', issue.toJira(uGiteaToJira, SECRETS['jira-project'])).then((res) => {
res.json().then((createdIssue) => {
console.log(createdIssue);
issue.id_jira = createdIssue['key'];
// Look through all sections of the Jira search
_.forEach(jiraSearch['sections'], (searchSection) => {
const issueList: any[] = searchSection['issues']
if (issueList.length > 0) { // If any issues were found
issueFound = issueList[0]; // Select the first one (probably matches best)
}
})
// If the issue does NOT exist...
if (issueFound === null) {
// ...then create one!
console.log(`\tIssue "${issue.title_jira}" not found on Jira, creating one...`);
return; // WOOPS DO NOT ALLOW CRASH AND BURN
// Send POST request to Jira, asking to create the issue
fetchJira('issue', 'POST', issue.toJira(uGiteaToJira, SECRETS['jira-project']))
.then((jiraIssueData) => {
// Convert our response back to JSON
jiraIssueData.json().then((jiraIssue) => {
issue.id_jira = jiraIssue['key']; // Store relevant information of newly created issue
})
})
.catch((reason) => {
console.error(`\tFailed to create issue "${issue.title_jira}" on Jira, for reason ${reason}`);
})
} else {
console.log(`\tIssue "${issue.title_jira}" already existed, skipping`);
}
})
})
} else {
console.log(`Issue "${issue.title_jira}" already existed, skipping`);
}
.catch((reason) => {
console.error(`\tFailed to fetch issues from Jira project for reason ${reason}`);
})
})
})
})
})
.catch((reason) => {
console.error(`\tFailed to fetch issues from Gitea repository "${repoURL}" for reason ${reason}`);
})
})
/*

View File

@ -1,11 +1,29 @@
import fetch from 'node-fetch'
import CONFIG from '../config/server.json';
import SECRETS from '../config/secrets.json';
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) {
function fetchGitea(protocol: string, method: string = 'GET', body: any = null) {
const options: RequestInit = {
method: method,
headers: {
'Authorization': SECRETS['gitea-token'],
'Accept': 'application/json',
'Content-Type': 'application/json',
},
};
if (body !== null) {
options.body = JSON.stringify(body);
}
return fetch(`${CONFIG['gitea']}/api/v1/${protocol}`, <any>options)
}
function fetchJira(protocol: string, method: string = 'GET', body: any = null) {
const options: RequestInit = {
method: method,
headers: {
@ -22,4 +40,4 @@ function jiraFetch(protocol: string, method: string = 'POST', body: any = null)
return fetch(`https://${SECRETS['jira-site']}.atlassian.net/rest/api/3/${protocol}`, <any>options);
}
export { jiraFetch };
export { fetchGitea, fetchJira };