Skip to content

Commit

Permalink
add logs and attach link always
Browse files Browse the repository at this point in the history
  • Loading branch information
sergeysova committed Jul 2, 2021
1 parent 8eeed84 commit 244f441
Showing 1 changed file with 75 additions and 13 deletions.
88 changes: 75 additions & 13 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,20 +46,27 @@ async function main() {
const { nodes: allTeams } = await linear.teams();
// Validate configuration state-map, that all pr state exist in allowedPullStates
const resolvedTeams = await resolveValidTeamsByPrefixes(allTeams);
LOG_LIST(
'Found teams for prefixes',
resolvedTeams.map((t) => t.name),
);

await Promise.all(resolvedTeams.map((team) => linearStateMapAssert(team)));

console.log('——', context);
// console.log('——', context);
const { action, eventName } = context.payload;
const pullRequest = context.payload.pull_request as PR;
const { title, draft, html_url: prHtmlUrl, number: prId } = pullRequest;

const foundIssuesIds = findIssuesInText(title);
LOG_LIST('Found issues in PR title', foundIssuesIds);
if (issuesRequired && foundIssuesIds.length === 0) {
// stop check
throw new Error('Please, set issues in PR title');
}

const foundIssues = await Promise.all(foundIssuesIds.map((id) => linearIssueFind(id)));
console.log(`Resolved ${foundIssues.length} issues with specified prefixes`);

// just the declarative wrapper over promise array
// we collect the promises here, and await them at the end
Expand All @@ -75,16 +82,23 @@ async function main() {
}

const prStatus = prStatusDetect(pullRequest);
LOG(`Pull request status`, prStatus);

const linearNextState = prStatusMapToLinear(prStatus);
LOG(`Linear issues status should be changed to`, linearNextState);

const linearComment = linearCommentText[prStatus](pullRequest);

const linearIssueProcess = async (issue: Issue) => {
const currentState = await issue.state;
if (currentState.name !== linearNextState) {
await linearLinkUpdate(issue, pullRequest);
const isDiffers = currentState.name !== linearNextState;
LOG(`Is linear issue state differs from next state`, isDiffers);

if (isDiffers) {
await linearIssueCommentSend(issue, linearComment);
await linearIssueMove(issue, linearNextState);
}
await linearLinkUpdate(issue, pullRequest);
};

for (const issue of foundIssues) {
Expand Down Expand Up @@ -148,10 +162,18 @@ async function githubSyncLabels({ linearIssues, pr }: { linearIssues: Issue[]; p
const batcher = createTaskBatcher();

if (toAdd.length > 0) {
LOG_LIST(
'Found labels to add',
toAdd.map((l) => l.name),
);
batcher.add(prLabelsAdd(pr, toAdd));
}

if (toAddMissing.length > 0 && createMissingLabels) {
LOG_LIST(
'Should create missing labels',
toAddMissing.map((l) => l.name),
);
const createAndAdd = async () => {
await repoLabelsCreate(toAddMissing);
await prLabelsAdd(pr, toAddMissing);
Expand All @@ -161,6 +183,10 @@ async function githubSyncLabels({ linearIssues, pr }: { linearIssues: Issue[]; p
}

if (toRemove.length > 0) {
LOG_LIST(
'Found labels to remove',
toRemove.map((l) => l.name),
);
batcher.add(prLabelsRemove(pr, toRemove));
}

Expand Down Expand Up @@ -193,16 +219,28 @@ async function repoLabelsCreate(labels: AbstractLabel[]) {
color: ColorFormat.github(label.color),
}),
),
);
).then(() => {
LOG_LIST(
'Labels created',
labels.map((l) => l.name),
);
});
}

async function prLabelsAdd(pr: PR, labels: AbstractLabel[]) {
await octokit.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
labels: labels.map((label) => label.name),
});
await octokit.rest.issues
.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: pr.number,
labels: labels.map((label) => label.name),
})
.then(() => {
LOG_LIST(
`Labels added to #${pr.number}`,
labels.map((l) => l.name),
);
});
}

async function prLabelsRemove(pr: PR, labels: AbstractLabel[]) {
Expand All @@ -215,7 +253,12 @@ async function prLabelsRemove(pr: PR, labels: AbstractLabel[]) {
name: label.name,
}),
),
);
).then(() => {
LOG_LIST(
`Labels removed from #${pr.number}`,
labels.map((l) => l.name),
);
});
}

const ColorFormat = {
Expand Down Expand Up @@ -373,6 +416,8 @@ async function linearIssueMove(issue: Issue, moveTo: string) {
throw new Error(`Not found state "${moveTo}" in team ${team.name} ${team.key}`);
}
await linear.issueUpdate(issue.id, { stateId: moveToState.id });

LOG(`Issue ${issue.identifier} moved from "${currentState.name}" to`, moveTo);
}
}

Expand All @@ -386,15 +431,20 @@ async function linearLinkUpdate(issue: Issue, pr: PR) {
}
}

const title = `#${pr.number} ${pr.title}`;
const subtitle = linearAttachmentStatus(pr);

await linear.attachmentCreate({
issueId: issue.id,
id: targetId,
title: `#${pr.number} ${pr.title}`,
subtitle: linearAttachmentStatus(pr),
title,
subtitle,
url: pr.html_url,
iconUrl: 'https://sergeysova.github.io/public/GitHub-Mark-64px.png',
metadata: { pullRequestId: pr.number },
});

LOG(`Attachment created for ${issue.identifier}`, `${title} ${subtitle}`);
}

function linearAttachmentStatus(pr: PR) {
Expand All @@ -412,10 +462,22 @@ function linearAttachmentStatus(pr: PR) {

async function linearIssueCommentSend(issue: Issue, markdown: string) {
await linear.commentCreate({ body: markdown, issueId: issue.id });
LOG(`Comment sent to ${issue.identifier}`, markdown);
}

main().catch((error) => {
console.error(error);
setFailed(error);
process.exit(-1);
});

function LOG_LIST<T extends string | number | boolean | null | undefined>(
prefix: string,
list: Array<T>,
) {
console.log(`${prefix}: [${list.map((e) => JSON.stringify(e)).join(', ')}]`);
}

function LOG<T extends string | number | boolean | null | undefined>(prefix: string, value: T) {
console.log(`${prefix}: ${JSON.stringify(value)}`);
}

0 comments on commit 244f441

Please sign in to comment.