Move from JavaScript to TypeScript #2
79
src/index.ts
79
src/index.ts
@ -1,9 +1,6 @@
|
|||||||
import fs from 'fs'
|
|
||||||
import https from 'https'
|
|
||||||
import bodyParser from 'body-parser'
|
|
||||||
import _ from 'lodash'
|
import _ from 'lodash'
|
||||||
import express from 'express'
|
// import bodyParser from 'body-parser'
|
||||||
import fetch from 'node-fetch'
|
// import express from 'express'
|
||||||
|
|
||||||
import Issue from './issue';
|
import Issue from './issue';
|
||||||
|
|
||||||
@ -11,56 +8,76 @@ import Issue from './issue';
|
|||||||
import CONFIG from '../config/server.json';
|
import CONFIG from '../config/server.json';
|
||||||
import SECRETS from '../config/secrets.json';
|
import SECRETS from '../config/secrets.json';
|
||||||
import uGiteaToJira from '../config/user_remap.json';
|
import uGiteaToJira from '../config/user_remap.json';
|
||||||
import { jiraFetch } from './requests'
|
import { fetchGitea, fetchJira } 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}`
|
|
||||||
|
|
||||||
const database: Issue[] = [];
|
const database: Issue[] = [];
|
||||||
|
|
||||||
// Copy issues over from Gitea to Jira
|
// GOAL: Copy issues over from Gitea to Jira
|
||||||
fetch(url).then((res) => {
|
// First, iterate through all relevant Gitea repositories and corresponding keys
|
||||||
res.json().then((data) => {
|
_.forEach(CONFIG['repositories'], (repoURL, repoName) => {
|
||||||
_.forEach(data, (ticket) => {
|
// Perform a search for all open issues
|
||||||
const issue = Issue.fromGitea(ticket, CONFIG['repository-default']);
|
fetchGitea(`repos/${repoURL}/issues?state=open`)
|
||||||
database.push(issue);
|
.then((giteaSearchData) => {
|
||||||
// Now, search Jira issues to make sure this isn't already duplicated
|
// Convert search results to JSON for indexing
|
||||||
|
giteaSearchData.json().then((giteaSearch) => {
|
||||||
|
console.log(`Indexxing repository "${repoURL}" -----------`);
|
||||||
|
|
||||||
|
// 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
|
||||||
|
|
||||||
|
// 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`
|
const jiraIssueSearch = `project = "${SECRETS['jira-project']}" and summary ~ "${issue.title_jira}"\nORDER BY created DESC`
|
||||||
|
|
||||||
jiraFetch(`issue/picker?currentJQL=${jiraIssueSearch}`, 'GET').then((jiraSearch) => {
|
fetchJira(`issue/picker?currentJQL=${jiraIssueSearch}`)
|
||||||
jiraSearch.json().then((searchJSON) => {
|
.then((jiraSearchData) => {
|
||||||
|
// Convert search results to JSON for indexing
|
||||||
|
jiraSearchData.json().then((jiraSearch) => {
|
||||||
let issueFound: any = null;
|
let issueFound: any = null;
|
||||||
|
|
||||||
_.forEach(searchJSON['sections'], (searchSection) => {
|
// Look through all sections of the Jira search
|
||||||
|
_.forEach(jiraSearch['sections'], (searchSection) => {
|
||||||
const issueList: any[] = searchSection['issues']
|
const issueList: any[] = searchSection['issues']
|
||||||
if (issueList.length > 0) { // If any issues were found
|
if (issueList.length > 0) { // If any issues were found
|
||||||
issueFound = issueList[0]; // Select the first one (probably matches best)
|
issueFound = issueList[0]; // Select the first one (probably matches best)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
// If the issue does not exist...
|
// If the issue does NOT exist...
|
||||||
if (issueFound === null) {
|
if (issueFound === null) {
|
||||||
// ...then create one!
|
// ...then create one!
|
||||||
console.log(`Issue "${issue.title_jira}" not found on Jira, creating one...`);
|
console.log(`\tIssue "${issue.title_jira}" not found on Jira, creating one...`);
|
||||||
|
|
||||||
jiraFetch('issue', 'POST', issue.toJira(uGiteaToJira, SECRETS['jira-project'])).then((res) => {
|
return; // WOOPS DO NOT ALLOW CRASH AND BURN
|
||||||
res.json().then((createdIssue) => {
|
// Send POST request to Jira, asking to create the issue
|
||||||
console.log(createdIssue);
|
fetchJira('issue', 'POST', issue.toJira(uGiteaToJira, SECRETS['jira-project']))
|
||||||
issue.id_jira = createdIssue['key'];
|
.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 {
|
} else {
|
||||||
console.log(`Issue "${issue.title_jira}" already existed, skipping`);
|
console.log(`\tIssue "${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}`);
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -1,11 +1,29 @@
|
|||||||
import fetch from 'node-fetch'
|
import fetch from 'node-fetch'
|
||||||
|
|
||||||
|
import CONFIG from '../config/server.json';
|
||||||
import SECRETS from '../config/secrets.json';
|
import SECRETS from '../config/secrets.json';
|
||||||
|
|
||||||
const OAUTH_JIRA = `${SECRETS['jira-email']}:${SECRETS['jira-token']}`;
|
const OAUTH_JIRA = `${SECRETS['jira-email']}:${SECRETS['jira-token']}`;
|
||||||
const OAUTH_GITEA = `token=${SECRETS['gitea-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 = {
|
const options: RequestInit = {
|
||||||
method: method,
|
method: method,
|
||||||
headers: {
|
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);
|
return fetch(`https://${SECRETS['jira-site']}.atlassian.net/rest/api/3/${protocol}`, <any>options);
|
||||||
}
|
}
|
||||||
|
|
||||||
export { jiraFetch };
|
export { fetchGitea, fetchJira };
|
||||||
|
Loading…
Reference in New Issue
Block a user