문서 메뉴

문서 홈Atlas App Services

GitHub 스니펫 [사용 중단됨]

이 페이지의 내용

  • 개요
  • MongoDB에 모든 커밋 기록
  • 새 pull 요청에 자동으로 댓글 달기

중요

타사 서비스 & 푸시 알림 사용 중단

App Services의 타사 서비스 및 푸시 알림은 함수에서 외부 종속성을 사용하는 HTTP 엔드포인트를 만들기 위해 더 이상 사용되지 않습니다.

웹훅은 동작에 대한 변경 없이 HTTPS 엔드포인트로 이름이 변경되었습니다. 기존 웹훅을 마이그레이션해야 합니다.

기존 서비스는 2024년 11월 1일까지 계속 운영됩니다.

타사 서비스 및 푸시 알림은 이제 더 이상 사용되지 않으므로 App Services UI에서 기본적으로 제거되었습니다. 기존 타사 서비스 또는 푸시 알림을 관리해야 하는 경우 다음을 수행하여 구성을 UI에 다시 추가할 수 있습니다.

  • 왼쪽 탐색의 Manage 섹션에서 App Settings를 클릭합니다.

  • Temporarily Re-Enable 3rd Party Services 옆의 토글 스위치를 활성화한 다음 변경 사항을 저장합니다.

이 페이지 커버의 코드 스니펫은 GitHub 서비스 를 통해 GitHub 리포지토리의 이벤트에 응답하는 방법을 보여줍니다. 모든 스니펫에는 스니펫에 사용된 서비스 조치를 허용하는 규칙이 포함된 Github 서비스 인터페이스가 필요합니다.

앱에 GitHub 서비스 인터페이스가 없는 경우 이 스니펫을 사용하기 전에 인터페이스를 만드세요 .

GitHub 수신 웹훅 함수는 푸시 이벤트 } 를 기반으로 MongoDB의 리포지토리에 푸시된 모든 커밋을 기록합니다. GitHub의 페이로드.

exports = async function(pushEvent) {
// Parse the list of commits from the PushEvent payload.
// Also grab the user that pushed the commits and the repo information.
const { commits, pusher, repository } = pushEvent;
// Create a new array of log documents, one for each commit
const commitLogs = commits.map(commit => {
return {
commit: commit,
pushed_by: pusher,
repo: {
name: repository.name,
url: repository.url
}
}
})
// Get a client for the `GitHubRepo.logs` collection in MongoDB
const mongodb = context.services.get("mongodb-atlas");
const logs = mongodb.db("GitHubRepo").collection("commit-logs");
// Insert the log documents in MongoDB
try {
const insertResult = await logs.insertMany(commitLogs)
console.log(insertResult.insertedIds);
} catch(err) {
console.error(err)
}
};

이 GitHub 수신 웹훅 함수는 새 pull 요청에 사용자가 제출해 준 것에 감사하는 댓글을 추가합니다. 웹훅은 pullRequestEvent 를 허용합니다. GitHub의 페이로드를 생성하고 HTTP 서비스 클라이언트를 사용하여 댓글 을 생성합니다. GitHub API를 통해.

exports = async function(pullRequest) {
// Get information about the PR from the PullRequestEvent
const { action, repository, pull_request: pr } = pullRequest;
// Only run if this is a new PR
if (action !== "opened") { return }
// Construct the GitHub API URL for this PR's Comments
const pr_comments_url = {
scheme: "https",
host: "api.github.com",
path: `/repos/${repository.owner.login}/${repository.name}/issues/${pr.number.$numberInt}/comments`,
};
// Specify GitHub API Basic Authentication Fields and Headers
const github_basic_auth = {
username: context.values.get("github-credentials").username,
password: context.values.get("github-credentials").password,
};
const headers = {
// OPTIONAL: Include this header if your security settings require a 2fa code
"X-GitHub-OTP": ["<2fa Code>"]
};
// Specify the comment text
const body = EJSON.stringify({
body: `Thank you for submitting a pull request, ${pr.user.login}!`
});
try {
// Get an HTTP service client. The service rules should allow you
// to send POST requests to `https://api.github.com`.
const http = context.services.get("<HTTP Service Name>");
// Send the Request to GitHub
const request = { ...github_basic_auth, ...pr_comments_url, headers, body };
const result = await http.post(request);
// Check for a Successful Result
if (result.statusCode == 201) {
return "Successfully commented on the pull request!";
} else {
throw new Error(`Received a bad result status from GitHub: ${result.body.text()}`);
}
} catch (err) {
console.error("Something went wrong while posting the comment.", err);
}
};
← Amazon Web Services S3 스니펫 [사용 중단됨]