Skip to content

Commit

Permalink
fix: check exists too early
Browse files Browse the repository at this point in the history
  • Loading branch information
z0al committed Dec 1, 2020
1 parent c78f686 commit 6fb5644
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 7 deletions.
47 changes: 44 additions & 3 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions src/__tests__/action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jest.mock('@actions/core', () => {
.fn()
.mockImplementation((key: string) => (inputs as any)[key]),
debug: jest.fn(),
startGroup: jest.fn(),
endGroup: jest.fn(),
};
});

Expand Down
32 changes: 30 additions & 2 deletions src/check.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
// Packages
import * as core from '@actions/core';

// Ours
import { ActionContext } from './types';

import {
IssueManager,
DependencyResolver,
DependencyExtractor,
formatDependency,
} from './helpers';

export async function checkIssues(context: ActionContext) {
Expand All @@ -15,16 +19,27 @@ export async function checkIssues(context: ActionContext) {
const resolver = new DependencyResolver(client, context.issues, repo);

for (const issue of context.issues) {
core.startGroup(`Checking #${issue.number}`);
let dependencies = extractor.fromIssue(issue);

if (dependencies.length === 0) {
core.debug('No dependencies found. Running clean-up');
await manager.removeLabel(issue);
await manager.removeAnyComments(issue);
return await manager.updateCommitStatus(issue, []);
await manager.removeActionComments(issue);
await manager.updateCommitStatus(issue, []);

core.endGroup();
continue;
}

let isBlocked = false;

core.debug(
`Depends on: ${dependencies
.map((dep) => formatDependency(dep, repo))
.join(', ')}`
);

dependencies = await Promise.all(
dependencies.map(async (dep) => {
const issue = await resolver.get(dep);
Expand All @@ -36,17 +51,30 @@ export async function checkIssues(context: ActionContext) {
})
);

core.debug(
`Blocked by: ${dependencies
.filter((dep) => dep.blocker)
.map((dep) => formatDependency(dep, repo))
.join(', ')}`
);

core.debug('Updating labels');
// Toggle label
isBlocked
? await manager.addLabel(issue)
: await manager.removeLabel(issue);

core.debug('Updating Action comments');
await manager.writeComment(
issue,
manager.generateComment(dependencies, dependencies, config),
!isBlocked
);

core.debug(
`Updating PR status${issue.pull_request ? '' : '. Skipped'}`
);
await manager.updateCommitStatus(issue, dependencies);
core.endGroup();
}
}
7 changes: 7 additions & 0 deletions src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import * as github from '@actions/github';
import { ActionContext, Issue } from './types';

export async function getActionContext(): Promise<ActionContext> {
core.startGroup('Context');

const config = {
actionName: 'Dependent Issues',
actionRepoURL: 'https://github.com/z0al/dependent-issues',
Expand Down Expand Up @@ -36,6 +38,7 @@ export async function getActionContext(): Promise<ActionContext> {

// Only run checks for the context.issue (if any)
if (issue?.number) {
core.debug(`Payload issue: #${issue?.number}`);
const remoteIssue = (
await client.issues.get({ ...repo, issue_number: issue.number })
).data;
Expand All @@ -48,6 +51,7 @@ export async function getActionContext(): Promise<ActionContext> {

// Otherwise, check all open issues
else {
core.debug(`Payload issue: None`);
const options = {
...repo,
state: 'open' as 'open',
Expand All @@ -60,8 +64,11 @@ export async function getActionContext(): Promise<ActionContext> {
: client.pulls.list;

issues = (await client.paginate(method, options)) as Issue[];
core.debug(`No. of open issues: ${issues.length}`);
}

core.endGroup();

return {
client,
config,
Expand Down
2 changes: 1 addition & 1 deletion src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ export class IssueManager {
});
}

async removeAnyComments(issue: Issue) {
async removeActionComments(issue: Issue) {
const issueComments = await this.gh.paginate(
this.gh.issues.listComments,
{ ...this.repo, issue_number: issue.number, per_page: 100 }
Expand Down

0 comments on commit 6fb5644

Please sign in to comment.