Skip to content

auto close issue after complete #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 16, 2025
Merged
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
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@
**/.editorconfig
**/dist
**/*.pem
Dockerfile
15 changes: 12 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
FROM node:20-slim

WORKDIR /usr/src/app
COPY package.json package-lock.json ./
RUN npm ci --production

RUN apt-get update && apt-get install -y iputils-ping curl

COPY . .

RUN npm ci

RUN npm run build

RUN npm cache clean --force

ENV NODE_ENV="production"
COPY . .

CMD [ "npm", "start" ]
57 changes: 57 additions & 0 deletions comment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
project:
noneProjectComment: >
Thanks for joining R2CN Projetc! You need to first submit a PR to modify the organization.yaml file to apply for your project to be included in the R2CN program.
noneMaintainerComment: >
You do not have permission to apply for the R2CN internship tasks. Please contact your organization administrator to submit a PR and to update the R2CN organization.yaml file.

task:
scoreUndefinedComment: >
Please add the R2CN-Score label to the issue. `Score` represents the task’s score.
scoreInvalidComment: >
The score must be within the range of 2 to 50.
insufficientScoreComment: >
The current organization has insufficient remaining points. Please contact the administrator.
toomanyTask: >
There are too many tasks currently active. Please check if there are 3 tasks already, or contact the administrator.

command:
noPermission: >
Operation failed, You are not allowed to operate this task.
invalidTaskState: >
Operation failed, the current task status does not allow this operation.

requestAssign:
success: >
Claim successful, awaiting review.
waitingInfoReview: >
Claim failed, personal information is under review.
existTask: >
Claim failed, you have already claimed another task.
alreadyClaim: >
Claim failed, you have already claimed this task, please do not repeat claim.

internDisapprove:
success: >
Operation successful, awaiting claim.

internApprove:
success: >
Operation successful, awaiting PR.

requestComplete:
success: >
Request complete successful, awaiting finish intern review.
noRelatedPR: >
Request complete failed, the task must be associated with at least one PR.

requestRelease:
success: >
Request release task successful, awaiting another claim.

internFail:
success: >
Release task successful, awaiting another claim.

internDone:
success: >
Operation successful, task close.
3 changes: 2 additions & 1 deletion src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ interface Repo {

export interface CommandRequest {
github_issue_id: number,
login?: string
login: string
github_id: number
}


Expand Down
88 changes: 46 additions & 42 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,59 @@ export default (app: Probot) => {
const creator = context.payload.issue.user.login;
const repo_full_name = context.payload.repository.full_name;

if (hasLabel && config !== null) {
const repo = config.repos.find((repo) => repo.name === repo_full_name);
if (!repo) {
await context.octokit.issues.createComment(context.issue({
body: config.project.noneProjectComment,
}));
return
}
if (hasLabel) {
context.log.debug("R2cn label not found, skipping message")
return
}
if (config == null) {
context.log.error("Config parsing error");
return
}
const repo = config.repos.find((repo) => repo.name === repo_full_name);
if (!repo) {
await context.octokit.issues.createComment(context.issue({
body: config.project.noneProjectComment,
}));
return
}

if (!repo.maintainers.includes(creator)) {
await context.octokit.issues.createComment(context.issue({
body: config.project.noneMaintainerComment,
}));
return
}
const task = await Task.getTask(context.payload.issue.id);
if (task == null) {
const checkRes: Task.CheckTaskResults = await Task.checkTask(context.payload.repository, context.payload.issue, config);
if (checkRes.result) {
const newTaskRes = await Task.newTask(context.payload.repository, context.payload.issue, checkRes.score);
if (newTaskRes) {
await context.octokit.issues.createComment(context.issue({
body: "Task created successfully."
}));
}
} else {
if (!repo.maintainers.includes(creator)) {
await context.octokit.issues.createComment(context.issue({
body: config.project.noneMaintainerComment,
}));
return
}
const task = await Task.getTask(context.payload.issue.id);
if (task == null) {
const checkRes: Task.CheckTaskResults = await Task.checkTask(context.payload.repository, context.payload.issue, config);
if (checkRes.result) {
const newTaskRes = await Task.newTask(context.payload.repository, context.payload.issue, checkRes.score);
if (newTaskRes) {
await context.octokit.issues.createComment(context.issue({
body: checkRes.message
body: "Task created successfully."
}));
}
} else {
const comment = context.payload.comment.body.trim();

if (comment.startsWith("/request")) {
let res = await Student.handle_stu_cmd(context.payload.comment.user, comment, config, task);
context.octokit.issues.createComment(context.issue({
body: res.message
}));
} else if (comment.startsWith("/intern")) {
let res = await handle_mentor_cmd(context.payload.comment.user, comment, config, task);
context.octokit.issues.createComment(context.issue({
body: res.message
}));
} else {
context.log.debug("Normal Comment, skipping...")
}
await context.octokit.issues.createComment(context.issue({
body: checkRes.message
}));
}
} else {
context.log.error("R2cn label not found or config parsing error")
const comment = context.payload.comment.body.trim();

if (comment.startsWith("/request")) {
let res = await Student.handle_stu_cmd(context, context.payload.comment.user, comment, config, task);
context.octokit.issues.createComment(context.issue({
body: res.message
}));
} else if (comment.startsWith("/intern")) {
let res = await handle_mentor_cmd(context, context.payload.comment.user, comment, config, task);
context.octokit.issues.createComment(context.issue({
body: res.message
}));
} else {
context.log.debug("Normal Comment, skipping...")
}
}
});
};
Expand Down
28 changes: 19 additions & 9 deletions src/mentor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import { User } from "@octokit/webhooks-types";
import { CommandRequest, Config, postData } from "./common.js";
import { Task, TaskStatus } from "./task.js";
import { releaseTask } from "./student.js";
import { Context } from "probot";


export async function handle_mentor_cmd(student: User, command: string, config: Config, task: Task) {
export async function handle_mentor_cmd(context: Context, mentor: User, command: string, config: Config, task: Task) {
var command_res = {
result: false,
message: "",
Expand All @@ -16,24 +17,24 @@ export async function handle_mentor_cmd(student: User, command: string, config:
return command_res;
};

const isMentorAuthorized = (task: Task, student: User) => {
return task.student_github_login === student.login;
const isMentorAuthorized = (task: Task, mentor: User) => {
return task.mentor_github_login === mentor.login;
};

if (!isMentorAuthorized(task, student)) {
if (!isMentorAuthorized(task, mentor)) {
return setResponse(config.command.noPermission);
}

switch (command) {

case "/intern-disapprove":
if (task.task_status !== TaskStatus.RequestAssign) {
return setResponse(config.command.invalidTaskState);
}

await releaseTask({
github_issue_id: task.github_issue_id,
login: student.login
login: mentor.login,
github_id: mentor.id
});
return setResponse(config.internDisapprove.success, true);

Expand All @@ -43,7 +44,8 @@ export async function handle_mentor_cmd(student: User, command: string, config:
}
await internApprove({
github_issue_id: task.github_issue_id,
login: student.login
login: mentor.login,
github_id: mentor.id
});
return setResponse(config.internApprove.success, true);

Expand All @@ -53,16 +55,24 @@ export async function handle_mentor_cmd(student: User, command: string, config:
}
await releaseTask({
github_issue_id: task.github_issue_id,
login: student.login
login: mentor.login,
github_id: mentor.id
});
return setResponse(config.internFail.success, true);
case "/intern-done":
if (task.task_status !== TaskStatus.RequestFinish) {
return setResponse(config.command.invalidTaskState);
}
await context.octokit.issues.update({
owner: task.owner,
repo: task.repo,
issue_number: task.github_issue_number,
state: "closed",
});
await internDone({
github_issue_id: task.github_issue_id,
login: student.login
login: mentor.login,
github_id: mentor.id
});
return setResponse(config.internDone.success, true);

Expand Down
26 changes: 19 additions & 7 deletions src/student.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
import { User } from "@octokit/webhooks-types";
import { CommandRequest, Config, postData } from "./common.js";
import { Task, TaskStatus } from "./task.js";
import { Context } from "probot";


export async function handle_stu_cmd(student: User, command: string, config: Config, task: Task) {
export async function handle_stu_cmd(context: Context, student: User, command: string, config: Config, task: Task) {
var command_res = {
result: false,
message: "",
Expand Down Expand Up @@ -40,7 +41,8 @@ export async function handle_stu_cmd(student: User, command: string, config: Con

if (await requestAssign({
github_issue_id: task.github_issue_id,
login: student.login
login: student.login,
github_id: student.id
})) {
return setResponse(config.requestAssign.success, true);
} else {
Expand All @@ -55,12 +57,21 @@ export async function handle_stu_cmd(student: User, command: string, config: Con
return setResponse(config.command.noPermission);
}

//check related PRs
// const res = await context.octokit.issues.get({
// owner: task.owner,
// repo: task.repo,
// issue_number: task.github_issue_number
// });

// if (res.data.pull_request == undefined) {
// return setResponse(config.requestComplete.noRelatedPR);
// }
await requestComplete({
github_issue_id: task.github_issue_id,
login: student.login
login: student.login,
github_id: student.id
})

// TODO: Add logic to check related PRs
return setResponse(config.requestComplete.success, true);

case "/request-release":
Expand All @@ -74,9 +85,10 @@ export async function handle_stu_cmd(student: User, command: string, config: Con

await releaseTask({
github_issue_id: task.github_issue_id,
login: student.login
login: student.login,
github_id: student.id
})
return setResponse(config.requestComplete.success, true);
return setResponse(config.requestRelease.success, true);

default:
return setResponse("Unsupported command");
Expand Down
17 changes: 13 additions & 4 deletions src/task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { Issue, Repository } from "@octokit/webhooks-types";
import { Config, fetchData, postData } from "./common.js";

export interface Task {
repo: string,
owner: string,
github_issue_number: number,
github_repo_id: number,
github_issue_id: number,
points?: number,
Expand All @@ -27,20 +30,26 @@ export async function getTask(issue_id: number) {
return res
}

interface newTask {
interface TaskCreate {
repo: string,
owner: string,
github_issue_number: number,
github_repo_id: number,
github_issue_id: number,
score: number,
mentor_github_login: string,
}

export async function newTask(repo: Repository, issue: Issue, score: number) {
export async function newTask(repository: Repository, issue: Issue, score: number) {
const req = {
github_repo_id: repo.id,
repo: repository.name,
owner: repository.owner.login,
github_issue_number: issue.number,
github_repo_id: repository.id,
github_issue_id: issue.id,
score: score,
mentor_github_login: issue.user.login,
}
} as TaskCreate;
const apiUrl = `${process.env.API_ENDPOINT}/task/new`;
const res = await postData<Task[], SearchTaskReq>(apiUrl, req).then((res) => {
return res.data
Expand Down