Cleanup and improvements
This commit is contained in:
parent
97f24641e0
commit
81ed441c7a
105
src/index.ts
105
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
|
||||||
const jiraIssueSearch = `project = "${SECRETS['jira-project']}" and summary ~ "${issue.title_jira}"\nORDER BY created DESC`
|
giteaSearchData.json().then((giteaSearch) => {
|
||||||
|
console.log(`Indexxing repository "${repoURL}" -----------`);
|
||||||
|
|
||||||
jiraFetch(`issue/picker?currentJQL=${jiraIssueSearch}`, 'GET').then((jiraSearch) => {
|
// For each search result, parse out an issue
|
||||||
jiraSearch.json().then((searchJSON) => {
|
_.forEach(giteaSearch, (ticket) => {
|
||||||
let issueFound: any = null;
|
// Generate issue from ticket JSON
|
||||||
|
const issue = Issue.fromGitea(ticket, repoName);
|
||||||
|
database.push(issue); // Save to our database for posterity
|
||||||
|
|
||||||
_.forEach(searchJSON['sections'], (searchSection) => {
|
// Now, search Jira issues (using JQL) to make sure this isn't already duplicated
|
||||||
const issueList: any[] = searchSection['issues']
|
// To do this, we need to search for issues using our given Project key
|
||||||
if (issueList.length > 0) { // If any issues were found
|
// ...then search for a matching title
|
||||||
issueFound = issueList[0]; // Select the first one (probably matches best)
|
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...`);
|
|
||||||
|
|
||||||
jiraFetch('issue', 'POST', issue.toJira(uGiteaToJira, SECRETS['jira-project'])).then((res) => {
|
fetchJira(`issue/picker?currentJQL=${jiraIssueSearch}`)
|
||||||
res.json().then((createdIssue) => {
|
.then((jiraSearchData) => {
|
||||||
console.log(createdIssue);
|
// Convert search results to JSON for indexing
|
||||||
issue.id_jira = createdIssue['key'];
|
jiraSearchData.json().then((jiraSearch) => {
|
||||||
|
let issueFound: any = null;
|
||||||
|
|
||||||
|
// 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 {
|
.catch((reason) => {
|
||||||
console.log(`Issue "${issue.title_jira}" already existed, skipping`);
|
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