Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions octosync.example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# The direction option available values are: bidirectional, github-to-jira or jira-to-github
direction:
comments: bidirectional
issue_creation: bidirectional
issue_closing: bidirectional
5 changes: 4 additions & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@
"publishConfig": {
"access": "public"
},
"gitHead": "8f3c3a731c82e6f65dae2bc3f9eb1e752eac6dc8"
"gitHead": "8f3c3a731c82e6f65dae2bc3f9eb1e752eac6dc8",
"dependencies": {
"js-yaml": "^4.1.0"
}
}
7 changes: 7 additions & 0 deletions packages/utils/src/hooks/useEnv.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { getPublicConfig } from "../publicConf";

export function useEnv() {
const {
NODE_ENV = "development",
Expand All @@ -20,6 +22,10 @@ export function useEnv() {
JIRA_CUSTOM_GITHUB_ISSUE_NUMBER_FIELD = "",
} = process.env;

console.log({ NODE_ENV });

const PUBLIC_CONFIG = getPublicConfig(NODE_ENV === "development");

const CUSTOM_GITHUB_REPO_FIELD = `customfield_${JIRA_CUSTOM_GITHUB_REPOSITORY_FIELD}`;
const CUSTOM_ISSUE_NUMBER_FIELD = `customfield_${JIRA_CUSTOM_GITHUB_ISSUE_NUMBER_FIELD}`;

Expand All @@ -38,5 +44,6 @@ export function useEnv() {
JIRA_DONE_STATUS_NAME,
JIRA_CUSTOM_GITHUB_REPOSITORY_FIELD: CUSTOM_GITHUB_REPO_FIELD,
JIRA_CUSTOM_GITHUB_ISSUE_NUMBER_FIELD: CUSTOM_ISSUE_NUMBER_FIELD,
PUBLIC_CONFIG,
};
}
55 changes: 55 additions & 0 deletions packages/utils/src/publicConf.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import fs from "fs";
import yaml from "js-yaml";
import { homedir } from "node:os";

function getFilePath(devMode: boolean) {
let filePath = "";

if (devMode) {
filePath = `../../octosync.example.yaml`;
} else {
filePath = `${homedir()}/octosync.yaml`;
}

return filePath;
}

export function getPublicConfig(devMode: boolean): PublicConfig {
const filePath = getFilePath(devMode);

try {
const fileContents = fs.readFileSync(filePath, "utf8");
const data = yaml.load(fileContents);

return data as PublicConfig;
} catch (error) {
console.warn(
`Custom octosync.yaml file not found @ ${filePath}. Returning default public configuration.`
);

return {
direction: {
comments: "bidirectional",
issue_closing: "bidirectional",
issue_creation: "bidirectional",
},
};
}
}

export function writePublicConfig() {
// TODO
// - Run this method right after installation
// - Check if file exists in the home directory
// - Write to file if it doesn't exist
}

export type Direction = "bidirectional" | "github-to-jira" | "jira-to-github";

export interface PublicConfig {
direction: {
comments: Direction;
issue_creation: Direction;
issue_closing: Direction;
};
}
30 changes: 30 additions & 0 deletions packages/webhooks/src/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ webhook.post("/github", async (req, res) => {

switch (action) {
case "opened":
const {
PUBLIC_CONFIG: {
direction: { issue_creation },
},
} = useEnv();

if (issue_creation === "jira-to-github") {
return;
}

await handleOpenedIssue({
clients: {
github: {
Expand All @@ -57,6 +67,16 @@ webhook.post("/github", async (req, res) => {
});
break;
case "closed":
const {
PUBLIC_CONFIG: {
direction: { issue_closing },
},
} = useEnv();

if (issue_closing === "jira-to-github") {
return;
}

const success = await handleClosedIssue({ title });

if (!success) {
Expand All @@ -66,6 +86,16 @@ webhook.post("/github", async (req, res) => {

break;
case "created":
const {
PUBLIC_CONFIG: {
direction: { comments },
},
} = useEnv();

if (comments === "jira-to-github") {
return;
}

const result = await handleIssueCommentCreation({
clients: {
github: {
Expand Down
30 changes: 30 additions & 0 deletions packages/webhooks/src/jira/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ webhook.post("/jira", async (req, res) => {

switch (webhookEvent) {
case "jira:issue_created":
const {
PUBLIC_CONFIG: {
direction: { issue_creation },
},
} = useEnv();

if (issue_creation === "github-to-jira") {
return;
}

// This means that this issue has already been created,
// and this hook must finish executing immediately.
if (jiraLabels?.includes(CONTROL_LABELS.FROM_GITHUB)) {
Expand All @@ -57,6 +67,16 @@ webhook.post("/jira", async (req, res) => {
issueNumber: issue.number.toString(),
});
case "jira:issue_updated":
const {
PUBLIC_CONFIG: {
direction: { issue_closing },
},
} = useEnv();

if (issue_closing === "github-to-jira") {
return;
}

if (status.name === JIRA_DONE_STATUS_NAME) {
await github.updateIssue({
issueNumber:
Expand All @@ -67,6 +87,16 @@ webhook.post("/jira", async (req, res) => {
});
}
case "comment_created":
const {
PUBLIC_CONFIG: {
direction: { comments },
},
} = useEnv();

if (comments === "github-to-jira") {
return;
}

const commentBody = comment?.body;

if (!commentBody) {
Expand Down